-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from sparkoo/46-latestDownload
46 latest download
- Loading branch information
Showing
14 changed files
with
550 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/main/java/cz/sparko/boxitory/controller/DownloadController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package cz.sparko.boxitory.controller; | ||
|
||
import cz.sparko.boxitory.conf.NotFoundException; | ||
import cz.sparko.boxitory.model.BoxStream; | ||
import cz.sparko.boxitory.service.BoxRepository; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.util.FileCopyUtils; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
|
||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
|
||
@Controller | ||
public class DownloadController { | ||
private static final Logger LOG = LoggerFactory.getLogger(DownloadController.class); | ||
|
||
private final BoxRepository boxRepository; | ||
|
||
public DownloadController(BoxRepository boxRepository) { | ||
this.boxRepository = boxRepository; | ||
} | ||
|
||
@RequestMapping(value = "/download/{boxName}/{boxProvider}/{boxVersion}", method = RequestMethod.GET) | ||
public void downloadBox( | ||
HttpServletResponse response, | ||
@PathVariable String boxName, | ||
@PathVariable String boxProvider, | ||
@PathVariable String boxVersion) throws IOException { | ||
LOG.info("Downloading box [{}], provider [{}], version [{}]", boxName, boxProvider, boxVersion); | ||
|
||
BoxStream boxFile = boxRepository.getBoxStream(boxName, boxProvider, boxVersion) | ||
.orElseThrow(() -> NotFoundException.boxVersionProviderNotFound(boxName, boxVersion, boxProvider)); | ||
|
||
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); | ||
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + boxFile.getFilename() + "\""); | ||
FileCopyUtils.copy(boxFile.getStream(), response.getOutputStream()); | ||
} | ||
|
||
@RequestMapping(value = "/download/{boxName}/{boxProvider}/latest", method = RequestMethod.GET) | ||
public void downloadLatestBox( | ||
HttpServletResponse response, | ||
@PathVariable String boxName, | ||
@PathVariable String boxProvider) throws IOException { | ||
LOG.info("Downloading latest version of box [{}], provider [{}]", boxName, boxProvider); | ||
downloadBox(response, boxName, boxProvider, boxRepository.latestVersionOfBox(boxName, boxProvider)); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/cz/sparko/boxitory/controller/GlobalControllerExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package cz.sparko.boxitory.controller; | ||
|
||
import cz.sparko.boxitory.conf.NotFoundException; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
|
||
@ControllerAdvice | ||
public class GlobalControllerExceptionHandler { | ||
@ExceptionHandler | ||
public ResponseEntity<String> handleException(Exception e) { | ||
final HttpStatus status; | ||
if (e instanceof NotFoundException) { | ||
status = HttpStatus.NOT_FOUND; | ||
} else { | ||
status = HttpStatus.INTERNAL_SERVER_ERROR; | ||
} | ||
return new ResponseEntity<>(e.getMessage(), status); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package cz.sparko.boxitory.model; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
public interface BoxStream { | ||
String getFilename(); | ||
|
||
InputStream getStream() throws IOException; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package cz.sparko.boxitory.model; | ||
|
||
import java.io.BufferedInputStream; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
public class FileBoxStream implements BoxStream { | ||
private final File file; | ||
|
||
public FileBoxStream(File file) { | ||
this.file = file; | ||
} | ||
|
||
@Override | ||
public String getFilename() { | ||
return file.getName(); | ||
} | ||
|
||
@Override | ||
public InputStream getStream() throws IOException { | ||
return new BufferedInputStream(new FileInputStream(file)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.