attachment.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. module.exports = function(crowi, app) {
  2. 'use strict';
  3. var debug = require('debug')('crowi:routs:attachment')
  4. , Attachment = crowi.model('Attachment')
  5. , User = crowi.model('User')
  6. , Page = crowi.model('Page')
  7. , Promise = require('bluebird')
  8. , config = crowi.getConfig()
  9. , fs = require('fs')
  10. , fileUploader = require('../util/fileUploader')(crowi, app)
  11. , ApiResponse = require('../util/apiResponse')
  12. , actions = {}
  13. , api = {};
  14. actions.api = api;
  15. /**
  16. * @api {get} /attachments.list Get attachments of the page
  17. * @apiName ListAttachments
  18. * @apiGroup Attachment
  19. *
  20. * @apiParam {String} page_id
  21. */
  22. api.list = function(req, res){
  23. var id = req.query.page_id || null;
  24. if (!id) {
  25. return res.json(ApiResponse.error('Parameters page_id is required.'));
  26. }
  27. Attachment.getListByPageId(id)
  28. .then(function(attachments) {
  29. return res.json(ApiResponse.success({
  30. attachments: attachments
  31. }));
  32. });
  33. };
  34. /**
  35. * @api {post} /attachments.add Add attachment to the page
  36. * @apiName AddAttachments
  37. * @apiGroup Attachment
  38. *
  39. * @apiParam {String} page_id
  40. * @apiParam {File} file
  41. */
  42. api.add = function(req, res){
  43. var id = req.body.page_id,
  44. path = decodeURIComponent(req.body.path),
  45. pageCreated = false,
  46. page = {};
  47. debug('id and path are: ', id, path);
  48. var tmpFile = req.files.file || null;
  49. debug('Uploaded tmpFile: ', tmpFile);
  50. if (!tmpFile) {
  51. return res.json(ApiResponse.error('File error.'));
  52. }
  53. new Promise(function(resolve, reject) {
  54. if (id == 0) {
  55. debug('Create page before file upload');
  56. Page.create(path, '# ' + path, req.user, {grant: Page.GRANT_OWNER})
  57. .then(function(page) {
  58. pageCreated = true;
  59. resolve(page);
  60. })
  61. .catch(reject);
  62. } else {
  63. Page.findPageById(id).then(resolve).catch(reject);
  64. }
  65. }).then(function(pageData) {
  66. page = pageData;
  67. id = pageData._id;
  68. var tmpPath = tmpFile.path,
  69. originalName = tmpFile.originalname,
  70. fileName = tmpFile.name,
  71. fileType = tmpFile.mimetype,
  72. fileSize = tmpFile.size,
  73. filePath = Attachment.createAttachmentFilePath(id, fileName, fileType),
  74. tmpFileStream = fs.createReadStream(tmpPath, {flags: 'r', encoding: null, fd: null, mode: '0666', autoClose: true });
  75. return fileUploader.uploadFile(filePath, fileType, tmpFileStream, {})
  76. .then(function(data) {
  77. debug('Uploaded data is: ', data);
  78. // TODO size
  79. return Attachment.create(id, req.user, filePath, originalName, fileName, fileType, fileSize);
  80. }).then(function(data) {
  81. var imageUrl = fileUploader.generateUrl(data.filePath);
  82. page.creator = User.filterToPublicFields(page.creator);
  83. data.creator = User.filterToPublicFields(data.creator);
  84. return res.json(ApiResponse.success({
  85. page: page,
  86. attachment: data,
  87. filename: imageUrl,
  88. pageCreated: pageCreated,
  89. }));
  90. }).catch(function (err) {
  91. debug('Error on saving attachment data', err);
  92. // @TODO
  93. // Remove from S3
  94. return res.json(ApiResponse.error('Error while uploading.'));
  95. }).finally(function() {
  96. fs.unlink(tmpPath, function (err) {
  97. if (err) {
  98. debug('Error while deleting tmp file.');
  99. }
  100. });
  101. })
  102. ;
  103. }).catch(function(err) {
  104. return res.json(ApiResponse.error('Error.'));
  105. });
  106. };
  107. api.remove = function(req, res){
  108. var id = req.params.id;
  109. };
  110. return actions;
  111. };