Browse Source

文件上传

master
zhizhi wu 5 years ago
parent
commit
dbc877cb6d
  1. 79
      src/main/java/com/ccsens/filedeal/api/FileController.java
  2. 30
      src/main/java/com/ccsens/filedeal/bean/dto/FileDto.java
  3. 24
      src/main/java/com/ccsens/filedeal/bean/po/FileChunkResult.java
  4. 96
      src/main/java/com/ccsens/filedeal/bean/po/FileChunkResultExample.java
  5. 11
      src/main/java/com/ccsens/filedeal/bean/po/FileCommit.java
  6. 60
      src/main/java/com/ccsens/filedeal/bean/po/FileCommitExample.java
  7. 12
      src/main/java/com/ccsens/filedeal/bean/po/FileLink.java
  8. 48
      src/main/java/com/ccsens/filedeal/bean/po/FileLinkExample.java
  9. 30
      src/main/java/com/ccsens/filedeal/bean/vo/FileVo.java
  10. 26
      src/main/java/com/ccsens/filedeal/persist/dao/FileDao.java
  11. 303
      src/main/java/com/ccsens/filedeal/service/FileService.java
  12. 19
      src/main/java/com/ccsens/filedeal/service/IFileService.java
  13. 2
      src/main/java/com/ccsens/filedeal/util/CodeEnum.java
  14. 2
      src/main/java/com/ccsens/filedeal/util/JsonResponse.java
  15. 13
      src/main/java/com/ccsens/filedeal/util/WebConstant.java
  16. 55
      src/main/resources/mapper_dao/FileDao.xml
  17. 54
      src/main/resources/mapper_raw/FileChunkResultMapper.xml
  18. 28
      src/main/resources/mapper_raw/FileCommitMapper.xml
  19. 28
      src/main/resources/mapper_raw/FileLinkMapper.xml

