attachment.js 3.9 KB

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