| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- module.exports = function(crowi) {
- var debug = require('debug')('crowi:models:attachment')
- , mongoose = require('mongoose')
- , ObjectId = mongoose.Schema.Types.ObjectId
- , Promise = require('bluebird')
- , fileUploader = require('../util/fileUploader')(crowi)
- ;
- function generateFileHash (fileName) {
- var hasher = require('crypto').createHash('md5');
- hasher.update(fileName);
- return hasher.digest('hex');
- }
- attachmentSchema = new mongoose.Schema({
- page: { type: ObjectId, ref: 'Page', index: true },
- creator: { type: ObjectId, ref: 'User', index: true },
- filePath: { type: String, required: true },
- fileName: { type: String, required: true },
- originalName: { type: String },
- fileFormat: { type: String, required: true },
- fileSize: { type: Number, default: 0 },
- createdAt: { type: Date, default: Date.now }
- }, {
- toJSON: {
- virtuals: true
- }
- });
- attachmentSchema.virtual('fileUrl').get(function() {
- return fileUploader.generateUrl(this.filePath);
- });
- attachmentSchema.statics.getListByPageId = function(id) {
- var self = this;
- return new Promise(function(resolve, reject) {
- self
- .find({page: id})
- .sort({'updatedAt': 1})
- .populate('creator')
- .exec(function(err, data) {
- if (err) {
- return reject(err);
- }
- if (data.length < 1) {
- return resolve([]);
- }
- debug(data);
- return resolve(data);
- });
- });
- };
- attachmentSchema.statics.create = function(pageId, creator, filePath, originalName, fileName, fileFormat, fileSize) {
- var Attachment = this;
- return new Promise(function(resolve, reject) {
- var newAttachment = new Attachment();
- newAttachment.page = pageId;
- newAttachment.creator = creator._id;
- newAttachment.filePath = filePath;
- newAttachment.originalName = originalName;
- newAttachment.fileName = fileName;
- newAttachment.fileFormat = fileFormat;
- newAttachment.fileSize = fileSize;
- newAttachment.createdAt = Date.now();
- newAttachment.save(function(err, data) {
- if (err) {
- debug('Error on saving attachment.', err);
- return reject(err);
- }
- debug('Attachment saved.', data);
- return resolve(data);
- });
- });
- };
- attachmentSchema.statics.createAttachmentFilePath = function (pageId, fileName, fileType) {
- var ext = '.' + fileName.match(/(.*)(?:\.([^.]+$))/)[2];
- return 'attachment/' + pageId + '/' + generateFileHash(fileName) + ext;
- };
- attachmentSchema.statics.removeAttachmentsByPageId = function(pageId) {
- var Attachment = this;
- // todo
- return Promise.resolve();
- //return new Promise(function(resolve, reject) {
- // // target find
- // Attachment.getListByPageId(pageId)
- // .then(function(attachments) {
- // }).then(function(done) {
- // }).catch(function(err) {
- // });
- //});
- };
- return mongoose.model('Attachment', attachmentSchema);
- };
|