Archived
feat: configurable download origin (gmp.download-origin), defaults to path-only
This commit is contained in:
@@ -2,6 +2,7 @@ package com.gmp.mock.controller;
|
|||||||
|
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
@@ -19,7 +20,6 @@ import com.gmp.mock.service.DocxService;
|
|||||||
import com.gmp.mock.service.DocxService.CachedResult;
|
import com.gmp.mock.service.DocxService.CachedResult;
|
||||||
import com.gmp.mock.service.FileCacheService;
|
import com.gmp.mock.service.FileCacheService;
|
||||||
|
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/docx")
|
@RequestMapping("/api/docx")
|
||||||
@@ -27,10 +27,13 @@ public class DocxController {
|
|||||||
|
|
||||||
private final DocxService docxService;
|
private final DocxService docxService;
|
||||||
private final FileCacheService fileCache;
|
private final FileCacheService fileCache;
|
||||||
|
private final String downloadOrigin;
|
||||||
|
|
||||||
public DocxController(DocxService docxService, FileCacheService fileCache) {
|
public DocxController(DocxService docxService, FileCacheService fileCache,
|
||||||
|
@Value("${gmp.download-origin:}") String downloadOrigin) {
|
||||||
this.docxService = docxService;
|
this.docxService = docxService;
|
||||||
this.fileCache = fileCache;
|
this.fileCache = fileCache;
|
||||||
|
this.downloadOrigin = downloadOrigin;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -41,23 +44,23 @@ public class DocxController {
|
|||||||
* "code": 0,
|
* "code": 0,
|
||||||
* "msg": "success",
|
* "msg": "success",
|
||||||
* "data": {
|
* "data": {
|
||||||
* "url": "http://localhost:6666/api/docx/download?id=xxxx",
|
* "url": "/api/docx/download?id=xxxx",
|
||||||
* "fileName": "annualReport.docx"
|
* "fileName": "annualReport.docx"
|
||||||
* }
|
* }
|
||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
@PostMapping("/export")
|
@PostMapping("/export")
|
||||||
public ResponseEntity<ApiResponse<ExportResultData>> exportDocx(
|
public ResponseEntity<ApiResponse<ExportResultData>> exportDocx(
|
||||||
@Valid @RequestBody DocxExportRequest request,
|
@Valid @RequestBody DocxExportRequest request
|
||||||
HttpServletRequest httpRequest
|
|
||||||
) {
|
) {
|
||||||
CachedResult result = docxService.export(request.getExportId(), request.getBusinessData());
|
CachedResult result = docxService.export(request.getExportId(), request.getBusinessData());
|
||||||
|
|
||||||
// 构造下载 URL
|
// 构造下载 URL — 如果配置了 gmp.download-origin 则拼上 origin,否则只返回路径
|
||||||
String baseUrl = getBaseUrl(httpRequest);
|
String url = downloadOrigin.isBlank()
|
||||||
String downloadUrl = baseUrl + "/api/docx/download?id=" + result.key();
|
? "/api/docx/download?id=" + result.key()
|
||||||
|
: downloadOrigin + "/api/docx/download?id=" + result.key();
|
||||||
|
|
||||||
ExportResultData data = new ExportResultData(downloadUrl, result.fileName());
|
ExportResultData data = new ExportResultData(url, result.fileName());
|
||||||
return ResponseEntity.ok(ApiResponse.ok(data));
|
return ResponseEntity.ok(ApiResponse.ok(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,8 +77,6 @@ public class DocxController {
|
|||||||
.body(ApiResponse.error(404, "文件不存在或已过期: " + key));
|
.body(ApiResponse.error(404, "文件不存在或已过期: " + key));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 从缓存文件名可以推断,但我们不需要 fileName,因为下载时由客户端决定。
|
|
||||||
// 这里固定一个通用的 Content-Disposition
|
|
||||||
return ResponseEntity.ok()
|
return ResponseEntity.ok()
|
||||||
.contentType(MediaType.parseMediaType(
|
.contentType(MediaType.parseMediaType(
|
||||||
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"))
|
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"))
|
||||||
@@ -83,14 +84,4 @@ public class DocxController {
|
|||||||
"attachment; filename=\"document.docx\"")
|
"attachment; filename=\"document.docx\"")
|
||||||
.body(fileBytes);
|
.body(fileBytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getBaseUrl(HttpServletRequest request) {
|
|
||||||
String scheme = request.getScheme();
|
|
||||||
String host = request.getServerName();
|
|
||||||
int port = request.getServerPort();
|
|
||||||
if ((scheme.equals("http") && port == 80) || (scheme.equals("https") && port == 443)) {
|
|
||||||
return scheme + "://" + host;
|
|
||||||
}
|
|
||||||
return scheme + "://" + host + ":" + port;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user