Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ft vil 381/upload files #914

Merged
merged 3 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
},
"eslint.nodePath": ".yarn/sdks",
"prettier.prettierPath": ".yarn/sdks/prettier/index.js",
"prettier.configPath": ".prettierrc.js",
"typescript.tsdk": ".yarn/sdks/typescript/lib",
"typescript.enablePromptUseWorkspaceTsdk": true,
"editor.formatOnSave": true,
Expand Down
138 changes: 138 additions & 0 deletions server/__tests__/fileUpload.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import type { Response, Request } from 'express';
import fs from 'fs';
import path from 'path';

import { uploadFiles } from '../controllers/filesController';
import type { User } from '../entities/user';
import { AwsS3 } from '../fileUpload/s3';
import { AppError } from '../middlewares/handleErrors';
import { appDataSource, fakeUser } from './mock';

beforeAll(() => {
return appDataSource.initialize();
});
beforeEach(() => {});
afterAll(() => {
return appDataSource.destroy();
});

const dummyPdf = fs.readFileSync(path.join(__dirname, 'files/dummy.pdf'));
describe('Upload files', () => {
test('Should return an url array', async () => {
jest.spyOn(AwsS3.prototype, 'uploadFile').mockImplementationOnce(() => {
return Promise.resolve('url/test');
});
jest.spyOn(AwsS3.prototype, 'uploadS3File').mockImplementationOnce(() => {
return Promise.resolve('url/test');
});

const mockRequest: Partial<Request> = {
user: fakeUser as User,
files: [
{
buffer: dummyPdf,
fieldname: 'files',
filename: 'dummy.pdf',
mimetype: 'application/pdf',
originalname: 'dummy.pdf',
path: 'server/__tests__/files',
size: 1000,
destination: 'server/__tests__/files',
} as Express.Multer.File,
],
};

const res = {} as unknown as Response;
res.json = jest.fn();
res.status = jest.fn(() => res); // chained

await uploadFiles(mockRequest as Request, res as Response);
expect(res.status).toHaveBeenCalledWith(200);
expect(res.json).toHaveBeenCalledWith(['url/test']);
});
test('Should throw - files are missing -', async () => {
const mockRequest: Partial<Request> = {
user: fakeUser as User,
files: [],
};

const res = {} as unknown as Response;
res.json = jest.fn();
res.status = jest.fn(() => res); // chained

await uploadFiles(mockRequest as Request, res as Response);
expect(res.status).toHaveBeenCalledWith(400);
expect(res.json).toHaveBeenCalledWith('Files are missing');
});
test('User forbiden', async () => {
const mockRequest: Partial<Request> = {
files: [],
};

const res = {} as unknown as Response;
res.json = jest.fn();
res.status = jest.fn(() => res); // chained

await uploadFiles(mockRequest as Request, res as Response);
expect(res.status).toHaveBeenCalledWith(400);
expect(res.json).toHaveBeenCalledWith('Forbidden');
});
test('Should throw an app error', async () => {
jest.spyOn(AwsS3.prototype, 'uploadFile').mockImplementationOnce(() => {
throw new AppError('Yolo', 0);
});

const mockRequest: Partial<Request> = {
user: fakeUser as User,
files: [
{
buffer: dummyPdf,
fieldname: 'files',
filename: 'dummy.pdf',
mimetype: 'application/pdf',
originalname: 'dummy.pdf',
path: 'server/__tests__/files',
size: 1000,
destination: 'server/__tests__/files',
} as Express.Multer.File,
],
};

const res = {} as unknown as Response;
res.json = jest.fn();
res.status = jest.fn(() => res); // chained

await uploadFiles(mockRequest as Request, res as Response);
expect(res.status).toHaveBeenCalledWith(400);
expect(res.json).toHaveBeenCalledWith('Yolo');
});
test('Should throw an unknown error', async () => {
jest.spyOn(AwsS3.prototype, 'uploadFile').mockImplementationOnce(() => {
throw new Error('Yolo');
});

const mockRequest: Partial<Request> = {
user: fakeUser as User,
files: [
{
buffer: dummyPdf,
fieldname: 'files',
filename: 'dummy.pdf',
mimetype: 'application/pdf',
originalname: 'dummy.pdf',
path: 'server/__tests__/files',
size: 1000,
destination: 'server/__tests__/files',
} as Express.Multer.File,
],
};

const res = {} as unknown as Response;
res.json = jest.fn();
res.status = jest.fn(() => res); // chained

await uploadFiles(mockRequest as Request, res as Response);
expect(res.status).toHaveBeenCalledWith(500);
expect(res.json).toHaveBeenCalledWith('Internal server error');
});
});
Binary file added server/__tests__/files/dummy-image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added server/__tests__/files/dummy.pdf
Binary file not shown.
3 changes: 2 additions & 1 deletion server/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { handleErrors } from './middlewares/handleErrors';
import { jsonify } from './middlewares/jsonify';
import { setVillage } from './middlewares/setVillage';
import { removeTrailingSlash } from './middlewares/trailingSlash';
import { filesRouter } from './routes/filesRouter';
import { connectToDatabase } from './utils/database';
import { logger } from './utils/logger';
import { getDefaultDirectives } from './utils/server';
Expand Down Expand Up @@ -90,11 +91,11 @@ export async function getApp() {
res.status(200).send('Hello World 1Village!');
});
backRouter.use(controllerRouter);
backRouter.use('/files', filesRouter);
backRouter.use((_, res: Response) => {
res.status(404).send('Error 404 - Not found.');
});
app.use('/api', backRouter);

