浏览代码

如果rename存在后缀,则使用 反之使用源文件名自带后缀

NorthLan 6 年之前
父节点
当前提交
6b02664f2f

+ 29 - 6
zen-core/src/main/kotlin/com/gxzc/zen/common/util/UploadUtil.kt

@@ -121,6 +121,7 @@ object UploadUtil {
      * 检测文件是否存在 实现文件秒传
      */
     fun checkUpload(fileMetadata: ZenFileMetadata): ZenFileResponse {
+        val dataPath = uploadProperties!!.dataPath!!
         val ret = ZenFileResponse()
         if (validateRequest(fileMetadata, null)) {
             if (fileExists(fileMetadata)) {
@@ -129,6 +130,7 @@ object UploadUtil {
                     ret.uploadedChunks?.add(i)
                 }
                 ret.status = setBatch(fileMetadata)
+                ret.file = File(getFullDestFilename(dataPath, fileMetadata.filename!!, fileMetadata.relativePath!!, fileMetadata.repath, fileMetadata.rename))
             } else {
                 // 检查分片存在情况
                 for (i in 1..fileMetadata.totalChunks!!) {
@@ -158,7 +160,7 @@ object UploadUtil {
         val filename = fileMetadata.filename!!
         val relativePath = fileMetadata.relativePath!!
         val md5 = fileMetadata.md5!!
-        val path = FilenameUtils.normalize("$dataPath$FILE_SEPARATOR${getDestFilePath(filename, relativePath, fileMetadata.repath)}$FILE_SEPARATOR${getDestFileName(filename, fileMetadata.rename)}")
+        val path = getFullDestFilename(dataPath, filename, relativePath, fileMetadata.repath, fileMetadata.rename)
         return Files.exists(Paths.get(path)) && FileUtil.md5HeadTail(path, uploadProperties!!.chunkSize!!.toInt()) == md5 // # 防篡改
     }
 
@@ -201,15 +203,16 @@ object UploadUtil {
 
         uploadedStatusMap[md5] = STATUS.MERGING
 
-        logger.debug("start merging chunks for [$filename]")
+        val t = System.currentTimeMillis()
+        logger.debug("start merging chunks for [$filename], now: $t")
 
-        val destPath = FilenameUtils.normalize("$destRootPath$FILE_SEPARATOR${getDestFilePath(filename, relativePath, fileMetadata.repath)}")
+        val destPath = getFullDestFilepath(destRootPath, filename, relativePath, fileMetadata.repath, fileMetadata.rename)
         val destDir = Paths.get(destPath)
         if (Files.notExists(destDir)) {
             Files.createDirectories(destDir)
         }
         // 目标文件流 通过文件名来拼接
-        val outputPath = FilenameUtils.normalize("$destPath$FILE_SEPARATOR${getDestFileName(filename, fileMetadata.rename)}")
+        val outputPath = getFullDestFilename(destRootPath, filename, relativePath, fileMetadata.repath, fileMetadata.rename)
         val outputFile = File(outputPath)
         val destOutputStream = BufferedOutputStream(FileOutputStream(outputFile))
 
@@ -232,13 +235,13 @@ object UploadUtil {
         destOutputStream.flush()
         destOutputStream.close()
 
-        logger.debug("merging successful.")
 
         // 修改文件 修改时间
         outputFile.setLastModified(fileMetadata.lastModified!!)
 
         uploadedStatusMap.remove(md5)
 
+        logger.debug("merging successful, cost: ${System.currentTimeMillis() - t} ms.")
         return outputFile
     }
 
@@ -256,7 +259,13 @@ object UploadUtil {
         return if (rename == null || rename.isEmpty()) {
             filename
         } else {
-            rename
+            // 后缀
+            val ext = FilenameUtils.getExtension(rename)
+            if (ext == null) {
+                "$rename.${FilenameUtils.getExtension(filename)}"
+            } else {
+                rename
+            }
         }
     }
 
@@ -271,6 +280,20 @@ object UploadUtil {
         }
     }
 
+    /**
+     * 获取输出完整文件路径
+     */
+    private fun getFullDestFilepath(dataPath: String, filename: String, relativePath: String, repath: String?, rename: String?): String {
+        return FilenameUtils.getFullPath(getFullDestFilename(dataPath, filename, relativePath, repath, rename))
+    }
+
+    /**
+     * 获取输出完整文件名
+     */
+    private fun getFullDestFilename(dataPath: String, filename: String, relativePath: String, repath: String?, rename: String?): String {
+        return FilenameUtils.normalize("$dataPath$FILE_SEPARATOR${getDestFilePath(filename, relativePath, repath)}$FILE_SEPARATOR${getDestFileName(filename, rename)}")
+    }
+
     /**
      * validate request multipart chunks
      */

+ 8 - 2
zen-core/src/test/kotlin/com/gxzc/zen/TestFileChunks.kt

@@ -72,9 +72,15 @@ class TestFileChunks {
     private val FILE_SEPARATOR = System.getProperty("file.separator") // 适配操作系统的文件路径
 
     @Test
-    fun testFor(){
-        for(i in 1..10){
+    fun testFor() {
+        for (i in 1..10) {
             println(i)
         }
     }
+
+    @Test
+    fun testExt() {
+        println(FilenameUtils.getExtension("skldjfklsjdf"))
+        println(FilenameUtils.getExtension("skldjfklsjdf.exe"))
+    }
 }