|
|
@@ -1,22 +1,29 @@
|
|
|
package com.yingpaipay.business.controller;
|
|
|
|
|
|
import com.yingpaipay.business.domain.Document;
|
|
|
+import com.yingpaipay.business.domain.dto.WpsGetVersionsDto;
|
|
|
import com.yingpaipay.business.domain.dto.WpsR;
|
|
|
+import com.yingpaipay.business.domain.dto.WpsVersionControlDto;
|
|
|
+import com.yingpaipay.business.domain.dto.WpsVersionDto;
|
|
|
import com.yingpaipay.business.service.impl.CommonDocumentService;
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.dromara.common.core.domain.R;
|
|
|
+import org.dromara.common.core.exception.BusinessException;
|
|
|
+import org.dromara.common.core.utils.StringUtils;
|
|
|
+import org.dromara.common.json.utils.JsonUtils;
|
|
|
+import org.dromara.common.oss.core.OssClient;
|
|
|
+import org.dromara.common.oss.entity.UploadResult;
|
|
|
+import org.dromara.common.oss.factory.OssFactory;
|
|
|
import org.dromara.common.web.core.BaseController;
|
|
|
+import org.dromara.system.domain.SysOssExt;
|
|
|
import org.dromara.system.domain.vo.SysOssVo;
|
|
|
import org.dromara.system.domain.vo.SysUserVo;
|
|
|
import org.dromara.system.service.ISysOssService;
|
|
|
import org.dromara.system.service.ISysUserService;
|
|
|
-import org.springframework.http.HttpMethod;
|
|
|
import org.springframework.http.MediaType;
|
|
|
-import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
-import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
import java.io.File;
|
|
|
import java.io.FileOutputStream;
|
|
|
@@ -31,18 +38,23 @@ import java.util.concurrent.ConcurrentHashMap;
|
|
|
@Slf4j
|
|
|
public class WpsController extends BaseController {
|
|
|
|
|
|
- private static final Map<Long, Long> DOCUMENT_MAP = new ConcurrentHashMap<>();
|
|
|
+ /**
|
|
|
+ * 这里为版本控制缓存
|
|
|
+ */
|
|
|
+ private static final Map<Long, WpsVersionControlDto> DOCUMENT_MAP = new ConcurrentHashMap<>();
|
|
|
|
|
|
private final ISysOssService ossService;
|
|
|
private final CommonDocumentService documentService;
|
|
|
private final ISysUserService userService;
|
|
|
|
|
|
@PutMapping(value = "/upload/file/{documentId}", consumes = MediaType.APPLICATION_OCTET_STREAM_VALUE)
|
|
|
- public void upload(@PathVariable Long documentId, HttpServletRequest request) {
|
|
|
+ public void upload(@PathVariable String documentId, HttpServletRequest request) {
|
|
|
+ String[] ids = documentId.split("_");
|
|
|
+ WpsVersionControlDto dto = DOCUMENT_MAP.get(Long.valueOf(ids[0]));
|
|
|
File tempFile = null;
|
|
|
try {
|
|
|
|
|
|
- tempFile = File.createTempFile("upload_doc_" + documentId + "_", ".tmp");
|
|
|
+ tempFile = File.createTempFile(dto.getFileName() + "_", dto.getSuffix());
|
|
|
|
|
|
try (InputStream in = request.getInputStream();
|
|
|
FileOutputStream out = new FileOutputStream(tempFile)) {
|
|
|
@@ -53,8 +65,22 @@ public class WpsController extends BaseController {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- SysOssVo vo = ossService.upload(tempFile);
|
|
|
- DOCUMENT_MAP.put(documentId, vo.getOssId());
|
|
|
+ // TODO 防止自增ID过早炸掉,这里采用更新操作,不使用自带的图片上传
|
|
|
+ String originalfileName = tempFile.getName();
|
|
|
+ String suffix = StringUtils.substring(originalfileName, originalfileName.lastIndexOf("."));
|
|
|
+ OssClient storage = OssFactory.instance();
|
|
|
+ UploadResult uploadResult = storage.uploadSuffix(tempFile, suffix, originalfileName);
|
|
|
+ dto.setCurrentVersion(Long.parseLong(ids[1]));
|
|
|
+ List<WpsVersionDto> versions = dto.getVersions();
|
|
|
+ for (WpsVersionDto version : versions) {
|
|
|
+ if (Objects.equals(version.getVersion(), Long.valueOf(ids[1]))) {
|
|
|
+ version.setFileName(uploadResult.getFilename());
|
|
|
+ version.setUrl(uploadResult.getUrl());
|
|
|
+ version.setUpdateTime(new Date());
|
|
|
+ }
|
|
|
+ }
|
|
|
+// dto.getVersions().get(Integer.parseInt(ids[1])).setFileName(uploadResult.getFilename());
|
|
|
+// dto.getVersions().get(Integer.parseInt(ids[1])).setUrl(uploadResult.getUrl());
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
log.error("文件写入失败, documentId={}", documentId, e);
|
|
|
@@ -76,11 +102,11 @@ public class WpsController extends BaseController {
|
|
|
) {}
|
|
|
|
|
|
@PostMapping("/files/{documentId}/upload/complete")
|
|
|
- public WpsR complete(@PathVariable Long documentId, @RequestBody WpsFilesCompleteRequestDto dto) {
|
|
|
+ public WpsR complete(@PathVariable String documentId, @RequestBody WpsFilesCompleteRequestDto dto) {
|
|
|
Map<String, Object> request = (LinkedHashMap<String, Object>) dto.request;
|
|
|
return WpsR.ok(
|
|
|
new WpsFilesCompleteResponseDto(
|
|
|
- String.valueOf(request.get("file_id")), String.valueOf(request.get("name")), 2, Integer.valueOf(String.valueOf(request.get("size"))), Math.toIntExact(new Date().getTime() / 1000L), Math.toIntExact(new Date().getTime() / 1000L), "1", "1"
|
|
|
+ String.valueOf(request.get("file_id")), String.valueOf(request.get("name")), 1, Integer.valueOf(String.valueOf(request.get("size"))), Math.toIntExact(System.currentTimeMillis() / 1000L), Math.toIntExact(System.currentTimeMillis() / 1000L), "1", "1"
|
|
|
)
|
|
|
);
|
|
|
}
|
|
|
@@ -88,7 +114,7 @@ public class WpsController extends BaseController {
|
|
|
private record WpsUplaodPrepareDto(String[] digest_types) {}
|
|
|
|
|
|
@GetMapping("/files/{documentId}/upload/prepare")
|
|
|
- public WpsR<WpsUplaodPrepareDto> uploadPrepare(@PathVariable Long documentId) {
|
|
|
+ public WpsR<WpsUplaodPrepareDto> uploadPrepare(@PathVariable String documentId) {
|
|
|
String[] types = {"md5"};
|
|
|
return WpsR.ok(new WpsUplaodPrepareDto(types));
|
|
|
}
|
|
|
@@ -100,7 +126,7 @@ public class WpsController extends BaseController {
|
|
|
private record WpsUploadAddressResponseDto(String method, String url) {}
|
|
|
|
|
|
@PostMapping("/files/{documentId}/upload/address")
|
|
|
- public WpsR uploadAddress(@PathVariable Long documentId, @RequestBody WpsUploadAddressRequestDto dto) {
|
|
|
+ public WpsR uploadAddress(@PathVariable String documentId, @RequestBody WpsUploadAddressRequestDto dto) {
|
|
|
return WpsR.ok(new WpsUploadAddressResponseDto("PUT", "http://yp1.yingpaipay.com:9029/wps/callback/v3/3rd/upload/file/" + documentId));
|
|
|
}
|
|
|
|
|
|
@@ -109,48 +135,29 @@ public class WpsController extends BaseController {
|
|
|
) {}
|
|
|
|
|
|
@GetMapping("/files/{documentId}")
|
|
|
- public WpsR<WpsGetFileInfoDto> getFileInfo(@PathVariable Long documentId) {
|
|
|
- Document document = documentService.getById(documentId);
|
|
|
- SysOssVo ossVo = ossService.getById(document.getOssId());
|
|
|
- String url = ossVo.getUrl();
|
|
|
-
|
|
|
- RestTemplate restTemplate = new RestTemplate();
|
|
|
- ResponseEntity<Void> response = restTemplate.exchange(
|
|
|
- url,
|
|
|
- HttpMethod.HEAD,
|
|
|
- null,
|
|
|
- Void.class
|
|
|
- );
|
|
|
-
|
|
|
- int fileSize = 0;
|
|
|
- if (response.getHeaders().getContentLength() > 0) {
|
|
|
- fileSize = (int) response.getHeaders().getContentLength();
|
|
|
- } else {
|
|
|
- log.warn("无法通过 HEAD 获取文件大小,fileUrl: {}", url);
|
|
|
+ public WpsR<WpsGetFileInfoDto> getFileInfo(@PathVariable String documentId) {
|
|
|
+ String[] ids = documentId.split("_");
|
|
|
+ long id = Long.parseLong(ids[0]);
|
|
|
+ long version = Long.parseLong(ids[1]);
|
|
|
+
|
|
|
+ if (DOCUMENT_MAP.containsKey(id)) {
|
|
|
+ WpsVersionControlDto dto = DOCUMENT_MAP.get(id);
|
|
|
+ return WpsR.ok(new WpsGetFileInfoDto(
|
|
|
+ documentId, dto.getFileName(), 1, dto.getSize(), Math.toIntExact(new Date().getTime() / 1000L), Math.toIntExact(new Date().getTime() / 1000L), dto.getCreator(), dto.getUpdator()
|
|
|
+ ));
|
|
|
}
|
|
|
-
|
|
|
+ WpsVersionControlDto dto = initDocument(id);
|
|
|
return WpsR.ok(new WpsGetFileInfoDto(
|
|
|
- String.valueOf(documentId), ossVo.getOriginalName(),
|
|
|
- 1,
|
|
|
- fileSize,
|
|
|
- Math.toIntExact(document.getCreateTime().getTime() / 1000L),
|
|
|
- Math.toIntExact(document.getUpdateTime().getTime() / 1000L),
|
|
|
- String.valueOf(document.getCreateBy()),
|
|
|
- String.valueOf(document.getUpdateBy())
|
|
|
+ documentId, dto.getFileName(), 1, dto.getSize(), Math.toIntExact(new Date().getTime() / 1000L), Math.toIntExact(new Date().getTime() / 1000L), dto.getCreator(), dto.getUpdator()
|
|
|
));
|
|
|
}
|
|
|
|
|
|
- @GetMapping("/files/{documentId}/watermark")
|
|
|
- public WpsR watermark(@PathVariable Long documentId) {
|
|
|
- return WpsR.ok();
|
|
|
- }
|
|
|
-
|
|
|
private record WpsFilesPermissionDto(
|
|
|
String user_id, Integer read, Integer update, Integer download, Integer rename, Integer history, Integer copy, Integer print, Integer saves, Integer comment
|
|
|
) {}
|
|
|
|
|
|
@GetMapping("/files/{documentId}/permission")
|
|
|
- public WpsR permission(@PathVariable Long documentId) {
|
|
|
+ public WpsR permission(@PathVariable String documentId) {
|
|
|
return WpsR.ok(
|
|
|
new WpsFilesPermissionDto(
|
|
|
"1", 1, 1, 1, 1, 1, 1, 1, 1, 1
|
|
|
@@ -161,14 +168,40 @@ public class WpsController extends BaseController {
|
|
|
private record WpsGetUrlDto(String url) {}
|
|
|
|
|
|
@GetMapping("/files/{documentId}/download")
|
|
|
- public WpsR getUrl(@PathVariable Long documentId) {
|
|
|
- if (DOCUMENT_MAP.containsKey(documentId)) {
|
|
|
- return WpsR.ok(new WpsGetUrlDto(ossService.getById(DOCUMENT_MAP.get(documentId)).getUrl()));
|
|
|
+ public WpsR getUrl(@PathVariable String documentId) {
|
|
|
+ String[] ids = documentId.split("_");
|
|
|
+ long id = Long.parseLong(ids[0]);
|
|
|
+ long version = Long.parseLong(ids[1]);
|
|
|
+
|
|
|
+ if (DOCUMENT_MAP.containsKey(id)) {
|
|
|
+ WpsVersionControlDto dto = DOCUMENT_MAP.get(id);
|
|
|
+ List<WpsVersionDto> versions = dto.getVersions();
|
|
|
+ for (WpsVersionDto versionDto : versions) {
|
|
|
+ if (versionDto.getVersion() == version) {
|
|
|
+ return WpsR.ok(new WpsGetUrlDto(versionDto.getUrl()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return WpsR.ok(new WpsGetUrlDto(dto.getVersions().get(0).getUrl()));
|
|
|
+ }
|
|
|
+
|
|
|
+ WpsVersionControlDto dto = initDocument(id);
|
|
|
+ List<WpsVersionDto> versions = dto.getVersions();
|
|
|
+ for (WpsVersionDto versionDto : versions) {
|
|
|
+ if (Objects.equals(versionDto.getVersion(), dto.getCurrentVersion())) {
|
|
|
+ return WpsR.ok(new WpsGetUrlDto(versionDto.getUrl()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return WpsR.ok(new WpsGetUrlDto(dto.getVersions().get(0).getUrl()));
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/files/{documentId}/versions/{version}/download")
|
|
|
+ public WpsR getUrlByVersion(@PathVariable String documentId, @PathVariable Integer version) {
|
|
|
+ if (DOCUMENT_MAP.containsKey(Long.valueOf(documentId.split("_")[0]))) {
|
|
|
+ WpsVersionControlDto dto = DOCUMENT_MAP.get(Long.valueOf(documentId.split("_")[0]));
|
|
|
+ return WpsR.ok(new WpsGetUrlDto(dto.getVersions().get(Integer.parseInt(documentId.split("_")[1])).getUrl()));
|
|
|
}
|
|
|
- Document document = documentService.getById(documentId);
|
|
|
- SysOssVo ossVo = ossService.getById(document.getOssId());
|
|
|
- String url = ossVo.getUrl();
|
|
|
- return WpsR.ok(new WpsGetUrlDto(url));
|
|
|
+ WpsVersionControlDto dto = initDocument(Long.valueOf(documentId.split("_")[0]));
|
|
|
+ return WpsR.ok(new WpsGetUrlDto(dto.getVersions().get(0).getUrl()));
|
|
|
}
|
|
|
|
|
|
private record WpsGetUsersDto(String id, String name, String avatar_url) {}
|
|
|
@@ -195,15 +228,118 @@ public class WpsController extends BaseController {
|
|
|
return WpsR.ok(dtos);
|
|
|
}
|
|
|
|
|
|
- @GetMapping("/{documentId}")
|
|
|
- public R<Long> getOssId(@PathVariable Long documentId) {
|
|
|
- return R.ok(DOCUMENT_MAP.remove(documentId));
|
|
|
+ @PostMapping(value = "/init/{documentId}")
|
|
|
+ public R init(@PathVariable Long documentId) {
|
|
|
+ if (DOCUMENT_MAP.containsKey(documentId)) {
|
|
|
+ throw new BusinessException("已有其他人正在审核该文件");
|
|
|
+ }
|
|
|
+ initDocument(documentId);
|
|
|
+ return R.ok(DOCUMENT_MAP.get(documentId).getCurrentVersion());
|
|
|
+ }
|
|
|
+
|
|
|
+ private WpsVersionControlDto initDocument(Long ossId) {
|
|
|
+ SysOssVo ossVo = ossService.getById(ossId);
|
|
|
+ Document document = documentService.getByOssId(ossId);
|
|
|
+ if (DOCUMENT_MAP.containsKey(document.getId())) {
|
|
|
+ throw new BusinessException("已有其他人正在审核该文件");
|
|
|
+ }
|
|
|
+ long currentVersion = System.currentTimeMillis();
|
|
|
+ List<WpsVersionDto> list = new ArrayList<>();
|
|
|
+ list.add(new WpsVersionDto(ossVo.getFileName(), 0L, ossVo.getUrl(), new Date(), new Date(), Math.toIntExact(document.getCreateTime().getTime() / 1000L), Math.toIntExact(document.getUpdateTime().getTime() / 1000L)));
|
|
|
+ list.add(new WpsVersionDto(ossVo.getFileName(), currentVersion, ossVo.getUrl(), new Date(), new Date(), Math.toIntExact(document.getCreateTime().getTime() / 1000L), Math.toIntExact(document.getUpdateTime().getTime() / 1000L)));
|
|
|
+ WpsVersionControlDto dto = new WpsVersionControlDto();
|
|
|
+ dto.setCurrentVersion(currentVersion);
|
|
|
+ dto.setVersions(list);
|
|
|
+ dto.setSuffix(ossVo.getFileSuffix());
|
|
|
+ dto.setCreator(String.valueOf(document.getCreateBy()));
|
|
|
+ dto.setUpdator(String.valueOf(document.getUpdateBy()));
|
|
|
+ dto.setFileName(ossVo.getOriginalName());
|
|
|
+ SysOssExt ext = JsonUtils.parseObject(ossVo.getExt1(), SysOssExt.class);
|
|
|
+ long size = ext.getFileSize();
|
|
|
+ dto.setSize((int) size);
|
|
|
+ DOCUMENT_MAP.put(ossId, dto);
|
|
|
+ return dto;
|
|
|
+ }
|
|
|
+
|
|
|
+ @PutMapping("/clean/{documentId}")
|
|
|
+ public R clean(@PathVariable Long documentId) {
|
|
|
+ WpsVersionControlDto dto = DOCUMENT_MAP.get(documentId);
|
|
|
+ long currentVersion = System.currentTimeMillis();
|
|
|
+ dto.getVersions().add(new WpsVersionDto(
|
|
|
+ dto.getVersions().get(0).getFileName(), currentVersion, dto.getVersions().get(0).getUrl(), new Date(), new Date(), Math.toIntExact(new Date().getTime() / 1000L), Math.toIntExact(new Date().getTime() / 1000L)
|
|
|
+ ));
|
|
|
+ dto.setCurrentVersion(currentVersion);
|
|
|
+ log.info("自行进行刷新版本");
|
|
|
+ dto.getVersions().forEach(e -> log.info("{}", JsonUtils.toJsonString(e)));
|
|
|
+ return R.ok();
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/files/{documentId}/versions")
|
|
|
+ public WpsR getVersions(@PathVariable Long documentId, @RequestParam(value = "offset", required = false) Integer offset, @RequestParam(value = "limit", required = false) Integer size) {
|
|
|
+ WpsVersionControlDto dto = DOCUMENT_MAP.get(documentId);
|
|
|
+ List<WpsVersionDto> versions = dto.getVersions();
|
|
|
+
|
|
|
+ List<WpsVersionDto> reversed = new ArrayList<>(versions);
|
|
|
+ Collections.reverse(reversed);
|
|
|
+
|
|
|
+ if (offset == null || size == null) {
|
|
|
+ return WpsR.ok(reversed);
|
|
|
+ }
|
|
|
+
|
|
|
+ int fromIndex = Math.min(offset, reversed.size());
|
|
|
+ int toIndex = Math.min(offset + size, reversed.size());
|
|
|
+
|
|
|
+ List<WpsVersionDto> paginated = reversed.subList(fromIndex, toIndex);
|
|
|
+
|
|
|
+ List<WpsGetVersionsDto> list = new ArrayList<>();
|
|
|
+ paginated.forEach(e -> list.add(new WpsGetVersionsDto(
|
|
|
+ String.valueOf(documentId), dto.getFileName(), Math.toIntExact(e.getVersion() / 1000L), dto.getSize(), e.getCreate_time(), e.getUpdate_time(), dto.getCreator(), dto.getUpdator()
|
|
|
+ )));
|
|
|
+
|
|
|
+ WpsR<List<WpsGetVersionsDto>> r = WpsR.ok(list);
|
|
|
+ String str = JsonUtils.toJsonString(r);
|
|
|
+ log.warn(str);
|
|
|
+ return r;
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/files/{documentId}/versions/{version}")
|
|
|
+ public WpsR getFileByVersion(@PathVariable Long documentId, @PathVariable Integer version) {
|
|
|
+ WpsVersionControlDto dto = DOCUMENT_MAP.get(documentId);
|
|
|
+ WpsVersionDto current = dto.getVersions().get(version - 1);
|
|
|
+ return WpsR.ok(new WpsGetVersionsDto(
|
|
|
+ String.valueOf(documentId), dto.getFileName(), Math.toIntExact(current.getVersion() / 1000L), dto.getSize(), current.getCreate_time(), current.getUpdate_time(), dto.getCreator(), dto.getUpdator()
|
|
|
+ ));
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/files/list")
|
|
|
+ public R<List<WpsVersionDto>> getVersionList(@RequestParam("ossId") Long ossId) {
|
|
|
+ List<WpsVersionDto> list = DOCUMENT_MAP.get(ossId).getVersions();
|
|
|
+ list.forEach(e -> log.info("{}", JsonUtils.toJsonString(e)));
|
|
|
+ return R.ok(list.stream().filter(e -> e.getVersion() != 0L).sorted((e1, e2) -> Math.toIntExact(e2.getVersion() - e1.getVersion())).toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/getFinal")
|
|
|
+ public R<SysOssVo> getFinal(@RequestParam("id") String id) {
|
|
|
+ String[] ids = id.split("_");
|
|
|
+ long documentId = Long.parseLong(ids[0]);
|
|
|
+ long version = Long.parseLong(ids[1]);
|
|
|
+
|
|
|
+ WpsVersionControlDto dto = DOCUMENT_MAP.get(documentId);
|
|
|
+ List<WpsVersionDto> versions = dto.getVersions();
|
|
|
+ for (WpsVersionDto versionDto : versions) {
|
|
|
+ if (versionDto.getVersion() == version) {
|
|
|
+ String url = versionDto.getUrl();
|
|
|
+ SysOssVo vo = ossService.insertByUrl(versionDto.getFileName(), dto.getFileName(), url);
|
|
|
+ DOCUMENT_MAP.remove(documentId);
|
|
|
+ return R.ok(vo);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ throw new BusinessException("审核内容保存失败");
|
|
|
}
|
|
|
|
|
|
- @PutMapping("/refresh/{documentId}")
|
|
|
- public R<Void> refresh(@PathVariable Long documentId) {
|
|
|
- Document document = documentService.getById(documentId);
|
|
|
- DOCUMENT_MAP.put(documentId, document.getOssId());
|
|
|
+ @DeleteMapping("/cancel/{documentId}")
|
|
|
+ public R<Void> cancel(@PathVariable Long documentId) {
|
|
|
+ DOCUMENT_MAP.remove(documentId);
|
|
|
return R.ok();
|
|
|
}
|
|
|
|