Browse Source

文件信息入库ok(但未做事务判定)

NorthLan 6 years ago
parent
commit
27f3a23bda

+ 15 - 0
zen-api/src/main/kotlin/com/gxzc/zen/api/sys/mapper/SysUploadInfoMapper.kt

@@ -0,0 +1,15 @@
+package com.gxzc.zen.api.sys.mapper
+
+import com.gxzc.zen.api.sys.model.SysUploadInfo
+import com.gxzc.zen.common.base.BaseMapper
+import org.springframework.stereotype.Repository
+/**
+ * <p>
+ * 上传文件信息表 Mapper 接口
+ * </p>
+ *
+ * @author NorthLan
+ * @since 2018-06-01
+ */
+@Repository
+interface SysUploadInfoMapper : BaseMapper<SysUploadInfo>

+ 78 - 0
zen-api/src/main/kotlin/com/gxzc/zen/api/sys/model/SysUploadInfo.kt

@@ -0,0 +1,78 @@
+package com.gxzc.zen.api.sys.model
+
+import com.baomidou.mybatisplus.annotations.TableField
+import com.baomidou.mybatisplus.annotations.TableName
+import java.util.Date
+import com.gxzc.zen.common.base.BaseModel
+
+/**
+ * <p>
+ * 上传文件信息表
+ * </p>
+ *
+ * @author NorthLan
+ * @since 2018-06-01
+ */
+@TableName("sys_upload_info")
+open class SysUploadInfo : BaseModel() {
+    /**
+     * 平台ID
+     */
+    var platformId: Int? = null
+    /**
+     * 真实文件名
+     */
+    var filename: String? = null
+    /**
+     * 存放在服务器中的相对路径
+     */
+    var relativePath: String? = null
+    /**
+     * 批次ID
+     */
+    var batchId: String? = null
+    /**
+     * 最后一次修改时间
+     */
+    var lastModified: Date? = null
+    /**
+     * 文件非完整md5(头+[尾]+修改时间)
+     */
+    var md5: String? = null
+    /**
+     * 后缀
+     */
+    var extension: String? = null
+    /**
+     * 文件大小(byte)
+     */
+    var filesize: Long? = null
+    /**
+     * 详细地址
+     */
+    var originFilename: String? = null
+    /**
+     * 工号
+     */
+    var originRelativePath: String? = null
+    /**
+     * 备用字段
+     */
+    var ext1: String? = null
+    /**
+     * 备用字段
+     */
+    var ext2: String? = null
+    /**
+     * 备用字段
+     */
+    var ext3: String? = null
+    /**
+     * 备用字段
+     */
+    var ext4: String? = null
+
+    ////////////////////// url
+    @TableField(exist = false)
+    var url: String? = null
+}

+ 19 - 0
zen-api/src/main/kotlin/com/gxzc/zen/api/sys/service/ISysUploadInfoService.kt

@@ -0,0 +1,19 @@
+package com.gxzc.zen.api.sys.service
+
+import com.gxzc.zen.api.sys.model.SysUploadInfo
+import com.gxzc.zen.common.base.BaseService
+import com.gxzc.zen.common.dto.ZenFileMetadata
+import java.io.File
+
+/**
+ * <p>
+ * 上传文件信息表 服务类
+ * </p>
+ *
+ * @author NorthLan
+ * @since 2018-06-01
+ */
+interface ISysUploadInfoService : BaseService<SysUploadInfo> {
+    fun addUploadFile(fileMetadata: ZenFileMetadata, file: File)
+    fun getUploadInfosByBatchId(batchId: String): MutableList<SysUploadInfo>
+}

+ 59 - 0
zen-api/src/main/kotlin/com/gxzc/zen/api/sys/service/impl/SysUploadInfoServiceImpl.kt

