-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathbuild-remote-path.ts
More file actions
35 lines (32 loc) · 984 Bytes
/
build-remote-path.ts
File metadata and controls
35 lines (32 loc) · 984 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { BaseRecord, UploadedFile } from 'adminjs'
import path from 'path'
import { ERROR_MESSAGES } from '../constants.js'
import { UploadPathFunction } from '../types/upload-options.type.js'
/**
* Creates a path to the file. Related to the given provider. If it is an AWS
* path is related to the bucket.
*
* @param {BaseRecord} record
* @param {UploadedFile} file uploaded file
* @param {UploadPathFunction} [pathFunction]
*
* @return {string}
* @private
*/
export const buildRemotePath = (
record: BaseRecord,
file: UploadedFile,
uploadPathFunction?: UploadPathFunction,
): string | Promise<string> => {
if (!record.id()) {
throw new Error(ERROR_MESSAGES.NO_PERSISTENT_RECORD_UPLOAD)
}
if (!file.name) {
throw new Error(ERROR_MESSAGES.NO_FILENAME)
}
const { ext, name } = path.parse(file.name)
if (uploadPathFunction) {
return uploadPathFunction(record, `${name}${ext}`, file)
}
return `${record.id()}/${name}${ext}`
}