attachment.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import path from 'path';
  2. import loggerFactory from '~/utils/logger';
  3. import { AttachmentType } from '../interfaces/attachment';
  4. const { addSeconds } = require('date-fns');
  5. const mongoose = require('mongoose');
  6. const mongoosePaginate = require('mongoose-paginate-v2');
  7. const uniqueValidator = require('mongoose-unique-validator');
  8. // eslint-disable-next-line no-unused-vars
  9. const logger = loggerFactory('growi:models:attachment');
  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. temporaryUrlCached: { type: String },
  26. temporaryUrlExpiredAt: { type: Date },
  27. attachmentType: {
  28. type: String,
  29. enum: AttachmentType,
  30. required: true,
  31. },
  32. }, {
  33. timestamps: { createdAt: true, updatedAt: false },
  34. });
  35. attachmentSchema.plugin(uniqueValidator);
  36. attachmentSchema.plugin(mongoosePaginate);
  37. attachmentSchema.virtual('filePathProxied').get(function() {
  38. return `/attachment/${this._id}`;
  39. });
  40. attachmentSchema.virtual('downloadPathProxied').get(function() {
  41. return `/download/${this._id}`;
  42. });
  43. attachmentSchema.set('toObject', { virtuals: true });
  44. attachmentSchema.set('toJSON', { virtuals: true });
  45. attachmentSchema.statics.createWithoutSave = function(pageId, user, fileStream, originalName, fileFormat, fileSize, attachmentType) {
  46. // eslint-disable-next-line @typescript-eslint/no-this-alias
  47. const Attachment = this;
  48. const extname = path.extname(originalName);
  49. let fileName = generateFileHash(originalName);
  50. if (extname.length > 1) { // ignore if empty or '.' only
  51. fileName = `${fileName}${extname}`;
  52. }
  53. const attachment = new Attachment();
  54. attachment.page = pageId;
  55. attachment.creator = user._id;
  56. attachment.originalName = originalName;
  57. attachment.fileName = fileName;
  58. attachment.fileFormat = fileFormat;
  59. attachment.fileSize = fileSize;
  60. attachment.attachmentType = attachmentType;
  61. return attachment;
  62. };
  63. attachmentSchema.methods.getValidTemporaryUrl = function() {
  64. if (this.temporaryUrlExpiredAt == null) {
  65. return null;
  66. }
  67. // return null when expired url
  68. if (this.temporaryUrlExpiredAt.getTime() < new Date().getTime()) {
  69. return null;
  70. }
  71. return this.temporaryUrlCached;
  72. };
  73. attachmentSchema.methods.cashTemporaryUrlByProvideSec = function(temporaryUrl, provideSec) {
  74. if (temporaryUrl == null) {
  75. throw new Error('url is required.');
  76. }
  77. this.temporaryUrlCached = temporaryUrl;
  78. this.temporaryUrlExpiredAt = addSeconds(new Date(), provideSec);
  79. return this.save();
  80. };
  81. return mongoose.model('Attachment', attachmentSchema);
  82. };