From dbc877cb6d71e54e5a5b08c49236013eba7cba48 Mon Sep 17 00:00:00 2001 From: zhizhi wu <2377881365@qq.com> Date: Tue, 23 Mar 2021 10:46:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ccsens/filedeal/api/FileController.java | 79 ++-- .../com/ccsens/filedeal/bean/dto/FileDto.java | 30 +- .../filedeal/bean/po/FileChunkResult.java | 24 +- .../bean/po/FileChunkResultExample.java | 96 ++--- .../ccsens/filedeal/bean/po/FileCommit.java | 11 + .../filedeal/bean/po/FileCommitExample.java | 60 +++ .../com/ccsens/filedeal/bean/po/FileLink.java | 12 +- .../filedeal/bean/po/FileLinkExample.java | 48 +-- .../com/ccsens/filedeal/bean/vo/FileVo.java | 30 +- .../ccsens/filedeal/persist/dao/FileDao.java | 26 ++ .../ccsens/filedeal/service/FileService.java | 341 ++++++++++++------ .../ccsens/filedeal/service/IFileService.java | 19 +- .../com/ccsens/filedeal/util/CodeEnum.java | 2 + .../ccsens/filedeal/util/JsonResponse.java | 2 +- .../com/ccsens/filedeal/util/WebConstant.java | 13 +- src/main/resources/mapper_dao/FileDao.xml | 55 ++- .../mapper_raw/FileChunkResultMapper.xml | 54 +-- .../resources/mapper_raw/FileCommitMapper.xml | 28 +- .../resources/mapper_raw/FileLinkMapper.xml | 28 +- 19 files changed, 669 insertions(+), 289 deletions(-) diff --git a/src/main/java/com/ccsens/filedeal/api/FileController.java b/src/main/java/com/ccsens/filedeal/api/FileController.java index f949971..d89a5c9 100644 --- a/src/main/java/com/ccsens/filedeal/api/FileController.java +++ b/src/main/java/com/ccsens/filedeal/api/FileController.java @@ -1,9 +1,7 @@ package com.ccsens.filedeal.api; -import com.alibaba.fastjson.JSONArray; -import com.alibaba.fastjson.JSONObject; +import cn.hutool.core.util.StrUtil; import com.ccsens.filedeal.bean.dto.FileDto; -import com.ccsens.filedeal.bean.po.FileCommit; import com.ccsens.filedeal.bean.vo.FileVo; import com.ccsens.filedeal.exception.BaseException; import com.ccsens.filedeal.service.IFileService; @@ -13,15 +11,14 @@ import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; -import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.Part; import java.io.*; -import java.util.Base64; import java.util.List; /** @@ -40,14 +37,29 @@ public class FileController { @Resource private RestTemplate restTemplate; - @ApiOperation(value = "单个文件上传") + @ApiOperation(value = "大文件分片上传-检查") @ApiImplicitParams({ - @ApiImplicitParam(name = "file", value = "单个文件") + @ApiImplicitParam(name = "fileCheck", value = "文件校验") + }) + @RequestMapping(value = "upload/bigFileCheck", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"}) + public JsonResponse bigFileCheck(HttpServletRequest request, FileDto.BigFileCheck fileCheck) throws Exception { + log.info("大文件上传检查:{}", fileCheck); + Long userId = getUserId(request); + FileVo.BigFileCheck bigFile = fileService.bigFileCheck(fileCheck, userId); + log.info("大文件上传检查返回:{}", bigFile); + return JsonResponse.newInstance().ok(bigFile); + } + + + @ApiOperation(value = "大文件分片上传") + @ApiImplicitParams({ + @ApiImplicitParam(name = "file", value = "单个分片文件") }) @RequestMapping(value = "upload/bigFile", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"}) - public JsonResponse uploadBigFile(FileDto.MultipartFileUpload fileUpload) throws Exception { + public JsonResponse uploadBigFile(HttpServletRequest request, FileDto.MultipartFileUpload fileUpload) throws Exception { log.info("大文件上传:{}", fileUpload); - FileVo.BigFile bigFile = fileService.uploadBigFile(fileUpload); + Long userId = getUserId(request); + FileVo.BigFile bigFile = fileService.uploadBigFile(fileUpload, userId); log.info("大文件上传返回:{}", bigFile); return JsonResponse.newInstance().ok(bigFile); } @@ -57,11 +69,12 @@ public class FileController { @ApiImplicitParam(name = "file", value = "单个文件") }) @RequestMapping(value = "upload/single", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"}) - public JsonResponse uploadSingle( Part file) throws Exception { + public JsonResponse uploadSingle(HttpServletRequest request, Part file) throws Exception { log.info("单个文件上传:{}", file); String dir = PropUtil.path + WebConstant.FILE_UPLOAD_DIR; - FileVo.Upload vo = fileService.saveFileSingle(dir, file); + Long userId = getUserId(request); + FileVo.Upload vo = fileService.saveFileSingle(dir, file, userId); log.info("单个文件上传返回:{}", vo); return JsonResponse.newInstance().ok(vo); } @@ -71,18 +84,24 @@ public class FileController { @ApiImplicitParam(name = "files", value = "文件数组") }) @RequestMapping(value = "upload/multiple", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"}) - public JsonResponse uploadMultiple( List files) throws Exception { + public JsonResponse uploadMultiple(HttpServletRequest request, List files) throws Exception { log.info("文件批量上传:{}", files); String dir = PropUtil.path + WebConstant.FILE_UPLOAD_DIR; - List vos = fileService.saveFileMultiple(dir, files); + Long userId = getUserId(request); + List vos = fileService.saveFileMultiple(dir, files, userId); log.info("文件批量上传返回:{}", vos); return JsonResponse.newInstance().ok(vos); } + private Long getUserId(HttpServletRequest request) { + String userIdStr = request.getParameter("userId"); + return StrUtil.isEmpty(userIdStr) ? null : Long.parseLong(userIdStr); + } + @ApiOperation(value = "文件下载") @GetMapping(value = "download/{id}") - public void download(HttpServletResponse response, @PathVariable("id")Long id) throws Exception { + public void download(HttpServletRequest request, HttpServletResponse response, @PathVariable("id")Long id) throws Exception { log.info("文件下载:{}", id); FileVo.FilePosition file = fileService.getById(id); log.info("文件信息;{}", file); @@ -107,28 +126,28 @@ public class FileController { @ApiImplicitParam(name = "end", value = "文件读取结束位置(不包含) -1:读取全部", required = true) }) @GetMapping(value = "read/{id}") - public JsonResponse readFile(@PathVariable("id")Long id, int start, int end) throws IOException { + public JsonResponse readFile(HttpServletRequest request,@PathVariable("id")Long id, int start, int end) throws IOException { log.info("文件读取信息:{},{},{}", id, start, end); FileVo.Content content = fileService.readFile(id, start, end); log.info("文件读取结束"); return JsonResponse.newInstance().ok(content); } - @GetMapping(value = "test/{id}") - public void test(@PathVariable("id")long id) throws IOException { - ResponseEntity entity = restTemplate.getForEntity("http://localhost:8001/file/read/" + id + "?start=0&end=-1", JSONObject.class); - JSONArray contents = entity.getBody().getJSONObject("data").getJSONArray("contents"); - String type = entity.getBody().getJSONObject("data").getString("type"); - File file = new File("D:\\1." + type); - OutputStream os = new FileOutputStream(file, true); - for (Object obj: contents) { - String content = (String) obj; - byte[] decode = Base64.getDecoder().decode(content); - os.write(decode); - - } - os.close(); - } +// @GetMapping(value = "test/{id}") +// public void test(@PathVariable("id")long id) throws IOException { +// ResponseEntity entity = restTemplate.getForEntity("http://localhost:8001/file/read/" + id + "?start=0&end=-1", JSONObject.class); +// JSONArray contents = entity.getBody().getJSONObject("data").getJSONArray("contents"); +// String type = entity.getBody().getJSONObject("data").getString("type"); +// File file = new File("D:\\1." + type); +// OutputStream os = new FileOutputStream(file, true); +// for (Object obj: contents) { +// String content = (String) obj; +// byte[] decode = Base64.getDecoder().decode(content); +// os.write(decode); +// +// } +// os.close(); +// } diff --git a/src/main/java/com/ccsens/filedeal/bean/dto/FileDto.java b/src/main/java/com/ccsens/filedeal/bean/dto/FileDto.java index e39e558..1129410 100644 --- a/src/main/java/com/ccsens/filedeal/bean/dto/FileDto.java +++ b/src/main/java/com/ccsens/filedeal/bean/dto/FileDto.java @@ -1,5 +1,6 @@ package com.ccsens.filedeal.bean.dto; +import com.sun.istack.internal.NotNull; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; @@ -12,21 +13,42 @@ import org.springframework.web.multipart.MultipartFile; */ public class FileDto { + @Data + @ApiModel("大文件上传-检查-请求") + public static class BigFileCheck{ + @NotNull + @ApiModelProperty("文件MD5") + private String md5; + @NotNull + @ApiModelProperty("文件sha1") + private String sha1; + @NotNull + @ApiModelProperty("文件名") + private String fileName; + @ApiModelProperty("文件传输任务ID") + private String taskId; + @ApiModelProperty("分片总数") + private int chunkTotal; + } + @Data @ApiModel("多文件上传") public static class MultipartFileUpload{ - @ApiModelProperty("appId") - private Long appId; +// @ApiModelProperty("appId") +// private Long appId; @ApiModelProperty("文件传输任务ID") private String taskId; @ApiModelProperty("当前为第几分片") private int chunkNum; @ApiModelProperty("每个分块的大小") private long chunkSize; - @ApiModelProperty("分片总数") - private int chunkTotal; + @ApiModelProperty("分块文件传输对象") private MultipartFile file; +// @ApiModelProperty("文件sha1字符串") +// private String sha1; +// @ApiModelProperty("文件md5字符串") +// private String md5; } diff --git a/src/main/java/com/ccsens/filedeal/bean/po/FileChunkResult.java b/src/main/java/com/ccsens/filedeal/bean/po/FileChunkResult.java index 352a399..01461d5 100644 --- a/src/main/java/com/ccsens/filedeal/bean/po/FileChunkResult.java +++ b/src/main/java/com/ccsens/filedeal/bean/po/FileChunkResult.java @@ -6,11 +6,11 @@ import java.util.Date; public class FileChunkResult implements Serializable { private Long id; - private Long appId; + private Long userId; private String taskId; - private Long commitId; + private Long linkId; private Integer total; @@ -30,12 +30,12 @@ public class FileChunkResult implements Serializable { this.id = id; } - public Long getAppId() { - return appId; + public Long getUserId() { + return userId; } - public void setAppId(Long appId) { - this.appId = appId; + public void setUserId(Long userId) { + this.userId = userId; } public String getTaskId() { @@ -46,12 +46,12 @@ public class FileChunkResult implements Serializable { this.taskId = taskId == null ? null : taskId.trim(); } - public Long getCommitId() { - return commitId; + public Long getLinkId() { + return linkId; } - public void setCommitId(Long commitId) { - this.commitId = commitId; + public void setLinkId(Long linkId) { + this.linkId = linkId; } public Integer getTotal() { @@ -93,9 +93,9 @@ public class FileChunkResult implements Serializable { sb.append(" ["); sb.append("Hash = ").append(hashCode()); sb.append(", id=").append(id); - sb.append(", appId=").append(appId); + sb.append(", userId=").append(userId); sb.append(", taskId=").append(taskId); - sb.append(", commitId=").append(commitId); + sb.append(", linkId=").append(linkId); sb.append(", total=").append(total); sb.append(", createdAt=").append(createdAt); sb.append(", updatedAt=").append(updatedAt); diff --git a/src/main/java/com/ccsens/filedeal/bean/po/FileChunkResultExample.java b/src/main/java/com/ccsens/filedeal/bean/po/FileChunkResultExample.java index 1221f6f..831409b 100644 --- a/src/main/java/com/ccsens/filedeal/bean/po/FileChunkResultExample.java +++ b/src/main/java/com/ccsens/filedeal/bean/po/FileChunkResultExample.java @@ -165,63 +165,63 @@ public class FileChunkResultExample { return (Criteria) this; } - public Criteria andAppIdIsNull() { - addCriterion("app_id is null"); + public Criteria andUserIdIsNull() { + addCriterion("user_id is null"); return (Criteria) this; } - public Criteria andAppIdIsNotNull() { - addCriterion("app_id is not null"); + public Criteria andUserIdIsNotNull() { + addCriterion("user_id is not null"); return (Criteria) this; } - public Criteria andAppIdEqualTo(Long value) { - addCriterion("app_id =", value, "appId"); + public Criteria andUserIdEqualTo(Long value) { + addCriterion("user_id =", value, "userId"); return (Criteria) this; } - public Criteria andAppIdNotEqualTo(Long value) { - addCriterion("app_id <>", value, "appId"); + public Criteria andUserIdNotEqualTo(Long value) { + addCriterion("user_id <>", value, "userId"); return (Criteria) this; } - public Criteria andAppIdGreaterThan(Long value) { - addCriterion("app_id >", value, "appId"); + public Criteria andUserIdGreaterThan(Long value) { + addCriterion("user_id >", value, "userId"); return (Criteria) this; } - public Criteria andAppIdGreaterThanOrEqualTo(Long value) { - addCriterion("app_id >=", value, "appId"); + public Criteria andUserIdGreaterThanOrEqualTo(Long value) { + addCriterion("user_id >=", value, "userId"); return (Criteria) this; } - public Criteria andAppIdLessThan(Long value) { - addCriterion("app_id <", value, "appId"); + public Criteria andUserIdLessThan(Long value) { + addCriterion("user_id <", value, "userId"); return (Criteria) this; } - public Criteria andAppIdLessThanOrEqualTo(Long value) { - addCriterion("app_id <=", value, "appId"); + public Criteria andUserIdLessThanOrEqualTo(Long value) { + addCriterion("user_id <=", value, "userId"); return (Criteria) this; } - public Criteria andAppIdIn(List values) { - addCriterion("app_id in", values, "appId"); + public Criteria andUserIdIn(List values) { + addCriterion("user_id in", values, "userId"); return (Criteria) this; } - public Criteria andAppIdNotIn(List values) { - addCriterion("app_id not in", values, "appId"); + public Criteria andUserIdNotIn(List values) { + addCriterion("user_id not in", values, "userId"); return (Criteria) this; } - public Criteria andAppIdBetween(Long value1, Long value2) { - addCriterion("app_id between", value1, value2, "appId"); + public Criteria andUserIdBetween(Long value1, Long value2) { + addCriterion("user_id between", value1, value2, "userId"); return (Criteria) this; } - public Criteria andAppIdNotBetween(Long value1, Long value2) { - addCriterion("app_id not between", value1, value2, "appId"); + public Criteria andUserIdNotBetween(Long value1, Long value2) { + addCriterion("user_id not between", value1, value2, "userId"); return (Criteria) this; } @@ -295,63 +295,63 @@ public class FileChunkResultExample { return (Criteria) this; } - public Criteria andCommitIdIsNull() { - addCriterion("commit_id is null"); + public Criteria andLinkIdIsNull() { + addCriterion("link_id is null"); return (Criteria) this; } - public Criteria andCommitIdIsNotNull() { - addCriterion("commit_id is not null"); + public Criteria andLinkIdIsNotNull() { + addCriterion("link_id is not null"); return (Criteria) this; } - public Criteria andCommitIdEqualTo(Long value) { - addCriterion("commit_id =", value, "commitId"); + public Criteria andLinkIdEqualTo(Long value) { + addCriterion("link_id =", value, "linkId"); return (Criteria) this; } - public Criteria andCommitIdNotEqualTo(Long value) { - addCriterion("commit_id <>", value, "commitId"); + public Criteria andLinkIdNotEqualTo(Long value) { + addCriterion("link_id <>", value, "linkId"); return (Criteria) this; } - public Criteria andCommitIdGreaterThan(Long value) { - addCriterion("commit_id >", value, "commitId"); + public Criteria andLinkIdGreaterThan(Long value) { + addCriterion("link_id >", value, "linkId"); return (Criteria) this; } - public Criteria andCommitIdGreaterThanOrEqualTo(Long value) { - addCriterion("commit_id >=", value, "commitId"); + public Criteria andLinkIdGreaterThanOrEqualTo(Long value) { + addCriterion("link_id >=", value, "linkId"); return (Criteria) this; } - public Criteria andCommitIdLessThan(Long value) { - addCriterion("commit_id <", value, "commitId"); + public Criteria andLinkIdLessThan(Long value) { + addCriterion("link_id <", value, "linkId"); return (Criteria) this; } - public Criteria andCommitIdLessThanOrEqualTo(Long value) { - addCriterion("commit_id <=", value, "commitId"); + public Criteria andLinkIdLessThanOrEqualTo(Long value) { + addCriterion("link_id <=", value, "linkId"); return (Criteria) this; } - public Criteria andCommitIdIn(List values) { - addCriterion("commit_id in", values, "commitId"); + public Criteria andLinkIdIn(List values) { + addCriterion("link_id in", values, "linkId"); return (Criteria) this; } - public Criteria andCommitIdNotIn(List values) { - addCriterion("commit_id not in", values, "commitId"); + public Criteria andLinkIdNotIn(List values) { + addCriterion("link_id not in", values, "linkId"); return (Criteria) this; } - public Criteria andCommitIdBetween(Long value1, Long value2) { - addCriterion("commit_id between", value1, value2, "commitId"); + public Criteria andLinkIdBetween(Long value1, Long value2) { + addCriterion("link_id between", value1, value2, "linkId"); return (Criteria) this; } - public Criteria andCommitIdNotBetween(Long value1, Long value2) { - addCriterion("commit_id not between", value1, value2, "commitId"); + public Criteria andLinkIdNotBetween(Long value1, Long value2) { + addCriterion("link_id not between", value1, value2, "linkId"); return (Criteria) this; } diff --git a/src/main/java/com/ccsens/filedeal/bean/po/FileCommit.java b/src/main/java/com/ccsens/filedeal/bean/po/FileCommit.java index 45d21ca..80cec11 100644 --- a/src/main/java/com/ccsens/filedeal/bean/po/FileCommit.java +++ b/src/main/java/com/ccsens/filedeal/bean/po/FileCommit.java @@ -20,6 +20,8 @@ public class FileCommit implements Serializable { private Integer count; + private Byte status; + private Date createdAt; private Date updatedAt; @@ -92,6 +94,14 @@ public class FileCommit implements Serializable { this.count = count; } + public Byte getStatus() { + return status; + } + + public void setStatus(Byte status) { + this.status = status; + } + public Date getCreatedAt() { return createdAt; } @@ -130,6 +140,7 @@ public class FileCommit implements Serializable { sb.append(", sha1=").append(sha1); sb.append(", time=").append(time); sb.append(", count=").append(count); + sb.append(", status=").append(status); sb.append(", createdAt=").append(createdAt); sb.append(", updatedAt=").append(updatedAt); sb.append(", recStatus=").append(recStatus); diff --git a/src/main/java/com/ccsens/filedeal/bean/po/FileCommitExample.java b/src/main/java/com/ccsens/filedeal/bean/po/FileCommitExample.java index 5ee42ff..8d6af3a 100644 --- a/src/main/java/com/ccsens/filedeal/bean/po/FileCommitExample.java +++ b/src/main/java/com/ccsens/filedeal/bean/po/FileCommitExample.java @@ -635,6 +635,66 @@ public class FileCommitExample { return (Criteria) this; } + public Criteria andStatusIsNull() { + addCriterion("status is null"); + return (Criteria) this; + } + + public Criteria andStatusIsNotNull() { + addCriterion("status is not null"); + return (Criteria) this; + } + + public Criteria andStatusEqualTo(Byte value) { + addCriterion("status =", value, "status"); + return (Criteria) this; + } + + public Criteria andStatusNotEqualTo(Byte value) { + addCriterion("status <>", value, "status"); + return (Criteria) this; + } + + public Criteria andStatusGreaterThan(Byte value) { + addCriterion("status >", value, "status"); + return (Criteria) this; + } + + public Criteria andStatusGreaterThanOrEqualTo(Byte value) { + addCriterion("status >=", value, "status"); + return (Criteria) this; + } + + public Criteria andStatusLessThan(Byte value) { + addCriterion("status <", value, "status"); + return (Criteria) this; + } + + public Criteria andStatusLessThanOrEqualTo(Byte value) { + addCriterion("status <=", value, "status"); + return (Criteria) this; + } + + public Criteria andStatusIn(List values) { + addCriterion("status in", values, "status"); + return (Criteria) this; + } + + public Criteria andStatusNotIn(List values) { + addCriterion("status not in", values, "status"); + return (Criteria) this; + } + + public Criteria andStatusBetween(Byte value1, Byte value2) { + addCriterion("status between", value1, value2, "status"); + return (Criteria) this; + } + + public Criteria andStatusNotBetween(Byte value1, Byte value2) { + addCriterion("status not between", value1, value2, "status"); + return (Criteria) this; + } + public Criteria andCreatedAtIsNull() { addCriterion("created_at is null"); return (Criteria) this; diff --git a/src/main/java/com/ccsens/filedeal/bean/po/FileLink.java b/src/main/java/com/ccsens/filedeal/bean/po/FileLink.java index a6617e8..9226125 100644 --- a/src/main/java/com/ccsens/filedeal/bean/po/FileLink.java +++ b/src/main/java/com/ccsens/filedeal/bean/po/FileLink.java @@ -6,7 +6,7 @@ import java.util.Date; public class FileLink implements Serializable { private Long id; - private Long appId; + private Long userId; private String name; @@ -28,12 +28,12 @@ public class FileLink implements Serializable { this.id = id; } - public Long getAppId() { - return appId; + public Long getUserId() { + return userId; } - public void setAppId(Long appId) { - this.appId = appId; + public void setUserId(Long userId) { + this.userId = userId; } public String getName() { @@ -83,7 +83,7 @@ public class FileLink implements Serializable { sb.append(" ["); sb.append("Hash = ").append(hashCode()); sb.append(", id=").append(id); - sb.append(", appId=").append(appId); + sb.append(", userId=").append(userId); sb.append(", name=").append(name); sb.append(", commitId=").append(commitId); sb.append(", createdAt=").append(createdAt); diff --git a/src/main/java/com/ccsens/filedeal/bean/po/FileLinkExample.java b/src/main/java/com/ccsens/filedeal/bean/po/FileLinkExample.java index eab6779..8897141 100644 --- a/src/main/java/com/ccsens/filedeal/bean/po/FileLinkExample.java +++ b/src/main/java/com/ccsens/filedeal/bean/po/FileLinkExample.java @@ -165,63 +165,63 @@ public class FileLinkExample { return (Criteria) this; } - public Criteria andAppIdIsNull() { - addCriterion("app_id is null"); + public Criteria andUserIdIsNull() { + addCriterion("user_id is null"); return (Criteria) this; } - public Criteria andAppIdIsNotNull() { - addCriterion("app_id is not null"); + public Criteria andUserIdIsNotNull() { + addCriterion("user_id is not null"); return (Criteria) this; } - public Criteria andAppIdEqualTo(Long value) { - addCriterion("app_id =", value, "appId"); + public Criteria andUserIdEqualTo(Long value) { + addCriterion("user_id =", value, "userId"); return (Criteria) this; } - public Criteria andAppIdNotEqualTo(Long value) { - addCriterion("app_id <>", value, "appId"); + public Criteria andUserIdNotEqualTo(Long value) { + addCriterion("user_id <>", value, "userId"); return (Criteria) this; } - public Criteria andAppIdGreaterThan(Long value) { - addCriterion("app_id >", value, "appId"); + public Criteria andUserIdGreaterThan(Long value) { + addCriterion("user_id >", value, "userId"); return (Criteria) this; } - public Criteria andAppIdGreaterThanOrEqualTo(Long value) { - addCriterion("app_id >=", value, "appId"); + public Criteria andUserIdGreaterThanOrEqualTo(Long value) { + addCriterion("user_id >=", value, "userId"); return (Criteria) this; } - public Criteria andAppIdLessThan(Long value) { - addCriterion("app_id <", value, "appId"); + public Criteria andUserIdLessThan(Long value) { + addCriterion("user_id <", value, "userId"); return (Criteria) this; } - public Criteria andAppIdLessThanOrEqualTo(Long value) { - addCriterion("app_id <=", value, "appId"); + public Criteria andUserIdLessThanOrEqualTo(Long value) { + addCriterion("user_id <=", value, "userId"); return (Criteria) this; } - public Criteria andAppIdIn(List values) { - addCriterion("app_id in", values, "appId"); + public Criteria andUserIdIn(List values) { + addCriterion("user_id in", values, "userId"); return (Criteria) this; } - public Criteria andAppIdNotIn(List values) { - addCriterion("app_id not in", values, "appId"); + public Criteria andUserIdNotIn(List values) { + addCriterion("user_id not in", values, "userId"); return (Criteria) this; } - public Criteria andAppIdBetween(Long value1, Long value2) { - addCriterion("app_id between", value1, value2, "appId"); + public Criteria andUserIdBetween(Long value1, Long value2) { + addCriterion("user_id between", value1, value2, "userId"); return (Criteria) this; } - public Criteria andAppIdNotBetween(Long value1, Long value2) { - addCriterion("app_id not between", value1, value2, "appId"); + public Criteria andUserIdNotBetween(Long value1, Long value2) { + addCriterion("user_id not between", value1, value2, "userId"); return (Criteria) this; } diff --git a/src/main/java/com/ccsens/filedeal/bean/vo/FileVo.java b/src/main/java/com/ccsens/filedeal/bean/vo/FileVo.java index c788a18..e08e04b 100644 --- a/src/main/java/com/ccsens/filedeal/bean/vo/FileVo.java +++ b/src/main/java/com/ccsens/filedeal/bean/vo/FileVo.java @@ -46,19 +46,37 @@ public class FileVo { private String name; @ApiModelProperty("存储路径") private String path; + @ApiModelProperty("存储路径") + private String visitUrl; } @ApiModel("大文件分块上传-返回") @Data public static class BigFile{ - @ApiModelProperty("存储结果 0:成功 1:分块文件已上传,无需重复上传") - private byte result; + @ApiModelProperty("存储结果 -1:上传失败 0:成功 1:分块文件已上传,无需重复上传 2:文件已全部上传") + private byte result = -1; @ApiModelProperty("当前为第几分片") private int chunkNum; - @ApiModelProperty("文件sha1字符串") - private String sha1; - @ApiModelProperty("文件md5字符串") - private String md5; + + @ApiModelProperty("id") + private Long id; + @ApiModelProperty("文件名") + private String name; + @ApiModelProperty("访问路径") + private String path; + } + + @Data + @ApiModel("大文件上传-检查-响应") + public static class BigFileCheck{ + @ApiModelProperty("是否存在 0:不存在 1:存在") + private byte exist; + @ApiModelProperty("id") + private Long id; + @ApiModelProperty("文件名") + private String name; + @ApiModelProperty("访问路径") + private String visitUrl; } diff --git a/src/main/java/com/ccsens/filedeal/persist/dao/FileDao.java b/src/main/java/com/ccsens/filedeal/persist/dao/FileDao.java index 484f5f7..fdbb116 100644 --- a/src/main/java/com/ccsens/filedeal/persist/dao/FileDao.java +++ b/src/main/java/com/ccsens/filedeal/persist/dao/FileDao.java @@ -23,4 +23,30 @@ public interface FileDao extends FileCommitMapper { * @return 文件路径信息 */ FileVo.FilePosition getByAppIdAndTaskId(@Param("appId") Long appId, @Param("taskId") String taskId); + + /** + * 添加引用次数 + * @param id + */ + void addLinkCount(@Param("id") Long id); + + /** + * 查询md5 + * @param resultId resultId + * @return md5 + */ + String getMd5ByResultId(@Param("resultId") long resultId); + + /** + * 查询sha1 + * @param resultId resultId + * @return sha1 + */ + String getSha1ByResultId(@Param("resultId") long resultId); + + /** + * 根据resultId修改commit的状态为全部上传成功 + * @param resultId resultId + */ + void updateCommitStatus(@Param("resultId") long resultId); } diff --git a/src/main/java/com/ccsens/filedeal/service/FileService.java b/src/main/java/com/ccsens/filedeal/service/FileService.java index d58a0ea..f8698be 100644 --- a/src/main/java/com/ccsens/filedeal/service/FileService.java +++ b/src/main/java/com/ccsens/filedeal/service/FileService.java @@ -57,17 +57,17 @@ public class FileService extends AbstractRedisService implements IFileService { @Override - public List saveFileMultiple(String dir, List files) throws IOException, BaseException { + public List saveFileMultiple(String dir, List files, long userId) throws IOException, BaseException { List fileList = new ArrayList<>(); String allowedExt = WebConstant.FILE_TYPE_ALL; for (Part file: files) { - FileVo.Upload upload = getFileCommit(dir, allowedExt, file); + FileVo.Upload upload = getFileCommit(dir, allowedExt, file, userId); fileList.add(upload); } return fileList; } - private FileVo.Upload getFileCommit(String dir, String allowedExt, Part file) throws IOException { + private FileVo.Upload getFileCommit(String dir, String allowedExt, Part file, Long userId) throws IOException { FileVo.Upload upload = new FileVo.Upload(); String fileName = file.getSubmittedFileName(); log.info("文件名:{}", fileName); @@ -105,8 +105,7 @@ public class FileService extends AbstractRedisService implements IFileService { FileCommit fileCommit = initFileCommit(dir, fileName, md5, sha1, path); fileDao.insertSelective(fileCommit); log.info("保存文件:{}", fileCommit); - // TODO 获取APP_ID - FileLink link = initFileLink(fileName, fileCommit.getId(), 0L); + FileLink link = initFileLink(fileName, fileCommit.getId(), userId); fileLinkMapper.insertSelective(link); return getUpload(upload, fileName, fileCommit, link); @@ -116,18 +115,23 @@ public class FileService extends AbstractRedisService implements IFileService { upload.setId(link.getId()); upload.setName(fileName); String visitPath = fileCommit.getVisitPath(); + visitPath = getVisitUrl(link.getId(), visitPath); + upload.setVisitUrl(visitPath); + return upload; + } + + private String getVisitUrl(Long linkId, String visitPath) { String fileDownload = "file/download/"; if (visitPath.contains(fileDownload)) { - visitPath = visitPath.substring(0, visitPath.indexOf(fileDownload) + fileDownload.length()) + link.getId(); + visitPath = visitPath.substring(0, visitPath.indexOf(fileDownload) + fileDownload.length()) + linkId; } - upload.setVisitUrl(visitPath); - return upload; + return visitPath; } - private FileLink initFileLink(String fileName, Long commitId, Long appId) { + private FileLink initFileLink(String fileName, Long commitId, Long userId) { FileLink link = new FileLink(); link.setId(snowflake.nextId()); - link.setAppId(appId); + link.setUserId(userId); link.setName(fileName); link.setCommitId(commitId); return link; @@ -226,45 +230,45 @@ public class FileService extends AbstractRedisService implements IFileService { } @Override - public FileVo.Upload saveFileSingle(String dir, Part file) throws IOException, BaseException { + public FileVo.Upload saveFileSingle(String dir, Part file, long userId) throws IOException, BaseException { String allowedExt = WebConstant.FILE_TYPE_ALL; - return getFileCommit(dir, allowedExt, file); + return getFileCommit(dir, allowedExt, file, userId); } @Override - public FileVo.BigFile uploadBigFile(FileDto.MultipartFileUpload fileUpload) throws IOException { + public FileVo.BigFile uploadBigFile(FileDto.MultipartFileUpload fileUpload, long userId) throws IOException { long t1 = System.currentTimeMillis(); FileVo.BigFile bigFile = new FileVo.BigFile(); bigFile.setChunkNum(fileUpload.getChunkNum()); + //查看文件是否上传 - String key = String.format(WebConstant.RedisParam.RESULT_ID_KEY, fileUpload.getAppId() , fileUpload.getTaskId()); - String pathKey = String.format(WebConstant.RedisParam.COMMIT_PATH_KEY, fileUpload.getAppId(), fileUpload.getTaskId()); + String key = String.format(WebConstant.RedisParam.RESULT_ID_KEY, userId , fileUpload.getTaskId()); + String pathKey = String.format(WebConstant.RedisParam.COMMIT_PATH_KEY, userId, fileUpload.getTaskId()); Object resultObj = queryRedis(key, WebConstant.RedisType.STRING, WebConstant.RedisParam.EXPIRE_SIXTY); log.info("查询resultId:{}", resultObj); MultipartFile file = fileUpload.getFile(); String filename = file.getOriginalFilename(); if (StrUtil.isEmpty(filename)) { + log.info("没有获取到文件名,返回文件格式异常"); throw new BaseException(CodeEnum.FILE_FORMAT_ERROR); } - long resultId = snowflake.nextId(); + bigFile.setName(filename); String path; if (resultObj == null) { // 存redis resultId - path = initBigFile(fileUpload, key, pathKey, filename, resultId); - } else { - resultId = (Long)resultObj; - Object pathObj = queryRedis(pathKey, WebConstant.RedisType.STRING, WebConstant.RedisParam.EXPIRE_SIXTY); - if (pathObj == null) { - path = getPathByRedis(pathKey, 0); - } else { - path = (String)pathObj; - } - + throw new BaseException(CodeEnum.BIG_FILE_NO_CHECK); } + long resultId = (Long)resultObj; + Object pathObj = queryRedis(pathKey, WebConstant.RedisType.STRING, WebConstant.RedisParam.EXPIRE_SIXTY); + path = (String)pathObj; if (StrUtil.isEmpty(path)) { throw new BaseException(CodeEnum.FILE_NOT_FOUND); } + String visitPathKey = String.format(WebConstant.RedisParam.VISIT_PATH_KEY, userId, fileUpload.getTaskId()); + String visitIdKey = String.format(WebConstant.RedisParam.VISIT_ID_KEY, userId, fileUpload.getTaskId()); + bigFile.setId((Long)queryRedis(visitIdKey, WebConstant.RedisType.STRING, WebConstant.RedisParam.EXPIRE_SIXTY)); + bigFile.setPath((String)queryRedis(visitPathKey, WebConstant.RedisType.STRING, WebConstant.RedisParam.EXPIRE_SIXTY)); //文件内容 byte[] content = file.getBytes(); String md5 = Md5Util.getFileMD5(content); @@ -278,10 +282,6 @@ public class FileService extends AbstractRedisService implements IFileService { if (chunkLog.getChunkResultId() == resultId){ log.info("文件块已上传"); bigFile.setResult(WebConstant.FileMsg.FILE_UPLOAD_READY); - //文件sha1 - File file1 = new File(path); - bigFile.setSha1(Sha1Util.getFileSha1(file1)); - bigFile.setMd5(Md5Util.getFileMD5(file1)); return bigFile; } } @@ -291,27 +291,56 @@ public class FileService extends AbstractRedisService implements IFileService { newChunkLog.setPath(chunkLog.getPath()); fileChunkLogMapper.insertSelective(newChunkLog); } else { - String dir = PropUtil.path + WebConstant.FILE_UPLOAD_CHUNK_DIR + File.separator + DateUtil.today(); - File subFile = createFile(filename, dir); - FileOutputStream os = null; - try { - os = new FileOutputStream(subFile, true); - os.write(content); - } finally { - if (os != null) { - try { - os.close(); - } catch (IOException e) { - e.printStackTrace(); - } + saveChunkLog(fileUpload, filename, resultId, content, md5, sha1); + } + // 存储文件 + writeFile(fileUpload, path, content); + + File generateFile = new File(path); + String commitMd5Key = StrUtil.format(WebConstant.RedisParam.MD5_KEY, resultId); + Object commitMd5 = queryRedis(commitMd5Key, WebConstant.RedisType.STRING, WebConstant.RedisParam.EXPIRE_SIXTY); + String commitSha1Key = StrUtil.format(WebConstant.RedisParam.SHA1_KEY, resultId); + Object commitSha1 = queryRedis(commitSha1Key, WebConstant.RedisType.STRING, WebConstant.RedisParam.EXPIRE_SIXTY); + log.info("md5:{}", Md5Util.getFileMD5(generateFile)); + log.info("sha1:{}", Sha1Util.getFileSha1(generateFile)); + if (Md5Util.getFileMD5(generateFile).equals(commitMd5) && Sha1Util.getFileSha1(generateFile).equals(commitSha1)){ + log.info("文件全部上传"); + bigFile.setResult(WebConstant.FileMsg.FILE_UPLOAD_ALL); + fileDao.updateCommitStatus(resultId); + } else { + log.info("文件上传成功但尚未全部上传"); + bigFile.setResult(WebConstant.FileMsg.FILE_UPLOAD_SUC); + } + + + log.info("响应时间:{}", System.currentTimeMillis() - t1); + // 校验文件是否已经 + return bigFile; + } + + private void saveChunkLog(FileDto.MultipartFileUpload fileUpload, String filename, long resultId, byte[] content, String md5, String sha1) throws IOException { + String dir = PropUtil.path + WebConstant.FILE_UPLOAD_CHUNK_DIR + File.separator + DateUtil.today(); + File subFile = createFile(filename, dir); + FileOutputStream os = null; + try { + os = new FileOutputStream(subFile, true); + os.write(content); + } finally { + if (os != null) { + try { + os.close(); + } catch (IOException e) { + e.printStackTrace(); } } - - FileChunkLog newChunkLog = initFileChunkLog(fileUpload, resultId, md5, sha1); - newChunkLog.setPath(subFile.getPath()); - fileChunkLogMapper.insertSelective(newChunkLog); } - // 存储文件 + + FileChunkLog newChunkLog = initFileChunkLog(fileUpload, resultId, md5, sha1); + newChunkLog.setPath(subFile.getPath()); + fileChunkLogMapper.insertSelective(newChunkLog); + } + + private void writeFile(FileDto.MultipartFileUpload fileUpload, String path, byte[] content) throws FileNotFoundException { RandomAccessFile raf = null; FileChannel channel = null; try { @@ -339,14 +368,94 @@ public class FileService extends AbstractRedisService implements IFileService { } } } - File file1 = new File(path); - bigFile.setSha1(Sha1Util.getFileSha1(file1)); - bigFile.setMd5(Md5Util.getFileMD5(file1)); - bigFile.setResult(WebConstant.FileMsg.FILE_UPLOAD_SUC); - log.info("响应时间:{}", System.currentTimeMillis() - t1); - return bigFile; } + @Override + public FileVo.BigFileCheck bigFileCheck(FileDto.BigFileCheck fileCheck, long userId) throws IOException { + log.info("文件检查:{},{}", fileCheck, userId); + FileVo.BigFileCheck check = new FileVo.BigFileCheck(); + + FileCommitExample example = new FileCommitExample(); + example.createCriteria().andMd5EqualTo(fileCheck.getMd5()).andSha1EqualTo(fileCheck.getSha1()).andStatusEqualTo(WebConstant.YES); + List fileCommits = fileDao.selectByExample(example); + + FileCommit commit; + if (CollectionUtil.isNotEmpty(fileCommits)) { + commit = fileCommits.get(0); + fileDao.addLinkCount(commit.getId()); + check.setExist(WebConstant.YES); + } else { + // 记录文件信息 + String dir = PropUtil.path + WebConstant.FILE_UPLOAD_DIR + File.separator + DateUtil.today(); + File saveFile = createFile(fileCheck.getFileName(), dir); + commit = initFileCommit(dir, saveFile.getName(), fileCheck.getMd5(), fileCheck.getSha1(), saveFile.getName()); + commit.setStatus(WebConstant.NO); + fileDao.insertSelective(commit); + + check.setExist(WebConstant.NO); + + } + + // 直接添加link,并添加commit被引用次数 + FileLink link = new FileLink(); + link.setId(snowflake.nextId()); + link.setUserId(userId); + link.setName(fileCheck.getFileName()); + link.setCommitId(commit.getId()); + fileLinkMapper.insertSelective(link); + // 获取访问路径 + String visitPath = commit.getVisitPath(); + visitPath = getVisitUrl(link.getId(), visitPath); + // 记录文件 + if (CollectionUtil.isEmpty(fileCommits)) { + FileChunkResult result = new FileChunkResult(); + result.setId(snowflake.nextId()); + result.setLinkId(link.getId()); + result.setUserId(userId); + result.setTaskId(fileCheck.getTaskId()); + result.setTotal(fileCheck.getChunkTotal()); + fileChunkResultMapper.insertSelective(result); + String key = String.format(WebConstant.RedisParam.RESULT_ID_KEY, userId , fileCheck.getTaskId()); + String pathKey = String.format(WebConstant.RedisParam.COMMIT_PATH_KEY, userId, fileCheck.getTaskId()); + String visitPathKey = String.format(WebConstant.RedisParam.VISIT_PATH_KEY, userId, fileCheck.getTaskId()); + String visitIdKey = String.format(WebConstant.RedisParam.VISIT_ID_KEY, userId, fileCheck.getTaskId()); + redisUtil.set(key, result.getId()); + redisUtil.set(pathKey, commit.getPath()); + redisUtil.set(visitIdKey, link.getId()); + redisUtil.set(visitPathKey, visitPath); + } + + + check.setId(link.getId()); + check.setName(fileCheck.getFileName()); + check.setVisitUrl(visitPath); + log.info("文件检查结果:{}", check); + return check; + + } + +// private boolean checkFull(FileDto.MultipartFileUpload fileUpload, FileVo.BigFile bigFile, Long userId) { +// FileCommitExample example = new FileCommitExample(); +// example.createCriteria().andMd5EqualTo(fileUpload.getMd5()).andSha1EqualTo(fileUpload.getSha1()); +// List fileCommits = fileDao.selectByExample(example); +// if (CollectionUtil.isEmpty(fileCommits)) { +// return false; +// } +// FileCommit commit = fileCommits.get(0); +// FileLinkExample linkExample = new FileLinkExample(); +// linkExample.createCriteria().andCommitIdEqualTo(commit.getId()).andUserIdEqualTo(userId); +// List fileLinks = fileLinkMapper.selectByExample(linkExample); +// if (CollectionUtil.isNotEmpty(fileLinks)) { +// FileLink link = fileLinks.get(0); +// bigFile.setId(link.getId()); +// bigFile.setPath(getVisitUrl(link.getId(), commit.getVisitPath())); +// return true; +// } +// +// +// return true; +// } + private MappedByteBuffer getMappedByteBuffer(byte[] content, FileChannel channel, long offset) throws InterruptedException { try { return channel.map(FileChannel.MapMode.READ_WRITE, offset, content.length); @@ -357,52 +466,59 @@ public class FileService extends AbstractRedisService implements IFileService { } } - private String getPathByRedis(String key, int count) { - int max = 1000; - if (count > max) { - return null; - } - Object o = redisUtil.get(key); - if (o != null) { - return (String)o; - } - try { - Thread.sleep(10); - } catch (InterruptedException e) { - log.error("查询pathByRedis休眠时出现异常", e); - } - return getPathByRedis(key, count + 1); - - } - - private String initBigFile(FileDto.MultipartFileUpload fileUpload, String key, String pathKey, String filename, long resultId) throws IOException { - - boolean saveId = redisUtil.setNx(key, resultId, WebConstant.RedisParam.EXPIRE_SIXTY); - if (!saveId) { - return getPathByRedis(pathKey, 0); - } - String dir = PropUtil.path + WebConstant.FILE_UPLOAD_DIR + File.separator + DateUtil.today(); - File saveFile = createFile(filename, dir); - FileCommit commit = initFileCommit(dir, saveFile.getName(), null, null, saveFile.getName()); - redisUtil.set(pathKey, commit.getPath(), WebConstant.RedisParam.EXPIRE_SIXTY); - fileDao.insertSelective(commit); - - //关联 - // TODO 如何获取appId - FileLink link = initFileLink(filename, commit.getId(), fileUpload.getAppId()); - fileLinkMapper.insertSelective(link); - // 记录和文件的关联 - FileChunkResult result = new FileChunkResult(); - result.setId(resultId); - // TODO appId - result.setAppId(fileUpload.getAppId()); - result.setTaskId(fileUpload.getTaskId()); - result.setCommitId(commit.getId()); - result.setTotal(fileUpload.getChunkTotal()); - fileChunkResultMapper.insertSelective(result); - - return commit.getPath(); - } +// private String getPathByRedis(String key, int count) { +// int max = 1000; +// if (count > max) { +// return null; +// } +// Object o = redisUtil.get(key); +// if (o != null) { +// return (String)o; +// } +// try { +// Thread.sleep(10); +// } catch (InterruptedException e) { +// log.error("查询pathByRedis休眠时出现异常", e); +// } +// return getPathByRedis(key, count + 1); +// +// } + +// private String initBigFile(FileDto.MultipartFileUpload fileUpload, String filename, long resultId, long userId) throws IOException { +// String key = String.format(WebConstant.RedisParam.RESULT_ID_KEY, userId , fileUpload.getTaskId()); +// String pathKey = String.format(WebConstant.RedisParam.COMMIT_PATH_KEY, userId, fileUpload.getTaskId()); +// String visitPathKey = String.format(WebConstant.RedisParam.VISIT_PATH_KEY, userId, fileUpload.getTaskId()); +// String visitIdKey = String.format(WebConstant.RedisParam.VISIT_ID_KEY, userId, fileUpload.getTaskId()); +// +// boolean saveId = redisUtil.setNx(key, resultId, WebConstant.RedisParam.EXPIRE_SIXTY); +// if (!saveId) { +// return getPathByRedis(pathKey, 0); +// } +// String dir = PropUtil.path + WebConstant.FILE_UPLOAD_DIR + File.separator + DateUtil.today(); +// File saveFile = createFile(filename, dir); +// FileCommit commit = initFileCommit(dir, saveFile.getName(), null, null, saveFile.getName()); +// redisUtil.set(pathKey, commit.getPath(), WebConstant.RedisParam.EXPIRE_SIXTY); +// fileDao.insertSelective(commit); +// +// //关联 +// FileLink link = initFileLink(filename, commit.getId(), userId); +// // 记录文件引用ID和访问路径 +// redisUtil.set(visitPathKey, getVisitUrl(link.getId(), commit.getVisitPath()), WebConstant.RedisParam.EXPIRE_SIXTY); +// redisUtil.set(visitIdKey, link.getId(), WebConstant.RedisParam.EXPIRE_SIXTY); +// fileLinkMapper.insertSelective(link); +// +// +// // 记录和文件的关联 +// FileChunkResult result = new FileChunkResult(); +// result.setId(resultId); +// result.setUserId(userId); +// result.setTaskId(fileUpload.getTaskId()); +// result.setLinkId(link.getId()); +//// result.setTotal(fileUpload.getChunkTotal()); +// fileChunkResultMapper.insertSelective(result); +// +// return commit.getPath(); +// } private FileChunkLog initFileChunkLog(FileDto.MultipartFileUpload fileUpload, long resultId, String md5, String sha1) { @@ -454,19 +570,36 @@ public class FileService extends AbstractRedisService implements IFileService { String resultKey = String.format(WebConstant.RedisParam.RESULT_ID_KEY, "\\d+" , "\\d+"); String pathKey = String.format(WebConstant.RedisParam.COMMIT_PATH_KEY, "\\d+", "\\d+"); - + String visitPathKey = String.format(WebConstant.RedisParam.VISIT_PATH_KEY, "\\d+", "\\d+"); + String visitIdKey = String.format(WebConstant.RedisParam.VISIT_ID_KEY, "\\d+", "\\d+"); + String md5Key = StrUtil.format(WebConstant.RedisParam.MD5_KEY, "\\d+"); + String sha1Key = StrUtil.format(WebConstant.RedisParam.SHA1_KEY, "\\d+"); + String[] str = key.split("_"); if (key.matches(resultKey)) { - String[] str = key.split("_"); + FileChunkResultExample resultExample = new FileChunkResultExample(); - resultExample.createCriteria().andAppIdEqualTo(Long.parseLong(str[1])).andTaskIdEqualTo(str[2]); + resultExample.createCriteria().andUserIdEqualTo(Long.parseLong(str[1])).andTaskIdEqualTo(str[2]); List chunkResults = fileChunkResultMapper.selectByExample(resultExample); return CollectionUtil.isEmpty(chunkResults) ? null : chunkResults.get(0).getId(); } else if (key.matches(pathKey)) { - String[] str = key.split("_"); Long appId = Long.parseLong(str[1]); String taskId = str[2]; FileVo.FilePosition position = fileDao.getByAppIdAndTaskId(appId, taskId); return position == null ? null : position.getPath(); + } else if (key.matches(visitIdKey)) { + Long appId = Long.parseLong(str[1]); + String taskId = str[2]; + FileVo.FilePosition position = fileDao.getByAppIdAndTaskId(appId, taskId); + return position == null ? null : position.getId(); + } else if (key.matches(visitPathKey)) { + Long appId = Long.parseLong(str[1]); + String taskId = str[2]; + FileVo.FilePosition position = fileDao.getByAppIdAndTaskId(appId, taskId); + return position == null ? null : getVisitUrl(position.getId(), position.getVisitUrl()); + } else if (key.matches(md5Key)) { + return fileDao.getMd5ByResultId(Long.parseLong(str[1])); + } else if (key.matches(sha1Key)) { + return fileDao.getSha1ByResultId(Long.parseLong(str[1])); } return null; } diff --git a/src/main/java/com/ccsens/filedeal/service/IFileService.java b/src/main/java/com/ccsens/filedeal/service/IFileService.java index 661b8ec..9400b3d 100644 --- a/src/main/java/com/ccsens/filedeal/service/IFileService.java +++ b/src/main/java/com/ccsens/filedeal/service/IFileService.java @@ -1,12 +1,10 @@ package com.ccsens.filedeal.service; import com.ccsens.filedeal.bean.dto.FileDto; -import com.ccsens.filedeal.bean.po.FileCommit; import com.ccsens.filedeal.bean.vo.FileVo; import com.ccsens.filedeal.exception.BaseException; import javax.servlet.http.Part; -import java.io.FileNotFoundException; import java.io.IOException; import java.util.List; @@ -19,11 +17,12 @@ public interface IFileService { * 批量上传文件 * @param dir 上级目录 * @param files 文件数组 + * @param userId * @return 文件信息 * @throws IOException 文件读写异常 * @throws BaseException 文件存储异常 */ - List saveFileMultiple(String dir, List files) throws IOException, BaseException; + List saveFileMultiple(String dir, List files, long userId) throws IOException, BaseException; /** * 根据ID查询数据 @@ -45,16 +44,26 @@ public interface IFileService { * 单个上传文件 * @param dir 上级目录 * @param file 文件 + * @param userId * @return 文件信息 * @throws IOException 文件读写异常 * @throws BaseException 文件存储异常 */ - FileVo.Upload saveFileSingle(String dir, Part file) throws IOException, BaseException; + FileVo.Upload saveFileSingle(String dir, Part file, long userId) throws IOException, BaseException; /** * 大文件分块上传 * @param fileUpload 分块上传 + * @param userId * @return 上传结果 */ - FileVo.BigFile uploadBigFile(FileDto.MultipartFileUpload fileUpload) throws IOException; + FileVo.BigFile uploadBigFile(FileDto.MultipartFileUpload fileUpload, long userId) throws IOException; + + /** + * 大文件检查 + * @param fileCheck + * @param userId + * @return + */ + FileVo.BigFileCheck bigFileCheck(FileDto.BigFileCheck fileCheck, long userId) throws IOException; } diff --git a/src/main/java/com/ccsens/filedeal/util/CodeEnum.java b/src/main/java/com/ccsens/filedeal/util/CodeEnum.java index 6cd0de1..c78384e 100644 --- a/src/main/java/com/ccsens/filedeal/util/CodeEnum.java +++ b/src/main/java/com/ccsens/filedeal/util/CodeEnum.java @@ -110,6 +110,8 @@ public enum CodeEnum { NOT_USER(109,"用户不存在",true), VERIFICATION_CODE_PAST(110,"验证码失效,请刷新重试",true), VERIFICATION_CODE_ERROR(111,"验证码错误",true), + BIG_FILE_NO_CHECK(112,"请您先校验文件内容",true), + diff --git a/src/main/java/com/ccsens/filedeal/util/JsonResponse.java b/src/main/java/com/ccsens/filedeal/util/JsonResponse.java index b04f8ba..e2c36cd 100644 --- a/src/main/java/com/ccsens/filedeal/util/JsonResponse.java +++ b/src/main/java/com/ccsens/filedeal/util/JsonResponse.java @@ -43,7 +43,7 @@ public class JsonResponse { return this; } - public JsonResponse ok(T data){ + public JsonResponse ok(T data){ ok(); this.data = data; return this; diff --git a/src/main/java/com/ccsens/filedeal/util/WebConstant.java b/src/main/java/com/ccsens/filedeal/util/WebConstant.java index 6965aa2..bb4b682 100644 --- a/src/main/java/com/ccsens/filedeal/util/WebConstant.java +++ b/src/main/java/com/ccsens/filedeal/util/WebConstant.java @@ -1,6 +1,5 @@ package com.ccsens.filedeal.util; -import cn.hutool.core.codec.Base64; import java.io.File; @@ -24,18 +23,30 @@ public class WebConstant { */ public static final String FILE_UPLOAD_DIR = "upload"; public static final String FILE_UPLOAD_CHUNK_DIR = FILE_UPLOAD_DIR + File.separator + "chunk"; + + public static final byte YES = 1; + public static final byte NO = 0; + /**属性名*/ public static class Field{ public static final String CODE = "code"; } public static class FileMsg{ + /**上传成功*/ public static final byte FILE_UPLOAD_SUC = 0; + /**已上传上传*/ public static final byte FILE_UPLOAD_READY = 1; + /**全部上传成功*/ + public static final byte FILE_UPLOAD_ALL = 2; } public static class RedisParam{ public static final String RESULT_ID_KEY = "resultId_%1$s_%2$s"; public static final String COMMIT_PATH_KEY = "commitPath_%1$s_%2$s"; + public static final String VISIT_PATH_KEY = "visitPath_%1$s_%2$s"; + public static final String VISIT_ID_KEY = "visitId_%1$s_%2$s"; + public static final String MD5_KEY = "md5_{}"; + public static final String SHA1_KEY = "sha1_{}"; public static long EXPIRE_SIXTY = 60*60; } diff --git a/src/main/resources/mapper_dao/FileDao.xml b/src/main/resources/mapper_dao/FileDao.xml index 9be1873..5890bb9 100644 --- a/src/main/resources/mapper_dao/FileDao.xml +++ b/src/main/resources/mapper_dao/FileDao.xml @@ -1,12 +1,32 @@ + + UPDATE t_file_commit + SET count = count + 1 + WHERE + id = #{id} + + + update t_file_chunk_result r, + t_file_link l, + t_file_commit c + set c.status = 1 + WHERE + r.link_id = l.id + AND l.commit_id = c.id + AND r.rec_status = 0 + AND l.rec_status = 0 + AND c.rec_status = 0 + AND r.id = #{resultId} + + + \ No newline at end of file diff --git a/src/main/resources/mapper_raw/FileChunkResultMapper.xml b/src/main/resources/mapper_raw/FileChunkResultMapper.xml index 11d9f26..833da6e 100644 --- a/src/main/resources/mapper_raw/FileChunkResultMapper.xml +++ b/src/main/resources/mapper_raw/FileChunkResultMapper.xml @@ -3,9 +3,9 @@ - + - + @@ -70,7 +70,7 @@ - id, app_id, task_id, commit_id, total, created_at, updated_at, rec_status + id, user_id, task_id, link_id, total, created_at, updated_at, rec_status select @@ -108,12 +110,14 @@ insert into t_file_commit (id, name, path, visit_path, md5, sha1, - time, count, created_at, - updated_at, rec_status) + time, count, status, + created_at, updated_at, rec_status + ) values (#{id,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR}, #{path,jdbcType=VARCHAR}, #{visitPath,jdbcType=VARCHAR}, #{md5,jdbcType=VARCHAR}, #{sha1,jdbcType=VARCHAR}, - #{time,jdbcType=BIGINT}, #{count,jdbcType=INTEGER}, #{createdAt,jdbcType=TIMESTAMP}, - #{updatedAt,jdbcType=TIMESTAMP}, #{recStatus,jdbcType=TINYINT}) + #{time,jdbcType=BIGINT}, #{count,jdbcType=INTEGER}, #{status,jdbcType=TINYINT}, + #{createdAt,jdbcType=TIMESTAMP}, #{updatedAt,jdbcType=TIMESTAMP}, #{recStatus,jdbcType=TINYINT} + ) insert into t_file_commit @@ -142,6 +146,9 @@ count, + + status, + created_at, @@ -177,6 +184,9 @@ #{count,jdbcType=INTEGER}, + + #{status,jdbcType=TINYINT}, + #{createdAt,jdbcType=TIMESTAMP}, @@ -221,6 +231,9 @@ count = #{record.count,jdbcType=INTEGER}, + + status = #{record.status,jdbcType=TINYINT}, + created_at = #{record.createdAt,jdbcType=TIMESTAMP}, @@ -245,6 +258,7 @@ sha1 = #{record.sha1,jdbcType=VARCHAR}, time = #{record.time,jdbcType=BIGINT}, count = #{record.count,jdbcType=INTEGER}, + status = #{record.status,jdbcType=TINYINT}, created_at = #{record.createdAt,jdbcType=TIMESTAMP}, updated_at = #{record.updatedAt,jdbcType=TIMESTAMP}, rec_status = #{record.recStatus,jdbcType=TINYINT} @@ -276,6 +290,9 @@ count = #{count,jdbcType=INTEGER}, + + status = #{status,jdbcType=TINYINT}, + created_at = #{createdAt,jdbcType=TIMESTAMP}, @@ -297,6 +314,7 @@ sha1 = #{sha1,jdbcType=VARCHAR}, time = #{time,jdbcType=BIGINT}, count = #{count,jdbcType=INTEGER}, + status = #{status,jdbcType=TINYINT}, created_at = #{createdAt,jdbcType=TIMESTAMP}, updated_at = #{updatedAt,jdbcType=TIMESTAMP}, rec_status = #{recStatus,jdbcType=TINYINT} diff --git a/src/main/resources/mapper_raw/FileLinkMapper.xml b/src/main/resources/mapper_raw/FileLinkMapper.xml index ac4b010..4214df5 100644 --- a/src/main/resources/mapper_raw/FileLinkMapper.xml +++ b/src/main/resources/mapper_raw/FileLinkMapper.xml @@ -3,7 +3,7 @@ - + @@ -69,7 +69,7 @@ - id, app_id, name, commit_id, created_at, updated_at, rec_status + id, user_id, name, commit_id, created_at, updated_at, rec_status