feat(server): Some tests and specific handling for the two raw formats

This commit is contained in:
Skyler Mäntysaari
2023-02-02 12:36:20 +02:00
parent 591d3b7afe
commit c9d618ed27
2 changed files with 51 additions and 25 deletions
@@ -55,7 +55,7 @@ describe('assetUploadOption', () => {
}); });
it('should allow videos', async () => { it('should allow videos', async () => {
const file = { mimetype: 'image/mp4', originalname: 'test.mp4' } as any; const file = { mimetype: 'video/mp4', originalname: 'test.mp4' } as any;
fileFilter(mock.userRequest, file, callback); fileFilter(mock.userRequest, file, callback);
expect(callback).toHaveBeenCalledWith(null, true); expect(callback).toHaveBeenCalledWith(null, true);
}); });
@@ -66,17 +66,40 @@ describe('assetUploadOption', () => {
expect(callback).toHaveBeenCalledWith(null, true); expect(callback).toHaveBeenCalledWith(null, true);
}); });
// TODO: Fix it when we have a API way to get accepted mimetypes. it('should allow .srw unrecognized', () => {
// it('should not allow unknown types', async () => { const file = { mimetype: 'application/octet-stream', originalname: 'test.srw' } as any;
// const file = { mimetype: 'application/html', originalname: 'test.html' } as any; fileFilter(mock.userRequest, file, callback);
// const callback = jest.fn(); expect(callback).toHaveBeenCalledWith(null, true);
// fileFilter(mock.userRequest, file, callback); });
//
// expect(callback).toHaveBeenCalled(); it('should allow .raf recognized', () => {
// const [error, accepted] = callback.mock.calls[0]; const file = { mimetype: 'image/x-fuji-raf', originalname: 'test.raf' } as any;
// expect(error).toBeDefined(); fileFilter(mock.userRequest, file, callback);
// expect(accepted).toBe(false); expect(callback).toHaveBeenCalledWith(null, true);
// }); });
it('should allow .raf unrecognized', () => {
const file = { mimetype: 'application/octet-stream', originalname: 'test.raf' } as any;
fileFilter(mock.userRequest, file, callback);
expect(callback).toHaveBeenCalledWith(null, true);
});
it('should allow .srw recognized', () => {
const file = { mimetype: 'image/x-samsung-srw', originalname: 'test.srw' } as any;
fileFilter(mock.userRequest, file, callback);
expect(callback).toHaveBeenCalledWith(null, true);
});
it('should not allow unknown types', async () => {
const file = { mimetype: 'application/html', originalname: 'test.html' } as any;
const callback = jest.fn();
fileFilter(mock.userRequest, file, callback);
expect(callback).toHaveBeenCalled();
const [error, accepted] = callback.mock.calls[0];
expect(error).toBeDefined();
expect(accepted).toBe(false);
});
}); });
describe('destination', () => { describe('destination', () => {
@@ -1,11 +1,11 @@
import { APP_UPLOAD_LOCATION } from '@app/common/constants'; import { APP_UPLOAD_LOCATION } from '@app/common/constants';
import { UnauthorizedException } from '@nestjs/common'; import { BadRequestException, Logger, UnauthorizedException } from '@nestjs/common';
import { MulterOptions } from '@nestjs/platform-express/multer/interfaces/multer-options.interface'; import { MulterOptions } from '@nestjs/platform-express/multer/interfaces/multer-options.interface';
import { createHash, randomUUID } from 'crypto'; import { createHash, randomUUID } from 'crypto';
import { Request } from 'express'; import { Request } from 'express';
import { existsSync, mkdirSync } from 'fs'; import { existsSync, mkdirSync } from 'fs';
import { diskStorage, StorageEngine } from 'multer'; import { diskStorage, StorageEngine } from 'multer';
import { join } from 'path'; import { extname, join } from 'path';
import sanitize from 'sanitize-filename'; import sanitize from 'sanitize-filename';
import { AuthUserDto } from '../decorators/auth-user.decorator'; import { AuthUserDto } from '../decorators/auth-user.decorator';
import { patchFormData } from '../utils/path-form-data.util'; import { patchFormData } from '../utils/path-form-data.util';
@@ -46,6 +46,8 @@ export function customStorage(): StorageEngine {
export const multerUtils = { fileFilter, filename, destination }; export const multerUtils = { fileFilter, filename, destination };
const logger = new Logger('AssetUploadConfig');
function fileFilter(req: Request, file: any, cb: any) { function fileFilter(req: Request, file: any, cb: any) {
if (!req.user || (req.user.isPublicUser && !req.user.isAllowUpload)) { if (!req.user || (req.user.isPublicUser && !req.user.isAllowUpload)) {
return cb(new UnauthorizedException()); return cb(new UnauthorizedException());
@@ -53,17 +55,18 @@ function fileFilter(req: Request, file: any, cb: any) {
// TODO: Create new API endpoint for mimetypes and use that here as browser's // TODO: Create new API endpoint for mimetypes and use that here as browser's
// file mimetype is not to be trusted. // file mimetype is not to be trusted.
// Reference about issue with it: https://stackoverflow.com/questions/26149389/mime-type-missing-for-rar-and-tar/26222177#26222177 // Reference about issue with it: https://stackoverflow.com/questions/26149389/mime-type-missing-for-rar-and-tar/26222177#26222177
cb(null, true); if (
//if ( file.mimetype.match(
// file.mimetype.match( /\/(jpg|jpeg|png|gif|mp4|webm|x-msvideo|quicktime|heic|heif|dng|x-adobe-dng|webp|tiff|3gpp|nef|x-nikon-nef|x-fuji-raf|x-samsung-srw)$/,
// /\/(jpg|jpeg|png|gif|mp4|webm|x-msvideo|quicktime|heic|heif|dng|x-adobe-dng|webp|tiff|3gpp|nef|x-nikon-nef)$/, ) ||
// ) (file.mimetype.match('application/octet-stream') && extname(file.originalname.toLowerCase()) == '.raf') ||
//) { (file.mimetype.match('application/octet-stream') && extname(file.originalname.toLowerCase()) == '.srw')
// cb(null, true); ) {
//} else { cb(null, true);
// logger.error(`Unsupported file type ${extname(file.originalname)} file MIME type ${filetype}`); } else {
// cb(new BadRequestException(`Unsupported file type ${extname(file.originalname)}`), false); logger.error(`Unsupported file type ${extname(file.originalname)} file MIME type ${file.mimetype}`);
// } cb(new BadRequestException(`Unsupported file type ${extname(file.originalname)}`), false);
}
} }
function destination(req: Request, file: Express.Multer.File, cb: any) { function destination(req: Request, file: Express.Multer.File, cb: any) {