|
@@ -0,0 +1,141 @@
|
|
|
+package edu.travel.upload.web;
|
|
|
+
|
|
|
+import com.obs.services.ObsClient;
|
|
|
+import com.obs.services.model.*;
|
|
|
+import edu.travel.remote.upload.UploadRemoteController;
|
|
|
+import edu.travel.remote.upload.dto.EduFileBlobDTO;
|
|
|
+import edu.travel.remote.upload.dto.EduFileDTO;
|
|
|
+import edu.travel.rpc.RPCBaseResponse;
|
|
|
+import edu.travel.upload.entity.EduFile;
|
|
|
+import edu.travel.upload.entity.EduFileBlob;
|
|
|
+import edu.travel.upload.obs.property.ObsProperties;
|
|
|
+import edu.travel.upload.service.EduFileBlobService;
|
|
|
+import edu.travel.upload.service.EduFileService;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+import org.springframework.security.access.prepost.PreAuthorize;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.Serializable;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.List;
|
|
|
+import java.util.UUID;
|
|
|
+import java.util.concurrent.ThreadPoolExecutor;
|
|
|
+
|
|
|
+@RestController
|
|
|
+@RequestMapping("/upload")
|
|
|
+public class UploadController implements UploadRemoteController {
|
|
|
+ @Autowired
|
|
|
+ private EduFileService eduFileService;
|
|
|
+ @Autowired
|
|
|
+ private EduFileBlobService eduFileBlobService;
|
|
|
+ @Autowired
|
|
|
+ private ObsClient obsClient;
|
|
|
+ @Autowired
|
|
|
+ private ObsProperties obsProperties;
|
|
|
+ Logger logger = LoggerFactory.getLogger(UploadController.class);
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @PostMapping("/init")
|
|
|
+ public RPCBaseResponse<String> initializeUpload() {
|
|
|
+ String uploadId = UUID.randomUUID().toString()+System.currentTimeMillis();
|
|
|
+ return RPCBaseResponse.success(uploadId);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 文件是否存在
|
|
|
+ * @param md5
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @GetMapping("/exits/md5")
|
|
|
+ @PreAuthorize("permitAll()")
|
|
|
+ public RPCBaseResponse<Serializable> exitsFile(@RequestParam("md5") String md5) {
|
|
|
+ return eduFileService.exitsFile(md5);
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 上传过哪些分片
|
|
|
+ * @param md5
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @GetMapping("/exits/blob")
|
|
|
+ @PreAuthorize("permitAll()")
|
|
|
+ public RPCBaseResponse<List<Integer>> exitsBlob(@RequestParam("md5") String md5) {
|
|
|
+ return eduFileBlobService.exitsBlob(md5);
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 上传大文件分片
|
|
|
+ * @param file
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @PostMapping(value = "/uploadBlob",headers = {"content-type=multipart/form-data"})
|
|
|
+ @PreAuthorize("permitAll()")
|
|
|
+ public RPCBaseResponse uploadBigFile(EduFileBlobDTO eduFileBlob, MultipartFile file) throws IOException {
|
|
|
+ EduFileBlob fileBlob = new EduFileBlob();
|
|
|
+ BeanUtils.copyProperties(eduFileBlob, fileBlob);
|
|
|
+ boolean exists = obsClient.headBucket(obsProperties.getBucketName());
|
|
|
+ if (!exists) {
|
|
|
+ // 若不存在,则创建桶
|
|
|
+ HeaderResponse response = obsClient.createBucket(obsProperties.getBucketName());
|
|
|
+ logger.info("创建桶成功" + response.getRequestId());
|
|
|
+ }
|
|
|
+ obsClient.putObject(obsProperties.getBucketName(),"/service/chunk/"+eduFileBlob.getUploadId()+"/"+fileBlob.getBlobNum(),file.getInputStream());
|
|
|
+ boolean save = eduFileBlobService.save(fileBlob);
|
|
|
+ if (save){
|
|
|
+ return RPCBaseResponse.success();
|
|
|
+ }
|
|
|
+ return RPCBaseResponse.error();
|
|
|
+
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 合并大文件
|
|
|
+ * @param eduFileDTO
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @PostMapping(value = "/mergeFile")
|
|
|
+ @PreAuthorize("permitAll()")
|
|
|
+ public RPCBaseResponse mergeFile(EduFileDTO eduFileDTO) {
|
|
|
+ CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(obsProperties.getBucketName(),
|
|
|
+ "/service/chunk/"+eduFileDTO.getUploadId(), eduFileDTO.getUploadId(), null);
|
|
|
+ CompleteMultipartUploadResult completeMultipartUploadResult = obsClient.completeMultipartUpload(completeMultipartUploadRequest);
|
|
|
+ String objectUrl = completeMultipartUploadResult.getObjectUrl();
|
|
|
+ EduFile eduFile = new EduFile();
|
|
|
+ BeanUtils.copyProperties(eduFileDTO, eduFile);
|
|
|
+ eduFile.setFilePath(objectUrl);
|
|
|
+ boolean save = eduFileService.save(eduFile);
|
|
|
+ if (save){
|
|
|
+ return RPCBaseResponse.success();
|
|
|
+ }
|
|
|
+ return RPCBaseResponse.error();
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 上传小文件
|
|
|
+ * @param file
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @PostMapping(value = "/upload",headers = {"content-type=multipart/form-data"})
|
|
|
+ @PreAuthorize("permitAll()")
|
|
|
+ public RPCBaseResponse uploadFile(EduFileDTO eduFile,MultipartFile file) {
|
|
|
+ PutObjectResult putObjectResult = obsClient.putObject(obsProperties.getBucketName(), "/service/chunk/" + eduFile.getUploadId() + "/" + file.getOriginalFilename(), file.getInputStream());
|
|
|
+ String objectUrl = putObjectResult.getObjectUrl();
|
|
|
+ EduFile files = new EduFile();
|
|
|
+ BeanUtils.copyProperties(files, eduFile);
|
|
|
+ eduFile.setFilePath(objectUrl);
|
|
|
+ boolean save = eduFileService.save(files);
|
|
|
+ if (save){
|
|
|
+ return RPCBaseResponse.success();
|
|
|
+ }
|
|
|
+ return RPCBaseResponse.error();
|
|
|
+ }
|
|
|
+}
|