@@ -0,0 +1,59 @@
+package com.gxzc.zen.api.sys.service.impl
+
+import com.baomidou.mybatisplus.mapper.EntityWrapper
+import com.baomidou.mybatisplus.service.impl.ServiceImpl
+import com.gxzc.zen.api.sys.mapper.SysUploadInfoMapper
+import com.gxzc.zen.api.sys.model.SysUploadInfo
+import com.gxzc.zen.api.sys.service.ISysUploadInfoService
+import com.gxzc.zen.common.dto.ZenFileMetadata
+import com.gxzc.zen.common.util.PlatformUtil
+import org.apache.commons.io.FilenameUtils
+import org.springframework.stereotype.Service
+import java.io.File
+import java.util.*
+
+/**
+ * <p>
+ * 上传文件信息表 服务实现类
+ * </p>
+ *
+ * @author NorthLan
+ * @since 2018-06-01
+ */
+@Service
+class SysUploadInfoServiceImpl : ServiceImpl<SysUploadInfoMapper, SysUploadInfo>(), ISysUploadInfoService {
+    override fun addUploadFile(fileMetadata: ZenFileMetadata, file: File) {
+        val entity = SysUploadInfo().apply {
+            this.platformId = PlatformUtil.getPlatformId()
+            this.batchId = fileMetadata.batchId
+            this.originFilename = fileMetadata.filename
+            this.originRelativePath = fileMetadata.relativePath?.replace(this.originFilename!!, "")
+            this.filename = file.name
+            if (fileMetadata.repath.isNullOrEmpty()) {
+                this.relativePath = FilenameUtils.normalize(this.originRelativePath)
+            } else {
+                this.relativePath = FilenameUtils.normalize(fileMetadata.repath)
+            }
+            this.md5 = fileMetadata.md5
+            this.extension = FilenameUtils.getExtension(file.name)
+            this.filesize = fileMetadata.totalSize
+            this.lastModified = Date(file.lastModified())
+        }
+        baseMapper.insert(entity)
+    }
+
+    override fun getUploadInfosByBatchId(batchId: String): MutableList<SysUploadInfo> {
+        val condition = SysUploadInfo().apply { this.batchId = batchId }
+        val ret = baseMapper.selectWOLogic(EntityWrapper(condition))
+        ret.forEach {
+            if (it.relativePath.isNullOrEmpty()) {
+                it.url = FilenameUtils.normalize("/${it.filename}")
+            } else {
+                it.url = FilenameUtils.normalize("/${it.relativePath}/${it.filename}")
+            }
+
+        }
+        return ret
+    }
+
+}

+ 30 - 0
zen-api/src/main/resources/mapping/sys/SysUploadInfoMapper.xml

@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.gxzc.zen.api.sys.mapper.SysUploadInfoMapper">
+
+    <!-- 通用查询映射结果 -->
+    <resultMap id="BaseResultMap" type="com.gxzc.zen.api.sys.model.SysUploadInfo">
+    <result column="id" property="id" />
+    <result column="enable" property="enable" />
+    <result column="remark" property="remark" />
+    <result column="create_time" property="createTime" />
+    <result column="create_by" property="createBy" />
+    <result column="update_time" property="updateTime" />
+    <result column="update_by" property="updateBy" />
+    <result column="platform_id" property="platformId" />
+        <result column="filename" property="filename" />
+        <result column="relative_path" property="relativePath" />
+        <result column="batch_id" property="batchId" />
+        <result column="last_modified" property="lastModified" />
+        <result column="md5" property="md5" />
+        <result column="extension" property="extension" />
+        <result column="filesize" property="filesize" />
+        <result column="origin_filename" property="originFilename" />
+        <result column="origin_relative_path" property="originRelativePath" />
+        <result column="ext1" property="ext1" />
+        <result column="ext2" property="ext2" />
+        <result column="ext3" property="ext3" />
+        <result column="ext4" property="ext4" />
+    </resultMap>
+
+</mapper>

+ 2 - 0
zen-core/src/main/kotlin/com/gxzc/zen/common/dto/ZenFileResponse.kt

@@ -17,4 +17,6 @@ open class ZenFileResponse {
 
     @JsonIgnore
     var file: File? = null // 服务端文件实例
+
+    var info: Any? = null // 信息
 }

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

@@ -42,7 +42,7 @@ object UploadUtil {
         const val CHECKING = "checking" // 检查中
         const val MERGING = "merging" // 合并中
         const val UPLOADED = "uploaded" // 单文件上传完毕
-        const val BATCH_UPLOADED = "batchUploaded" // 文件上传完毕
+        const val BATCH_UPLOADED = "batchUploaded" // 批量文件上传完毕
         const val UPLOADING = "uploading" // 上传中
     }
 

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

