| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- 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.common.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.SysUser;
- import org.dromara.system.domain.vo.SysOssVo;
- import org.dromara.system.service.ISysOssService;
- import org.dromara.system.service.ISysUserService;
- import org.springframework.http.MediaType;
- import org.springframework.web.bind.annotation.*;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.*;
- import java.util.concurrent.ConcurrentHashMap;
- /**
- * @Author: Huanyi
- * @Description: 由于 WPS 服务对于版本管理非常的麻烦,版本回退一直没有很好的解决方案,因此直接由我们自己负责管理版本。
- */
- @Deprecated
- @RestController
- @RequestMapping("/wps/callback/v3/3rd")
- @RequiredArgsConstructor
- @Slf4j
- public class WpsController extends BaseController {
- /**
- * 这里为版本控制缓存
- */
- 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 String documentId, HttpServletRequest request) {
- String[] ids = documentId.split("_");
- WpsVersionControlDTO dto = DOCUMENT_MAP.get(Long.valueOf(ids[0]));
- File tempFile = null;
- try {
- tempFile = File.createTempFile(dto.getFileName() + "_", dto.getSuffix());
- try (InputStream in = request.getInputStream();
- FileOutputStream out = new FileOutputStream(tempFile)) {
- byte[] buffer = new byte[8192];
- int bytesRead;
- while ((bytesRead = in.read(buffer)) != -1) {
- out.write(buffer, 0, bytesRead);
- }
- }
- // 防止自增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]))) {
- // FIXME 由于原始版本不能被删除,暂时先不实现: 每新增一个版本,就将原始版本上传一个全新的版本
- // 旧的版本可以不需要了,只需要新的版本
- // storage.delete(version.getUrl());
- version.setFileName(uploadResult.getFilename());
- version.setUrl(uploadResult.getUrl());
- version.setUpdateTime(new Date());
- }
- }
- } catch (IOException e) {
- log.error("文件写入失败, documentId={}", documentId, e);
- } finally {
- if (tempFile != null && tempFile.exists()) {
- boolean deleted = tempFile.delete();
- if (!deleted) {
- log.warn("临时文件删除失败: {}", tempFile.getAbsolutePath());
- }
- }
- }
- }
- private record WpsFilesCompleteRequestDto(
- String file_id, Object request, String name, Integer size, Map<String, String> digest, Boolean is_manual, Object response, Integer status_code
- ) {}
- private record WpsFilesCompleteResponseDto(
- String id, String name, Integer version, Integer size, Integer create_time, Integer modify_time, String creator_id, String modifier_id
- ) {}
- @PostMapping("/files/{documentId}/upload/complete")
- 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")), 1, Integer.valueOf(String.valueOf(request.get("size"))), Math.toIntExact(System.currentTimeMillis() / 1000L), Math.toIntExact(System.currentTimeMillis() / 1000L), "1", "1"
- )
- );
- }
- private record WpsUplaodPrepareDto(String[] digest_types) {}
- @GetMapping("/files/{documentId}/upload/prepare")
- public WpsR<WpsUplaodPrepareDto> uploadPrepare(@PathVariable String documentId) {
- String[] types = {"md5"};
- return WpsR.ok(new WpsUplaodPrepareDto(types));
- }
- private record WpsUploadAddressRequestDto(
- String file_id, String name, Integer size, Map<String, String> digest, Boolean is_manual, Integer attachment_size, String content_type
- ) {}
- private record WpsUploadAddressResponseDto(String method, String url) {}
- @PostMapping("/files/{documentId}/upload/address")
- 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));
- }
- private record WpsGetFileInfoDto(
- String id, String name, Integer version, Integer size, Integer create_time, Integer modify_time, String creator_id, String modifier_id
- ) {}
- @GetMapping("/files/{documentId}")
- 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(
- documentId, dto.getFileName(), 1, dto.getSize(), Math.toIntExact(new Date().getTime() / 1000L), Math.toIntExact(new Date().getTime() / 1000L), dto.getCreator(), dto.getUpdator()
- ));
- }
- 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 String documentId) {
- return WpsR.ok(
- new WpsFilesPermissionDto(
- "1", 1, 1, 1, 1, 1, 1, 1, 1, 1
- )
- );
- }
- private record WpsGetUrlDto(String url) {}
- @GetMapping("/files/{documentId}/download")
- 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()));
- }
- 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) {}
- @GetMapping("/users")
- public WpsR getUsers(@RequestParam("user_ids") Long[] userIds) {
- if (userIds == null || userIds.length == 0) {
- return WpsR.ok();
- }
- List<SysUser> vos = userService.selectUserByIds(Arrays.stream(userIds).toList());
- List<Long> ossIds = new ArrayList<>();
- vos.forEach(e -> ossIds.add(e.getAvatar()));
- List<SysOssVo> ossVos = ossService.queryListByIds(ossIds);
- List<WpsGetUsersDto> dtos = new ArrayList<>();
- for (SysUser vo : vos) {
- String url = "";
- for (SysOssVo ossVo : ossVos) {
- if (ossVo.getOssId().equals(vo.getAvatar())) {
- url = ossVo.getUrl();
- }
- }
- dtos.add(new WpsGetUsersDto(String.valueOf(vo.getUserId()), vo.getNickName(), url));
- }
- return WpsR.ok(dtos);
- }
- @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(currentVersion);
- }
- /**
- * 已废弃
- */
- @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]);
- log.info("文档编号为 {}, 版本号为 : {}", documentId, version);
- WpsVersionControlDTO dto = DOCUMENT_MAP.get(documentId);
- List<WpsVersionDTO> versions = dto.getVersions();
- for (WpsVersionDTO versionDto : versions) {
- log.info("当前轮询的版本号为 {}", versionDto.getVersion());
- 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("审核内容保存失败");
- }
- @DeleteMapping("/cancel/{documentId}")
- public R<Void> cancel(@PathVariable Long documentId) {
- DOCUMENT_MAP.remove(documentId);
- return R.ok();
- }
- }
|