uploader.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. deleteFiles() {
  25. throw new Error('Implemnt this');
  26. }
  27. /**
  28. * Check files size limits for all uploaders
  29. *
  30. * @param {*} uploadFileSize
  31. * @param {*} maxFileSize
  32. * @param {*} totalLimit
  33. * @returns
  34. * @memberof Uploader
  35. */
  36. async doCheckLimit(uploadFileSize, maxFileSize, totalLimit) {
  37. if (uploadFileSize > maxFileSize) {
  38. return { isUploadable: false, errorMessage: 'File size exceeds the size limit per file' };
  39. }
  40. const Attachment = this.crowi.model('Attachment');
  41. // Get attachment total file size
  42. const res = await Attachment.aggregate().group({
  43. _id: null,
  44. total: { $sum: '$fileSize' },
  45. });
  46. // Return res is [] if not using
  47. const usingFilesSize = res.length === 0 ? 0 : res[0].total;
  48. if (usingFilesSize + uploadFileSize > totalLimit) {
  49. return { isUploadable: false, errorMessage: 'Uploading files reaches limit' };
  50. }
  51. return { isUploadable: true };
  52. }
  53. /**
  54. * Checks if Uploader can respond to the HTTP request.
  55. */
  56. canRespond() {
  57. return false;
  58. }
  59. /**
  60. * Respond to the HTTP request.
  61. * @param {Response} res
  62. * @param {Response} attachment
  63. */
  64. respond(res, attachment) {
  65. throw new Error('Implement this');
  66. }
  67. }
  68. module.exports = Uploader;