local.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. const logger = require('@alias/logger')('growi:service:fileUploaderLocal');
  2. const fs = require('fs');
  3. const path = require('path');
  4. const mkdir = require('mkdirp');
  5. const streamToPromise = require('stream-to-promise');
  6. const urljoin = require('url-join');
  7. module.exports = function(crowi) {
  8. const Uploader = require('./uploader');
  9. const lib = new Uploader(crowi);
  10. const basePath = path.posix.join(crowi.publicDir, 'uploads');
  11. function getFilePathOnStorage(attachment) {
  12. let filePath;
  13. if (attachment.filePath != null) { // backward compatibility for v3.3.x or below
  14. filePath = path.posix.join(basePath, attachment.filePath);
  15. }
  16. else {
  17. const dirName = (attachment.page != null)
  18. ? 'attachment'
  19. : 'user';
  20. filePath = path.posix.join(basePath, dirName, attachment.fileName);
  21. }
  22. return filePath;
  23. }
  24. lib.isValidUploadSettings = function() {
  25. return true;
  26. };
  27. lib.deleteFile = async function(attachment) {
  28. const filePath = getFilePathOnStorage(attachment);
  29. return lib.deleteFileByFilePath(filePath);
  30. };
  31. lib.deleteFiles = async function(attachments) {
  32. attachments.map((attachment) => {
  33. return this.deleteFile(attachment);
  34. });
  35. };
  36. lib.deleteFileByFilePath = async function(filePath) {
  37. // check file exists
  38. try {
  39. fs.statSync(filePath);
  40. }
  41. catch (err) {
  42. logger.warn(`Any AttachmentFile which path is '${filePath}' does not exist in local fs`);
  43. return;
  44. }
  45. return fs.unlinkSync(filePath);
  46. };
  47. lib.uploadFile = async function(fileStream, attachment) {
  48. logger.debug(`File uploading: fileName=${attachment.fileName}`);
  49. const filePath = getFilePathOnStorage(attachment);
  50. const dirpath = path.posix.dirname(filePath);
  51. // mkdir -p
  52. mkdir.sync(dirpath);
  53. const stream = fileStream.pipe(fs.createWriteStream(filePath));
  54. return streamToPromise(stream);
  55. };
  56. /**
  57. * Find data substance
  58. *
  59. * @param {Attachment} attachment
  60. * @return {stream.Readable} readable stream
  61. */
  62. lib.findDeliveryFile = async function(attachment) {
  63. const filePath = getFilePathOnStorage(attachment);
  64. // check file exists
  65. try {
  66. fs.statSync(filePath);
  67. }
  68. catch (err) {
  69. throw new Error(`Any AttachmentFile that relate to the Attachment (${attachment._id.toString()}) does not exist in local fs`);
  70. }
  71. // return stream.Readable
  72. return fs.createReadStream(filePath);
  73. };
  74. /**
  75. * check the file size limit
  76. *
  77. * In detail, the followings are checked.
  78. * - per-file size limit (specified by MAX_FILE_SIZE)
  79. */
  80. lib.checkLimit = async(uploadFileSize) => {
  81. const maxFileSize = crowi.configManager.getConfig('crowi', 'app:maxFileSize');
  82. const totalLimit = crowi.configManager.getConfig('crowi', 'app:fileUploadTotalLimit');
  83. return lib.doCheckLimit(uploadFileSize, maxFileSize, totalLimit);
  84. };
  85. /**
  86. * Checks if Uploader can respond to the HTTP request.
  87. */
  88. lib.canRespond = () => {
  89. // Check whether to use internal redirect of nginx or Apache.
  90. return lib.configManager.getConfig('crowi', 'fileUpload:local:useInternalRedirect');
  91. };
  92. /**
  93. * Respond to the HTTP request.
  94. * @param {Response} res
  95. * @param {Response} attachment
  96. */
  97. lib.respond = (res, attachment) => {
  98. // Responce using internal redirect of nginx or Apache.
  99. const storagePath = getFilePathOnStorage(attachment);
  100. const relativePath = path.relative(crowi.publicDir, storagePath);
  101. const internalPathRoot = lib.configManager.getConfig('crowi', 'fileUpload:local:internalRedirectPath');
  102. const internalPath = urljoin(internalPathRoot, relativePath);
  103. res.set('X-Accel-Redirect', internalPath);
  104. res.set('X-Sendfile', storagePath);
  105. return res.end();
  106. };
  107. return lib;
  108. };