feat: add download functionality for example operator and update Dock… (#188)

* feat: add download functionality for example operator and update Dockerfile

* feat: enhance download response by exposing content disposition header

* feat: update download function to accept filename parameter for example operator
This commit is contained in:
hhhhsc701
2025-12-22 15:39:32 +08:00
committed by GitHub
parent 8fc4455b57
commit 46f4a8c219
6 changed files with 64 additions and 1 deletions

View File

@@ -3,6 +3,7 @@ package com.datamate.operator.application;
import com.datamate.common.domain.model.ChunkUploadPreRequest;
import com.datamate.common.domain.service.FileService;
import com.datamate.common.infrastructure.exception.BusinessException;
import com.datamate.common.infrastructure.exception.SystemErrorCode;
import com.datamate.operator.domain.contants.OperatorConstant;
import com.datamate.operator.infrastructure.converter.OperatorConverter;
import com.datamate.operator.domain.model.OperatorView;
@@ -21,10 +22,13 @@ import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.File;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -165,6 +169,20 @@ public class OperatorService {
}
}
public Resource downloadExampleOperator(File file) {
try {
Resource resource = new UrlResource(file.toURI());
if (resource.exists()) {
return resource;
} else {
throw BusinessException.of(SystemErrorCode.RESOURCE_NOT_FOUND);
}
} catch (MalformedURLException ex) {
log.error("File not found: {}", file.getName(), ex);
throw BusinessException.of(SystemErrorCode.RESOURCE_NOT_FOUND);
}
}
private String convertObjectToListString(Object object) {
if (object == null) {
return null;

View File

@@ -1,5 +1,6 @@
package com.datamate.operator.interfaces.rest;
import com.datamate.common.infrastructure.common.IgnoreResponseWrap;
import com.datamate.common.interfaces.PagedResponse;
import com.datamate.operator.application.OperatorService;
import com.datamate.operator.domain.contants.OperatorConstant;
@@ -8,9 +9,14 @@ import com.datamate.operator.interfaces.dto.OperatorsListPostRequest;
import com.datamate.operator.interfaces.dto.UploadOperatorRequest;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.io.File;
import java.util.List;
@RestController
@@ -70,4 +76,23 @@ public class OperatorController {
public void operatorDelete(@PathVariable("id") String id) {
operatorService.deleteOperator(id);
}
@IgnoreResponseWrap
@GetMapping(value = "/examples/download", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE + ";charset=UTF-8")
public ResponseEntity<Resource> downloadDatasetFileById() {
try {
File file = new File("/opt/backend/test_operator.tar");
Resource resource = operatorService.downloadExampleOperator(file);
return ResponseEntity.ok()
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.header(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, HttpHeaders.CONTENT_DISPOSITION)
.header(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=\"" + file.getName() + "\"")
.body(resource);
} catch (IllegalArgumentException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
}