Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: pass file to upload path function #76

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/features/upload-file/factories/update-record-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const updateRecordFactory = (
const uploadedFiles = files as Array<UploadedFile>

const keys = await Promise.all<string>(uploadedFiles.map(async (uploadedFile) => {
const key = buildRemotePath(record, uploadedFile, uploadPath)
const key = await buildRemotePath(record, uploadedFile, uploadPath)
await provider.upload(uploadedFile, key, context)
return key
}))
Expand Down Expand Up @@ -109,7 +109,7 @@ export const updateRecordFactory = (
const uploadedFile: UploadedFile = files[0]

const oldRecordParams = { ...record.params }
const key = buildRemotePath(record, uploadedFile, uploadPath)
const key = await buildRemotePath(record, uploadedFile, uploadPath)

await provider.upload(uploadedFile, key, context)

Expand Down
8 changes: 6 additions & 2 deletions src/features/upload-file/types/upload-options.type.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BaseRecord, ComponentLoader } from 'adminjs'
import { BaseRecord, ComponentLoader, UploadedFile } from 'adminjs'
import { AWSOptions } from '../providers/aws-provider.js'
import { BaseProvider } from '../providers/base-provider.js'
import { GCPOptions } from '../providers/gcp-provider.js'
Expand All @@ -24,7 +24,11 @@ export type UploadPathFunction = (
* filename with extension
*/
filename: string,
) => string
/**
* File to upload
*/
file: UploadedFile,
) => string | Promise<string>

/**
* Configuration options for @adminjs/upload feature
Expand Down
12 changes: 6 additions & 6 deletions src/features/upload-file/utils/build-remote-path.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ describe('buildPath', () => {
recordStub.params = {}
})

it('returns default path when no custom function is given', () => {
expect(buildRemotePath(recordStub, File)).to.equal(`${recordId}/${File.name}`)
it('returns default path when no custom function is given', async () => {
expect(await buildRemotePath(recordStub, File)).to.equal(`${recordId}/${File.name}`)
})

it('returns default custom path when function is given', () => {
it('returns custom path when function is given', async () => {
const newPath = '1/1/filename'
const fnStub = sinon.stub<[BaseRecord, string], string>().returns(newPath)
const fnStub = sinon.stub<[BaseRecord, string, UploadedFile], string>().returns(newPath)

const path = buildRemotePath(recordStub, File, fnStub)
const path = await buildRemotePath(recordStub, File, fnStub)

expect(path).to.equal(newPath)
expect(fnStub).to.have.been.calledWith(recordStub, File.name)
expect(fnStub).to.have.been.calledWith(recordStub, File.name, File)
})
})
4 changes: 2 additions & 2 deletions src/features/upload-file/utils/build-remote-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const buildRemotePath = (
record: BaseRecord,
file: UploadedFile,
uploadPathFunction?: UploadPathFunction,
): string => {
): string | Promise<string> => {
if (!record.id()) {
throw new Error(ERROR_MESSAGES.NO_PERSISTENT_RECORD_UPLOAD)
}
Expand All @@ -28,7 +28,7 @@ export const buildRemotePath = (
const { ext, name } = path.parse(file.name)

if (uploadPathFunction) {
return uploadPathFunction(record, `${name}${ext}`)
return uploadPathFunction(record, `${name}${ext}`, file)
}

return `${record.id()}/${name}${ext}`
Expand Down