|
1 | 1 | package me.TechsCode.ReleaseServer; |
2 | 2 |
|
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; |
5 | 13 |
|
6 | 14 | @RestController |
7 | 15 | public class WebRequestsHandler { |
8 | 16 |
|
9 | 17 | @RequestMapping("/") |
10 | 18 | 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 | + } |
12 | 60 | } |
13 | 61 | } |
14 | 62 |
|
|
0 commit comments