gridfs.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // crowi-fileupload-gridFS
  2. module.exports = function(crowi) {
  3. 'use strict';
  4. var debug = require('debug')('growi:service:fileUploadergridfs')
  5. var logger = require('@alias/logger')('growi:routes:attachment')
  6. var mongoose = require('mongoose');
  7. var path = require('path');
  8. var fs = require('fs');
  9. var lib = {};
  10. var AttachmentFile = {};
  11. // instantiate mongoose-gridfs
  12. var gridfs = require('mongoose-gridfs')({
  13. collection: 'attachmentFiles',
  14. model: 'AttachmentFile',
  15. mongooseConnection: mongoose.connection
  16. });
  17. // obtain a model
  18. AttachmentFile = gridfs.model;
  19. // // delete a file
  20. // lib.deleteFile = async function(fileId, filePath) {
  21. // debug('File deletion: ' + fileId);
  22. // await AttachmentFile.unlinkById(fileId, function(error, unlinkedAttachment) {
  23. // if (error) {
  24. // throw new Error(error);
  25. // }
  26. // });
  27. // };
  28. // create or save a file
  29. lib.uploadFile = async function(filePath, contentType, fileStream, options) {
  30. debug('File uploading: ' + filePath);
  31. AttachmentFile.write({filename: filePath, contentType: contentType}, fileStream,
  32. function(error, createdFile) {
  33. if (error) {
  34. throw new Error('Failed to upload ' + createdFile + 'to gridFS', error);
  35. }
  36. });
  37. };
  38. lib.findDeliveryFile = function(fileId, filePath) {
  39. // const cacheFile = lib.createCacheFileName(fileId);
  40. // debug('find delivery file', cacheFile);
  41. // if (!lib.shouldUpdateCacheFile(cacheFile)) {
  42. // return cacheFile;
  43. // }
  44. // const fileStream = fs.createWriteStream(cacheFile);
  45. // const fileUrl = lib.generateUrl(filePath);
  46. // debug('Load attachement file into local cache file', fileUrl, cacheFile);
  47. // return cacheFile;
  48. // };
  49. // // private
  50. // lib.createCacheFileName = function(fileId) {
  51. // return path.join(crowi.cacheDir, `attachment-${fileId}`);
  52. // };
  53. // // private
  54. // lib.shouldUpdateCacheFile = function(filePath) {
  55. // try {
  56. // const stats = fs.statSync(filePath);
  57. // if (!stats.isFile()) {
  58. // debug('Cache file not found or the file is not a regular fil.');
  59. // return true;
  60. // }
  61. // if (stats.size <= 0) {
  62. // debug('Cache file found but the size is 0');
  63. // return true;
  64. // }
  65. // }
  66. // catch (e) {
  67. // // no such file or directory
  68. // debug('Stats error', e);
  69. // return true;
  70. // }
  71. // return false;
  72. };
  73. lib.generateUrl = function(filePath) {
  74. // var config = crowi.getConfig();
  75. // var baseUrl = (config.crowi['app:siteUrl:fixed'] || '');
  76. // const url = `${baseUrl}/_api/attachments.getMongo?filePath=${filePath}`;
  77. // return url;
  78. return `/files/${filePath}`;
  79. };
  80. return lib;
  81. };