fix(项目文件更新): 修复项目文件更新时不能上传同名文件

This commit is contained in:
Captain.B 2021-04-01 13:33:11 +08:00 committed by BugKing
parent f7e0c65074
commit 51b6ea2850
4 changed files with 24 additions and 11 deletions

View File

@ -91,9 +91,9 @@ public class ProjectController {
return projectService.uploadFiles(projectId, files); return projectService.uploadFiles(projectId, files);
} }
@PostMapping(value = "/update/file/{projectId}/{fileId}", consumes = {"multipart/form-data"}) @PostMapping(value = "/update/file/{fileId}", consumes = {"multipart/form-data"})
public FileMetadata updateFile(@PathVariable String projectId, @PathVariable String fileId, @RequestPart(value = "file") MultipartFile file) { public FileMetadata updateFile(@PathVariable String fileId, @RequestPart(value = "file") MultipartFile file) {
return projectService.updateFile(projectId, fileId, file); return projectService.updateFile(fileId, file);
} }
@GetMapping(value = "delete/file/{fileId}") @GetMapping(value = "delete/file/{fileId}")

View File

@ -43,6 +43,13 @@ public class FileService {
return fileContentMapper.selectByPrimaryKey(fileId); 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<String> ids) { public void deleteFileByIds(List<String> ids) {
if (CollectionUtils.isEmpty(ids)) { if (CollectionUtils.isEmpty(ids)) {
return; return;

View File

@ -28,6 +28,7 @@ import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
@ -186,16 +187,21 @@ public class ProjectService {
return result; return result;
} }
public FileMetadata updateFile(String projectId, String fileId, MultipartFile file) { public FileMetadata updateFile( String fileId, MultipartFile file) {
QueryProjectFileRequest request = new QueryProjectFileRequest(); QueryProjectFileRequest request = new QueryProjectFileRequest();
request.setName(file.getOriginalFilename()); request.setName(file.getOriginalFilename());
if (CollectionUtils.isEmpty(fileService.getProjectFiles(projectId, request))) { FileMetadata fileMetadata = fileService.getFileMetadataById(fileId);
fileService.deleteFileById(fileId); if (fileMetadata != null) {
return fileService.saveFile(file, projectId); fileMetadata.setSize(file.getSize());
} else { fileMetadata.setUpdateTime(System.currentTimeMillis());
MSException.throwException(Translator.get("project_file_already_exists")); fileService.updateFileMetadata(fileMetadata);
try {
fileService.setFileContent(fileId, file.getBytes());
} catch (IOException e) {
MSException.throwException(e);
}
} }
return null; return fileMetadata;
} }
public void deleteFile(String fileId) { public void deleteFile(String fileId) {

View File

@ -167,7 +167,7 @@ export default {
} }
let formData = new FormData(); 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); formData.append("file", file);
let options = { let options = {
method: 'POST', method: 'POST',