Răsfoiți Sursa

WIP: refactor attachment

* change 'filePathOnStorage' to getter method from virtual property
Yuki Takei 7 ani în urmă
părinte
comite
2ceb2024aa

+ 3 - 5
src/server/models/attachment.js

@@ -16,6 +16,7 @@ module.exports = function(crowi) {
     return hash.digest('hex');
   }
 
+
   attachmentSchema = new mongoose.Schema({
     page: { type: ObjectId, ref: 'Page', index: true },
     creator: { type: ObjectId, ref: 'User', index: true  },
@@ -24,10 +25,7 @@ module.exports = function(crowi) {
     originalName: { type: String },
     fileFormat: { type: String, required: true },
     fileSize: { type: Number, default: 0 },
-    createdAt: { type: Date, default: Date.now },
-  }, {
-    toJSON: {
-    },
+    createdAt: { type: Date, default: Date.now() },
   });
 
   attachmentSchema.virtual('filePathProxied').get(function() {
@@ -38,7 +36,7 @@ module.exports = function(crowi) {
     return `/download/${this._id}`;
   });
 
-  attachmentSchema.virtual('filePathOnStorage').get(function() {
+  attachmentSchema.methods.getFilePathOnStorage = function() {
     if (this.filePath != null) {
       return this.filePath;
     }

+ 1 - 9
src/server/routes/attachment.js

@@ -140,15 +140,7 @@ module.exports = function(crowi, app) {
       .sort({'updatedAt': 1})
       .populate('creator', User.USER_PUBLIC_FIELDS);
 
-    // toJSON
-    attachments = attachments.map(attachment => {
-      const json = attachment.toJSON({ virtuals: true});
-
-      // omit unnecessary property
-      json.filePathOnStorage = undefined;
-
-      return json;
-    });
+    attachments = attachments.map(attachment => attachment.toObject({ virtuals: true }));
 
     return res.json(ApiResponse.success({ attachments }));
   };

+ 1 - 1
src/server/service/file-uploader/aws.js

@@ -92,7 +92,7 @@ module.exports = function(crowi) {
     // construct url
     const awsConfig = getAwsConfig();
     const baseUrl = `https://${awsConfig.bucket}.s3.amazonaws.com`;
-    const url = urljoin(baseUrl, attachment.filePathOnStorage);
+    const url = urljoin(baseUrl, attachment.getFilePathOnStorage());
 
     let response;
     try {

+ 1 - 1
src/server/service/file-uploader/local.js

@@ -55,7 +55,7 @@ module.exports = function(crowi) {
    */
   lib.findDeliveryFile = async function(attachment) {
     const uploadDir = path.posix.join(crowi.publicDir, 'uploads');
-    const filePath = path.posix.join(uploadDir, attachment.filePathOnStorage);
+    const filePath = path.posix.join(uploadDir, attachment.getFilePathOnStorage());
 
     // check file exists
     try {