attachment.js 3.1 KB

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