Skip to content

Commit 69735c8

Browse files
authored
feat: add an api for downloading file (#30)
Co-authored-by: Rick <linuxsuren@users.noreply.github.com>
1 parent 4072902 commit 69735c8

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

.github/workflows/build.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ jobs:
2424
PULL_REQUEST: ${{ github.event.number }}
2525
run: |
2626
sudo curl -L https://github.com/docker/compose/releases/download/v2.23.0/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
27-
sudo chmod u+x /usr/local/bin/docker-compose
27+
sudo chmod +x /usr/local/bin/docker-compose
2828
export OWNER=$(echo ${{ github.repository_owner }} | tr '[:upper:]' '[:lower:]')
2929
make test-e2e

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ run-demo:
66
cd e2e && docker compose up server
77
run:
88
mvn package && java -jar target/demo-0.0.1-SNAPSHOT.jar
9+
run-8081:
10+
mvn package && java -Dserver.port=8081 -jar target/demo-0.0.1-SNAPSHOT.jar
911
local-test:
1012
atest run -p e2e/test-suite.yaml
1113

e2e/test-suite.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,13 @@ items:
110110
api: /actuator/prometheus
111111
header:
112112
Authorization: "{{ .param.auth }}"
113+
114+
## files
115+
- name: downloadFile
116+
request:
117+
api: /file/download
118+
header:
119+
Authorization: "{{ .param.auth }}"
120+
expect:
121+
header:
122+
Content-Type: application/octet-stream

src/main/java/io/github/devopsws/demo/service/FileService.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
import java.io.FileOutputStream;
44

5+
import org.springframework.core.io.Resource;
6+
import org.springframework.core.io.UrlResource;
7+
import org.springframework.http.HttpHeaders;
8+
import org.springframework.http.MediaType;
9+
import org.springframework.http.ResponseEntity;
510
import org.springframework.util.FileCopyUtils;
11+
import org.springframework.web.bind.annotation.GetMapping;
612
import org.springframework.web.bind.annotation.PostMapping;
713
import org.springframework.web.bind.annotation.RequestMapping;
814
import org.springframework.web.bind.annotation.RequestParam;
@@ -12,9 +18,9 @@
1218
import io.github.devopsws.demo.model.Message;
1319

1420
@RestController
15-
@RequestMapping("/upload")
21+
@RequestMapping("/file")
1622
public class FileService {
17-
@PostMapping("/")
23+
@PostMapping("/upload")
1824
public Message<?> upload(@RequestParam("file") MultipartFile file) {
1925
System.out.println("Received file uploading request");
2026
Message<String> message = new Message<String>();
@@ -34,4 +40,17 @@ public Message<?> upload(@RequestParam("file") MultipartFile file) {
3440
}
3541
return message;
3642
}
43+
44+
@GetMapping("/download")
45+
public ResponseEntity<Resource> downloadFile() {
46+
Resource resource = new UrlResource(FileService.class.getResource("/application.yml"));
47+
if (resource.exists() || resource.isReadable()) {
48+
return ResponseEntity.ok()
49+
.contentType(MediaType.APPLICATION_OCTET_STREAM)
50+
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
51+
.body(resource);
52+
} else {
53+
return ResponseEntity.notFound().build();
54+
}
55+
}
3756
}

0 commit comments

Comments
 (0)