Skip to content

Commit 1e66b75

Browse files
author
TechsCode
committed
Added Download Request
1 parent 3b9c3b5 commit 1e66b75

3 files changed

Lines changed: 66 additions & 5 deletions

File tree

src/main/java/me/TechsCode/ReleaseServer/Config.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,22 @@
2020

2121
public class Config {
2222

23+
private static Config instance;
24+
25+
public static Config getInstance(){
26+
if(instance == null){
27+
instance = new Config();
28+
}
29+
30+
return instance;
31+
}
32+
2333
private static final Gson gson = new Gson();
2434

2535
private File file;
2636
private JsonObject root;
2737

28-
public Config() {
38+
private Config() {
2939
this.file = new File("config.json");
3040

3141
if(!file.exists()){

src/main/java/me/TechsCode/ReleaseServer/ReleaseServer.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class ReleaseServer {
1717
private static List<Artifact> artifacts;
1818

1919
public static void main(String[] args){
20-
Config config = new Config();
20+
Config config = Config.getInstance();
2121

2222
SpringApplication app = new SpringApplication(ReleaseServer.class);
2323
app.setDefaultProperties(Collections.singletonMap("server.port", config.getPort()));
@@ -52,6 +52,9 @@ public List<Artifact> getArtifacts() {
5252
};
5353
}
5454

55+
public static List<Artifact> getArtifacts() {
56+
return artifacts;
57+
}
5558
}
5659

5760

src/main/java/me/TechsCode/ReleaseServer/WebRequestsHandler.java

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,62 @@
11
package me.TechsCode.ReleaseServer;
22

3-
import org.springframework.web.bind.annotation.RequestMapping;
4-
import org.springframework.web.bind.annotation.RestController;
3+
import org.springframework.core.io.Resource;
4+
import org.springframework.core.io.UrlResource;
5+
import org.springframework.http.HttpHeaders;
6+
import org.springframework.http.MediaType;
7+
import org.springframework.http.ResponseEntity;
8+
import org.springframework.web.bind.annotation.*;
9+
10+
import java.io.File;
11+
import java.net.MalformedURLException;
12+
import java.util.Optional;
513

614
@RestController
715
public class WebRequestsHandler {
816

917
@RequestMapping("/")
1018
public String index() {
11-
return "Im a happy server that moves around new software releases";
19+
return "Im a happy server that moves around new software releases :)";
20+
}
21+
22+
@GetMapping("/download/{project}/{tag}")
23+
public Object download(@RequestParam(value = "token") String token, @PathVariable(value = "project") String project, @PathVariable(value = "tag") String tag){
24+
if(token == null){
25+
return "You dont have any token provided!";
26+
}
27+
28+
if(!Config.getInstance().getTokens().contains(token)){
29+
return "The token you have provided is invalid!";
30+
}
31+
32+
Optional<Artifact> artifact = ReleaseServer.getArtifacts().stream()
33+
.filter(x -> x.getRelease().getProject().getName().equalsIgnoreCase(project))
34+
.filter(x -> x.getRelease().getUniqueTag().equals(tag))
35+
.findFirst();
36+
37+
if(artifact.isPresent()){
38+
if(artifact.get().getAssets().length == 0){
39+
return "This artifact does not have any assets!";
40+
}
41+
42+
if(artifact.get().getAssets().length > 1){
43+
return "This artifact has too many assets!";
44+
}
45+
46+
try {
47+
File asset = artifact.get().getAssets()[0];
48+
49+
return ResponseEntity.ok()
50+
.contentType(MediaType.APPLICATION_OCTET_STREAM)
51+
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + asset.getName() + "\"")
52+
.body(new UrlResource(asset.getPath()));
53+
} catch (MalformedURLException e) {
54+
e.printStackTrace();
55+
return "Error: "+e.getMessage();
56+
}
57+
} else {
58+
return "Could not find any artifact";
59+
}
1260
}
1361
}
1462

0 commit comments

Comments
 (0)