|
|
@@ -0,0 +1,197 @@
|
|
|
+package com.yingpaipay.business.controller;
|
|
|
+
|
|
|
+import com.yingpaipay.business.domain.Document;
|
|
|
+import com.yingpaipay.business.domain.dto.WpsR;
|
|
|
+import com.yingpaipay.business.service.impl.CommonDocumentService;
|
|
|
+import jakarta.servlet.http.HttpServletRequest;
|
|
|
+import lombok.Getter;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.Setter;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.dromara.common.core.utils.MapstructUtils;
|
|
|
+import org.dromara.common.web.core.BaseController;
|
|
|
+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;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.util.*;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
+
|
|
|
+@RestController
|
|
|
+@RequestMapping("/wps/callback/v3/3rd")
|
|
|
+@RequiredArgsConstructor
|
|
|
+@Slf4j
|
|
|
+public class WpsController extends BaseController {
|
|
|
+
|
|
|
+ private static final Map<Long, String> 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) {
|
|
|
+ File tempFile = null;
|
|
|
+ try {
|
|
|
+
|
|
|
+ tempFile = File.createTempFile("upload_doc_" + documentId + "_", ".tmp");
|
|
|
+
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ SysOssVo vo = ossService.upload(tempFile);
|
|
|
+ DOCUMENT_MAP.put(documentId, vo.getUrl());
|
|
|
+
|
|
|
+ } 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 Long 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"
|
|
|
+ )
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ private record WpsUplaodPrepareDto(String[] digest_types) {}
|
|
|
+
|
|
|
+ @GetMapping("/files/{documentId}/upload/prepare")
|
|
|
+ public WpsR<WpsUplaodPrepareDto> uploadPrepare(@PathVariable Long 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 Long 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 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);
|
|
|
+ }
|
|
|
+
|
|
|
+ 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())
|
|
|
+ ));
|
|
|
+ }
|
|
|
+
|
|
|
+ @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) {
|
|
|
+ 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 Long documentId) {
|
|
|
+ Document document = documentService.getById(documentId);
|
|
|
+ SysOssVo ossVo = ossService.getById(document.getOssId());
|
|
|
+ String url = ossVo.getUrl();
|
|
|
+ return WpsR.ok(new WpsGetUrlDto(url));
|
|
|
+ }
|
|
|
+
|
|
|
+ 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<SysUserVo> 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 (SysUserVo 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);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|