Просмотр исходного кода

refactor getFilePathOnStorage function

Yuki Takei 7 лет назад
Родитель
Сommit
82cae2be8b

+ 1 - 14
src/server/models/attachment.js

@@ -3,8 +3,6 @@ module.exports = function(crowi) {
   const mongoose = require('mongoose');
   const mongoose = require('mongoose');
   const ObjectId = mongoose.Schema.Types.ObjectId;
   const ObjectId = mongoose.Schema.Types.ObjectId;
 
 
-  const urljoin = require('url-join');
-
   const fileUploader = require('../service/file-uploader')(crowi);
   const fileUploader = require('../service/file-uploader')(crowi);
 
 
   let attachmentSchema;
   let attachmentSchema;
@@ -20,7 +18,7 @@ module.exports = function(crowi) {
   attachmentSchema = new mongoose.Schema({
   attachmentSchema = new mongoose.Schema({
     page: { type: ObjectId, ref: 'Page', index: true },
     page: { type: ObjectId, ref: 'Page', index: true },
     creator: { type: ObjectId, ref: 'User', index: true  },
     creator: { type: ObjectId, ref: 'User', index: true  },
-    filePath: { type: String },   // DEPRECATED: remains for backward compatibility for v3.3.3 or below
+    filePath: { type: String },   // DEPRECATED: remains for backward compatibility for v3.3.5 or below
     fileName: { type: String, required: true },
     fileName: { type: String, required: true },
     originalName: { type: String },
     originalName: { type: String },
     fileFormat: { type: String, required: true },
     fileFormat: { type: String, required: true },
@@ -36,17 +34,6 @@ module.exports = function(crowi) {
     return `/download/${this._id}`;
     return `/download/${this._id}`;
   });
   });
 
 
-  attachmentSchema.methods.getFilePathOnStorage = function() {
-    if (this.filePath != null) {
-      return this.filePath;
-    }
-
-    const pageId = this.page._id || this.page;
-    const filePath = urljoin('/attachment', pageId.toString(), this.fileName);
-
-    return filePath;
-  };
-
   attachmentSchema.statics.create = async function(pageId, user, fileStream, originalName, fileFormat, fileSize) {
   attachmentSchema.statics.create = async function(pageId, user, fileStream, originalName, fileFormat, fileSize) {
     const Attachment = this;
     const Attachment = this;
 
 

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

@@ -37,6 +37,17 @@ module.exports = function(crowi) {
     return new aws.S3();
     return new aws.S3();
   }
   }
 
 
+  function getFilePathOnStorage(attachment) {
+    if (attachment.filePath != null) {  // remains for backward compatibility for v3.3.4 or below
+      return attachment.filePath;
+    }
+
+    const pageId = attachment.page._id || attachment.page;
+    const filePath = urljoin('/attachment', pageId.toString(), attachment.fileName);
+
+    return filePath;
+  }
+
   lib.deleteFile = function(fileId, filePath) {
   lib.deleteFile = function(fileId, filePath) {
     const s3 = S3Factory();
     const s3 = S3Factory();
     const awsConfig = getAwsConfig();
     const awsConfig = getAwsConfig();
@@ -92,7 +103,7 @@ module.exports = function(crowi) {
     // construct url
     // construct url
     const awsConfig = getAwsConfig();
     const awsConfig = getAwsConfig();
     const baseUrl = `https://${awsConfig.bucket}.s3.amazonaws.com`;
     const baseUrl = `https://${awsConfig.bucket}.s3.amazonaws.com`;
-    const url = urljoin(baseUrl, attachment.getFilePathOnStorage());
+    const url = urljoin(baseUrl, getFilePathOnStorage(attachment));
 
 
     let response;
     let response;
     try {
     try {

+ 13 - 3
src/server/service/file-uploader/local.js

@@ -8,7 +8,18 @@ module.exports = function(crowi) {
   'use strict';
   'use strict';
 
 
   const lib = {};
   const lib = {};
-  const basePath = path.posix.join(crowi.publicDir, 'uploads'); // TODO: to configurable
+  const basePath = path.posix.join(crowi.publicDir, 'uploads');
+
+  function getFilePathOnStorage(attachment) {
+    if (attachment.filePath != null) {  // remains for backward compatibility for v3.3.5 or below
+      return attachment.filePath;
+    }
+
+    const pageId = attachment.page._id || attachment.page;
+    const filePath = path.posix.join(basePath, pageId.toString(), attachment.fileName);
+
+    return filePath;
+  }
 
 
   lib.deleteFile = function(fileId, filePath) {
   lib.deleteFile = function(fileId, filePath) {
     debug('File deletion: ' + filePath);
     debug('File deletion: ' + filePath);
@@ -54,8 +65,7 @@ module.exports = function(crowi) {
    * @return {stream.Readable} readable stream
    * @return {stream.Readable} readable stream
    */
    */
   lib.findDeliveryFile = async function(attachment) {
   lib.findDeliveryFile = async function(attachment) {
-    const uploadDir = path.posix.join(crowi.publicDir, 'uploads');
-    const filePath = path.posix.join(uploadDir, attachment.getFilePathOnStorage());
+    const filePath = getFilePathOnStorage(attachment);
 
 
     // check file exists
     // check file exists
     try {
     try {