-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
47 additions
and
10 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
41 changes: 34 additions & 7 deletions
41
src/main/java/com/lmco/spectrum/systemnavigation3d/controller/ResourceController.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 |
---|---|---|
@@ -1,29 +1,56 @@ | ||
package com.lmco.spectrum.systemnavigation3d.controller; | ||
|
||
import com.amazonaws.services.s3.model.AmazonS3Exception; | ||
import com.amazonaws.util.IOUtils; | ||
import com.lmco.spectrum.systemnavigation3d.service.ResourceService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.lang.Nullable; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
@Controller | ||
@RequestMapping("api/resources") | ||
public class ResourceController { | ||
|
||
@Autowired | ||
ResourceService resourceService; | ||
|
||
@RequestMapping(value = "/pdf/{id}", method = RequestMethod.GET) | ||
public ResponseEntity<?> testEndpoint(@PathVariable("id") Long id){ | ||
// TODO call ResourceService | ||
String x = resourceService.getFileTest(); | ||
if(x == null) { | ||
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); | ||
private ResponseEntity<?> handleS3Input(String key, @Nullable HttpHeaders headers) { | ||
try (InputStream is = resourceService.getFileAsStream(key)) { | ||
return new ResponseEntity<>(IOUtils.toByteArray(is), headers, HttpStatus.OK); | ||
} catch (AmazonS3Exception e) { | ||
if(e.getErrorCode().equals("NoSuchKey")) { | ||
return new ResponseEntity<>(e.getErrorMessage(), HttpStatus.NOT_FOUND); | ||
} else { | ||
e.printStackTrace(); | ||
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
return ResponseEntity.ok(x); | ||
} | ||
|
||
@RequestMapping(value = "/pdf/{key}", method = RequestMethod.GET) | ||
public ResponseEntity<?> getResourcePDF(@PathVariable("key") String key){ | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setContentType(MediaType.APPLICATION_PDF); | ||
// headers.setContentDispositionFormData(key, key); // Auto download | ||
return handleS3Input(key, headers); | ||
} | ||
|
||
@RequestMapping(value = "/{key}", method = RequestMethod.GET) | ||
public ResponseEntity<?> getResource(@PathVariable("key") String key){ | ||
return handleS3Input(key, null); | ||
} | ||
|
||
} |
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