|
|
@@ -9,30 +9,43 @@ module.exports = function(crowi, app) {
|
|
|
, config = crowi.getConfig()
|
|
|
, fs = require('fs')
|
|
|
, fileUploader = require('../util/fileUploader')(crowi, app)
|
|
|
+ , ApiResponse = require('../util/apiResponse')
|
|
|
, actions = {}
|
|
|
, api = {};
|
|
|
|
|
|
actions.api = api;
|
|
|
|
|
|
+ /**
|
|
|
+ * @api {get} /attachments.list Get attachments of the page
|
|
|
+ * @apiName ListAttachments
|
|
|
+ * @apiGroup Attachment
|
|
|
+ *
|
|
|
+ * @apiParam {String} page_id
|
|
|
+ */
|
|
|
api.list = function(req, res){
|
|
|
- var id = req.params.pageId;
|
|
|
+ var id = req.query.page_id || null;
|
|
|
+ if (!id) {
|
|
|
+ return res.json(ApiResponse.error('Parameters page_id is required.'));
|
|
|
+ }
|
|
|
|
|
|
Attachment.getListByPageId(id)
|
|
|
.then(function(attachments) {
|
|
|
- res.json({
|
|
|
- status: true,
|
|
|
- data: {
|
|
|
- attachments: attachments
|
|
|
- }
|
|
|
- });
|
|
|
+ return res.json(ApiResponse.success({
|
|
|
+ attachments: attachments
|
|
|
+ }));
|
|
|
});
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
+ * @api {post} /attachments.add Add attachment to the page
|
|
|
+ * @apiName AddAttachments
|
|
|
+ * @apiGroup Attachment
|
|
|
*
|
|
|
+ * @apiParam {String} page_id
|
|
|
+ * @apiParam {File} file
|
|
|
*/
|
|
|
api.add = function(req, res){
|
|
|
- var id = req.params.pageId,
|
|
|
+ var id = req.body.page_id,
|
|
|
path = decodeURIComponent(req.body.path),
|
|
|
pageCreated = false,
|
|
|
page = {};
|
|
|
@@ -42,10 +55,7 @@ module.exports = function(crowi, app) {
|
|
|
var tmpFile = req.files.file || null;
|
|
|
debug('Uploaded tmpFile: ', tmpFile);
|
|
|
if (!tmpFile) {
|
|
|
- return res.json({
|
|
|
- status: false,
|
|
|
- message: 'File error.'
|
|
|
- });
|
|
|
+ return res.json(ApiResponse.error('File error.'));
|
|
|
}
|
|
|
|
|
|
new Promise(function(resolve, reject) {
|
|
|
@@ -80,22 +90,20 @@ module.exports = function(crowi, app) {
|
|
|
return Attachment.create(id, req.user, filePath, originalName, fileName, fileType, fileSize);
|
|
|
}).then(function(data) {
|
|
|
var imageUrl = fileUploader.generateUrl(data.filePath);
|
|
|
- return res.json({
|
|
|
- status: true,
|
|
|
- filename: imageUrl,
|
|
|
- attachment: data,
|
|
|
+
|
|
|
+ page.creator = User.filterToPublicFields(page.creator);
|
|
|
+ data.creator = User.filterToPublicFields(data.creator);
|
|
|
+ return res.json(ApiResponse.success({
|
|
|
page: page,
|
|
|
+ attachment: data,
|
|
|
+ filename: imageUrl,
|
|
|
pageCreated: pageCreated,
|
|
|
- message: 'Successfully uploaded.',
|
|
|
- });
|
|
|
+ }));
|
|
|
}).catch(function (err) {
|
|
|
debug('Error on saving attachment data', err);
|
|
|
// @TODO
|
|
|
// Remove from S3
|
|
|
- return res.json({
|
|
|
- status: false,
|
|
|
- message: 'Error while uploading.',
|
|
|
- });
|
|
|
+ return res.json(ApiResponse.error('Error while uploading.'));
|
|
|
}).finally(function() {
|
|
|
fs.unlink(tmpPath, function (err) {
|
|
|
if (err) {
|
|
|
@@ -104,6 +112,8 @@ module.exports = function(crowi, app) {
|
|
|
});
|
|
|
})
|
|
|
;
|
|
|
+ }).catch(function(err) {
|
|
|
+ return res.json(ApiResponse.error('Error.'));
|
|
|
});
|
|
|
};
|
|
|
|