From b659206b0e8b6f3f42ae867b3392872cd4709a1f Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Thu, 1 Apr 2021 13:33:11 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E9=A1=B9=E7=9B=AE=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=9B=B4=E6=96=B0):=20=E4=BF=AE=E5=A4=8D=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=9B=B4=E6=96=B0=E6=97=B6=E4=B8=8D=E8=83=BD?= =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E5=90=8C=E5=90=8D=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ProjectController.java | 6 +++--- .../io/metersphere/service/FileService.java | 7 +++++++ .../metersphere/service/ProjectService.java | 20 ++++++++++++------- .../test/components/ResourceFiles.vue | 2 +- 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/backend/src/main/java/io/metersphere/controller/ProjectController.java b/backend/src/main/java/io/metersphere/controller/ProjectController.java index f9feec4b15..9f4321d986 100644 --- a/backend/src/main/java/io/metersphere/controller/ProjectController.java +++ b/backend/src/main/java/io/metersphere/controller/ProjectController.java @@ -91,9 +91,9 @@ public class ProjectController { return projectService.uploadFiles(projectId, files); } - @PostMapping(value = "/update/file/{projectId}/{fileId}", consumes = {"multipart/form-data"}) - public FileMetadata updateFile(@PathVariable String projectId, @PathVariable String fileId, @RequestPart(value = "file") MultipartFile file) { - return projectService.updateFile(projectId, fileId, file); + @PostMapping(value = "/update/file/{fileId}", consumes = {"multipart/form-data"}) + public FileMetadata updateFile(@PathVariable String fileId, @RequestPart(value = "file") MultipartFile file) { + return projectService.updateFile(fileId, file); } @GetMapping(value = "delete/file/{fileId}") diff --git a/backend/src/main/java/io/metersphere/service/FileService.java b/backend/src/main/java/io/metersphere/service/FileService.java index 817ca5826d..74c095bc0f 100644 --- a/backend/src/main/java/io/metersphere/service/FileService.java +++ b/backend/src/main/java/io/metersphere/service/FileService.java @@ -43,6 +43,13 @@ public class FileService { return fileContentMapper.selectByPrimaryKey(fileId); } + public void setFileContent(String fileId, byte[] content) { + FileContent record = new FileContent(); + record .setFile(content); + record.setFileId(fileId); + fileContentMapper.updateByPrimaryKeySelective(record); + } + public void deleteFileByIds(List ids) { if (CollectionUtils.isEmpty(ids)) { return; diff --git a/backend/src/main/java/io/metersphere/service/ProjectService.java b/backend/src/main/java/io/metersphere/service/ProjectService.java index 7fea391b86..d44beaa170 100644 --- a/backend/src/main/java/io/metersphere/service/ProjectService.java +++ b/backend/src/main/java/io/metersphere/service/ProjectService.java @@ -28,6 +28,7 @@ import org.springframework.util.CollectionUtils; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; +import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.UUID; @@ -186,16 +187,21 @@ public class ProjectService { return result; } - public FileMetadata updateFile(String projectId, String fileId, MultipartFile file) { + public FileMetadata updateFile( String fileId, MultipartFile file) { QueryProjectFileRequest request = new QueryProjectFileRequest(); request.setName(file.getOriginalFilename()); - if (CollectionUtils.isEmpty(fileService.getProjectFiles(projectId, request))) { - fileService.deleteFileById(fileId); - return fileService.saveFile(file, projectId); - } else { - MSException.throwException(Translator.get("project_file_already_exists")); + FileMetadata fileMetadata = fileService.getFileMetadataById(fileId); + if (fileMetadata != null) { + fileMetadata.setSize(file.getSize()); + fileMetadata.setUpdateTime(System.currentTimeMillis()); + fileService.updateFileMetadata(fileMetadata); + try { + fileService.setFileContent(fileId, file.getBytes()); + } catch (IOException e) { + MSException.throwException(e); + } } - return null; + return fileMetadata; } public void deleteFile(String fileId) { diff --git a/frontend/src/business/components/performance/test/components/ResourceFiles.vue b/frontend/src/business/components/performance/test/components/ResourceFiles.vue index b05920888b..880323d75c 100644 --- a/frontend/src/business/components/performance/test/components/ResourceFiles.vue +++ b/frontend/src/business/components/performance/test/components/ResourceFiles.vue @@ -167,7 +167,7 @@ export default { } let formData = new FormData(); - let url = '/project/update/file/' + this.projectId + '/' + this.currentRow.id + let url = '/project/update/file/' + '/' + this.currentRow.id formData.append("file", file); let options = { method: 'POST',