|
@@ -3,8 +3,6 @@
|
|
|
|
|
|
|
|
const logger = require('@alias/logger')('growi:routes:attachment');
|
|
const logger = require('@alias/logger')('growi:routes:attachment');
|
|
|
|
|
|
|
|
-const fs = require('fs');
|
|
|
|
|
-
|
|
|
|
|
const ApiResponse = require('../util/apiResponse');
|
|
const ApiResponse = require('../util/apiResponse');
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -131,7 +129,7 @@ module.exports = function(crowi, app) {
|
|
|
const Attachment = crowi.model('Attachment');
|
|
const Attachment = crowi.model('Attachment');
|
|
|
const User = crowi.model('User');
|
|
const User = crowi.model('User');
|
|
|
const Page = crowi.model('Page');
|
|
const Page = crowi.model('Page');
|
|
|
- const fileUploader = require('../service/file-uploader')(crowi, app);
|
|
|
|
|
|
|
+ const { fileUploadService, attachmentService } = crowi;
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -193,13 +191,13 @@ module.exports = function(crowi, app) {
|
|
|
return res.sendStatus(304);
|
|
return res.sendStatus(304);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if (fileUploader.canRespond()) {
|
|
|
|
|
- return fileUploader.respond(res, attachment);
|
|
|
|
|
|
|
+ if (fileUploadService.canRespond()) {
|
|
|
|
|
+ return fileUploadService.respond(res, attachment);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
let fileStream;
|
|
let fileStream;
|
|
|
try {
|
|
try {
|
|
|
- fileStream = await fileUploader.findDeliveryFile(attachment);
|
|
|
|
|
|
|
+ fileStream = await fileUploadService.findDeliveryFile(attachment);
|
|
|
}
|
|
}
|
|
|
catch (e) {
|
|
catch (e) {
|
|
|
logger.error(e);
|
|
logger.error(e);
|
|
@@ -234,32 +232,17 @@ module.exports = function(crowi, app) {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- async function createAttachment(file, user, pageId = null) {
|
|
|
|
|
- // check limit
|
|
|
|
|
- const res = await fileUploader.checkLimit(file.size);
|
|
|
|
|
- if (!res.isUploadable) {
|
|
|
|
|
- throw new Error(res.errorMessage);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ async function removeAttachment(attachmentId) {
|
|
|
|
|
+ const { fileUploadService } = crowi;
|
|
|
|
|
|
|
|
- const fileStream = fs.createReadStream(file.path, {
|
|
|
|
|
- flags: 'r', encoding: null, fd: null, mode: '0666', autoClose: true,
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ // retrieve data from DB to get a completely populated instance
|
|
|
|
|
+ const attachment = await Attachment.findById(attachmentId);
|
|
|
|
|
|
|
|
- // create an Attachment document and upload file
|
|
|
|
|
- let attachment;
|
|
|
|
|
- try {
|
|
|
|
|
- attachment = await Attachment.create(pageId, user, fileStream, file.originalname, file.mimetype, file.size);
|
|
|
|
|
- }
|
|
|
|
|
- catch (err) {
|
|
|
|
|
- // delete temporary file
|
|
|
|
|
- fs.unlink(file.path, (err) => { if (err) { logger.error('Error while deleting tmp file.') } });
|
|
|
|
|
- throw err;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ await fileUploadService.deleteFile(attachment);
|
|
|
|
|
|
|
|
- return attachment;
|
|
|
|
|
|
|
+ return attachment.remove();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
const actions = {};
|
|
const actions = {};
|
|
|
const api = {};
|
|
const api = {};
|
|
|
|
|
|
|
@@ -409,7 +392,7 @@ module.exports = function(crowi, app) {
|
|
|
*/
|
|
*/
|
|
|
api.limit = async function(req, res) {
|
|
api.limit = async function(req, res) {
|
|
|
const fileSize = Number(req.query.fileSize);
|
|
const fileSize = Number(req.query.fileSize);
|
|
|
- return res.json(ApiResponse.success(await fileUploader.checkLimit(fileSize)));
|
|
|
|
|
|
|
+ return res.json(ApiResponse.success(await fileUploadService.checkLimit(fileSize)));
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -522,7 +505,7 @@ module.exports = function(crowi, app) {
|
|
|
|
|
|
|
|
let attachment;
|
|
let attachment;
|
|
|
try {
|
|
try {
|
|
|
- attachment = await createAttachment(file, req.user, pageId);
|
|
|
|
|
|
|
+ attachment = await attachmentService.createAttachment(file, req.user, pageId);
|
|
|
}
|
|
}
|
|
|
catch (err) {
|
|
catch (err) {
|
|
|
logger.error(err);
|
|
logger.error(err);
|
|
@@ -618,7 +601,7 @@ module.exports = function(crowi, app) {
|
|
|
let attachment;
|
|
let attachment;
|
|
|
try {
|
|
try {
|
|
|
req.user.deleteImage();
|
|
req.user.deleteImage();
|
|
|
- attachment = await createAttachment(file, req.user);
|
|
|
|
|
|
|
+ attachment = await attachmentService.createAttachment(file, req.user);
|
|
|
await req.user.updateImage(attachment);
|
|
await req.user.updateImage(attachment);
|
|
|
}
|
|
}
|
|
|
catch (err) {
|
|
catch (err) {
|
|
@@ -687,7 +670,7 @@ module.exports = function(crowi, app) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
- await Attachment.removeWithSubstanceById(id);
|
|
|
|
|
|
|
+ await removeAttachment(attachment);
|
|
|
}
|
|
}
|
|
|
catch (err) {
|
|
catch (err) {
|
|
|
logger.error(err);
|
|
logger.error(err);
|