feat: File argument in create/update operation (#708)
Co-authored-by: Dan Ribbens <dan.ribbens@gmail.com>
This commit is contained in:
@@ -102,6 +102,10 @@ const post = await payload.create({
|
||||
// a file directly through the Local API by providing
|
||||
// its full, absolute file path.
|
||||
filePath: path.resolve(__dirname, './path-to-image.jpg'),
|
||||
|
||||
// Alternatively, you can directly pass a File,
|
||||
// if file is provided, filePath will be omitted
|
||||
file: uploadedFile,
|
||||
})
|
||||
```
|
||||
|
||||
|
||||
@@ -4,6 +4,8 @@ import { PayloadRequest } from '../../../express/types';
|
||||
import { Document } from '../../../types';
|
||||
import getFileByPath from '../../../uploads/getFileByPath';
|
||||
import create from '../create';
|
||||
import { File } from '../../../uploads/types';
|
||||
|
||||
|
||||
export type Options<T> = {
|
||||
collection: string
|
||||
@@ -16,6 +18,7 @@ export type Options<T> = {
|
||||
disableVerificationEmail?: boolean
|
||||
showHiddenFields?: boolean
|
||||
filePath?: string
|
||||
file?: File
|
||||
overwriteExistingFiles?: boolean
|
||||
req?: PayloadRequest
|
||||
draft?: boolean
|
||||
@@ -33,6 +36,7 @@ export default async function createLocal<T = any>(payload: Payload, options: Op
|
||||
disableVerificationEmail,
|
||||
showHiddenFields,
|
||||
filePath,
|
||||
file,
|
||||
overwriteExistingFiles = false,
|
||||
req,
|
||||
draft,
|
||||
@@ -57,7 +61,7 @@ export default async function createLocal<T = any>(payload: Payload, options: Op
|
||||
fallbackLocale: fallbackLocale || req?.fallbackLocale || null,
|
||||
payload,
|
||||
files: {
|
||||
file: getFileByPath(filePath) as UploadedFile,
|
||||
file: file ?? getFileByPath(filePath),
|
||||
},
|
||||
} as PayloadRequest,
|
||||
});
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { UploadedFile } from 'express-fileupload';
|
||||
import payload from '../../..';
|
||||
import getFileByPath from '../../../uploads/getFileByPath';
|
||||
|
||||
let createdMediaID;
|
||||
|
||||
@@ -34,6 +36,32 @@ describe('Collections - Local', () => {
|
||||
expect(result.filesize).toStrictEqual(size);
|
||||
createdMediaID = result.id;
|
||||
});
|
||||
|
||||
it('should allow an upload-enabled file to be created from file instead of filepath', async () => {
|
||||
const name = 'upload-local.svg';
|
||||
const alt = 'Alt Text Here';
|
||||
const filePath = path.resolve(
|
||||
__dirname,
|
||||
'../../../admin/assets/images/generic-block-image.svg',
|
||||
);
|
||||
const { size } = fs.statSync(filePath);
|
||||
const file = getFileByPath(filePath);
|
||||
file.name = name;
|
||||
|
||||
const result: Media = await payload.create({
|
||||
collection: 'media',
|
||||
data: {
|
||||
alt,
|
||||
},
|
||||
file,
|
||||
});
|
||||
|
||||
expect(result.id).toBeDefined();
|
||||
expect(result.alt).toStrictEqual(alt);
|
||||
expect(result.filename).toStrictEqual(name);
|
||||
expect(result.filesize).toStrictEqual(size);
|
||||
createdMediaID = result.id;
|
||||
});
|
||||
});
|
||||
|
||||
describe('Update', () => {
|
||||
|
||||
@@ -15,6 +15,7 @@ export type Options<T> = {
|
||||
overrideAccess?: boolean
|
||||
showHiddenFields?: boolean
|
||||
filePath?: string
|
||||
file?: File
|
||||
overwriteExistingFiles?: boolean
|
||||
draft?: boolean
|
||||
autosave?: boolean
|
||||
@@ -32,6 +33,7 @@ export default async function updateLocal<T = any>(payload: Payload, options: Op
|
||||
overrideAccess = true,
|
||||
showHiddenFields,
|
||||
filePath,
|
||||
file,
|
||||
overwriteExistingFiles = false,
|
||||
draft,
|
||||
autosave,
|
||||
@@ -57,7 +59,7 @@ export default async function updateLocal<T = any>(payload: Payload, options: Op
|
||||
fallbackLocale,
|
||||
payload,
|
||||
files: {
|
||||
file: getFileByPath(filePath),
|
||||
file: file ?? getFileByPath(filePath),
|
||||
},
|
||||
} as PayloadRequest,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user