gridfs.js 2.7 KB

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