|
|
@@ -6,6 +6,7 @@ module.exports = function(crowi, app) {
|
|
|
, User = crowi.model('User')
|
|
|
, Page = crowi.model('Page')
|
|
|
, config = crowi.getConfig()
|
|
|
+ , path = require('path')
|
|
|
, fs = require('fs')
|
|
|
, fileUploader = require('../util/fileUploader')(crowi, app)
|
|
|
, ApiResponse = require('../util/apiResponse')
|
|
|
@@ -14,41 +15,36 @@ module.exports = function(crowi, app) {
|
|
|
|
|
|
actions.api = api;
|
|
|
|
|
|
- api.redirector = function(req, res, next){
|
|
|
- var id = req.params.id;
|
|
|
+ api.download = function(req, res) {
|
|
|
+ const id = req.params.id;
|
|
|
|
|
|
Attachment.findById(id)
|
|
|
- .then(function(data) {
|
|
|
-
|
|
|
- // TODO: file delivery plugin for cdn
|
|
|
- Attachment.findDeliveryFile(data)
|
|
|
- .then(fileName => {
|
|
|
- const encodedFileName = encodeURIComponent(data.originalName);
|
|
|
-
|
|
|
- var deliveryFile = {
|
|
|
- fileName: fileName,
|
|
|
- options: {
|
|
|
- headers: {
|
|
|
- 'Content-Type': data.fileFormat,
|
|
|
- 'Content-Disposition': `inline;filename*=UTF-8''${encodedFileName}`,
|
|
|
- },
|
|
|
- },
|
|
|
- };
|
|
|
-
|
|
|
- if (deliveryFile.fileName.match(/^\/uploads/)) {
|
|
|
- debug('Using loacal file module, just redirecting.')
|
|
|
- return res.redirect(deliveryFile.fileName);
|
|
|
- } else {
|
|
|
- return res.sendFile(deliveryFile.fileName, deliveryFile.options);
|
|
|
- }
|
|
|
- }).catch(err => {
|
|
|
- //debug('error', err);
|
|
|
- });
|
|
|
- }).catch((err) => {
|
|
|
- //debug('err', err);
|
|
|
+ .then(function(data) {
|
|
|
+
|
|
|
+ Attachment.findDeliveryFile(data)
|
|
|
+ .then(fileName => {
|
|
|
+
|
|
|
+ // local
|
|
|
+ if (fileName.match(/^\/uploads/)) {
|
|
|
+ return res.download(path.join(crowi.publicDir, fileName), data.originalName);
|
|
|
+ }
|
|
|
+ // aws
|
|
|
+ else {
|
|
|
+ const options = {
|
|
|
+ headers: {
|
|
|
+ 'Content-Type': 'application/force-download',
|
|
|
+ 'Content-Disposition': `inline;filename*=UTF-8''${encodeURIComponent(data.originalName)}`,
|
|
|
+ }
|
|
|
+ };
|
|
|
+ return res.sendFile(fileName, options);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ })
|
|
|
// not found
|
|
|
- return res.status(404).sendFile(crowi.publicDir + '/images/file-not-found.png');
|
|
|
- });
|
|
|
+ .catch((err) => {
|
|
|
+ debug('download err', err);
|
|
|
+ return res.status(404).sendFile(crowi.publicDir + '/images/file-not-found.png');
|
|
|
+ });
|
|
|
};
|
|
|
|
|
|
/**
|