Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
Expand Down Expand Up @@ -203,14 +204,15 @@
* 获取文件详情
*/
@Transactional(readOnly = true)
public DatasetFile getDatasetFile(Dataset dataset, String fileId) {
if (dataset != null && !CommonUtils.isUUID(fileId) && !fileId.startsWith(".")) {
public DatasetFile getDatasetFile(Dataset dataset, String fileId, String prefix) {
prefix = StringUtils.isBlank(prefix) ? "" : prefix;
if (dataset != null && !CommonUtils.isUUID(fileId) && !fileId.startsWith(".") && !prefix.startsWith(".")) {
DatasetFile file = new DatasetFile();
file.setId(fileId);
file.setFileName(fileId);
file.setDatasetId(dataset.getId());
file.setFileSize(0L);
file.setFilePath(dataset.getPath() + File.separator + fileId);
file.setFilePath(dataset.getPath() + File.separator + prefix + fileId);
return file;
}
DatasetFile file = datasetFileRepository.getById(fileId);
Expand All @@ -227,9 +229,9 @@
* 删除文件
*/
@Transactional
public void deleteDatasetFile(String datasetId, String fileId) {
public void deleteDatasetFile(String datasetId, String fileId, String prefix) {
Dataset dataset = datasetRepository.getById(datasetId);
DatasetFile file = getDatasetFile(dataset, fileId);
DatasetFile file = getDatasetFile(dataset, fileId, prefix);
dataset.setFiles(new ArrayList<>(Collections.singleton(file)));
datasetFileRepository.removeById(fileId);
if (CommonUtils.isUUID(fileId)) {
Expand All @@ -238,7 +240,7 @@
datasetRepository.updateById(dataset);
// 删除文件时,上传到数据集中的文件会同时删除数据库中的记录和文件系统中的文件,归集过来的文件仅删除数据库中的记录
if (file.getFilePath().startsWith(dataset.getPath())) {
try {

Check failure

Code scanning / CodeQL

Uncontrolled data used in path expression High

This path depends on a
user-provided value
.
This path depends on a
user-provided value
.
Path filePath = Paths.get(file.getFilePath());
Files.deleteIfExists(filePath);
} catch (IOException ex) {
Expand All @@ -253,7 +255,7 @@
@Transactional(readOnly = true)
public Resource downloadFile(DatasetFile file) {
try {
Path filePath = Paths.get(file.getFilePath()).normalize();

Check failure

Code scanning / CodeQL

Uncontrolled data used in path expression High

This path depends on a
user-provided value
.
This path depends on a
user-provided value
.
log.info("start download file {}", file.getFilePath());
Resource resource = new UrlResource(filePath.toUri());
if (resource.exists()) {
Expand Down Expand Up @@ -655,7 +657,7 @@
throw BusinessException.of(DataManagementErrorCode.DATASET_NOT_FOUND);
}

DatasetFile file = getDatasetFile(dataset, fileId);
DatasetFile file = getDatasetFile(dataset, fileId, null);
String newName = Optional.ofNullable(request.getNewName()).orElse("").trim();
if (newName.isEmpty()) {
throw BusinessException.of(CommonErrorCode.PARAM_ERROR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@ public Response<PagedResponse<DatasetFile>> getDatasetFiles(
@GetMapping("/{fileId}")
public ResponseEntity<Response<DatasetFileResponse>> getDatasetFileById(
@PathVariable("datasetId") String datasetId,
@PathVariable("fileId") String fileId) {
@PathVariable("fileId") String fileId,
@RequestParam(value = "prefix", required = false, defaultValue = "") String prefix) {
try {
Dataset dataset = datasetApplicationService.getDataset(datasetId);
DatasetFile datasetFile = datasetFileApplicationService.getDatasetFile(dataset, fileId);
DatasetFile datasetFile = datasetFileApplicationService.getDatasetFile(dataset, fileId, prefix);
return ResponseEntity.ok(Response.ok(DatasetConverter.INSTANCE.convertToResponse(datasetFile)));
} catch (IllegalArgumentException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(Response.error(SystemErrorCode.UNKNOWN_ERROR, null));
Expand All @@ -78,9 +79,10 @@ public ResponseEntity<Response<DatasetFileResponse>> getDatasetFileById(
@DeleteMapping("/{fileId}")
public ResponseEntity<Response<Void>> deleteDatasetFile(
@PathVariable("datasetId") String datasetId,
@PathVariable("fileId") String fileId) {
@PathVariable("fileId") String fileId,
@RequestParam(value = "prefix", required = false, defaultValue = "") String prefix) {
try {
datasetFileApplicationService.deleteDatasetFile(datasetId, fileId);
datasetFileApplicationService.deleteDatasetFile(datasetId, fileId, prefix);
return ResponseEntity.ok().build();
} catch (IllegalArgumentException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(Response.error(SystemErrorCode.UNKNOWN_ERROR, null));
Expand All @@ -90,10 +92,12 @@ public ResponseEntity<Response<Void>> deleteDatasetFile(
@IgnoreResponseWrap
@GetMapping(value = "/{fileId}/download", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE + ";charset=UTF-8")
public ResponseEntity<Resource> downloadDatasetFileById(@PathVariable("datasetId") String datasetId,
@PathVariable("fileId") String fileId) {
@PathVariable("fileId") String fileId,
@RequestParam(value = "prefix", required = false, defaultValue = "") String prefix) {
try {
log.info("downloadDatasetFileById datasetId:{}, fileId:{}, prefix:{}", datasetId, fileId, prefix);
Dataset dataset = datasetApplicationService.getDataset(datasetId);
DatasetFile datasetFile = datasetFileApplicationService.getDatasetFile(dataset, fileId);
DatasetFile datasetFile = datasetFileApplicationService.getDatasetFile(dataset, fileId, prefix);
Resource resource = datasetFileApplicationService.downloadFile(datasetFile);

return ResponseEntity.ok()
Expand Down
13 changes: 8 additions & 5 deletions frontend/src/pages/DataManagement/Detail/useFilesOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ export function useFilesOperation(dataset: Dataset) {

const handleDownloadFile = async (file: DatasetFile) => {
// 实际导出逻辑
await downloadFileByIdUsingGet(dataset.id, file.id, file.fileName);
const prefix = pagination.prefix || "";
await downloadFileByIdUsingGet(dataset.id, prefix, file.id, file.fileName);
// 假设导出成功
message.success({
content: `已导出 1 个文件`,
Expand Down Expand Up @@ -112,12 +113,13 @@ export function useFilesOperation(dataset: Dataset) {
setPreviewFileDetail(undefined);
try {
// 获取文件元信息(来自 t_dm_dataset_files)
const detailRes: any = await getDatasetFileByIdUsingGet(datasetId, file.id);
const prefix = pagination.prefix || "";
const detailRes: any = await getDatasetFileByIdUsingGet(datasetId, file.id, prefix);
const detail = detailRes?.data || detailRes;
setPreviewFileDetail(detail);

const image = isImageFile(detail?.fileName || file.fileName, detail?.fileType);
const { blob, blobUrl } = await downloadFileByIdUsingGet(datasetId, file.id, file.fileName, "preview");
const { blob, blobUrl } = await downloadFileByIdUsingGet(datasetId, prefix, file.id, file.fileName, "preview");

if (image) {
setPreviewUrl(blobUrl);
Expand All @@ -134,7 +136,8 @@ export function useFilesOperation(dataset: Dataset) {

const handleDeleteFile = async (file) => {
try {
await deleteDatasetFileUsingDelete(dataset.id, file.id);
const prefix = pagination.prefix || "";
await deleteDatasetFileUsingDelete(dataset.id, file.id, prefix);
fetchFiles(); // 刷新文件列表
message.success({ content: `文件 ${file.fileName} 已删除` });
} catch (error) {
Expand Down Expand Up @@ -189,7 +192,7 @@ export function useFilesOperation(dataset: Dataset) {
// 先删除文件
for (const file of files) {
try {
await deleteDatasetFileUsingDelete(dataset.id, file.id);
await deleteDatasetFileUsingDelete(dataset.id, file.id, directoryPath);
} catch (e) {
console.error("删除文件失败", file, e);
}
Expand Down
13 changes: 8 additions & 5 deletions frontend/src/pages/DataManagement/dataset.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ export function queryDatasetFilesUsingGet(id: string | number, params?: any) {
// 根据ID获取单个数据集文件详情
export function getDatasetFileByIdUsingGet(
datasetId: string | number,
fileId: string | number
fileId: string | number,
prefix: string
) {
return get(
`/api/data-management/datasets/${datasetId}/files/${fileId}`
`/api/data-management/datasets/${datasetId}/files/${fileId}`, { prefix: prefix}
);
}

Expand Down Expand Up @@ -108,13 +109,14 @@ export function renameDirectoryUsingPut(

export function downloadFileByIdUsingGet(
id: string | number,
prefix: string,
fileId: string | number,
fileName: string,
action: string = "download"
) {
return download(
`/api/data-management/datasets/${id}/files/${fileId}/download`,
null,
{ prefix: prefix },
fileName,
action
);
Expand All @@ -123,9 +125,10 @@ export function downloadFileByIdUsingGet(
// 删除数据集文件
export function deleteDatasetFileUsingDelete(
datasetId: string | number,
fileId: string | number
fileId: string | number,
prefix: string
) {
return del(`/api/data-management/datasets/${datasetId}/files/${fileId}`);
return del(`/api/data-management/datasets/${datasetId}/files/${fileId}`, { prefix: prefix });
}

// 重命名数据集文件(仅修改主名称,后缀保持不变)
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/utils/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,8 @@ class Request {
const isQueryParams =
Object.keys(params).length === 1 &&
(Object.prototype.hasOwnProperty.call(params, "id") ||
Object.prototype.hasOwnProperty.call(params, "ids"));
Object.prototype.hasOwnProperty.call(params, "ids") ||
Object.prototype.hasOwnProperty.call(params, "prefix"));

if (isQueryParams) {
fullURL = this.buildURL(url, params);
Expand Down
Loading