Co-authored-by: shikhantmaungs <shinkhantmaungs@gmail.com> Co-authored-by: Thomas Ghysels <info@thomasg.be> Co-authored-by: Kokutse Djoguenou <kokutse@Kokutses-MacBook-Pro.local> Co-authored-by: Christian Gil <47041342+ChrisGV04@users.noreply.github.com> Co-authored-by: Łukasz Rabiec <lukaszrabiec@gmail.com> Co-authored-by: Jenny <jennifer.eberlei@gmail.com> Co-authored-by: Hung Vu <hunghvu2017@gmail.com> Co-authored-by: Shin Khant Maung <101539335+shinkhantmaungs@users.noreply.github.com> Co-authored-by: Carlo Brualdi <carlo.brualdi@gmail.com> Co-authored-by: Ariel Tonglet <ariel.tonglet@gmail.com> Co-authored-by: Roman Ryzhikov <general+github@ya.ru> Co-authored-by: maekoya <maekoya@stromatolite.jp> Co-authored-by: Emilia Trollros <3m1l1a@emiliatrollros.se> Co-authored-by: Kokutse J Djoguenou <90865585+Julesdj@users.noreply.github.com> Co-authored-by: Mitch Dries <mitch.dries@gmail.com> BREAKING CHANGE: If you assigned labels to collections, globals or block names, you need to update your config! Your GraphQL schema and generated Typescript interfaces may have changed. Payload no longer uses labels for code based naming. To prevent breaking changes to your GraphQL API and typescript types in your project, you can assign the below properties to match what Payload previously generated for you from labels. On Collections Use `graphQL.singularName`, `graphQL.pluralName` for GraphQL schema names. Use `typescript.interface` for typescript generation name. On Globals Use `graphQL.name` for GraphQL Schema name. Use `typescript.interface` for typescript generation name. On Blocks (within Block fields) Use `graphQL.singularName` for graphQL schema names.
156 lines
3.4 KiB
TypeScript
156 lines
3.4 KiB
TypeScript
import { Field } from '../fields/config/types';
|
|
import { Config } from '../config/types';
|
|
import { CollectionConfig } from '../collections/config/types';
|
|
import { mimeTypeValidator } from './mimeTypeValidator';
|
|
import { IncomingUploadType } from './types';
|
|
import { extractTranslations } from '../translations/extractTranslations';
|
|
|
|
const labels = extractTranslations(['upload:width', 'upload:height', 'upload:fileSize', 'upload:fileName', 'upload:sizes']);
|
|
|
|
type Options = {
|
|
config: Config
|
|
collection: CollectionConfig
|
|
}
|
|
|
|
const getBaseUploadFields = ({ config, collection }: Options): Field[] => {
|
|
const uploadOptions: IncomingUploadType = typeof collection.upload === 'object' ? collection.upload : {};
|
|
|
|
const mimeType: Field = {
|
|
name: 'mimeType',
|
|
label: 'MIME Type',
|
|
type: 'text',
|
|
admin: {
|
|
readOnly: true,
|
|
disabled: true,
|
|
},
|
|
};
|
|
|
|
const url: Field = {
|
|
name: 'url',
|
|
label: 'URL',
|
|
type: 'text',
|
|
admin: {
|
|
readOnly: true,
|
|
disabled: true,
|
|
},
|
|
};
|
|
|
|
const width: Field = {
|
|
name: 'width',
|
|
label: labels['upload:width'],
|
|
type: 'number',
|
|
admin: {
|
|
readOnly: true,
|
|
disabled: true,
|
|
},
|
|
};
|
|
|
|
const height: Field = {
|
|
name: 'height',
|
|
label: labels['upload:height'],
|
|
type: 'number',
|
|
admin: {
|
|
readOnly: true,
|
|
disabled: true,
|
|
},
|
|
};
|
|
|
|
const filesize: Field = {
|
|
name: 'filesize',
|
|
label: labels['upload:fileSize'],
|
|
type: 'number',
|
|
admin: {
|
|
readOnly: true,
|
|
disabled: true,
|
|
},
|
|
};
|
|
|
|
const filename: Field = {
|
|
name: 'filename',
|
|
label: labels['upload:fileName'],
|
|
type: 'text',
|
|
index: true,
|
|
unique: true,
|
|
admin: {
|
|
readOnly: true,
|
|
disabled: true,
|
|
},
|
|
};
|
|
|
|
let uploadFields: Field[] = [
|
|
{
|
|
...url,
|
|
hooks: {
|
|
afterRead: [
|
|
({ data }) => {
|
|
if (data?.filename) {
|
|
return `${config.serverURL}${uploadOptions.staticURL}/${data.filename}`;
|
|
}
|
|
|
|
return undefined;
|
|
},
|
|
],
|
|
},
|
|
},
|
|
filename,
|
|
mimeType,
|
|
filesize,
|
|
width,
|
|
height,
|
|
];
|
|
|
|
if (uploadOptions.mimeTypes) {
|
|
mimeType.validate = mimeTypeValidator(uploadOptions.mimeTypes);
|
|
}
|
|
|
|
if (uploadOptions.imageSizes) {
|
|
uploadFields = uploadFields.concat([
|
|
{
|
|
name: 'sizes',
|
|
label: labels['upload:Sizes'],
|
|
type: 'group',
|
|
admin: {
|
|
disabled: true,
|
|
},
|
|
fields: uploadOptions.imageSizes.map((size) => ({
|
|
label: size.name,
|
|
name: size.name,
|
|
type: 'group',
|
|
admin: {
|
|
disabled: true,
|
|
},
|
|
fields: [
|
|
{
|
|
...url,
|
|
hooks: {
|
|
afterRead: [
|
|
({ data }) => {
|
|
const sizeFilename = data?.sizes?.[size.name]?.filename;
|
|
|
|
if (sizeFilename) {
|
|
return `${config.serverURL}${uploadOptions.staticURL}/${sizeFilename}`;
|
|
}
|
|
|
|
return undefined;
|
|
},
|
|
],
|
|
},
|
|
},
|
|
width,
|
|
height,
|
|
mimeType,
|
|
filesize,
|
|
{
|
|
...filename,
|
|
unique: false,
|
|
},
|
|
],
|
|
})),
|
|
},
|
|
]);
|
|
}
|
|
return uploadFields;
|
|
};
|
|
|
|
export default getBaseUploadFields;
|