attachment.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. module.exports = function(crowi) {
  2. var debug = require('debug')('crowi:models:attachment')
  3. , mongoose = require('mongoose')
  4. , ObjectId = mongoose.Schema.Types.ObjectId
  5. , Promise = require('bluebird')
  6. , fileUploader = require('../util/fileUploader')(crowi)
  7. ;
  8. function generateFileHash (fileName) {
  9. var hasher = require('crypto').createHash('md5');
  10. hasher.update(fileName);
  11. return hasher.digest('hex');
  12. }
  13. attachmentSchema = new mongoose.Schema({
  14. page: { type: ObjectId, ref: 'Page', index: true },
  15. creator: { type: ObjectId, ref: 'User', index: true },
  16. filePath: { type: String, required: true },
  17. fileName: { type: String, required: true },
  18. originalName: { type: String },
  19. fileFormat: { type: String, required: true },
  20. fileSize: { type: Number, default: 0 },
  21. createdAt: { type: Date, default: Date.now }
  22. }, {
  23. toJSON: {
  24. virtuals: true
  25. }
  26. });
  27. attachmentSchema.virtual('fileUrl').get(function() {
  28. return fileUploader.generateUrl(this.filePath);
  29. });
  30. attachmentSchema.statics.findById = function(id) {
  31. var Attachment = this;
  32. return new Promise(function(resolve, reject) {
  33. Attachment.findOne({_id: id}, function(err, data) {
  34. if (err) {
  35. return reject(err);
  36. }
  37. if (data === null) {
  38. return reject(new Error('Attachment not found'));
  39. }
  40. return resolve(data);
  41. });
  42. });
  43. };
  44. attachmentSchema.statics.getListByPageId = function(id) {
  45. var self = this;
  46. return new Promise(function(resolve, reject) {
  47. self
  48. .find({page: id})
  49. .sort({'updatedAt': 1})
  50. .populate('creator')
  51. .exec(function(err, data) {
  52. if (err) {
  53. return reject(err);
  54. }
  55. if (data.length < 1) {
  56. return resolve([]);
  57. }
  58. debug(data);
  59. return resolve(data);
  60. });
  61. });
  62. };
  63. attachmentSchema.statics.create = function(pageId, creator, filePath, originalName, fileName, fileFormat, fileSize) {
  64. var Attachment = this;
  65. return new Promise(function(resolve, reject) {
  66. var newAttachment = new Attachment();
  67. newAttachment.page = pageId;
  68. newAttachment.creator = creator._id;
  69. newAttachment.filePath = filePath;
  70. newAttachment.originalName = originalName;
  71. newAttachment.fileName = fileName;
  72. newAttachment.fileFormat = fileFormat;
  73. newAttachment.fileSize = fileSize;
  74. newAttachment.createdAt = Date.now();
  75. newAttachment.save(function(err, data) {
  76. if (err) {
  77. debug('Error on saving attachment.', err);
  78. return reject(err);
  79. }
  80. debug('Attachment saved.', data);
  81. return resolve(data);
  82. });
  83. });
  84. };
  85. attachmentSchema.statics.createAttachmentFilePath = function (pageId, fileName, fileType) {
  86. var ext = '.' + fileName.match(/(.*)(?:\.([^.]+$))/)[2];
  87. return 'attachment/' + pageId + '/' + generateFileHash(fileName) + ext;
  88. };
  89. attachmentSchema.statics.removeAttachmentsByPageId = function(pageId) {
  90. var Attachment = this;
  91. // todo
  92. return Promise.resolve();
  93. //return new Promise(function(resolve, reject) {
  94. // // target find
  95. // Attachment.getListByPageId(pageId)
  96. // .then(function(attachments) {
  97. // }).then(function(done) {
  98. // }).catch(function(err) {
  99. // });
  100. //});
  101. };
  102. attachmentSchema.statics.createCacheFileName = function(attachment) {
  103. return crowi.cacheDir + 'attachment-' + attachment._id;
  104. };
  105. attachmentSchema.statics.findDeliveryFile = function(attachment) {
  106. // find local
  107. var fs = require('fs');
  108. var deliveryFile = {
  109. filename: '',
  110. options: {
  111. headers: {
  112. 'Content-Type': attachment.fileFormat,
  113. },
  114. },
  115. };
  116. var cacheFileName = this.createCacheFileName(attachment);
  117. // とちゅう
  118. return deliveryFile;
  119. };
  120. return mongoose.model('Attachment', attachmentSchema);
  121. };