diff --git a/gringotts-frontend/components/constants.js b/gringotts-frontend/components/constants.js index 176d024..8f70f83 100644 --- a/gringotts-frontend/components/constants.js +++ b/gringotts-frontend/components/constants.js @@ -1,5 +1,5 @@ export const information = { - debug: false, + debug: true, // baseUrl: '', baseUrl: 'http://127.0.0.1:20080', title: '可信供给中心', @@ -347,7 +347,12 @@ const formInputFileStaticColumns = [ type: 'action', label: '下载', level: 'link', - icon: 'fa fa-download' + icon: 'fa fa-download', + actionType: 'ajax', + api: { + ...apiGet('${base}/upload/download/${id}'), + responseType: 'blob', + } } ] } diff --git a/gringotts-frontend/components/ware/dialog-ware.js b/gringotts-frontend/components/ware/dialog-ware.js index 2c90fec..bcc524e 100644 --- a/gringotts-frontend/components/ware/dialog-ware.js +++ b/gringotts-frontend/components/ware/dialog-ware.js @@ -21,6 +21,10 @@ function detailForm() { name: 'icon', label: '图标', required: true, + receiver: apiPost("${base}/upload"), + autoFill: { + iconId: '${id}', + } }, { type: 'picker', @@ -75,6 +79,7 @@ function detailForm() { name: 'content', label: '商品详情', required: true, + receiver: '', options: { min_height: 300, } diff --git a/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/controller/DataFileController.java b/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/controller/DataFileController.java index eaf3067..b95ca16 100644 --- a/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/controller/DataFileController.java +++ b/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/controller/DataFileController.java @@ -1,10 +1,12 @@ package com.eshore.gringotts.web.domain.controller; import cn.hutool.core.io.FileUtil; +import cn.hutool.core.io.IoUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.crypto.SecureUtil; import com.eshore.gringotts.web.configuration.UploadConfiguration; import com.eshore.gringotts.web.configuration.amis.AmisResponse; +import com.eshore.gringotts.web.domain.entity.DataFile; import com.eshore.gringotts.web.domain.service.DataFileService; import com.fasterxml.jackson.annotation.JsonProperty; import java.io.ByteArrayInputStream; @@ -13,12 +15,15 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; +import javax.servlet.http.HttpServletResponse; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.eclipse.collections.api.list.ImmutableList; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -50,6 +55,40 @@ public class DataFileController { this.sliceFolderPath = StrUtil.format("{}/slice", uploadFolderPath); } + @PostMapping("") + public AmisResponse upload(@RequestParam("file") MultipartFile file) throws IOException { + String filename = file.getOriginalFilename(); + Long id = dataFileService.initialDataFile(filename); + String url = StrUtil.format("/upload/download/{}", id); + byte[] bytes = file.getBytes(); + String originMd5 = SecureUtil.md5(new ByteArrayInputStream(bytes)); + File targetFile = new File(StrUtil.format("{}/{}", uploadFolderPath, originMd5)); + if (targetFile.exists()) { + dataFileService.updateDataFile(id, FileUtil.getAbsolutePath(targetFile), FileUtil.size(targetFile), originMd5, file.getContentType()); + return AmisResponse.responseSuccess(new FinishResponse(id, filename, url, url)); + } + File cacheFile = new File(StrUtil.format("{}/{}", cacheFolderPath, id)); + cacheFile = FileUtil.writeBytes(bytes, cacheFile); + String targetMd5 = SecureUtil.md5(cacheFile); + if (!StrUtil.equals(originMd5, targetMd5)) { + throw new RuntimeException("文件上传失败,校验不匹配"); + } + FileUtil.move(cacheFile, targetFile, true); + dataFileService.updateDataFile(id, FileUtil.getAbsolutePath(targetFile), FileUtil.size(targetFile), targetMd5, file.getContentType()); + return AmisResponse.responseSuccess(new FinishResponse(id, filename, url, url)); + } + + @GetMapping("/download/{id}") + public void download(@PathVariable Long id, HttpServletResponse response) throws IOException { + DataFile dataFile = dataFileService.detailOrThrow(id); + File targetFile = new File(dataFile.getPath()); + response.setHeader("Access-Control-Expose-Headers", "Content-Type"); + response.setHeader("Content-Type", dataFile.getType()); + response.setHeader("Access-Control-Expose-Headers", "Content-Disposition"); + response.setHeader("Content-Disposition", StrUtil.format("attachment; filename={}", dataFile.getFilename())); + IoUtil.copy(new FileInputStream(targetFile), response.getOutputStream()); + } + @PostMapping("/start") public AmisResponse start(@RequestBody StartRequest request) { logger.info("Request: {}", request); @@ -107,12 +146,19 @@ public class DataFileController { if (!targetFile.exists()) { FileUtil.move(cacheFile, targetFile, true); } - dataFileService.updateDataFile(request.uploadId, FileUtil.getAbsolutePath(targetFile), FileUtil.size(targetFile), SecureUtil.md5(targetFile)); + String absolutePath = FileUtil.getAbsolutePath(targetFile); + dataFileService.updateDataFile( + request.uploadId, + absolutePath, + FileUtil.size(targetFile), + SecureUtil.md5(targetFile), + FileUtil.getMimeType(absolutePath) + ); return AmisResponse.responseSuccess(new FinishResponse( request.uploadId, request.filename, - request.uploadId, - StrUtil.format("https://localhost:9090") + request.uploadId.toString(), + StrUtil.format("/upload/download/{}", request.uploadId) )); } else { throw new RuntimeException("合并文件失败"); @@ -166,7 +212,7 @@ public class DataFileController { public static final class FinishResponse { private Long id; private String filename; - private Long value; + private String value; private String url; } } diff --git a/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/controller/WareController.java b/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/controller/WareController.java index 7b54f5d..408c61e 100644 --- a/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/controller/WareController.java +++ b/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/controller/WareController.java @@ -1,7 +1,6 @@ package com.eshore.gringotts.web.domain.controller; import com.eshore.gringotts.web.domain.base.controller.SimpleControllerSupport; -import com.eshore.gringotts.web.domain.base.entity.FileInfo; import com.eshore.gringotts.web.domain.base.entity.SimpleListItem; import com.eshore.gringotts.web.domain.base.entity.SimpleSaveItem; import com.eshore.gringotts.web.domain.entity.Ware; @@ -36,9 +35,9 @@ public class WareController extends SimpleControllerSupport { return dataFileRepository.save(dataFile).getId(); } - public void updateDataFile(Long id, String path, Long size, String md5) { + public void updateDataFile(Long id, String path, Long size, String md5, String type) { DataFile dataFile = dataFileRepository.findById(id).orElseThrow(UpdateDataFileFailedException::new); dataFile.setSize(size); dataFile.setMd5(md5); dataFile.setPath(path); + dataFile.setType(type); User loginUser = userService.currentLoginUser(); dataFile.setModifiedUser(loginUser); dataFileRepository.save(dataFile);