@@ -80,7 +80,10 @@ class TestFileChunks {
 
     @Test
     fun testExt() {
-        println(FilenameUtils.getExtension("skldjfklsjdf"))
-        println(FilenameUtils.getExtension("skldjfklsjdf.exe"))
+        println(FilenameUtils.normalize("/d/d.exe"))
+        println(FilenameUtils.normalize("d/d.exe"))
+        println(FilenameUtils.normalize("/d.exe"))
+        println(FilenameUtils.normalize("//d.exe"))
+        println(FilenameUtils.normalize("d.exe"))
     }
 }

+ 2 - 1
zen-orm/src/main/kotlin/com/gxzc/zen/Generator.kt

@@ -74,6 +74,7 @@ fun generate(isKotlin: Boolean, author: String, pkgType: String, dataSourceConfi
     mpg.strategy = StrategyConfig().also {
         it.setDbColumnUnderline(true)
         it.isEntityLombokModel = true
+        it.isEntityLombokModel = true
         it.isCapitalMode = false
         it.naming = NamingStrategy.underline_to_camel
 //        it.setTablePrefix("t")
@@ -83,7 +84,7 @@ fun generate(isKotlin: Boolean, author: String, pkgType: String, dataSourceConfi
         it.superServiceClass = "com.gxzc.zen.common.base.BaseService"
         it.superServiceImplClass = "com.baomidou.mybatisplus.service.impl.ServiceImpl"
 //            superControllerClass = ""
-//        it.setInclude("t_test")
+        it.setInclude("sys_upload_info")
         it.isEntityBooleanColumnRemoveIsPrefix = true
         it.logicDeleteFieldName = "enable"
 //            setExclude("")

+ 23 - 0
zen-web/src/main/kotlin/com/gxzc/zen/web/sys/controller/UploadController.kt

@@ -1,11 +1,13 @@
 package com.gxzc.zen.web.sys.controller
 
+import com.gxzc.zen.api.sys.service.ISysUploadInfoService
 import com.gxzc.zen.common.base.BaseController
 import com.gxzc.zen.common.dto.ZenFileMetadata
 import com.gxzc.zen.common.exception.ZenException
 import com.gxzc.zen.common.util.UploadUtil
 import io.swagger.annotations.ApiOperation
 import org.slf4j.LoggerFactory
+import org.springframework.beans.factory.annotation.Autowired
 import org.springframework.http.HttpStatus
 import org.springframework.http.ResponseEntity
 import org.springframework.web.bind.annotation.GetMapping
@@ -27,12 +29,24 @@ class UploadController : BaseController() {
         private val logger = LoggerFactory.getLogger(UploadController::class.java)
     }
 
+    @Autowired
+    private lateinit var uploadInfoService: ISysUploadInfoService
+
     @ApiOperation("获取已上传分片列表")
     @GetMapping
     fun checkChunk(fileMetadata: ZenFileMetadata): ResponseEntity<*> {
         // 检查已上传文件分片
         val ret = UploadUtil.checkUpload(fileMetadata)
         return if (ret.uploadedChunks != null) {
+            when (ret.status) {
+            // 单文件完成,插入数据库
+                UploadUtil.STATUS.UPLOADED -> uploadInfoService.addUploadFile(fileMetadata, ret.file!!)
+            // 批次完成,将最后个文件插入 而后取出所有本批次文件信息
+                UploadUtil.STATUS.BATCH_UPLOADED -> {
+                    uploadInfoService.addUploadFile(fileMetadata, ret.file!!)
+                    ret.info = uploadInfoService.getUploadInfosByBatchId(fileMetadata.batchId!!)
+                }
+            }
             ResponseEntity.ok(ret)
         } else {
             ResponseEntity.status(204).body(ret)
@@ -52,6 +66,15 @@ class UploadController : BaseController() {
         }
 
         return if (uploadResponse != null) {
+            when (uploadResponse.status) {
+            // 单文件完成,插入数据库
+                UploadUtil.STATUS.UPLOADED -> uploadInfoService.addUploadFile(fileMetadata, uploadResponse.file!!)
+            // 批次完成,将最后个文件插入 而后取出所有本批次文件信息
+                UploadUtil.STATUS.BATCH_UPLOADED -> {
+                    uploadInfoService.addUploadFile(fileMetadata, uploadResponse.file!!)
+                    uploadResponse.info = uploadInfoService.getUploadInfosByBatchId(fileMetadata.batchId!!)
+                }
+            }
             ResponseEntity.ok(uploadResponse)
         } else {
             ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE.value()).body(null)