import { useEnv } from '@directus/env'; import { InvalidPayloadError, InvalidQueryError, RangeNotSatisfiableError } from '@directus/errors'; import type { Range, TransformationFormat, TransformationParams } from '@directus/types'; import { TransformationMethods } from '@directus/types'; import { getDateTimeFormatted, isValidUuid, parseJSON } from '@directus/utils'; import contentDisposition from 'content-disposition'; import { Router } from 'express'; import { merge, pick } from 'lodash-es'; import * as z from 'zod'; import { fromZodError } from 'zod-validation-error'; import { ASSET_TRANSFORM_QUERY_KEYS, SYSTEM_ASSET_ALLOW_LIST } from '../constants.js'; import getDatabase from '../database/index.js'; import { useLogger } from '../logger/index.js'; import checkIsLocked from '../middleware/is-locked.js'; import useCollection from '../middleware/use-collection.js'; import { validateAccess } from '../permissions/modules/validate-access/validate-access.js'; import { AssetsService } from '../services/assets.js'; import { FilesService } from '../services/files.js'; import { PayloadService } from '../services/payload.js'; import asyncHandler from '../utils/async-handler.js'; import { destroyOnDisconnect } from '../utils/destroy-on-disconnect.js'; import { getCacheControlHeader } from '../utils/get-cache-headers.js'; import { getConfigFromEnv } from '../utils/get-config-from-env.js'; const router = Router(); const env = useEnv(); router.use(useCollection('directus_files')); router.use(checkIsLocked('assets')); router.post( '/folder/:pk', asyncHandler(async (req, res) => { const logger = useLogger(); const service = new AssetsService({ accountability: req.accountability, schema: req.schema, }); const { archive, complete, metadata } = await service.zipFolder(req.params['pk']!); res.setHeader('Content-Type', 'application/zip'); const folderName = `folder-${metadata['name'] ? metadata['name'] : 'unknown'}-${getDateTimeFormatted()}.zip`; res.setHeader('Content-Disposition', contentDisposition(folderName, { type: 'attachment' })); // Clean up the archive stream if the client disconnects, or is already gone const gone = destroyOnDisconnect(res, () => { archive.destroy(); archive.abort(); }); if (gone) return; archive.pipe(res); try { await complete(); } catch (error) { logger.error(error, `Couldn't archive folder ${req.params['pk']} to the client`); archive.destroy(); if (!res.headersSent) { res.removeHeader('Content-Type'); res.removeHeader('Content-Disposition'); res.removeHeader('Cache-Control'); res.status(500).json({ errors: [ { message: 'An unexpected error occurred.', extensions: { code: 'INTERNAL_SERVER_ERROR', }, }, ], }); } else { res.end(); } } }), ); router.post( '/files/', asyncHandler(async (req, res) => { const logger = useLogger(); const service = new AssetsService({ accountability: req.accountability, schema: req.schema, }); const { error, data } = z .object({ ids: z .array( z.string().refine((v) => isValidUuid(v), { error: '"id" must be a uuid', }), ) .min(1), }) .safeParse(req.body); if (error) { throw new InvalidPayloadError({ reason: fromZodError(error).message }); } const { archive, complete } = await service.zipFiles(data.ids); res.setHeader('Content-Type', 'application/zip'); res.setHeader('Content-Disposition', `attachment; filename="files-${getDateTimeFormatted()}.zip"`); // Clean up the archive stream if the client disconnects, or is already gone const gone = destroyOnDisconnect(res, () => { archive.destroy(); archive.abort(); }); if (gone) return; archive.pipe(res); try { await complete(); } catch (error) { logger.error(error, `Couldn't archive files to the client`); archive.destroy(); if (!res.headersSent) { res.removeHeader('Content-Type'); res.removeHeader('Content-Disposition'); res.removeHeader('Cache-Control'); res.status(500).json({ errors: [ { message: 'An unexpected error occurred.', extensions: { code: 'INTERNAL_SERVER_ERROR', }, }, ], }); } else { res.end(); } } }), ); router.get( '/:pk/:filename?', // Validate query params asyncHandler(async (req, res, next) => { const payloadService = new PayloadService('directus_settings', { schema: req.schema }); const defaults = { storage_asset_presets: [], storage_asset_transform: 'all' }; const database = getDatabase(); const savedAssetSettings = await database .select('storage_asset_presets', 'storage_asset_transform') .from('directus_settings') .first(); if (savedAssetSettings) { await payloadService.processValues('read', savedAssetSettings); } const assetSettings = savedAssetSettings || defaults; const transformation = pick(req.query, ASSET_TRANSFORM_QUERY_KEYS); if ('transforms' in transformation) { let transforms: unknown; // Try parse the JSON array try { transforms = parseJSON(transformation['transforms'] as string); } catch { throw new InvalidQueryError({ reason: `"transforms" Parameter needs to be a JSON array of allowed transformations`, }); } // Check if it is actually an array. if (!Array.isArray(transforms)) { throw new InvalidQueryError({ reason: `"transforms" Parameter needs to be a JSON array of allowed transformations`, }); } // Check against ASSETS_TRANSFORM_MAX_OPERATIONS if (transforms.length > env.ASSETS_TRANSFORM_MAX_OPERATIONS) { throw new InvalidQueryError({ reason: `"transforms" Parameter is only allowed ${env.ASSETS_TRANSFORM_MAX_OPERATIONS} transformations`, }); } // Check the transformations are valid transforms.forEach((transform) => { const name = transform[0]; if (!TransformationMethods.includes(name)) { throw new InvalidQueryError({ reason: `"transforms" Parameter does not allow "${name}" as a transformation`, }); } }); transformation['transforms'] = transforms; } const systemKeys = SYSTEM_ASSET_ALLOW_LIST.map((transformation) => transformation['key']!); const allKeys: string[] = [ ...systemKeys, ...(assetSettings.storage_asset_presets || []).map( (transformation: TransformationParams) => transformation['key'], ), ]; // For use in the next request handler res.locals['shortcuts'] = [...SYSTEM_ASSET_ALLOW_LIST, ...(assetSettings.storage_asset_presets || [])]; res.locals['transformation'] = transformation; if ( Object.keys(transformation).length === 0 || ('transforms' in transformation && transformation['transforms']!.length === 0) ) { return next(); } if (assetSettings.storage_asset_transform === 'all') { if (transformation['key'] && allKeys.includes(transformation['key'] as string) === false) { throw new InvalidQueryError({ reason: `Key "${transformation['key']}" isn't configured` }); } return next(); } else if (assetSettings.storage_asset_transform === 'presets') { if (allKeys.includes(transformation['key'] as string) && Object.keys(transformation).length === 1) { return next(); } throw new InvalidQueryError({ reason: `Only configured presets can be used in asset generation` }); } else { if ( transformation['key'] && systemKeys.includes(transformation['key'] as string) && Object.keys(transformation).length === 1 ) { return next(); } throw new InvalidQueryError({ reason: `Dynamic asset generation has been disabled for this project` }); } }), asyncHandler(async (req, res, next) => { const helmet = await import('helmet'); return helmet.contentSecurityPolicy( merge( { useDefaults: false, directives: { defaultSrc: [`'none'`], }, }, getConfigFromEnv('ASSETS_CONTENT_SECURITY_POLICY'), ), )(req, res, next); }), // Return file asyncHandler(async (req, res) => { const logger = useLogger(); const id = req.params['pk']!.substring(0, 36); const service = new AssetsService({ accountability: req.accountability, schema: req.schema, }); const vary = ['Origin', 'Cache-Control']; const transformationParams: TransformationParams = { ...(res.locals['shortcuts'] as TransformationParams[]).find( (transformation) => transformation['key'] === res.locals['transformation']?.key, ), ...res.locals['transformation'], }; let acceptFormat: TransformationFormat | undefined; if (transformationParams.format === 'auto') { if (req.headers.accept?.includes('image/avif')) { acceptFormat = 'avif'; } else if (req.headers.accept?.includes('image/webp')) { acceptFormat = 'webp'; } vary.push('Accept'); } let range: Range | undefined = undefined; if (req.headers.range && Object.keys(transformationParams).length === 0) { const rangeParts = /bytes=([0-9]*)-([0-9]*)/.exec(req.headers.range); if (rangeParts && rangeParts.length > 1) { range = { start: undefined, end: undefined, }; if (rangeParts[1]) { range.start = Number(rangeParts[1]); if (Number.isNaN(range.start)) throw new RangeNotSatisfiableError({ range }); } if (rangeParts[2]) { range.end = Number(rangeParts[2]); if (Number.isNaN(range.end)) throw new RangeNotSatisfiableError({ range }); } } } const revalidate = env.ASSETS_CACHE_REVALIDATE; // Check conditional headers before loading the full asset from storage if (revalidate) { const ifNoneMatch = req.headers['if-none-match']; const ifModifiedSince = req.headers['if-modified-since']; if (ifNoneMatch || ifModifiedSince) { if (req.accountability) { await validateAccess( { accountability: req.accountability, action: 'read', collection: 'directus_files', primaryKeys: [id], }, { knex: getDatabase(), schema: req.schema, }, ); } const filesService = new FilesService({ schema: req.schema, }); const fileRecord = await filesService.readOne(id, { fields: ['modified_on'] }); if (fileRecord?.modified_on) { const modifiedOnTime = new Date(fileRecord.modified_on).getTime(); const etag = `"${Math.floor(modifiedOnTime / 1000)}"`; if (ifNoneMatch === etag) { res.setHeader('Cache-Control', 'max-age=0, must-revalidate'); res.setHeader('ETag', etag); res.setHeader('Last-Modified', new Date(modifiedOnTime).toUTCString()); res.status(304); return res.end(); } if (ifModifiedSince) { const ifModifiedSinceTime = new Date(ifModifiedSince).getTime(); if (Math.floor(modifiedOnTime / 1000) <= Math.floor(ifModifiedSinceTime / 1000)) { res.setHeader('Cache-Control', 'max-age=0, must-revalidate'); res.setHeader('ETag', etag); res.setHeader('Last-Modified', new Date(modifiedOnTime).toUTCString()); res.status(304); return res.end(); } } } } } const { stream, file, stat } = await service.getAsset(id, { transformationParams, acceptFormat }, range, true); const filename = req.params['filename'] ?? file.filename_download ?? file.id; res.attachment(filename); res.setHeader('Content-Type', file.type); res.setHeader('Accept-Ranges', 'bytes'); if (revalidate) { res.setHeader('Cache-Control', 'max-age=0, must-revalidate'); } else { res.setHeader('Cache-Control', getCacheControlHeader(req, env.ASSETS_CACHE_TTL, false, true)); } res.setHeader('Vary', vary.join(', ')); const unixTime = Date.parse(file.modified_on); if (!Number.isNaN(unixTime)) { const lastModifiedDate = new Date(unixTime); res.setHeader('Last-Modified', lastModifiedDate.toUTCString()); res.setHeader('ETag', `"${Math.floor(unixTime / 1000)}"`); } if (range) { res.setHeader('Content-Range', `bytes ${range.start}-${range.end || stat.size - 1}/${stat.size}`); res.status(206); res.setHeader('Content-Length', (range.end ? range.end + 1 : stat.size) - (range.start || 0)); } else { res.setHeader('Content-Length', stat.size); } if ('download' in req.query === false) { res.setHeader('Content-Disposition', contentDisposition(filename, { type: 'inline' })); } if (req.method.toLowerCase() === 'head') { res.status(200); res.setHeader('Accept-Ranges', 'bytes'); res.setHeader('Content-Length', stat.size); return res.end(); } const sourceStream = await stream(); // Clean up the source stream if the client disconnects, or is already gone if (destroyOnDisconnect(res, () => sourceStream.destroy())) { return undefined; } sourceStream .on('error', (error) => { logger.error(error, `Couldn't stream file ${file.id} to the client`); sourceStream.destroy(); if (!res.headersSent) { res.removeHeader('Content-Type'); res.removeHeader('Content-Disposition'); res.removeHeader('Cache-Control'); res.status(500).json({ errors: [ { message: 'An unexpected error occurred.', extensions: { code: 'INTERNAL_SERVER_ERROR', }, }, ], }); } else { res.end(); } }) .pipe(res); return undefined; }), ); export default router;