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

This commit is contained in:
Captain.B 2021-04-01 13:33:11 +08:00 committed by 刘瑞斌
parent 97df3b83aa
commit 0131d959f2
4 changed files with 24 additions and 11 deletions

View File

@ -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}")

View File

@ -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<String> ids) {
if (CollectionUtils.isEmpty(ids)) {
return;

View File

@ -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) {

View File

@ -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',