uploader.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // file uploader virtual class
  2. // 各アップローダーで共通のメソッドはここで定義する
  3. class Uploader {
  4. constructor(crowi) {
  5. this.crowi = crowi;
  6. this.configManager = crowi.configManager;
  7. }
  8. getIsUploadable() {
  9. return !this.configManager.getConfig('crowi', 'app:fileUploadDisabled') && this.isValidUploadSettings();
  10. }
  11. // File reading is possible even if uploading is disabled
  12. getIsReadable() {
  13. return this.isValidUploadSettings();
  14. }
  15. isValidUploadSettings() {
  16. throw new Error('Implement this');
  17. }
  18. getFileUploadEnabled() {
  19. if (!this.getIsUploadable()) {
  20. return false;
  21. }
  22. return !!this.configManager.getConfig('crowi', 'app:fileUpload');
  23. }
  24. /**
  25. * Check files size limits for all uploaders
  26. *
  27. * @param {*} uploadFileSize
  28. * @param {*} maxFileSize
  29. * @param {*} totalLimit
  30. * @returns
  31. * @memberof Uploader
  32. */
  33. async doCheckLimit(uploadFileSize, maxFileSize, totalLimit) {
  34. if (uploadFileSize > maxFileSize) {
  35. return { isUploadable: false, errorMessage: 'File size exceeds the size limit per file' };
  36. }
  37. const Attachment = this.crowi.model('Attachment');
  38. // Get attachment total file size
  39. const res = await Attachment.aggregate().group({
  40. _id: null,
  41. total: { $sum: '$fileSize' },
  42. });
  43. // Return res is [] if not using
  44. const usingFilesSize = res.length === 0 ? 0 : res[0].total;
  45. if (usingFilesSize + uploadFileSize > totalLimit) {
  46. return { isUploadable: false, errorMessage: 'Uploading files reaches limit' };
  47. }
  48. return { isUploadable: true };
  49. }
  50. /**
  51. * Checks if Uploader can respond to the HTTP request.
  52. */
  53. canRespond() {
  54. return false;
  55. }
  56. /**
  57. * Respond to the HTTP request.
  58. * @param {Response} res
  59. * @param {Response} attachment
  60. */
  61. respond(res, attachment) {
  62. throw new Error('Implement this');
  63. }
  64. }
  65. module.exports = Uploader;