Skip to content

Commit

Permalink
1703-storing-attached-file
Browse files Browse the repository at this point in the history
  • Loading branch information
basarbk committed Aug 10, 2020
1 parent ccf2cd3 commit 65852c2
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 11 deletions.
11 changes: 11 additions & 0 deletions __tests__/FileUpload.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ const app = require('../src/app');
const path = require('path');
const FileAttachment = require('../src/file/FileAttachment');
const sequelize = require('../src/config/database');
const fs = require('fs');
const config = require('config');

const { uploadDir, attachmentDir } = config;

beforeAll(async () => {
if (process.env.NODE_ENV === 'test') {
Expand Down Expand Up @@ -33,4 +37,11 @@ describe('Upload File for Hoax', () => {
expect(attachment.filename).not.toBe('test-png.png');
expect(attachment.uploadDate.getTime()).toBeGreaterThan(beforeSubmit);
});
it('saves file to attachment folder', async () => {
await uploadFile();
const attachments = await FileAttachment.findAll();
const attachment = attachments[0];
const filePath = path.join('.', uploadDir, attachmentDir, attachment.filename);
expect(fs.existsSync(filePath)).toBe(true);
});
});
108 changes: 106 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"i18next": "^19.4.5",
"i18next-fs-backend": "^1.0.6",
"i18next-http-middleware": "^2.1.2",
"multer": "^1.4.2",
"nodemailer": "^6.4.8",
"pg": "^8.3.0",
"pg-hstore": "^2.3.3",
Expand Down
7 changes: 5 additions & 2 deletions src/file/FileRouter.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const express = require('express');
const router = express.Router();
const FileService = require('./FileService');
const multer = require('multer');

router.post('/api/1.0/hoaxes/attachments', async (req, res) => {
await FileService.saveAttachment();
const upload = multer();

router.post('/api/1.0/hoaxes/attachments', upload.single('file'), async (req, res) => {
await FileService.saveAttachment(req.file);
res.send();
});

Expand Down
6 changes: 4 additions & 2 deletions src/file/FileService.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ const isSupportedFileType = async (buffer) => {
return !type ? false : type.mime === 'image/png' || type.mime === 'image/jpeg';
};

const saveAttachment = async () => {
const saveAttachment = async (file) => {
const filename = randomString(32);
await fs.promises.writeFile(path.join(attachmentFolder, filename), file.buffer);
await FileAttachment.create({
filename: randomString(32),
filename,
uploadDate: new Date(),
});
};
Expand Down
16 changes: 11 additions & 5 deletions test-cleanup.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ const fs = require('fs');
const path = require('path');
const config = require('config');

const { uploadDir, profileDir } = config;
const { uploadDir, profileDir, attachmentDir } = config;
const profileDirectory = path.join('.', uploadDir, profileDir);
const attachmentDirectory = path.join('.', uploadDir, attachmentDir);

const files = fs.readdirSync(profileDirectory);
for (const file of files) {
fs.unlinkSync(path.join(profileDirectory, file));
}
const clearFolders = (folder) => {
const files = fs.readdirSync(folder);
for (const file of files) {
fs.unlinkSync(path.join(folder, file));
}
};

clearFolders(profileDirectory);
clearFolders(attachmentDirectory);

0 comments on commit 65852c2

Please sign in to comment.