attachment.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // disable no-return-await for model functions
  2. /* eslint-disable no-return-await */
  3. const debug = require('debug')('growi:models:attachment');
  4. // eslint-disable-next-line no-unused-vars
  5. const logger = require('@alias/logger')('growi:models:attachment');
  6. const path = require('path');
  7. const mongoose = require('mongoose');
  8. const ObjectId = mongoose.Schema.Types.ObjectId;
  9. module.exports = function(crowi) {
  10. function generateFileHash(fileName) {
  11. const hash = require('crypto').createHash('md5');
  12. hash.update(`${fileName}_${Date.now()}`);
  13. return hash.digest('hex');
  14. }
  15. const attachmentSchema = new mongoose.Schema({
  16. page: { type: ObjectId, ref: 'Page', index: true },
  17. creator: { type: ObjectId, ref: 'User', index: true },
  18. filePath: { type: String }, // DEPRECATED: remains for backward compatibility for v3.3.x or below
  19. fileName: { type: String, required: true },
  20. originalName: { type: String },
  21. fileFormat: { type: String, required: true },
  22. fileSize: { type: Number, default: 0 },
  23. createdAt: { type: Date, default: Date.now },
  24. });
  25. attachmentSchema.virtual('filePathProxied').get(function() {
  26. return `/attachment/${this._id}`;
  27. });
  28. attachmentSchema.virtual('downloadPathProxied').get(function() {
  29. return `/download/${this._id}`;
  30. });
  31. attachmentSchema.set('toObject', { virtuals: true });
  32. attachmentSchema.set('toJSON', { virtuals: true });
  33. attachmentSchema.statics.create = async function(pageId, user, fileStream, originalName, fileFormat, fileSize) {
  34. const fileUploader = require('../service/file-uploader')(crowi);
  35. const Attachment = this;
  36. const extname = path.extname(originalName);
  37. let fileName = generateFileHash(originalName);
  38. if (extname.length > 1) { // ignore if empty or '.' only
  39. fileName = `${fileName}${extname}`;
  40. }
  41. let attachment = new Attachment();
  42. attachment.page = pageId;
  43. attachment.creator = user._id;
  44. attachment.originalName = originalName;
  45. attachment.fileName = fileName;
  46. attachment.fileFormat = fileFormat;
  47. attachment.fileSize = fileSize;
  48. attachment.createdAt = Date.now();
  49. // upload file
  50. await fileUploader.uploadFile(fileStream, attachment);
  51. // save attachment
  52. attachment = await attachment.save();
  53. return attachment;
  54. };
  55. attachmentSchema.statics.removeAttachmentsByPageId = function(pageId) {
  56. const Attachment = this;
  57. return new Promise((resolve, reject) => {
  58. Attachment.find({ page: pageId })
  59. .then((attachments) => {
  60. for (const attachment of attachments) {
  61. Attachment.removeWithSubstanceById(attachment._id)
  62. .then((res) => {
  63. // do nothing
  64. })
  65. .catch((err) => {
  66. debug('Attachment remove error', err);
  67. });
  68. }
  69. resolve(attachments);
  70. })
  71. .catch((err) => {
  72. reject(err);
  73. });
  74. });
  75. };
  76. attachmentSchema.statics.removeWithSubstanceById = async function(id) {
  77. const fileUploader = require('../service/file-uploader')(crowi);
  78. // retrieve data from DB to get a completely populated instance
  79. const attachment = await this.findById(id);
  80. await fileUploader.deleteFile(attachment);
  81. return await attachment.remove();
  82. };
  83. return mongoose.model('Attachment', attachmentSchema);
  84. };