attachment.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. temporaryUrlCached: { type: String },
  27. temporaryUrlExpiredAt: { 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.getValidTemporaryUrl = function() {
  57. if (this.temporaryUrlExpiredAt == null) {
  58. return null;
  59. }
  60. // return null when expired url
  61. if (this.temporaryUrlExpiredAt.getTime() < new Date().getTime()) {
  62. return null;
  63. }
  64. return this.temporaryUrlCached;
  65. };
  66. attachmentSchema.methods.cashTemporaryUrlByProvideSec = function(temporaryUrl, provideSec) {
  67. if (temporaryUrl == null) {
  68. throw new Error('url is required.');
  69. }
  70. this.temporaryUrlCached = temporaryUrl;
  71. this.temporaryUrlExpiredAt = addSeconds(new Date(), provideSec);
  72. return this.save();
  73. };
  74. return mongoose.model('Attachment', attachmentSchema);
  75. };