Browse Source

添加必要注释

NorthLan 6 years ago
parent
commit
a9988ab174

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

@@ -22,7 +22,8 @@ import java.nio.file.Files
 import java.nio.file.Paths
 
 /**
- *
+ * FastDFS 上传工具类 <br>
+ * 前端上传文件到 分布式文件服务器
  * @author NorthLan
  * @date 2018/8/29
  * @url https://noahlan.com
@@ -30,17 +31,15 @@ import java.nio.file.Paths
 object FdfsUploadUtil {
     private val logger = LoggerFactory.getLogger(FdfsUploadUtil::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 appendFileStorageClient: AppendFileStorageClient by lazy { SpringContextHolder.getBean(AppendFileStorageClient::class.java)!! }
     private val fastFileStorageClient: FastFileStorageClient by lazy { SpringContextHolder.getBean(FastFileStorageClient::class.java)!! }
 
+    /*-------------------------------------------
+    |           Multipart File 文件上传           |
+    ============================================*/
+
     // 针对文件md5加锁,保证上传合并文件时线程安全
     private val checkMergeLock: KeyLock<Int> = KeyLock()
     private val mergeLock: KeyLock<Int> = KeyLock()
@@ -97,7 +96,7 @@ object FdfsUploadUtil {
                     ret.checkStatus = CheckStatus.FDFS_NO_DB_FULL_CHUNK
 
                     val hashedMd5 = fileMetadata.md5!!.hashCode()
-                    val tmpPath = uploadProperties!!.tmpPath!!
+                    val tmpPath = uploadProperties.tmpPath!!
 
                     checkMergeLock.lock(hashedMd5)
                     try {
@@ -121,8 +120,8 @@ object FdfsUploadUtil {
      * 文件夹结构/文件名
      */
     fun upload(fileMetadata: ZenFileMetadata, file: MultipartFile): UploadResult {
-        val tmpPath = uploadProperties!!.tmpPath!!
-        val chunkSize = uploadProperties!!.chunkSize!!
+        val tmpPath = uploadProperties.tmpPath!!
+        val chunkSize = uploadProperties.chunkSize!!
         var storePath: StorePath? = null
         if (validateRequest(fileMetadata, file)) {
             val filename = fileMetadata.filename!!
@@ -225,7 +224,7 @@ object FdfsUploadUtil {
      * 检查分片是否存在 (tmp目录)
      */
     private fun chunkExists(chunkNumber: Int?, md5: String): Boolean {
-        val tmpPath = uploadProperties!!.tmpPath!!
+        val tmpPath = uploadProperties.tmpPath!!
         return Files.exists(Paths.get(getChunkFilename(tmpPath, chunkNumber, md5)))
     }
 
@@ -276,7 +275,7 @@ object FdfsUploadUtil {
         }
 
         // is the file too large?
-        uploadProperties?.let {
+        uploadProperties.let {
             val maxFileSize = it.maxFileSize
             if (maxFileSize != null && maxFileSize > 0) {
                 if (totalSize > maxFileSize) {