attachment.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // disable no-return-await for model functions
  2. /* eslint-disable no-return-await */
  3. // eslint-disable-next-line no-unused-vars
  4. const logger = require('@alias/logger')('growi:models:attachment');
  5. const path = require('path');
  6. const mongoose = require('mongoose');
  7. const uniqueValidator = require('mongoose-unique-validator');
  8. const mongoosePaginate = require('mongoose-paginate-v2');
  9. const { addSeconds } = require('date-fns');
  10. const ObjectId = mongoose.Schema.Types.ObjectId;
  11. module.exports = function(crowi) {
  12. function generateFileHash(fileName) {
  13. const hash = require('crypto').createHash('md5');
  14. hash.update(`${fileName}_${Date.now()}`);
  15. return hash.digest('hex');
  16. }
  17. const attachmentSchema = new mongoose.Schema({
  18. page: { type: ObjectId, ref: 'Page', index: true },
  19. creator: { type: ObjectId, ref: 'User', index: true },
  20. filePath: { type: String }, // DEPRECATED: remains for backward compatibility for v3.3.x or below
  21. fileName: { type: String, required: true, unique: true },
  22. originalName: { type: String },
  23. fileFormat: { type: String, required: true },
  24. fileSize: { type: Number, default: 0 },
  25. createdAt: { type: Date, default: Date.now },
  26. externalUrlCached: { type: String },
  27. externalUrlExpiredAt: { type: Date },
  28. });
  29. attachmentSchema.plugin(uniqueValidator);
  30. attachmentSchema.plugin(mongoosePaginate);
  31. attachmentSchema.virtual('filePathProxied').get(function() {
  32. return `/attachment/${this._id}`;
  33. });
  34. attachmentSchema.virtual('downloadPathProxied').get(function() {
  35. return `/download/${this._id}`;
  36. });
  37. attachmentSchema.set('toObject', { virtuals: true });
  38. attachmentSchema.set('toJSON', { virtuals: true });
  39. attachmentSchema.statics.createWithoutSave = function(pageId, user, fileStream, originalName, fileFormat, fileSize) {
  40. const Attachment = this;
  41. const extname = path.extname(originalName);
  42. let fileName = generateFileHash(originalName);
  43. if (extname.length > 1) { // ignore if empty or '.' only
  44. fileName = `${fileName}${extname}`;
  45. }
  46. const attachment = new Attachment();
  47. attachment.page = pageId;
  48. attachment.creator = user._id;
  49. attachment.originalName = originalName;
  50. attachment.fileName = fileName;
  51. attachment.fileFormat = fileFormat;
  52. attachment.fileSize = fileSize;
  53. attachment.createdAt = Date.now();
  54. return attachment;
  55. };
  56. attachmentSchema.methods.isValidExternalUrl = function() {
  57. if (this.externalUrlExpiredAt == null) {
  58. return false;
  59. }
  60. return this.externalUrlExpiredAt.getTime() > new Date().getTime();
  61. };
  62. attachmentSchema.methods.cashExternalUrl = function(externalUrl) {
  63. if (externalUrl == null) {
  64. throw new Error('url is required.');
  65. }
  66. this.externalUrlCached = externalUrl;
  67. this.externalUrlExpiredAt = addSeconds(new Date(), 120);
  68. return this.save();
  69. };
  70. return mongoose.model('Attachment', attachmentSchema);
  71. };