attachment.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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}, function(err, pageData) {
  47. if (err) {
  48. debug('Page create error', err);
  49. return reject(err);
  50. }
  51. pageCreated = true;
  52. return resolve(pageData);
  53. });
  54. } else {
  55. Page.findPageById(id).then(resolve).catch(reject);
  56. }
  57. }).then(function(pageData) {
  58. page = pageData;
  59. id = pageData._id;
  60. var tmpPath = tmpFile.path,
  61. originalName = tmpFile.originalname,
  62. fileName = tmpFile.name,
  63. fileType = tmpFile.mimetype,
  64. fileSize = tmpFile.size,
  65. filePath = Attachment.createAttachmentFilePath(id, fileName, fileType),
  66. tmpFileStream = fs.createReadStream(tmpPath, {flags: 'r', encoding: null, fd: null, mode: '0666', autoClose: true });
  67. return fileUploader.uploadFile(filePath, fileType, tmpFileStream, {})
  68. .then(function(data) {
  69. debug('Uploaded data is: ', data);
  70. // TODO size
  71. return Attachment.create(id, req.user, filePath, originalName, fileName, fileType, fileSize);
  72. }).then(function(data) {
  73. var imageUrl = fileUploader.generateUrl(data.filePath);
  74. return res.json({
  75. status: true,
  76. filename: imageUrl,
  77. attachment: data,
  78. page: page,
  79. pageCreated: pageCreated,
  80. message: 'Successfully uploaded.',
  81. });
  82. }).catch(function (err) {
  83. debug('Error on saving attachment data', err);
  84. // @TODO
  85. // Remove from S3
  86. return res.json({
  87. status: false,
  88. message: 'Error while uploading.',
  89. });
  90. }).finally(function() {
  91. fs.unlink(tmpPath, function (err) {
  92. if (err) {
  93. debug('Error while deleting tmp file.');
  94. }
  95. });
  96. })
  97. ;
  98. });
  99. };
  100. api.remove = function(req, res){
  101. var id = req.params.id;
  102. };
  103. return actions;
  104. };