|
|
@@ -9,8 +9,6 @@ import cn.reghao.dfs.store.service.ObjectNameService;
|
|
|
import cn.reghao.dfs.store.service.PutObjectService;
|
|
|
import cn.reghao.dfs.store.task.FileProcessor;
|
|
|
import cn.reghao.dfs.store.util.JwtUtil;
|
|
|
-import cn.reghao.dfs.store.util.ObjectUtil;
|
|
|
-import cn.reghao.dfs.store.util.StringUtil;
|
|
|
import cn.reghao.dfs.store.util.UserContext;
|
|
|
import cn.reghao.jutil.jdk.result.WebResult;
|
|
|
import cn.reghao.jutil.jdk.security.DigestUtil;
|
|
|
@@ -18,15 +16,10 @@ import cn.reghao.jutil.web.ServletUtil;
|
|
|
import cn.reghao.oss.api.dto.OssPayload;
|
|
|
import cn.reghao.oss.api.rest.UploadFileRet;
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
-import org.springframework.web.bind.annotation.PostMapping;
|
|
|
-import org.springframework.web.bind.annotation.PutMapping;
|
|
|
-import org.springframework.web.bind.annotation.RequestBody;
|
|
|
-import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
-import javax.servlet.http.HttpServletRequest;
|
|
|
import java.io.File;
|
|
|
-import java.io.FileInputStream;
|
|
|
import java.util.UUID;
|
|
|
|
|
|
/**
|
|
|
@@ -53,73 +46,13 @@ public class ObjectUploadController {
|
|
|
|
|
|
@PutMapping(value = "/**")
|
|
|
public String putObject(@RequestBody File file) {
|
|
|
- try {
|
|
|
- HttpServletRequest servletRequest = ServletUtil.getRequest();
|
|
|
- int channelId = Integer.parseInt(servletRequest.getHeader("x-channel-id"));
|
|
|
- String sha256sum = servletRequest.getHeader("x-content-sha256sum");
|
|
|
- String objectName = ObjectUtil.getObjectName();
|
|
|
-
|
|
|
- String token = ServletUtil.getBearerToken();
|
|
|
- if (token == null) {
|
|
|
- return WebResult.failWithMsg("no token in request");
|
|
|
- }
|
|
|
-
|
|
|
- OssPayload ossPayload = JwtUtil.getOssPayload(token);
|
|
|
- String action = ossPayload.getAction();
|
|
|
- if (!"upload".equals(action)) {
|
|
|
- return WebResult.failWithMsg("it's not upload token");
|
|
|
- }
|
|
|
-
|
|
|
- int channelId1 = ossPayload.getChannelId();
|
|
|
- if (channelId != channelId1) {
|
|
|
- return WebResult.failWithMsg("channel not match in token");
|
|
|
- }
|
|
|
-
|
|
|
- long userId1 = ossPayload.getUserId();
|
|
|
- long userId = UserContext.getUser();
|
|
|
- if (userId != userId1) {
|
|
|
- return WebResult.failWithMsg("user not match in token");
|
|
|
- }
|
|
|
-
|
|
|
- boolean ret = channelValidateService.validate(file, channelId);
|
|
|
- if (!ret) {
|
|
|
- return WebResult.failWithMsg("the format or size of upload file error");
|
|
|
- }
|
|
|
-
|
|
|
- FileInputStream fis = new FileInputStream(file);
|
|
|
- String sha256sum1 = DigestUtil.sha256sum(fis);
|
|
|
- /*if (!sha256sum.equals(sha256sum1)) {
|
|
|
- FileUtils.deleteQuietly(file);
|
|
|
- return WebResult.failWithMsg("sha256sum not match");
|
|
|
- }*/
|
|
|
-
|
|
|
- String contentId = UUID.randomUUID().toString().replace("-", "");
|
|
|
- File savedFile = fileStoreService.saveFile(file, contentId);
|
|
|
-
|
|
|
- String originalFilename = file.getName();
|
|
|
- String suffix = StringUtil.getSuffix(originalFilename);
|
|
|
- ObjectProp objectProp = objectNameService.getObjectProp(channelId, suffix);
|
|
|
- ObjectResult objectResult = putObjectService.putObject(objectProp, contentId, savedFile, originalFilename, sha256sum1);
|
|
|
-
|
|
|
- UploadFileRet uploadFileRet = fileProcessor.process(objectResult, channelId);
|
|
|
- if (uploadFileRet == null) {
|
|
|
- String url = objectNameService.getObjectUrl(objectResult.getObjectName());
|
|
|
- uploadFileRet = new UploadFileRet(objectResult.getObjectId(), url, false);
|
|
|
- }
|
|
|
-
|
|
|
- // TODO PutMessageConverter 中生成的文件需要显式删除
|
|
|
- FileUtils.deleteQuietly(file);
|
|
|
- return WebResult.success(uploadFileRet);
|
|
|
- } catch (Exception e) {
|
|
|
- FileUtils.deleteQuietly(file);
|
|
|
- String errMsg = e.getMessage();
|
|
|
- return WebResult.failWithMsg(errMsg);
|
|
|
- }
|
|
|
+ return WebResult.failWithMsg("not implement");
|
|
|
}
|
|
|
|
|
|
@AuthUser
|
|
|
@PostMapping(value = "/")
|
|
|
public String postObject(MultipartFile file, String objectName, String sha256sum, Integer channelId) throws Exception {
|
|
|
+ /* permission check */
|
|
|
String token = ServletUtil.getBearerToken();
|
|
|
if (token == null) {
|
|
|
return WebResult.failWithMsg("no token in request");
|
|
|
@@ -142,6 +75,7 @@ public class ObjectUploadController {
|
|
|
return WebResult.failWithMsg("user not match in token");
|
|
|
}
|
|
|
|
|
|
+ /* channel validate */
|
|
|
String contentId = UUID.randomUUID().toString().replace("-", "");
|
|
|
long size = file.getSize();
|
|
|
File savedFile = fileStoreService.saveFile(file.getInputStream(), contentId, size);
|
|
|
@@ -151,21 +85,27 @@ public class ObjectUploadController {
|
|
|
return WebResult.failWithMsg("the format or size of upload file error");
|
|
|
}
|
|
|
|
|
|
+ /* store file */
|
|
|
String sha256sum1 = DigestUtil.sha256sum(savedFile.getAbsolutePath());
|
|
|
- String originalFilename = file.getOriginalFilename();
|
|
|
- if (originalFilename == null) {
|
|
|
- originalFilename = "";
|
|
|
+ String filename = file.getOriginalFilename();
|
|
|
+ if (filename == null) {
|
|
|
+ filename = "";
|
|
|
}
|
|
|
|
|
|
- String suffix = StringUtil.getSuffix(originalFilename);
|
|
|
- ObjectProp objectProp = objectNameService.getObjectProp(channelId, suffix);
|
|
|
- ObjectResult objectResult = putObjectService.putObject(objectProp, contentId, savedFile, originalFilename, sha256sum1);
|
|
|
-
|
|
|
+ ObjectProp objectProp = objectNameService.getObjectProp(channelId, filename);
|
|
|
+ ObjectResult objectResult = putObjectService.putObject(objectProp, contentId, savedFile, filename, sha256sum1);
|
|
|
UploadFileRet uploadFileRet = fileProcessor.process(objectResult, channelId);
|
|
|
if (uploadFileRet == null) {
|
|
|
- String url = objectNameService.getObjectUrl(objectResult.getObjectName());
|
|
|
- uploadFileRet = new UploadFileRet(objectResult.getObjectId(), url, false);
|
|
|
+ putObjectService.deleteObject(objectResult.getObjectId());
|
|
|
+ return WebResult.fail();
|
|
|
}
|
|
|
return WebResult.success(uploadFileRet);
|
|
|
}
|
|
|
+
|
|
|
+ @AuthUser
|
|
|
+ @DeleteMapping(value = "/")
|
|
|
+ public String deleteObject(String objectId) {
|
|
|
+ putObjectService.deleteObject(objectId);
|
|
|
+ return WebResult.success();
|
|
|
+ }
|
|
|
}
|