79
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<FileVo.BigFileCheck> 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<FileVo.BigFile> uploadBigFile(FileDto.MultipartFileUpload fileUpload) throws Exception {
public JsonResponse<FileVo.BigFile> 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<FileVo.Upload> uploadSingle( Part file) throws Exception {
public JsonResponse<FileVo.Upload> 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<FileVo.Upload> uploadMultiple( List<Part> files) throws Exception {
public JsonResponse<FileVo.Upload> uploadMultiple(HttpServletRequest request, List<Part> files) throws Exception {
log.info("文件批量上传:{}", files);
String dir = PropUtil.path + WebConstant.FILE_UPLOAD_DIR;
List<FileVo.Upload> vos = fileService.saveFileMultiple(dir, files);
Long userId = getUserId(request);
List<FileVo.Upload> 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<String> readFile(@PathVariable("id")Long id, int start, int end) throws IOException {
public JsonResponse<String> 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<JSONObject> 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<JSONObject> 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();
// }

30
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;
}

24
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);

96
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<Long> values) {
addCriterion("app_id in", values, "appId");
public Criteria andUserIdIn(List<Long> values) {
addCriterion("user_id in", values, "userId");
return (Criteria) this;
}
public Criteria andAppIdNotIn(List<Long> values) {
addCriterion("app_id not in", values, "appId");
public Criteria andUserIdNotIn(List<Long> 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<Long> values) {
addCriterion("commit_id in", values, "commitId");
public Criteria andLinkIdIn(List<Long> values) {
addCriterion("link_id in", values, "linkId");
return (Criteria) this;
}
public Criteria andCommitIdNotIn(List<Long> values) {
addCriterion("commit_id not in", values, "commitId");
public Criteria andLinkIdNotIn(List<Long> 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;
}

11
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);

60
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<Byte> values) {
addCriterion("status in", values, "status");
return (Criteria) this;
}
public Criteria andStatusNotIn(List<Byte> 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;

12
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);

48
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<Long> values) {
addCriterion("app_id in", values, "appId");
public Criteria andUserIdIn(List<Long> values) {
addCriterion("user_id in", values, "userId");
return (Criteria) this;
}
public Criteria andAppIdNotIn(List<Long> values) {
addCriterion("app_id not in", values, "appId");
public Criteria andUserIdNotIn(List<Long> 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;
}

30
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;
}

26
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);
}

303
src/main/java/com/ccsens/filedeal/service/FileService.java

@ -57,17 +57,17 @@ public class FileService extends AbstractRedisService implements IFileService {
@Override
public List<FileVo.Upload> saveFileMultiple(String dir, List<Part> files) throws IOException, BaseException {
public List<FileVo.Upload> saveFileMultiple(String dir, List<Part> files, long userId) throws IOException, BaseException {
List<FileVo.Upload> 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;
throw new BaseException(CodeEnum.BIG_FILE_NO_CHECK);
}
long 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;
}
}
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,6 +291,34 @@ public class FileService extends AbstractRedisService implements IFileService {
newChunkLog.setPath(chunkLog.getPath());
fileChunkLogMapper.insertSelective(newChunkLog);
} else {
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;
@ -311,7 +339,8 @@ public class FileService extends AbstractRedisService implements IFileService {
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,71 +368,158 @@ 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;
}
private MappedByteBuffer getMappedByteBuffer(byte[] content, FileChannel channel, long offset) throws InterruptedException {
try {
return channel.map(FileChannel.MapMode.READ_WRITE, offset, content.length);
} catch (IOException e) {
log.info("请求的操作无法在使用用户映射区域打开的文件上执行, 休眠后重新尝试");
Thread.sleep(100);
return getMappedByteBuffer(content, channel, offset);
}
}
@Override
public FileVo.BigFileCheck bigFileCheck(FileDto.BigFileCheck fileCheck, long userId) throws IOException {
log.info("文件检查:{},{}", fileCheck, userId);
FileVo.BigFileCheck check = new FileVo.BigFileCheck();
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);
FileCommitExample example = new FileCommitExample();
example.createCriteria().andMd5EqualTo(fileCheck.getMd5()).andSha1EqualTo(fileCheck.getSha1()).andStatusEqualTo(WebConstant.YES);
List<FileCommit> 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);
private String initBigFile(FileDto.MultipartFileUpload fileUpload, String key, String pathKey, String filename, long resultId) throws IOException {
check.setExist(WebConstant.NO);
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());
// 直接添加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(resultId);
// TODO appId
result.setAppId(fileUpload.getAppId());
result.setTaskId(fileUpload.getTaskId());
result.setCommitId(commit.getId());
result.setTotal(fileUpload.getChunkTotal());
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<FileCommit> 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<FileLink> 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;
// }
return commit.getPath();
private MappedByteBuffer getMappedByteBuffer(byte[] content, FileChannel channel, long offset) throws InterruptedException {
try {
return channel.map(FileChannel.MapMode.READ_WRITE, offset, content.length);
} catch (IOException e) {
log.info("请求的操作无法在使用用户映射区域打开的文件上执行, 休眠后重新尝试");
Thread.sleep(100);
return getMappedByteBuffer(content, channel, offset);
}
}
// 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) {
FileChunkLog newChunkLog = new FileChunkLog();
@ -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+");
if (key.matches(resultKey)) {
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)) {
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<FileChunkResult> 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;
}

19
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<FileVo.Upload> saveFileMultiple(String dir, List<Part> files) throws IOException, BaseException;
List<FileVo.Upload> saveFileMultiple(String dir, List<Part> 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;
}

2
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),

2
src/main/java/com/ccsens/filedeal/util/JsonResponse.java

@ -43,7 +43,7 @@ public class JsonResponse<T> {
return this;
}
public JsonResponse ok(T data){
public JsonResponse<T> ok(T data){
ok();
this.data = data;
return this;

13
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;
}

55
src/main/resources/mapper_dao/FileDao.xml

@ -1,12 +1,32 @@
<?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.ccsens.filedeal.persist.dao.FileDao">
<update id="addLinkCount">
UPDATE t_file_commit
SET count = count + 1
WHERE
id = #{id}
</update>
<update id="updateCommitStatus">
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}
</update>
<select id="getByLinkId" resultType="com.ccsens.filedeal.bean.vo.FileVo$FilePosition">
SELECT
l.id,
l.name,
c.path
c.path,
c.visit_path as visitUrl
FROM
t_file_link l,
t_file_commit c
@ -20,7 +40,8 @@
SELECT
c.id,
c.NAME,
c.path
c.path,
c.visit_path as visitUrl
FROM
t_file_commit c,
t_file_chunk_result r
@ -31,4 +52,34 @@
AND c.rec_status = 0
AND r.rec_status = 0
</select>
<select id="getMd5ByResultId" resultType="java.lang.String">
SELECT
c.md5
FROM
t_file_chunk_result r,
t_file_link l,
t_file_commit c
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}
</select>
<select id="getSha1ByResultId" resultType="java.lang.String">
SELECT
c.sha1
FROM
t_file_chunk_result r,
t_file_link l,
t_file_commit c
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}
</select>
</mapper>

