attachment.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. , actions = {}
  12. , api = {};
  13. actions.api = api;
  14. api.list = function(req, res){
  15. var id = req.params.pageId;
  16. Attachment.getListByPageId(id)
  17. .then(function(attachments) {
  18. res.json({
  19. status: true,
  20. data: {
  21. attachments: attachments
  22. }
  23. });
  24. });
  25. };
  26. /**
  27. *
  28. */
  29. api.add = function(req, res){
  30. var id = req.params.pageId,
  31. path = decodeURIComponent(req.body.path),
  32. pageCreated = false,
  33. page = {};
  34. debug('id and path are: ', id, path);
  35. var tmpFile = req.files.file || null;
  36. debug('Uploaded tmpFile: ', tmpFile);
  37. if (!tmpFile) {
  38. return res.json({
  39. status: false,
  40. message: 'File error.'
  41. });
  42. }
  43. new Promise(function(resolve, reject) {
  44. if (id == 0) {
  45. debug('Create page before file upload');
  46. Page.create(path, '# ' + path, req.user, {grant: Page.GRANT_OWNER})
  47. .then(function(page) {
  48. pageCreated = true;
  49. resolve(page);
  50. })
  51. .catch(reject);
  52. } else {
  53. Page.findPageById(id).then(resolve).catch(reject);
  54. }
  55. }).then(function(pageData) {
  56. page = pageData;
  57. id = pageData._id;
  58. var tmpPath = tmpFile.path,
  59. originalName = tmpFile.originalname,
  60. fileName = tmpFile.name,
  61. fileType = tmpFile.mimetype,
  62. fileSize = tmpFile.size,
  63. filePath = Attachment.createAttachmentFilePath(id, fileName, fileType),
  64. tmpFileStream = fs.createReadStream(tmpPath, {flags: 'r', encoding: null, fd: null, mode: '0666', autoClose: true });
  65. return fileUploader.uploadFile(filePath, fileType, tmpFileStream, {})
  66. .then(function(data) {
  67. debug('Uploaded data is: ', data);
  68. // TODO size
  69. return Attachment.create(id, req.user, filePath, originalName, fileName, fileType, fileSize);
  70. }).then(function(data) {
  71. var imageUrl = fileUploader.generateUrl(data.filePath);
  72. return res.json({
  73. status: true,
  74. filename: imageUrl,
  75. attachment: data,
  76. page: page,
  77. pageCreated: pageCreated,
  78. message: 'Successfully uploaded.',
  79. });
  80. }).catch(function (err) {
  81. debug('Error on saving attachment data', err);
  82. // @TODO
  83. // Remove from S3
  84. return res.json({
  85. status: false,
  86. message: 'Error while uploading.',
  87. });
  88. }).finally(function() {
  89. fs.unlink(tmpPath, function (err) {
  90. if (err) {
  91. debug('Error while deleting tmp file.');
  92. }
  93. });
  94. })
  95. ;
  96. });
  97. };
  98. api.remove = function(req, res){
  99. var id = req.params.id;
  100. };
  101. return actions;
  102. };