Browse Source

重新整理文件系统配置文件以及用法

NorthLan 6 years ago
parent
commit
c13272ba66

+ 17 - 0
zen-core/src/main/kotlin/com/gxzc/zen/common/properties/FSProperties.kt

@@ -0,0 +1,17 @@
+package com.gxzc.zen.common.properties
+
+import org.springframework.boot.context.properties.ConfigurationProperties
+import org.springframework.stereotype.Component
+
+/**
+ * FileSystem配置
+ * @author NorthLan
+ * @date 2018/9/5
+ * @url https://noahlan.com
+ */
+@Component
+@ConfigurationProperties(prefix = "fs")
+open class FSProperties {
+    var maxFileSize: Long = 4294967296L // 4 * 1024 * 1024 * 1024
+    var chunkSize: Long = 1048576L // 1 * 1024 * 1024
+}

+ 17 - 0
zen-core/src/main/kotlin/com/gxzc/zen/common/properties/FdfsProperties.kt

@@ -0,0 +1,17 @@
+package com.gxzc.zen.common.properties
+
+import org.springframework.boot.context.properties.ConfigurationProperties
+import org.springframework.stereotype.Component
+
+/**
+ * FastDFS配置
+ * @author NorthLan
+ * @date 2018/9/5
+ * @url https://noahlan.com
+ */
+@Component
+@ConfigurationProperties(prefix = "fdfs")
+open class FdfsProperties {
+    var enable: Boolean = true
+    var tmpPath: String = "/tmp/zen"
+}

+ 2 - 4
zen-core/src/main/kotlin/com/gxzc/zen/common/properties/UploadProperties.kt

@@ -12,8 +12,6 @@ import org.springframework.stereotype.Component
 @Component
 @ConfigurationProperties(prefix = "upload")
 open class UploadProperties {
-    var tmpPath: String? = "/tmp/zen"
-    var dataPath: String? = "/data/zen"
-    var maxFileSize: Long? = 4294967296L // 4 * 1024 * 1024 * 1024
-    var chunkSize: Long? = 1048576L // 1 * 1024 * 1024
+    var tmpPath: String = "/tmp/zen"
+    var dataPath: String = "/data/zen"
 }

+ 11 - 9
zen-core/src/main/kotlin/com/gxzc/zen/common/util/upload/FdfsUploadUtil.kt

@@ -6,7 +6,8 @@ import com.github.tobato.fastdfs.service.AppendFileStorageClient
 import com.github.tobato.fastdfs.service.FastFileStorageClient
 import com.gxzc.zen.common.exception.ZenException
 import com.gxzc.zen.common.exception.ZenExceptionEnum
-import com.gxzc.zen.common.properties.UploadProperties
+import com.gxzc.zen.common.properties.FSProperties
+import com.gxzc.zen.common.properties.FdfsProperties
 import com.gxzc.zen.common.util.KeyLock
 import com.gxzc.zen.common.util.SpringContextHolder
 import com.gxzc.zen.common.util.upload.constants.CheckStatus
