feat: File argument in create/update operation (#708)

Co-authored-by: Dan Ribbens <dan.ribbens@gmail.com>
This commit is contained in:
Henri Tuan
2022-07-06 20:26:04 +02:00
committed by GitHub
parent 67331eb975
commit f3b7dcff57
4 changed files with 40 additions and 2 deletions

View File

@@ -102,6 +102,10 @@ const post = await payload.create({
// a file directly through the Local API by providing // a file directly through the Local API by providing
// its full, absolute file path. // its full, absolute file path.
filePath: path.resolve(__dirname, './path-to-image.jpg'), 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,
}) })
``` ```

View File

@@ -4,6 +4,8 @@ import { PayloadRequest } from '../../../express/types';
import { Document } from '../../../types'; import { Document } from '../../../types';
import getFileByPath from '../../../uploads/getFileByPath'; import getFileByPath from '../../../uploads/getFileByPath';
import create from '../create'; import create from '../create';
import { File } from '../../../uploads/types';
export type Options<T> = { export type Options<T> = {
collection: string collection: string
@@ -16,6 +18,7 @@ export type Options<T> = {
disableVerificationEmail?: boolean disableVerificationEmail?: boolean
showHiddenFields?: boolean showHiddenFields?: boolean
filePath?: string filePath?: string
file?: File
overwriteExistingFiles?: boolean overwriteExistingFiles?: boolean
req?: PayloadRequest req?: PayloadRequest
draft?: boolean draft?: boolean
@@ -33,6 +36,7 @@ export default async function createLocal<T = any>(payload: Payload, options: Op
disableVerificationEmail, disableVerificationEmail,
showHiddenFields, showHiddenFields,
filePath, filePath,
file,
overwriteExistingFiles = false, overwriteExistingFiles = false,
req, req,
draft, draft,
@@ -57,7 +61,7 @@ export default async function createLocal<T = any>(payload: Payload, options: Op
fallbackLocale: fallbackLocale || req?.fallbackLocale || null, fallbackLocale: fallbackLocale || req?.fallbackLocale || null,
payload, payload,
files: { files: {
file: getFileByPath(filePath) as UploadedFile, file: file ?? getFileByPath(filePath),
}, },
} as PayloadRequest, } as PayloadRequest,
}); });

View File

@@ -1,6 +1,8 @@
import fs from 'fs'; import fs from 'fs';
import path from 'path'; import path from 'path';
import { UploadedFile } from 'express-fileupload';
import payload from '../../..'; import payload from '../../..';
import getFileByPath from '../../../uploads/getFileByPath';
let createdMediaID; let createdMediaID;
@@ -34,6 +36,32 @@ describe('Collections - Local', () => {
expect(result.filesize).toStrictEqual(size); expect(result.filesize).toStrictEqual(size);
createdMediaID = result.id; 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', () => { describe('Update', () => {

View File

@@ -15,6 +15,7 @@ export type Options<T> = {
overrideAccess?: boolean overrideAccess?: boolean
showHiddenFields?: boolean showHiddenFields?: boolean
filePath?: string filePath?: string
file?: File
overwriteExistingFiles?: boolean overwriteExistingFiles?: boolean
draft?: boolean draft?: boolean
autosave?: boolean autosave?: boolean
@@ -32,6 +33,7 @@ export default async function updateLocal<T = any>(payload: Payload, options: Op
overrideAccess = true, overrideAccess = true,
showHiddenFields, showHiddenFields,
filePath, filePath,
file,
overwriteExistingFiles = false, overwriteExistingFiles = false,
draft, draft,
autosave, autosave,
@@ -57,7 +59,7 @@ export default async function updateLocal<T = any>(payload: Payload, options: Op
fallbackLocale, fallbackLocale,
payload, payload,
files: { files: {
file: getFileByPath(filePath), file: file ?? getFileByPath(filePath),
}, },
} as PayloadRequest, } as PayloadRequest,
}; };