|
@@ -1,10 +1,10 @@
|
|
|
import { APP_UPLOAD_LOCATION } from '@app/common/constants';
|
|
|
import { BadRequestException, Logger, UnauthorizedException } from '@nestjs/common';
|
|
|
import { MulterOptions } from '@nestjs/platform-express/multer/interfaces/multer-options.interface';
|
|
|
-import { randomUUID } from 'crypto';
|
|
|
+import { createHash, randomUUID } from 'crypto';
|
|
|
import { Request } from 'express';
|
|
|
import { existsSync, mkdirSync } from 'fs';
|
|
|
-import { diskStorage } from 'multer';
|
|
|
+import { diskStorage, StorageEngine } from 'multer';
|
|
|
import { extname, join } from 'path';
|
|
|
import sanitize from 'sanitize-filename';
|
|
|
import { AuthUserDto } from '../decorators/auth-user.decorator';
|
|
@@ -12,14 +12,40 @@ import { patchFormData } from '../utils/path-form-data.util';
|
|
|
|
|
|
const logger = new Logger('AssetUploadConfig');
|
|
|
|
|
|
+export interface ImmichFile extends Express.Multer.File {
|
|
|
+ /** sha1 hash of file */
|
|
|
+ checksum: Buffer;
|
|
|
+}
|
|
|
+
|
|
|
export const assetUploadOption: MulterOptions = {
|
|
|
fileFilter,
|
|
|
- storage: diskStorage({
|
|
|
- destination,
|
|
|
- filename,
|
|
|
- }),
|
|
|
+ storage: customStorage(),
|
|
|
};
|
|
|
|
|
|
+export function customStorage(): StorageEngine {
|
|
|
+ const storage = diskStorage({ destination, filename });
|
|
|
+
|
|
|
+ return {
|
|
|
+ _handleFile(req, file, callback) {
|
|
|
+ const hash = createHash('sha1');
|
|
|
+ file.stream.on('data', (chunk) => hash.update(chunk));
|
|
|
+
|
|
|
+ storage._handleFile(req, file, (error, response) => {
|
|
|
+ if (error) {
|
|
|
+ hash.destroy();
|
|
|
+ callback(error);
|
|
|
+ } else {
|
|
|
+ callback(null, { ...response, checksum: hash.digest() } as ImmichFile);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ _removeFile(req, file, callback) {
|
|
|
+ storage._removeFile(req, file, callback);
|
|
|
+ },
|
|
|
+ };
|
|
|
+}
|
|
|
+
|
|
|
export const multerUtils = { fileFilter, filename, destination };
|
|
|
|
|
|
function fileFilter(req: Request, file: any, cb: any) {
|