Skip to content

Commit

Permalink
1702-storing-attachment-info-in-db
Browse files Browse the repository at this point in the history
  • Loading branch information
basarbk committed Aug 10, 2020
1 parent 07f0e44 commit ccf2cd3
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 0 deletions.
36 changes: 36 additions & 0 deletions __tests__/FileUpload.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const request = require('supertest');
const app = require('../src/app');
const path = require('path');
const FileAttachment = require('../src/file/FileAttachment');
const sequelize = require('../src/config/database');

beforeAll(async () => {
if (process.env.NODE_ENV === 'test') {
await sequelize.sync();
}
});

beforeEach(async () => {
await FileAttachment.destroy({ truncate: true });
});

const uploadFile = () => {
return request(app)
.post('/api/1.0/hoaxes/attachments')
.attach('file', path.join('.', '__tests__', 'resources', 'test-png.png'));
};

describe('Upload File for Hoax', () => {
it('returns 200 ok after successful upload', async () => {
const response = await uploadFile();
expect(response.status).toBe(200);
});
it('saves dynamicFilename, uploadDate as attachment object in database', async () => {
const beforeSubmit = Date.now();
await uploadFile();
const attachments = await FileAttachment.findAll();
const attachment = attachments[0];
expect(attachment.filename).not.toBe('test-png.png');
expect(attachment.uploadDate.getTime()).toBeGreaterThan(beforeSubmit);
});
});
2 changes: 2 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const FileService = require('./file/FileService');
const config = require('config');
const path = require('path');
const HoaxRouter = require('./hoax/HoaxRouter');
const FileRouter = require('./file/FileRouter');

const { uploadDir, profileDir } = config;
const profileFolder = path.join('.', uploadDir, profileDir);
Expand Down Expand Up @@ -47,6 +48,7 @@ app.use(tokenAuthentication);
app.use(UserRouter);
app.use(AuthenticationRouter);
app.use(HoaxRouter);
app.use(FileRouter);

app.use(errorHandler);

Expand Down
24 changes: 24 additions & 0 deletions src/file/FileAttachment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const Sequelize = require('sequelize');
const sequelize = require('../config/database');

const Model = Sequelize.Model;

class FileAttachment extends Model {}

FileAttachment.init(
{
filename: {
type: Sequelize.STRING,
},
uploadDate: {
type: Sequelize.DATE,
},
},
{
sequelize,
modelName: 'fileAttachment',
timestamps: false,
}
);

module.exports = FileAttachment;
10 changes: 10 additions & 0 deletions src/file/FileRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const express = require('express');
const router = express.Router();
const FileService = require('./FileService');

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

module.exports = router;
9 changes: 9 additions & 0 deletions src/file/FileService.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const path = require('path');
const config = require('config');
const { randomString } = require('../shared/generator');
const FileType = require('file-type');
const FileAttachment = require('./FileAttachment');

const { uploadDir, profileDir, attachmentDir } = config;
const profileFolder = path.join('.', uploadDir, profileDir);
Expand Down Expand Up @@ -41,10 +42,18 @@ const isSupportedFileType = async (buffer) => {
return !type ? false : type.mime === 'image/png' || type.mime === 'image/jpeg';
};

const saveAttachment = async () => {
await FileAttachment.create({
filename: randomString(32),
uploadDate: new Date(),
});
};

module.exports = {
createFolders,
saveProfileImage,
deleteProfileImage,
isLessThan2MB,
isSupportedFileType,
saveAttachment,
};

0 comments on commit ccf2cd3

Please sign in to comment.