attachment.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. module.exports = function(crowi) {
  2. var debug = require('debug')('crowi:models:attachment')
  3. , mongoose = require('mongoose')
  4. , ObjectId = mongoose.Schema.Types.ObjectId
  5. , fileUploader = require('../util/fileUploader')(crowi)
  6. ;
  7. function generateFileHash (fileName) {
  8. var hasher = require('crypto').createHash('md5');
  9. hasher.update(fileName);
  10. return hasher.digest('hex');
  11. }
  12. attachmentSchema = new mongoose.Schema({
  13. page: { type: ObjectId, ref: 'Page', index: true },
  14. creator: { type: ObjectId, ref: 'User', index: true },
  15. filePath: { type: String, required: true },
  16. fileName: { type: String, required: true },
  17. originalName: { type: String },
  18. fileFormat: { type: String, required: true },
  19. fileSize: { type: Number, default: 0 },
  20. createdAt: { type: Date, default: Date.now }
  21. }, {
  22. toJSON: {
  23. virtuals: true
  24. },
  25. });
  26. attachmentSchema.virtual('fileUrl').get(function() {
  27. return `/files/${this._id}`;
  28. });
  29. attachmentSchema.statics.findById = function(id) {
  30. var Attachment = this;
  31. return new Promise(function(resolve, reject) {
  32. Attachment.findOne({_id: id}, function(err, data) {
  33. if (err) {
  34. return reject(err);
  35. }
  36. if (data === null) {
  37. return reject(new Error('Attachment not found'));
  38. }
  39. return resolve(data);
  40. });
  41. });
  42. };
  43. attachmentSchema.statics.getListByPageId = function(id) {
  44. var self = this;
  45. return new Promise(function(resolve, reject) {
  46. self
  47. .find({page: id})
  48. .sort({'updatedAt': 1})
  49. .populate('creator')
  50. .exec(function(err, data) {
  51. if (err) {
  52. return reject(err);
  53. }
  54. if (data.length < 1) {
  55. return resolve([]);
  56. }
  57. debug(data);
  58. return resolve(data);
  59. });
  60. });
  61. };
  62. attachmentSchema.statics.create = function(pageId, creator, filePath, originalName, fileName, fileFormat, fileSize) {
  63. var Attachment = this;
  64. return new Promise(function(resolve, reject) {
  65. var newAttachment = new Attachment();
  66. newAttachment.page = pageId;
  67. newAttachment.creator = creator._id;
  68. newAttachment.filePath = filePath;
  69. newAttachment.originalName = originalName;
  70. newAttachment.fileName = fileName;
  71. newAttachment.fileFormat = fileFormat;
  72. newAttachment.fileSize = fileSize;
  73. newAttachment.createdAt = Date.now();
  74. newAttachment.save(function(err, data) {
  75. if (err) {
  76. debug('Error on saving attachment.', err);
  77. return reject(err);
  78. }
  79. debug('Attachment saved.', data);
  80. return resolve(data);
  81. });
  82. });
  83. };
  84. attachmentSchema.statics.guessExtByFileType = function (fileType) {
  85. let ext = '';
  86. const isImage = fileType.match(/^image\/(.+)/i);
  87. if (isImage) {
  88. ext = isImage[1].toLowerCase();
  89. }
  90. return ext;
  91. };
  92. attachmentSchema.statics.createAttachmentFilePath = function (pageId, fileName, fileType) {
  93. const Attachment = this;
  94. let ext = '';
  95. const fnExt = fileName.match(/(.*)(?:\.([^.]+$))/);
  96. if (fnExt) {
  97. ext = '.' + fnExt[2];
  98. } else {
  99. ext = Attachment.guessExtByFileType(fileType);
  100. if (ext !== '') {
  101. ext = '.' + ext;
  102. }
  103. }
  104. return 'attachment/' + pageId + '/' + generateFileHash(fileName) + ext;
  105. };
  106. attachmentSchema.statics.removeAttachmentsByPageId = function(pageId) {
  107. var Attachment = this;
  108. return new Promise((resolve, reject) => {
  109. Attachment.getListByPageId(pageId)
  110. .then((attachments) => {
  111. for (attachment of attachments) {
  112. Attachment.removeAttachment(attachment).then((res) => {
  113. // do nothing
  114. }).catch((err) => {
  115. debug('Attachment remove error', err);
  116. });
  117. }
  118. resolve(attachments);
  119. }).catch((err) => {
  120. reject(err);
  121. });
  122. });
  123. };
  124. attachmentSchema.statics.findDeliveryFile = function(attachment, forceUpdate) {
  125. var Attachment = this;
  126. // TODO
  127. var forceUpdate = forceUpdate || false;
  128. return fileUploader.findDeliveryFile(attachment._id, attachment.filePath);
  129. };
  130. attachmentSchema.statics.removeAttachment = function(attachment) {
  131. const Attachment = this;
  132. const filePath = attachment.filePath;
  133. return new Promise((resolve, reject) => {
  134. Attachment.remove({_id: attachment._id}, (err, data) => {
  135. if (err) {
  136. return reject(err);
  137. }
  138. fileUploader.deleteFile(attachment._id, filePath)
  139. .then(data => {
  140. resolve(data); // this may null
  141. }).catch(err => {
  142. reject(err);
  143. });
  144. });
  145. });
  146. };
  147. return mongoose.model('Attachment', attachmentSchema);
  148. };