|
|
@@ -14,19 +14,38 @@ module.exports = function(crowi, app) {
|
|
|
|
|
|
actions.api = api;
|
|
|
|
|
|
- api.redirector = function(req, res){
|
|
|
+ api.redirector = function(req, res, next){
|
|
|
var id = req.params.id;
|
|
|
|
|
|
Attachment.findById(id)
|
|
|
.then(function(data) {
|
|
|
|
|
|
// TODO: file delivery plugin for cdn
|
|
|
- var deliveryFile = Attachment.findDeliveryFile(data);
|
|
|
- return res.sendFile(deliveryFile.filename, deliveryFile.options);
|
|
|
- }).catch(function(err) {
|
|
|
-
|
|
|
+ Attachment.findDeliveryFile(data)
|
|
|
+ .then(fileName => {
|
|
|
+
|
|
|
+ var deliveryFile = {
|
|
|
+ fileName: fileName,
|
|
|
+ options: {
|
|
|
+ headers: {
|
|
|
+ 'Content-Type': data.fileFormat,
|
|
|
+ },
|
|
|
+ },
|
|
|
+ };
|
|
|
+
|
|
|
+ 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);
|
|
|
// not found
|
|
|
- return res.sendFile(crowi.publicDir + '/images/file-not-found.png');
|
|
|
+ return res.status(404).sendFile(crowi.publicDir + '/images/file-not-found.png');
|
|
|
});
|
|
|
};
|
|
|
|
|
|
@@ -45,8 +64,15 @@ module.exports = function(crowi, app) {
|
|
|
|
|
|
Attachment.getListByPageId(id)
|
|
|
.then(function(attachments) {
|
|
|
+ var config = crowi.getConfig();
|
|
|
+ var baseUrl = (config.crowi['app:url'] || '');
|
|
|
return res.json(ApiResponse.success({
|
|
|
- attachments: attachments
|
|
|
+ attachments: attachments.map(at => {
|
|
|
+ var fileUrl = at.fileUrl;
|
|
|
+ at = at.toObject();
|
|
|
+ at.url = baseUrl + fileUrl;
|
|
|
+ return at;
|
|
|
+ })
|
|
|
}));
|
|
|
});
|
|
|
};
|
|
|
@@ -107,11 +133,19 @@ module.exports = function(crowi, app) {
|
|
|
// TODO size
|
|
|
return Attachment.create(id, req.user, filePath, originalName, fileName, fileType, fileSize);
|
|
|
}).then(function(data) {
|
|
|
- var imageUrl = fileUploader.generateUrl(data.filePath);
|
|
|
+ var fileUrl = data.fileUrl;
|
|
|
+ var config = crowi.getConfig();
|
|
|
+
|
|
|
+ // isLocalUrl??
|
|
|
+ if (!fileUrl.match(/^https?/)) {
|
|
|
+ fileUrl = (config.crowi['app:url'] || '') + fileUrl;
|
|
|
+ }
|
|
|
+
|
|
|
var result = {
|
|
|
page: page.toObject(),
|
|
|
attachment: data.toObject(),
|
|
|
- filename: imageUrl,
|
|
|
+ url: fileUrl,
|
|
|
+ filename: fileUrl, // this is for inline-attachemnets plugin http://inlineattachment.readthedocs.io/en/latest/pages/configuration.html
|
|
|
pageCreated: pageCreated,
|
|
|
};
|
|
|
|