54
src/main/resources/mapper_raw/FileChunkResultMapper.xml

@ -3,9 +3,9 @@
<mapper namespace="com.ccsens.filedeal.persist.mapper.FileChunkResultMapper">
<resultMap id="BaseResultMap" type="com.ccsens.filedeal.bean.po.FileChunkResult">
<id column="id" jdbcType="BIGINT" property="id" />
<result column="app_id" jdbcType="BIGINT" property="appId" />
<result column="user_id" jdbcType="BIGINT" property="userId" />
<result column="task_id" jdbcType="VARCHAR" property="taskId" />
<result column="commit_id" jdbcType="BIGINT" property="commitId" />
<result column="link_id" jdbcType="BIGINT" property="linkId" />
<result column="total" jdbcType="INTEGER" property="total" />
<result column="created_at" jdbcType="TIMESTAMP" property="createdAt" />
<result column="updated_at" jdbcType="TIMESTAMP" property="updatedAt" />
@ -70,7 +70,7 @@
</where>
</sql>
<sql id="Base_Column_List">
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
</sql>
<select id="selectByExample" parameterType="com.ccsens.filedeal.bean.po.FileChunkResultExample" resultMap="BaseResultMap">
select
@ -103,11 +103,11 @@
</if>
</delete>
<insert id="insert" parameterType="com.ccsens.filedeal.bean.po.FileChunkResult">
insert into t_file_chunk_result (id, app_id, task_id,
commit_id, total, created_at,
insert into t_file_chunk_result (id, user_id, task_id,
link_id, total, created_at,
updated_at, rec_status)
values (#{id,jdbcType=BIGINT}, #{appId,jdbcType=BIGINT}, #{taskId,jdbcType=VARCHAR},
#{commitId,jdbcType=BIGINT}, #{total,jdbcType=INTEGER}, #{createdAt,jdbcType=TIMESTAMP},
values (#{id,jdbcType=BIGINT}, #{userId,jdbcType=BIGINT}, #{taskId,jdbcType=VARCHAR},
#{linkId,jdbcType=BIGINT}, #{total,jdbcType=INTEGER}, #{createdAt,jdbcType=TIMESTAMP},
#{updatedAt,jdbcType=TIMESTAMP}, #{recStatus,jdbcType=TINYINT})
</insert>
<insert id="insertSelective" parameterType="com.ccsens.filedeal.bean.po.FileChunkResult">
@ -116,14 +116,14 @@
<if test="id != null">
id,
</if>
<if test="appId != null">
app_id,
<if test="userId != null">
user_id,
</if>
<if test="taskId != null">
task_id,
</if>
<if test="commitId != null">
commit_id,
<if test="linkId != null">
link_id,
</if>
<if test="total != null">
total,
@ -142,14 +142,14 @@
<if test="id != null">
#{id,jdbcType=BIGINT},
</if>
<if test="appId != null">
#{appId,jdbcType=BIGINT},
<if test="userId != null">
#{userId,jdbcType=BIGINT},
</if>
<if test="taskId != null">
#{taskId,jdbcType=VARCHAR},
</if>
<if test="commitId != null">
#{commitId,jdbcType=BIGINT},
<if test="linkId != null">
#{linkId,jdbcType=BIGINT},
</if>
<if test="total != null">
#{total,jdbcType=INTEGER},
@ -177,14 +177,14 @@
<if test="record.id != null">
id = #{record.id,jdbcType=BIGINT},
</if>
<if test="record.appId != null">
app_id = #{record.appId,jdbcType=BIGINT},
<if test="record.userId != null">
user_id = #{record.userId,jdbcType=BIGINT},
</if>
<if test="record.taskId != null">
task_id = #{record.taskId,jdbcType=VARCHAR},
</if>
<if test="record.commitId != null">
commit_id = #{record.commitId,jdbcType=BIGINT},
<if test="record.linkId != null">
link_id = #{record.linkId,jdbcType=BIGINT},
</if>
<if test="record.total != null">
total = #{record.total,jdbcType=INTEGER},
@ -206,9 +206,9 @@
<update id="updateByExample" parameterType="map">
update t_file_chunk_result
set id = #{record.id,jdbcType=BIGINT},
app_id = #{record.appId,jdbcType=BIGINT},
user_id = #{record.userId,jdbcType=BIGINT},
task_id = #{record.taskId,jdbcType=VARCHAR},
commit_id = #{record.commitId,jdbcType=BIGINT},
link_id = #{record.linkId,jdbcType=BIGINT},
total = #{record.total,jdbcType=INTEGER},
created_at = #{record.createdAt,jdbcType=TIMESTAMP},
updated_at = #{record.updatedAt,jdbcType=TIMESTAMP},
@ -220,14 +220,14 @@
<update id="updateByPrimaryKeySelective" parameterType="com.ccsens.filedeal.bean.po.FileChunkResult">
update t_file_chunk_result
<set>
<if test="appId != null">
app_id = #{appId,jdbcType=BIGINT},
<if test="userId != null">
user_id = #{userId,jdbcType=BIGINT},
</if>
<if test="taskId != null">
task_id = #{taskId,jdbcType=VARCHAR},
</if>
<if test="commitId != null">
commit_id = #{commitId,jdbcType=BIGINT},
<if test="linkId != null">
link_id = #{linkId,jdbcType=BIGINT},
</if>
<if test="total != null">
total = #{total,jdbcType=INTEGER},
@ -246,9 +246,9 @@
</update>
<update id="updateByPrimaryKey" parameterType="com.ccsens.filedeal.bean.po.FileChunkResult">
update t_file_chunk_result
set app_id = #{appId,jdbcType=BIGINT},
set user_id = #{userId,jdbcType=BIGINT},
task_id = #{taskId,jdbcType=VARCHAR},
commit_id = #{commitId,jdbcType=BIGINT},
link_id = #{linkId,jdbcType=BIGINT},
total = #{total,jdbcType=INTEGER},
created_at = #{createdAt,jdbcType=TIMESTAMP},
updated_at = #{updatedAt,jdbcType=TIMESTAMP},

28
src/main/resources/mapper_raw/FileCommitMapper.xml

@ -10,6 +10,7 @@
<result column="sha1" jdbcType="VARCHAR" property="sha1" />
<result column="time" jdbcType="BIGINT" property="time" />
<result column="count" jdbcType="INTEGER" property="count" />
<result column="status" jdbcType="TINYINT" property="status" />
<result column="created_at" jdbcType="TIMESTAMP" property="createdAt" />
<result column="updated_at" jdbcType="TIMESTAMP" property="updatedAt" />
<result column="rec_status" jdbcType="TINYINT" property="recStatus" />
@ -73,7 +74,8 @@
</where>
</sql>
<sql id="Base_Column_List">
id, name, path, visit_path, md5, sha1, time, count, created_at, updated_at, rec_status
id, name, path, visit_path, md5, sha1, time, count, status, created_at, updated_at,
rec_status
</sql>
<select id="selectByExample" parameterType="com.ccsens.filedeal.bean.po.FileCommitExample" resultMap="BaseResultMap">
select
@ -108,12 +110,14 @@
<insert id="insert" parameterType="com.ccsens.filedeal.bean.po.FileCommit">
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>
<insert id="insertSelective" parameterType="com.ccsens.filedeal.bean.po.FileCommit">
insert into t_file_commit
@ -142,6 +146,9 @@
<if test="count != null">
count,
</if>
<if test="status != null">
status,
</if>
<if test="createdAt != null">
created_at,
</if>
@ -177,6 +184,9 @@
<if test="count != null">
#{count,jdbcType=INTEGER},
</if>
<if test="status != null">
#{status,jdbcType=TINYINT},
</if>
<if test="createdAt != null">
#{createdAt,jdbcType=TIMESTAMP},
</if>
@ -221,6 +231,9 @@
<if test="record.count != null">
count = #{record.count,jdbcType=INTEGER},
</if>
<if test="record.status != null">
status = #{record.status,jdbcType=TINYINT},
</if>
<if test="record.createdAt != null">
created_at = #{record.createdAt,jdbcType=TIMESTAMP},
</if>
@ -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 @@
<if test="count != null">
count = #{count,jdbcType=INTEGER},
</if>
<if test="status != null">
status = #{status,jdbcType=TINYINT},
</if>
<if test="createdAt != null">
created_at = #{createdAt,jdbcType=TIMESTAMP},
</if>
@ -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}

28
src/main/resources/mapper_raw/FileLinkMapper.xml

@ -3,7 +3,7 @@
<mapper namespace="com.ccsens.filedeal.persist.mapper.FileLinkMapper">
<resultMap id="BaseResultMap" type="com.ccsens.filedeal.bean.po.FileLink">
<id column="id" jdbcType="BIGINT" property="id" />
<result column="app_id" jdbcType="BIGINT" property="appId" />
<result column="user_id" jdbcType="BIGINT" property="userId" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="commit_id" jdbcType="BIGINT" property="commitId" />
<result column="created_at" jdbcType="TIMESTAMP" property="createdAt" />
@ -69,7 +69,7 @@
</where>
</sql>
<sql id="Base_Column_List">
id, app_id, name, commit_id, created_at, updated_at, rec_status
id, user_id, name, commit_id, created_at, updated_at, rec_status
</sql>
<select id="selectByExample" parameterType="com.ccsens.filedeal.bean.po.FileLinkExample" resultMap="BaseResultMap">
select
@ -102,10 +102,10 @@
</if>
</delete>
<insert id="insert" parameterType="com.ccsens.filedeal.bean.po.FileLink">
insert into t_file_link (id, app_id, name,
insert into t_file_link (id, user_id, name,
commit_id, created_at, updated_at,
rec_status)
values (#{id,jdbcType=BIGINT}, #{appId,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR},
values (#{id,jdbcType=BIGINT}, #{userId,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR},
#{commitId,jdbcType=BIGINT}, #{createdAt,jdbcType=TIMESTAMP}, #{updatedAt,jdbcType=TIMESTAMP},
#{recStatus,jdbcType=TINYINT})
</insert>
@ -115,8 +115,8 @@
<if test="id != null">
id,
</if>
<if test="appId != null">
app_id,
<if test="userId != null">
user_id,
</if>
<if test="name != null">
name,
@ -138,8 +138,8 @@
<if test="id != null">
#{id,jdbcType=BIGINT},
</if>
<if test="appId != null">
#{appId,jdbcType=BIGINT},
<if test="userId != null">
#{userId,jdbcType=BIGINT},
</if>
<if test="name != null">
#{name,jdbcType=VARCHAR},
@ -170,8 +170,8 @@
<if test="record.id != null">
id = #{record.id,jdbcType=BIGINT},
</if>
<if test="record.appId != null">
app_id = #{record.appId,jdbcType=BIGINT},
<if test="record.userId != null">
user_id = #{record.userId,jdbcType=BIGINT},
</if>
<if test="record.name != null">
name = #{record.name,jdbcType=VARCHAR},
@ -196,7 +196,7 @@
<update id="updateByExample" parameterType="map">
update t_file_link
set id = #{record.id,jdbcType=BIGINT},
app_id = #{record.appId,jdbcType=BIGINT},
user_id = #{record.userId,jdbcType=BIGINT},
name = #{record.name,jdbcType=VARCHAR},
commit_id = #{record.commitId,jdbcType=BIGINT},
created_at = #{record.createdAt,jdbcType=TIMESTAMP},
@ -209,8 +209,8 @@
<update id="updateByPrimaryKeySelective" parameterType="com.ccsens.filedeal.bean.po.FileLink">
update t_file_link
<set>
<if test="appId != null">
app_id = #{appId,jdbcType=BIGINT},
<if test="userId != null">
user_id = #{userId,jdbcType=BIGINT},
</if>
<if test="name != null">
name = #{name,jdbcType=VARCHAR},
@ -232,7 +232,7 @@
</update>
<update id="updateByPrimaryKey" parameterType="com.ccsens.filedeal.bean.po.FileLink">
update t_file_link
set app_id = #{appId,jdbcType=BIGINT},
set user_id = #{userId,jdbcType=BIGINT},
name = #{name,jdbcType=VARCHAR},
commit_id = #{commitId,jdbcType=BIGINT},
created_at = #{createdAt,jdbcType=TIMESTAMP},

Loading…
Cancel
Save