// [4-bis] --- Add h5p ---
if (process.env.DYNAMODB_REGION) {
try {
Expand Down
12 changes: 1 addition & 11 deletions server/controllers/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,18 @@ import { Router } from 'express';
import fs from 'fs-extra';
import multer from 'multer';
import path from 'path';
import { v4 as uuidv4 } from 'uuid';

import type { UserType } from '../entities/user';
import { authenticate } from '../middlewares/authenticate';
import { handleErrors } from '../middlewares/handleErrors';
import { diskStorage } from './multer';

type RouteOptions = {
path: string;
userType?: UserType;
};

fs.ensureDir(path.join(__dirname, '../fileUpload/videos')).catch();
const diskStorage = multer.diskStorage({
destination: function (_req, _file, cb) {
cb(null, path.join(__dirname, '../fileUpload/videos/'));
},
filename: function (_req, file, cb) {
const uuid = uuidv4();
cb(null, `${uuid}${path.extname(file.originalname)}`);
},
});

export class Controller {
private uploadMiddleware = multer({ storage: multer.memoryStorage() });
private uploadVideoMiddleware = multer({ storage: diskStorage });
Expand Down
33 changes: 33 additions & 0 deletions server/controllers/filesController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type { Request, Response } from 'express';

import { uploadFile } from '../fileUpload';
import { AppError, ErrorCode } from '../middlewares/handleErrors';

export async function uploadFiles(req: Request, res: Response) {
try {
const user = req.user;
if (!user) {
throw new AppError('Forbidden', ErrorCode.UNKNOWN);
}
const { files } = req;
if (!files || !files.length) {
throw new AppError('Files are missing', ErrorCode.UNKNOWN);
}
const promises: Promise<string | null>[] = [];
for (const file of files as Express.Multer.File[]) {
// making the filename being the path here is a trick to use
// upload function...
const filename = `images/${user.id}/${file.filename}`;
const promise = uploadFile(filename, file.mimetype);
promises.push(promise);
}
const results = await Promise.all(promises);
res.status(200).json(results);
} catch (error) {
if (error instanceof AppError) {
res.status(400).json(error.message);
} else {
res.status(500).json('Internal server error');
}
}
}
46 changes: 46 additions & 0 deletions server/controllers/multer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import fs from 'fs';
import multer from 'multer';
import path from 'path';
import { v4 } from 'uuid';

export const diskStorage = multer.diskStorage({
destination: function (_req, _file, cb) {
cb(null, path.join(__dirname, '../fileUpload/videos/'));
},
filename: function (_req, file, cb) {
const uuid = v4();
cb(null, `${uuid}${path.extname(file.originalname)}`);
},
});
export const upload = multer({ storage: diskStorage });

export const diskStorageToImages = multer.diskStorage({
destination: function (_req, _file, cb) {
const dirPath = path.join(__dirname, '../fileUpload/images/');
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath);
}
cb(null, dirPath);
},
filename: function (_req, file, cb) {
const uuid = v4();
cb(null, `${uuid}${path.extname(file.originalname)}`);
},
});

const whitelist = [
'application/pdf',
'application/vnd.ms-powerpoint',
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
'application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
];
export const fileUplad = multer({
storage: diskStorageToImages,
fileFilter: (_req, file, cb) => {
if (!whitelist.includes(file.mimetype)) {
return cb(new Error('file is not allowed'));
}
cb(null, true);
},
});
11 changes: 11 additions & 0 deletions server/routes/filesRouter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Router } from 'express';

import { UserType } from '../../types/user.type';
import { uploadFiles } from '../controllers/filesController';
import { fileUplad } from '../controllers/multer';
import { authenticate } from '../middlewares/authenticate';
import { handleErrors } from '../middlewares/handleErrors';

export const filesRouter = Router();

filesRouter.post('/', fileUplad.array('files'), handleErrors(authenticate(UserType.ADMIN)), uploadFiles);
Loading