| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- // disable no-return-await for model functions
- /* eslint-disable no-return-await */
- // eslint-disable-next-line no-unused-vars
- const logger = require('@alias/logger')('growi:models:attachment');
- const path = require('path');
- const mongoose = require('mongoose');
- const uniqueValidator = require('mongoose-unique-validator');
- const ObjectId = mongoose.Schema.Types.ObjectId;
- module.exports = function(crowi) {
- function generateFileHash(fileName) {
- const hash = require('crypto').createHash('md5');
- hash.update(`${fileName}_${Date.now()}`);
- return hash.digest('hex');
- }
- const attachmentSchema = new mongoose.Schema({
- page: { type: ObjectId, ref: 'Page', index: true },
- creator: { type: ObjectId, ref: 'User', index: true },
- filePath: { type: String }, // DEPRECATED: remains for backward compatibility for v3.3.x or below
- fileName: { type: String, required: true, unique: true },
- originalName: { type: String },
- fileFormat: { type: String, required: true },
- fileSize: { type: Number, default: 0 },
- createdAt: { type: Date, default: Date.now },
- });
- attachmentSchema.plugin(uniqueValidator);
- attachmentSchema.virtual('filePathProxied').get(function() {
- return `/attachment/${this._id}`;
- });
- attachmentSchema.virtual('downloadPathProxied').get(function() {
- return `/download/${this._id}`;
- });
- attachmentSchema.set('toObject', { virtuals: true });
- attachmentSchema.set('toJSON', { virtuals: true });
- attachmentSchema.statics.create = async function(pageId, user, fileStream, originalName, fileFormat, fileSize) {
- const fileUploader = require('../service/file-uploader')(crowi);
- const Attachment = this;
- const extname = path.extname(originalName);
- let fileName = generateFileHash(originalName);
- if (extname.length > 1) { // ignore if empty or '.' only
- fileName = `${fileName}${extname}`;
- }
- let attachment = new Attachment();
- attachment.page = pageId;
- attachment.creator = user._id;
- attachment.originalName = originalName;
- attachment.fileName = fileName;
- attachment.fileFormat = fileFormat;
- attachment.fileSize = fileSize;
- attachment.createdAt = Date.now();
- // upload file
- await fileUploader.uploadFile(fileStream, attachment);
- // save attachment
- attachment = await attachment.save();
- return attachment;
- };
- attachmentSchema.statics.removeAttachmentsByPageId = async function(pageId) {
- const attachments = await this.find({ page: pageId });
- const promises = attachments.map(async(attachment) => {
- return this.removeWithSubstanceById(attachment._id);
- });
- return Promise.all(promises);
- };
- attachmentSchema.statics.removeWithSubstanceById = async function(id) {
- const fileUploader = require('../service/file-uploader')(crowi);
- // retrieve data from DB to get a completely populated instance
- const attachment = await this.findById(id);
- await fileUploader.deleteFile(attachment);
- return await attachment.remove();
- };
- return mongoose.model('Attachment', attachmentSchema);
- };
|