| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- const debug = require('debug')('growi:routss:attachment');
- const logger = require('@alias/logger')('growi:routes:attachment');
- const path = require('path');
- const fs = require('fs');
- const ApiResponse = require('../util/apiResponse');
- module.exports = function(crowi, app) {
- const Attachment = crowi.model('Attachment');
- const User = crowi.model('User');
- const Page = crowi.model('Page');
- const fileUploader = require('../service/file-uploader')(crowi, app);
- async function responseForAttachment(res, attachment, forceDownload) {
- let fileStream;
- try {
- fileStream = await fileUploader.findDeliveryFile(attachment);
- }
- catch (e) {
- logger.error(e);
- return res.json(ApiResponse.error(e.message));
- }
- setHeaderToRes(res, attachment, forceDownload);
- return fileStream.pipe(res);
- }
- function setHeaderToRes(res, attachment, forceDownload) {
- // download
- if (forceDownload) {
- const headers = {
- 'Content-Type': 'application/force-download',
- 'Content-Disposition': `inline;filename*=UTF-8''${encodeURIComponent(attachment.originalName)}`,
- };
- res.writeHead(200, headers);
- }
- // reference
- else {
- res.set('Content-Type', attachment.fileFormat);
- }
- }
- const actions = {};
- const api = {};
- actions.api = api;
- api.download = async function(req, res) {
- const id = req.params.id;
- const attachment = await Attachment.findById(id);
- if (attachment == null) {
- return res.json(ApiResponse.error('attachment not found'));
- }
- // TODO consider page restriction
- return responseForAttachment(res, attachment, true);
- };
- /**
- * @api {get} /attachments.get get attachments
- * @apiName get
- * @apiGroup Attachment
- *
- * @apiParam {String} id
- */
- api.get = async function(req, res) {
- const id = req.params.id;
- const attachment = await Attachment.findById(id);
- if (attachment == null) {
- return res.json(ApiResponse.error('attachment not found'));
- }
- // TODO consider page restriction
- return responseForAttachment(res, attachment);
- };
- /**
- * @api {get} /attachments.obsoletedGetForMongoDB get attachments from mongoDB
- * @apiName get
- * @apiGroup Attachment
- *
- * @apiParam {String} pageId, fileName
- */
- api.obsoletedGetForMongoDB = async function(req, res) {
- if (process.env.FILE_UPLOAD !== 'mongodb') {
- return res.status(400);
- }
- const pageId = req.params.pageId;
- const fileName = req.params.fileName;
- const filePath = `attachment/${pageId}/${fileName}`;
- const attachment = await Attachment.findOne({ filePath });
- if (attachment == null) {
- return res.json(ApiResponse.error('attachment not found'));
- }
- return responseForAttachment(res, attachment);
- };
- /**
- * @api {get} /attachments.list Get attachments of the page
- * @apiName ListAttachments
- * @apiGroup Attachment
- *
- * @apiParam {String} page_id
- */
- api.list = async function(req, res) {
- const id = req.query.page_id || null;
- if (!id) {
- return res.json(ApiResponse.error('Parameters page_id is required.'));
- }
- let attachments = await Attachment.find({page: id})
- .sort({'updatedAt': 1})
- .populate('creator', User.USER_PUBLIC_FIELDS);
- // toJSON
- attachments = attachments.map(attachment => {
- const json = attachment.toJSON({ virtuals: true});
- // omit unnecessary property
- json.filePathOnStorage = undefined;
- return json;
- });
- return res.json(ApiResponse.success({ attachments }));
- };
- /**
- * @api {get} /attachments.limit get available capacity of uploaded file with GridFS
- * @apiName AddAttachments
- * @apiGroup Attachment
- */
- api.limit = async function(req, res) {
- const isUploadable = await fileUploader.checkCapacity(req.query.fileSize);
- return res.json(ApiResponse.success({isUploadable: isUploadable}));
- };
- /**
- * @api {post} /attachments.add Add attachment to the page
- * @apiName AddAttachments
- * @apiGroup Attachment
- *
- * @apiParam {String} page_id
- * @apiParam {File} file
- */
- api.add = async function(req, res) {
- var id = req.body.page_id || 0,
- path = decodeURIComponent(req.body.path) || null,
- pageCreated = false,
- page = {};
- debug('id and path are: ', id, path);
- var tmpFile = req.file || null;
- const isUploadable = await fileUploader.checkCapacity(tmpFile.size);
- if (!isUploadable) {
- return res.json(ApiResponse.error('MongoDB for uploading files reaches limit'));
- }
- debug('Uploaded tmpFile: ', tmpFile);
- if (!tmpFile) {
- return res.json(ApiResponse.error('File error.'));
- }
- new Promise(function(resolve, reject) {
- if (id == 0) {
- if (path === null) {
- throw new Error('path required if page_id is not specified.');
- }
- debug('Create page before file upload');
- Page.create(path, '# ' + path, req.user, {grant: Page.GRANT_OWNER})
- .then(function(page) {
- pageCreated = true;
- resolve(page);
- })
- .catch(reject);
- }
- else {
- Page.findById(id).then(resolve).catch(reject);
- }
- }).then(function(pageData) {
- page = pageData;
- id = pageData._id;
- var tmpPath = tmpFile.path,
- originalName = tmpFile.originalname,
- fileName = tmpFile.filename + tmpFile.originalname,
- fileType = tmpFile.mimetype,
- fileSize = tmpFile.size,
- filePath = Attachment.createAttachmentFilePath(id, fileName, fileType),
- tmpFileStream = fs.createReadStream(tmpPath, {flags: 'r', encoding: null, fd: null, mode: '0666', autoClose: true });
- return fileUploader.uploadFile(filePath, fileType, tmpFileStream, {})
- .then(function(data) {
- debug('Uploaded data is: ', data);
- // TODO size
- return Attachment.create(id, req.user, filePath, originalName, fileName, fileType, fileSize);
- }).then(function(data) {
- var fileUrl = data.fileUrl;
- var result = {
- page: page.toObject(),
- attachment: data.toObject(),
- url: fileUrl,
- pageCreated: pageCreated,
- };
- result.page.creator = User.filterToPublicFields(result.page.creator);
- result.attachment.creator = User.filterToPublicFields(result.attachment.creator);
- // delete anyway
- fs.unlink(tmpPath, function(err) { if (err) { debug('Error while deleting tmp file.') } });
- return res.json(ApiResponse.success(result));
- }).catch(function(err) {
- logger.error('Error on saving attachment data', err);
- // @TODO
- // Remove from S3
- // delete anyway
- fs.unlink(tmpPath, function(err) { if (err) { logger.error('Error while deleting tmp file.') } });
- return res.json(ApiResponse.error('Error while uploading.'));
- });
- }).catch(function(err) {
- logger.error('Attachement upload error', err);
- return res.json(ApiResponse.error('Error.'));
- });
- };
- /**
- * @api {post} /attachments.remove Remove attachments
- * @apiName RemoveAttachments
- * @apiGroup Attachment
- *
- * @apiParam {String} attachment_id
- */
- api.remove = function(req, res) {
- const id = req.body.attachment_id;
- Attachment.findById(id)
- .then(function(data) {
- const attachment = data;
- Attachment.removeAttachment(attachment)
- .then(data => {
- debug('removeAttachment', data);
- return res.json(ApiResponse.success({}));
- }).catch(err => {
- logger.error('Error', err);
- return res.status(500).json(ApiResponse.error('Error while deleting file'));
- });
- }).catch(err => {
- logger.error('Error', err);
- return res.status(404);
- });
- };
- return actions;
- };
|