@@ -31,7 +32,8 @@ import java.nio.file.Paths
 object FdfsUploadUtil {
     private val logger = LoggerFactory.getLogger(FdfsUploadUtil::class.java)
 
-    private val uploadProperties: UploadProperties by lazy { SpringContextHolder.getBean(UploadProperties::class.java)!! }
+    private val fdfsProperties: FdfsProperties by lazy { SpringContextHolder.getBean(FdfsProperties::class.java)!! }
+    private val fsProperties: FSProperties by lazy { SpringContextHolder.getBean(FSProperties::class.java)!! }
 
     private val appendFileStorageClient: AppendFileStorageClient by lazy { SpringContextHolder.getBean(AppendFileStorageClient::class.java)!! }
     private val fastFileStorageClient: FastFileStorageClient by lazy { SpringContextHolder.getBean(FastFileStorageClient::class.java)!! }
@@ -96,7 +98,7 @@ object FdfsUploadUtil {
                     ret.checkStatus = CheckStatus.FDFS_NO_DB_FULL_CHUNK
 
                     val hashedMd5 = fileMetadata.md5!!.hashCode()
-                    val tmpPath = uploadProperties.tmpPath!!
+                    val tmpPath = fdfsProperties.tmpPath
 
                     checkMergeLock.lock(hashedMd5)
                     try {
@@ -120,8 +122,8 @@ object FdfsUploadUtil {
      * 文件夹结构/文件名
      */
     fun upload(fileMetadata: ZenFileMetadata, file: MultipartFile): UploadResult {
-        val tmpPath = uploadProperties.tmpPath!!
-        val chunkSize = uploadProperties.chunkSize!!
+        val tmpPath = fdfsProperties.tmpPath
+        val chunkSize = fsProperties.chunkSize
         var storePath: StorePath? = null
         if (validateRequest(fileMetadata, file)) {
             val filename = fileMetadata.filename!!
@@ -182,7 +184,7 @@ object FdfsUploadUtil {
         val totalChunks = fileMetadata.totalChunks!!
         val md5 = fileMetadata.md5!!
         //        val hashedMd5 = md5.hashCode()
-        //        val chunkSize = uploadProperties!!.chunkSize!!
+        //        val chunkSize = fdfsProperties!!.chunkSize!!
         var storePath: StorePath? = null
         for (i in 1..totalChunks) {
             val sourceFile = File(getChunkFilename(sourceRootPath, i, md5))
@@ -224,7 +226,7 @@ object FdfsUploadUtil {
      * 检查分片是否存在 (tmp目录)
      */
     private fun chunkExists(chunkNumber: Int?, md5: String): Boolean {
-        val tmpPath = uploadProperties.tmpPath!!
+        val tmpPath = fdfsProperties.tmpPath
         return Files.exists(Paths.get(getChunkFilename(tmpPath, chunkNumber, md5)))
     }
 
@@ -275,9 +277,9 @@ object FdfsUploadUtil {
         }
 
         // is the file too large?
-        uploadProperties.let {
+        fsProperties.let {
             val maxFileSize = it.maxFileSize
-            if (maxFileSize != null && maxFileSize > 0) {
+            if (maxFileSize > 0) {
                 if (totalSize > maxFileSize) {
                     logger.error("filesize limit: [${maxFileSize / 1024 / 1024} MB], now [${totalSize / 1024 / 1024} MB]")
                     return false

+ 12 - 16
zen-core/src/main/kotlin/com/gxzc/zen/common/util/upload/UploadUtil.kt

@@ -2,6 +2,7 @@ package com.gxzc.zen.common.util.upload
 
 import com.gxzc.zen.common.exception.ZenException
 import com.gxzc.zen.common.exception.ZenExceptionEnum
+import com.gxzc.zen.common.properties.FSProperties
 import com.gxzc.zen.common.properties.UploadProperties
 import com.gxzc.zen.common.util.FileUtil
 import com.gxzc.zen.common.util.KeyLock
@@ -26,13 +27,8 @@ import java.nio.file.Paths
 object UploadUtil {
     private val logger = LoggerFactory.getLogger(UploadUtil::class.java)
 
-    private var uploadProperties = SpringContextHolder.getBean(UploadProperties::class.java)
-        get() {
-            if (field == null) {
-                field = SpringContextHolder.getBean(UploadProperties::class.java)
-            }
-            return field
-        }
+    private val uploadProperties: UploadProperties by lazy { SpringContextHolder.getBean(UploadProperties::class.java)!! }
+    private val fsProperties: FSProperties by lazy { SpringContextHolder.getBean(FSProperties::class.java)!! }
 
     private val checkMergeLock = KeyLock<Int>()
     private val mergeLock = KeyLock<Int>()
@@ -45,9 +41,9 @@ object UploadUtil {
      * 文件夹结构/文件名
      */
     fun upload(fileMetadata: ZenFileMetadata, file: MultipartFile): UploadResult {
-        val tmpPath = uploadProperties!!.tmpPath!!
-        val dataPath = uploadProperties!!.dataPath!!
-        val chunkSize = uploadProperties!!.chunkSize!!
+        val tmpPath = uploadProperties.tmpPath
+        val dataPath = uploadProperties.dataPath
+        val chunkSize = fsProperties.chunkSize
         var retFile: File? = null
         if (validateRequest(fileMetadata, file)) {
             val filename = fileMetadata.filename!!
@@ -114,8 +110,8 @@ object UploadUtil {
      * 真实文件不存在,检查分片上传情况
      */
     fun checkUpload(fileMetadata: ZenFileMetadata): CheckResult {
-        val tmpPath = uploadProperties!!.tmpPath!!
-        val dataPath = uploadProperties!!.dataPath!!
+        val tmpPath = uploadProperties.tmpPath
+        val dataPath = uploadProperties.dataPath
         val ret = CheckResult()
         if (validateRequest(fileMetadata, null)) {
             if (fileExists(fileMetadata)) {
@@ -163,12 +159,12 @@ object UploadUtil {
      * 检查文件是否存在 (真实文件)
      */
     private fun fileExists(fileMetadata: ZenFileMetadata): Boolean {
-        val dataPath = uploadProperties!!.dataPath!!
+        val dataPath = uploadProperties.dataPath
         // 获取filename 真实文件
         val filename = fileMetadata.filename!!
         val md5 = fileMetadata.md5!!
         val realFilename = getFullDestFilename(dataPath, filename, md5)
-        return Files.exists(Paths.get(realFilename)) && FileUtil.md5HeadTail(realFilename, uploadProperties!!.chunkSize!!.toInt()) == md5 // # 防篡改
+        return Files.exists(Paths.get(realFilename)) && FileUtil.md5HeadTail(realFilename, fsProperties.chunkSize.toInt()) == md5 // # 防篡改
     }
 
     /**
@@ -288,9 +284,9 @@ object UploadUtil {
         }
 
         // is the file too large?
-        uploadProperties?.let {
+        fsProperties.let {
             val maxFileSize = it.maxFileSize
-            if (maxFileSize != null && maxFileSize > 0) {
+            if (maxFileSize > 0) {
                 if (totalSize > maxFileSize) {
                     logger.error("filesize limit: [${maxFileSize / 1024 / 1024} MB], now [${totalSize / 1024 / 1024} MB]")
                     return false

+ 11 - 6
zen-web/src/main/resources/application-fs.yml

@@ -7,16 +7,18 @@ spring:
       max-file-size: 25MB #最大文件大小
       location: ${java.io.tmpdir}
       file-size-threshold: 5MB
+fs:
+  maxFileSize: 4294967296 # 单位 byte 为0表示无限制
+  chunkSize: 10485760 # 10*1024*1024 = 10MB
 
 # 自定义配置
 upload:
   tmpPath: /tmp # D:\\tmp # 临时文件存放位置 默认 /tmp/zen
   dataPath: /archives/upload # D:\\data
-  maxFileSize: 4294967296 # 单位 byte 为0表示无限制
-  chunkSize: 10485760 # 10*1024*1024 = 10MB
 
 fdfs:
   enabled: true
+  tmpPath: /tmp/archives # # 临时文件存放位置 默认 /tmp/archives
   connect-timeout: 3000
   so-timeout: 3000
   tracker-list:
@@ -40,15 +42,18 @@ spring:
       location: ${java.io.tmpdir}
       file-size-threshold: 5MB
 
-# 自定义配置
-upload:
-  tmpPath: /tmp # 临时文件存放位置 默认 /tmp/zen
-  dataPath: /archives/upload
+fs:
   maxFileSize: 4294967296 # 单位 byte 为0表示无限制
   chunkSize: 10485760 # 10*1024*1024 = 10MB
 
+# 自定义配置
+upload:
+  tmpPath: /tmp # D:\\tmp # 临时文件存放位置 默认 /tmp/zen
+  dataPath: /archives/upload # D:\\data
+
 fdfs:
   enabled: true
+  tmpPath: /tmp/archives # # 临时文件存放位置 默认 /tmp/archives
   connect-timeout: 3000
   so-timeout: 3000
   tracker-list: