|
@@ -1,5 +1,6 @@
|
|
package com.aip.gateway.api.controller;
|
|
package com.aip.gateway.api.controller;
|
|
|
|
|
|
|
|
+import com.aip.gateway.api.model.file.UploadFileResponse;
|
|
import com.aip.gateway.api.model.request.RequestFile;
|
|
import com.aip.gateway.api.model.request.RequestFile;
|
|
import com.aip.gateway.api.model.request.RequestFileAllSet;
|
|
import com.aip.gateway.api.model.request.RequestFileAllSet;
|
|
import com.aip.gateway.api.model.request.RequestFileDel;
|
|
import com.aip.gateway.api.model.request.RequestFileDel;
|
|
@@ -7,6 +8,7 @@ import com.aip.gateway.api.model.request.RequestFileSet;
|
|
import com.aip.gateway.api.model.response.ApiResponseModel;
|
|
import com.aip.gateway.api.model.response.ApiResponseModel;
|
|
import com.aip.gateway.api.repository.AppRepository;
|
|
import com.aip.gateway.api.repository.AppRepository;
|
|
import com.aip.gateway.api.repository.GlobalConstants;
|
|
import com.aip.gateway.api.repository.GlobalConstants;
|
|
|
|
+import com.aip.gateway.api.service.FileStorageService;
|
|
import com.aip.gateway.api.service.IApiFileService;
|
|
import com.aip.gateway.api.service.IApiFileService;
|
|
import io.swagger.annotations.ApiOperation;
|
|
import io.swagger.annotations.ApiOperation;
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
@@ -17,9 +19,16 @@ import jakarta.servlet.http.HttpServletRequest;
|
|
import jakarta.validation.Valid;
|
|
import jakarta.validation.Valid;
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.springframework.core.io.Resource;
|
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
|
+import org.springframework.http.MediaType;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.validation.annotation.Validated;
|
|
import org.springframework.validation.annotation.Validated;
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
|
|
|
|
+
|
|
|
|
+import java.io.IOException;
|
|
|
|
|
|
@Slf4j
|
|
@Slf4j
|
|
@Tag(name = "ApiFile", description = "FILE AIP API")
|
|
@Tag(name = "ApiFile", description = "FILE AIP API")
|
|
@@ -32,6 +41,7 @@ public class ApiFileController extends BaseController {
|
|
|
|
|
|
private final AppRepository appRepo;
|
|
private final AppRepository appRepo;
|
|
private final IApiFileService service;
|
|
private final IApiFileService service;
|
|
|
|
+ private final FileStorageService fileStorageService;
|
|
|
|
|
|
@Operation(summary = "File Info", description = "AIP File Information Query.")
|
|
@Operation(summary = "File Info", description = "AIP File Information Query.")
|
|
@ApiResponse(responseCode = "200", description = "AIP File Information query Success.")
|
|
@ApiResponse(responseCode = "200", description = "AIP File Information query Success.")
|
|
@@ -47,7 +57,7 @@ public class ApiFileController extends BaseController {
|
|
if (authError != 0) {
|
|
if (authError != 0) {
|
|
return responseApiKeyValidationError(request, authError);
|
|
return responseApiKeyValidationError(request, authError);
|
|
}
|
|
}
|
|
- return responseSuccess(this.service.getInfo(req));
|
|
|
|
|
|
+ return responseSuccess(request, this.service.getInfo(req));
|
|
}
|
|
}
|
|
catch (Exception e) {
|
|
catch (Exception e) {
|
|
log.error("exception: /v1/aip-api/file/info, {}", e.getMessage());
|
|
log.error("exception: /v1/aip-api/file/info, {}", e.getMessage());
|
|
@@ -69,7 +79,7 @@ public class ApiFileController extends BaseController {
|
|
if (authError != 0) {
|
|
if (authError != 0) {
|
|
return responseApiKeyValidationError(request, authError);
|
|
return responseApiKeyValidationError(request, authError);
|
|
}
|
|
}
|
|
- return responseSuccess(this.service.setLabel(req));
|
|
|
|
|
|
+ return responseSuccess(request, this.service.setLabel(req));
|
|
}
|
|
}
|
|
catch (Exception e) {
|
|
catch (Exception e) {
|
|
log.error("exception: /v1/aip-api/file/set-label, {}", e.getMessage());
|
|
log.error("exception: /v1/aip-api/file/set-label, {}", e.getMessage());
|
|
@@ -91,7 +101,7 @@ public class ApiFileController extends BaseController {
|
|
if (authError != 0) {
|
|
if (authError != 0) {
|
|
return responseApiKeyValidationError(request, authError);
|
|
return responseApiKeyValidationError(request, authError);
|
|
}
|
|
}
|
|
- return responseSuccess(this.service.delLabel(req));
|
|
|
|
|
|
+ return responseSuccess(request, this.service.delLabel(req));
|
|
}
|
|
}
|
|
catch (Exception e) {
|
|
catch (Exception e) {
|
|
log.error("exception: /v1/aip-api/file/delete-label, {}", e.getMessage());
|
|
log.error("exception: /v1/aip-api/file/delete-label, {}", e.getMessage());
|
|
@@ -113,7 +123,7 @@ public class ApiFileController extends BaseController {
|
|
if (authError != 0) {
|
|
if (authError != 0) {
|
|
return responseApiKeyValidationError(request, authError);
|
|
return responseApiKeyValidationError(request, authError);
|
|
}
|
|
}
|
|
- return responseSuccess(this.service.setProtection(req));
|
|
|
|
|
|
+ return responseSuccess(request, this.service.setProtection(req));
|
|
}
|
|
}
|
|
catch (Exception e) {
|
|
catch (Exception e) {
|
|
log.error("exception: /v1/aip-api/file/set-protection, {}", e.getMessage());
|
|
log.error("exception: /v1/aip-api/file/set-protection, {}", e.getMessage());
|
|
@@ -135,7 +145,7 @@ public class ApiFileController extends BaseController {
|
|
if (authError != 0) {
|
|
if (authError != 0) {
|
|
return responseApiKeyValidationError(request, authError);
|
|
return responseApiKeyValidationError(request, authError);
|
|
}
|
|
}
|
|
- return responseSuccess(this.service.delProtection(req));
|
|
|
|
|
|
+ return responseSuccess(request, this.service.delProtection(req));
|
|
}
|
|
}
|
|
catch (Exception e) {
|
|
catch (Exception e) {
|
|
log.error("exception: /v1/aip-api/file/delete-protection, {}", e.getMessage());
|
|
log.error("exception: /v1/aip-api/file/delete-protection, {}", e.getMessage());
|
|
@@ -157,7 +167,7 @@ public class ApiFileController extends BaseController {
|
|
if (authError != 0) {
|
|
if (authError != 0) {
|
|
return responseApiKeyValidationError(request, authError);
|
|
return responseApiKeyValidationError(request, authError);
|
|
}
|
|
}
|
|
- return responseSuccess(this.service.setLabelProtection(req));
|
|
|
|
|
|
+ return responseSuccess(request, this.service.setLabelProtection(req));
|
|
}
|
|
}
|
|
catch (Exception e) {
|
|
catch (Exception e) {
|
|
log.error("exception: /v1/aip-api/file/set-label-protection, {}", e.getMessage());
|
|
log.error("exception: /v1/aip-api/file/set-label-protection, {}", e.getMessage());
|
|
@@ -179,7 +189,7 @@ public class ApiFileController extends BaseController {
|
|
if (authError != 0) {
|
|
if (authError != 0) {
|
|
return responseApiKeyValidationError(request, authError);
|
|
return responseApiKeyValidationError(request, authError);
|
|
}
|
|
}
|
|
- return responseSuccess(this.service.delLabelProtection(req));
|
|
|
|
|
|
+ return responseSuccess(request, this.service.delLabelProtection(req));
|
|
}
|
|
}
|
|
catch (Exception e) {
|
|
catch (Exception e) {
|
|
log.error("exception: /v1/aip-api/file/delete-label-protection, {}", e.getMessage());
|
|
log.error("exception: /v1/aip-api/file/delete-label-protection, {}", e.getMessage());
|
|
@@ -187,4 +197,61 @@ public class ApiFileController extends BaseController {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // https://www.callicoder.com/spring-boot-file-upload-download-rest-api-example/
|
|
|
|
+ @PostMapping(value = "uploadFile", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE}, produces = {MediaType.APPLICATION_JSON_VALUE})
|
|
|
|
+ public UploadFileResponse uploadFile(
|
|
|
|
+ @Parameter(description = "multipart/form-data 형식의 파일을 input 으로 받습니다. 이때 key 값은 multipartFile 입니다.")
|
|
|
|
+ @RequestParam("file") MultipartFile file,
|
|
|
|
+ @RequestParam("apiKey") String apiKey,
|
|
|
|
+ @RequestParam("password") String password,
|
|
|
|
+ @RequestParam("email") String email)
|
|
|
|
+ {
|
|
|
|
+ log.error(" file: {}", file.getOriginalFilename());
|
|
|
|
+ log.error(" file: {}", file.getName());
|
|
|
|
+ String fileName = fileStorageService.storeFile(file);
|
|
|
|
+ log.error("fileName: {}", fileName);
|
|
|
|
+ log.error(" apiKey: {}", apiKey);
|
|
|
|
+ log.error("password: {}", password);
|
|
|
|
+ log.error(" email: {}", email);
|
|
|
|
+
|
|
|
|
+ String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath()
|
|
|
|
+ .path("/downloadFile/")
|
|
|
|
+ .path(fileName)
|
|
|
|
+ .toUriString();
|
|
|
|
+
|
|
|
|
+ return new UploadFileResponse(fileName, fileDownloadUri,
|
|
|
|
+ file.getContentType(), file.getSize());
|
|
|
|
+ }
|
|
|
|
+// @PostMapping("/uploadMultipleFiles")
|
|
|
|
+// public List<UploadFileResponse> uploadMultipleFiles(@RequestParam("files") MultipartFile[] files) {
|
|
|
|
+// return Arrays.asList(files)
|
|
|
|
+// .stream()
|
|
|
|
+// .map(file -> uploadFile(file))
|
|
|
|
+// .collect(Collectors.toList());
|
|
|
|
+// }
|
|
|
|
+
|
|
|
|
+ @GetMapping("/downloadFile/{fileName:.+}")
|
|
|
|
+ public ResponseEntity<Resource> downloadFile(@PathVariable String fileName, HttpServletRequest request) {
|
|
|
|
+ // Load file as Resource
|
|
|
|
+ Resource resource = fileStorageService.loadFileAsResource(fileName);
|
|
|
|
+
|
|
|
|
+ // Try to determine file's content type
|
|
|
|
+ String contentType = null;
|
|
|
|
+ try {
|
|
|
|
+ contentType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath());
|
|
|
|
+ } catch (IOException ex) {
|
|
|
|
+ log.info("Could not determine file type.");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // Fallback to the default content type if type could not be determined
|
|
|
|
+ if(contentType == null) {
|
|
|
|
+ contentType = "application/octet-stream";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return ResponseEntity.ok()
|
|
|
|
+ .contentType(MediaType.parseMediaType(contentType))
|
|
|
|
+ .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
|
|
|
|
+ .body(resource);
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|