Skip to content

Instantly share code, notes, and snippets.

@ainsleyclark
Created March 12, 2025 21:49
Show Gist options
  • Select an option

  • Save ainsleyclark/95de34ae3ae17e33d2f306aceb7060e1 to your computer and use it in GitHub Desktop.

Select an option

Save ainsleyclark/95de34ae3ae17e33d2f306aceb7060e1 to your computer and use it in GitHub Desktop.
import { imageSizes } from '@ainsleydev/payload-helper/dist/collections/Media';
import env from '@ainsleydev/payload-helper/dist/util/env';
import { lexicalHTML } from '@payloadcms/richtext-lexical';
import * as mime from 'mime-types';
import { findBySource } from '@/collections/Media/endpoints/find-by-source';
import type { CollectionConfig, PayloadRequest } from 'payload';
export const Media: CollectionConfig = {
slug: 'media',
dbName: 'media',
timestamps: false,
labels: {
singular: 'Media',
plural: 'Media Items',
},
versions: false,
access: {
read: () => true,
},
admin: {
defaultColumns: ['id', 'filename', 'url', 'source'],
},
endpoints: [
{
path: '/source',
method: 'get',
handler: findBySource,
},
],
fields: [
{
name: 'alt',
type: 'text',
label: 'Alternative Text',
},
{
name: 'caption',
type: 'richText',
label: 'Caption',
// editor: lexicalEditor({
// features: ({defaultFeatures}) => {
// const def = defaultFeatures.filter((feature) => {
// return feature.key === 'paragraph' || feature.key === 'link';
// })
// return [
// ...def,
// HTMLConverterFeature({}),
// ];
// },
// }),
},
lexicalHTML('caption', {
name: 'captionHtml',
storeInDB: true,
}),
{
name: 'source',
label: 'Source',
type: 'text',
defaultValue: 'user',
required: true,
},
{
name: 'sourceUrl',
label: 'Source URL',
type: 'text',
unique: true,
},
{
name: 'meta',
type: 'json',
defaultValue: '{}',
required: true,
},
],
upload: {
staticDir: '../.store/payload',
disableLocalStorage: env.isProduction,
adminThumbnail: 'desktop',
displayPreview: true,
crop: false,
formatOptions: {
format: 'jpg',
},
imageSizes: imageSizes,
handlers: [
async (req: PayloadRequest, args) => {
const logger = req.payload.logger;
const {params} = args;
const {collection, filename} = params;
if (collection !== 'media') {
return;
}
const contentType = mime.lookup(filename);
if (!contentType) {
logger.error(`Unable to find mime type for file: ${filename}`);
return;
}
const headers = new Headers();
headers.set('Content-Type', contentType);
headers.set('Cache-Control', 'public, max-age=31536000');
req.responseHeaders = headers;
},
],
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment