/
files.ts
517 lines (422 loc) · 15 KB
/
files.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
import { useEnv } from '@directus/env';
import { ContentTooLargeError, ForbiddenError, InvalidPayloadError, ServiceUnavailableError } from '@directus/errors';
import formatTitle from '@directus/format-title';
import type { BusboyFileStream, File, PrimaryKey } from '@directus/types';
import { toArray } from '@directus/utils';
import type { AxiosResponse } from 'axios';
import encodeURL from 'encodeurl';
import exif, { type GPSInfoTags, type ImageTags, type IopTags, type PhotoTags } from 'exif-reader';
import type { IccProfile } from 'icc';
import { parse as parseIcc } from 'icc';
import { clone, pick } from 'lodash-es';
import { extension } from 'mime-types';
import type { Readable } from 'node:stream';
import { PassThrough as PassThroughStream, Transform as TransformStream } from 'node:stream';
import { pipeline } from 'node:stream/promises';
import zlib from 'node:zlib';
import path from 'path';
import sharp from 'sharp';
import url from 'url';
import { SUPPORTED_IMAGE_METADATA_FORMATS } from '../constants.js';
import emitter from '../emitter.js';
import { useLogger } from '../logger.js';
import { getAxios } from '../request/index.js';
import { getStorage } from '../storage/index.js';
import type { AbstractServiceOptions, MutationOptions } from '../types/index.js';
import { parseIptc, parseXmp } from '../utils/parse-image-metadata.js';
import { ItemsService } from './items.js';
const env = useEnv();
const logger = useLogger();
type Metadata = Partial<Pick<File, 'height' | 'width' | 'description' | 'title' | 'tags' | 'metadata'>>;
export class FilesService extends ItemsService {
constructor(options: AbstractServiceOptions) {
super('directus_files', options);
}
/**
* Upload a single new file to the configured storage adapter
*/
async uploadOne(
stream: BusboyFileStream | Readable,
data: Partial<File> & { storage: string },
primaryKey?: PrimaryKey,
opts?: MutationOptions,
): Promise<PrimaryKey> {
const storage = await getStorage();
let existingFile: Record<string, any> | null = null;
// If the payload contains a primary key, we'll check if the file already exists
if (primaryKey !== undefined) {
// If the file you're uploading already exists, we'll consider this upload a replace so we'll fetch the existing file's folder and filename_download
existingFile =
(await this.knex
.select('folder', 'filename_download', 'filename_disk', 'title', 'description', 'metadata')
.from('directus_files')
.where({ id: primaryKey })
.first()) ?? null;
}
// Merge the existing file's folder and filename_download with the new payload
const payload = { ...(existingFile ?? {}), ...clone(data) };
const disk = storage.location(payload.storage);
// If no folder is specified, we'll use the default folder from the settings if it exists
if ('folder' in payload === false) {
const settings = await this.knex.select('storage_default_folder').from('directus_settings').first();
if (settings?.storage_default_folder) {
payload.folder = settings.storage_default_folder;
}
}
// Is this file a replacement? if the file data already exists and we have a primary key
const isReplacement = existingFile !== null && primaryKey !== undefined;
// If this is a new file upload, we need to generate a new primary key and DB record
if (isReplacement === false || primaryKey === undefined) {
primaryKey = await this.createOne(payload, { emitEvents: false });
}
const fileExtension =
path.extname(payload.filename_download!) || (payload.type && '.' + extension(payload.type)) || '';
// The filename_disk is the FINAL filename on disk
payload.filename_disk = primaryKey + (fileExtension || '');
// Temp filename is used for replacements
const tempFilenameDisk = 'temp_' + payload.filename_disk;
if (!payload.type) {
payload.type = 'application/octet-stream';
}
// Used to clean up if something goes wrong
const cleanUp = async () => {
try {
if (isReplacement === true) {
// If this is a replacement that failed, we need to delete the temp file
await disk.delete(tempFilenameDisk);
} else {
// If this is a new file that failed
// delete the DB record
await super.deleteMany([primaryKey!]);
// delete the final file
await disk.delete(payload.filename_disk!);
}
} catch (err: any) {
if (isReplacement === true) {
logger.warn(`Couldn't delete temp file ${tempFilenameDisk}`);
} else {
logger.warn(`Couldn't delete file ${payload.filename_disk}`);
}
logger.warn(err);
}
};
try {
// If this is a replacement, we'll write the file to a temp location first to ensure we don't overwrite the existing file if something goes wrong
if (isReplacement === true) {
await disk.write(tempFilenameDisk, stream, payload.type);
} else {
// If this is a new file upload, we'll write the file to the final location
await disk.write(payload.filename_disk, stream, payload.type);
}
// Check if the file was truncated (if the stream ended early) and throw limit error if it was
if ('truncated' in stream && stream.truncated === true) {
throw new ContentTooLargeError();
}
} catch (err: any) {
logger.warn(`Couldn't save file ${payload.filename_disk}`);
logger.warn(err);
await cleanUp();
if (err instanceof ContentTooLargeError) {
throw err;
} else {
throw new ServiceUnavailableError({ service: 'files', reason: `Couldn't save file ${payload.filename_disk}` });
}
}
// If the file is a replacement, we need to update the DB record with the new payload, delete the old files, and upgrade the temp file
if (isReplacement === true) {
await this.updateOne(primaryKey, payload, { emitEvents: false });
// delete the previously saved file and thumbnails to ensure they're generated fresh
for await (const filepath of disk.list(String(primaryKey))) {
await disk.delete(filepath);
}
// Upgrade the temp file to the final filename
await disk.move(tempFilenameDisk, payload.filename_disk);
}
const { size } = await storage.location(data.storage).stat(payload.filename_disk);
payload.filesize = size;
if (SUPPORTED_IMAGE_METADATA_FORMATS.includes(payload.type)) {
const stream = await storage.location(data.storage).read(payload.filename_disk);
const { height, width, description, title, tags, metadata } = await this.getMetadata(stream);
if (!payload.height && height) {
payload.height = height;
}
if (!payload.width && width) {
payload.width = width;
}
if (!payload.metadata && metadata) {
payload.metadata = metadata;
}
// Note that if this is a replace file upload, the below properties are fetched and included in the payload above
// in the `existingFile` variable... so this will ONLY set the values if they're not already set
if (!payload.description && description) {
payload.description = description;
}
if (!payload.title && title) {
payload.title = title;
}
if (!payload.tags && tags) {
payload.tags = tags;
}
}
// We do this in a service without accountability. Even if you don't have update permissions to the file,
// we still want to be able to set the extracted values from the file on create
const sudoService = new ItemsService('directus_files', {
knex: this.knex,
schema: this.schema,
});
await sudoService.updateOne(primaryKey, payload, { emitEvents: false });
if (opts?.emitEvents !== false) {
emitter.emitAction(
'files.upload',
{
payload,
key: primaryKey,
collection: this.collection,
},
{
database: this.knex,
schema: this.schema,
accountability: this.accountability,
},
);
}
return primaryKey;
}
/**
* Extract metadata from a buffer's content
*/
async getMetadata(
stream: Readable,
allowList: string | string[] = env['FILE_METADATA_ALLOW_LIST'] as string[],
): Promise<Metadata> {
return new Promise((resolve, reject) => {
pipeline(
stream,
sharp().metadata(async (err, sharpMetadata) => {
if (err) {
reject(err);
return;
}
const metadata: Metadata = {};
if (sharpMetadata.orientation && sharpMetadata.orientation >= 5) {
metadata.height = sharpMetadata.width ?? null;
metadata.width = sharpMetadata.height ?? null;
} else {
metadata.width = sharpMetadata.width ?? null;
metadata.height = sharpMetadata.height ?? null;
}
// Backward-compatible layout as it used to be with 'exifr'
const fullMetadata: {
ifd0?: Partial<ImageTags>;
ifd1?: Partial<ImageTags>;
exif?: Partial<PhotoTags>;
gps?: Partial<GPSInfoTags>;
interop?: Partial<IopTags>;
icc?: IccProfile;
iptc?: Record<string, unknown>;
xmp?: Record<string, unknown>;
} = {};
if (sharpMetadata.exif) {
try {
const { Image, ThumbnailTags, Iop, GPSInfo, Photo } = (exif as unknown as typeof exif.default)(
sharpMetadata.exif,
);
if (Image) {
fullMetadata.ifd0 = Image;
}
if (ThumbnailTags) {
fullMetadata.ifd1 = ThumbnailTags;
}
if (Iop) {
fullMetadata.interop = Iop;
}
if (GPSInfo) {
fullMetadata.gps = GPSInfo;
}
if (Photo) {
fullMetadata.exif = Photo;
}
} catch (err) {
logger.warn(`Couldn't extract Exif metadata from file`);
logger.warn(err);
}
}
if (sharpMetadata.icc) {
try {
fullMetadata.icc = parseIcc(sharpMetadata.icc);
} catch (err) {
logger.warn(`Couldn't extract ICC profile data from file`);
logger.warn(err);
}
}
if (sharpMetadata.iptc) {
try {
fullMetadata.iptc = parseIptc(sharpMetadata.iptc);
} catch (err) {
logger.warn(`Couldn't extract IPTC Photo Metadata from file`);
logger.warn(err);
}
}
if (sharpMetadata.xmp) {
try {
fullMetadata.xmp = parseXmp(sharpMetadata.xmp);
} catch (err) {
logger.warn(`Couldn't extract XMP data from file`);
logger.warn(err);
}
}
if (fullMetadata?.iptc?.['Caption'] && typeof fullMetadata.iptc['Caption'] === 'string') {
metadata.description = fullMetadata.iptc?.['Caption'];
}
if (fullMetadata?.iptc?.['Headline'] && typeof fullMetadata.iptc['Headline'] === 'string') {
metadata.title = fullMetadata.iptc['Headline'];
}
if (fullMetadata?.iptc?.['Keywords']) {
metadata.tags = fullMetadata.iptc['Keywords'] as string;
}
if (allowList === '*' || allowList?.[0] === '*') {
metadata.metadata = fullMetadata;
} else {
metadata.metadata = pick(fullMetadata, allowList);
}
// Fix (incorrectly parsed?) values starting / ending with spaces,
// limited to one level and string values only
for (const section of Object.keys(metadata.metadata)) {
for (const [key, value] of Object.entries(metadata.metadata[section])) {
if (typeof value === 'string') {
metadata.metadata[section][key] = value.trim();
}
}
}
resolve(metadata);
}),
);
});
}
/**
* Import a single file from an external URL
*/
async importOne(importURL: string, body: Partial<File>): Promise<PrimaryKey> {
const fileCreatePermissions = this.accountability?.permissions?.find(
(permission) => permission.collection === 'directus_files' && permission.action === 'create',
);
if (this.accountability && this.accountability?.admin !== true && !fileCreatePermissions) {
throw new ForbiddenError();
}
let fileResponse;
try {
const axios = await getAxios();
fileResponse = await axios.get<Readable>(encodeURL(importURL), {
responseType: 'stream',
decompress: false,
});
} catch (error: any) {
logger.warn(`Couldn't fetch file from URL "${importURL}"${error.message ? `: ${error.message}` : ''}`);
logger.trace(error);
throw new ServiceUnavailableError({
service: 'external-file',
reason: `Couldn't fetch file from URL "${importURL}"`,
});
}
const parsedURL = url.parse(fileResponse.request.res.responseUrl);
const filename = decodeURI(path.basename(parsedURL.pathname as string));
const payload = {
filename_download: filename,
storage: toArray(env['STORAGE_LOCATIONS'] as string)[0]!,
type: fileResponse.headers['content-type'],
title: formatTitle(filename),
...(body || {}),
};
return await this.uploadOne(decompressResponse(fileResponse.data, fileResponse.headers), payload, payload.id);
}
/**
* Create a file (only applicable when it is not a multipart/data POST request)
* Useful for associating metadata with existing file in storage
*/
override async createOne(data: Partial<File>, opts?: MutationOptions): Promise<PrimaryKey> {
if (!data.type) {
throw new InvalidPayloadError({ reason: `"type" is required` });
}
const key = await super.createOne(data, opts);
return key;
}
/**
* Delete a file
*/
override async deleteOne(key: PrimaryKey): Promise<PrimaryKey> {
await this.deleteMany([key]);
return key;
}
/**
* Delete multiple files
*/
override async deleteMany(keys: PrimaryKey[]): Promise<PrimaryKey[]> {
const storage = await getStorage();
const files = await super.readMany(keys, { fields: ['id', 'storage', 'filename_disk'], limit: -1 });
if (!files) {
throw new ForbiddenError();
}
await super.deleteMany(keys);
for (const file of files) {
const disk = storage.location(file['storage']);
const filePrefix = path.parse(file['filename_disk']).name;
// Delete file + thumbnails
for await (const filepath of disk.list(filePrefix)) {
await disk.delete(filepath);
}
}
return keys;
}
}
function decompressResponse(stream: Readable, headers: AxiosResponse['headers']) {
const contentEncoding = (headers['content-encoding'] || '').toLowerCase();
if (!['gzip', 'deflate', 'br'].includes(contentEncoding)) {
return stream;
}
let isEmpty = true;
const checker = new TransformStream({
transform(data, _encoding, callback) {
if (isEmpty === false) {
callback(null, data);
return;
}
isEmpty = false;
handleContentEncoding(data);
callback(null, data);
},
flush(callback) {
callback();
},
});
const finalStream = new PassThroughStream({
autoDestroy: false,
destroy(error, callback) {
stream.destroy();
callback(error);
},
});
stream.pipe(checker);
return finalStream;
function handleContentEncoding(data: any) {
let decompressStream;
if (contentEncoding === 'br') {
decompressStream = zlib.createBrotliDecompress();
} else if (contentEncoding === 'deflate' && isDeflateAlgorithm(data)) {
decompressStream = zlib.createInflateRaw();
} else {
decompressStream = zlib.createUnzip();
}
decompressStream.once('error', (error) => {
if (isEmpty && !stream.readable) {
finalStream.end();
return;
}
finalStream.destroy(error);
});
checker.pipe(decompressStream).pipe(finalStream);
}
function isDeflateAlgorithm(data: any) {
const DEFLATE_ALGORITHM_HEADER = 0x08;
return data.length > 0 && (data[0] & DEFLATE_ALGORITHM_HEADER) === 0;
}
}