-
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
1 parent
908bf77
commit 4c84621
Showing
9 changed files
with
114 additions
and
86 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
26 changes: 26 additions & 0 deletions
26
src/main/java/ca/levimiller/smsbridge/rest/AttachmentController.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,26 @@ | ||
package ca.levimiller.smsbridge.rest; | ||
|
||
import io.swagger.annotations.Api; | ||
import io.swagger.annotations.ApiOperation; | ||
import io.swagger.annotations.ApiResponse; | ||
import io.swagger.annotations.ApiResponses; | ||
import java.util.UUID; | ||
import org.springframework.core.io.InputStreamResource; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/attachment") | ||
@Api(value = "/attachment", tags = "Api for attachments") | ||
public interface AttachmentController { | ||
|
||
@GetMapping("/{media_uid}") | ||
@ApiOperation("Returns the attachment file") | ||
@ApiResponses(value = { | ||
@ApiResponse(code = 200, message = "Success", response = String.class), | ||
@ApiResponse(code = 400, message = "Request not valid")}) | ||
ResponseEntity<InputStreamResource> getAttachment(@PathVariable("media_uid") UUID mediaUid); | ||
} |
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
46 changes: 46 additions & 0 deletions
46
src/main/java/ca/levimiller/smsbridge/rest/impl/AttachmentApi.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,46 @@ | ||
package ca.levimiller.smsbridge.rest.impl; | ||
|
||
import ca.levimiller.smsbridge.data.db.MediaRepository; | ||
import ca.levimiller.smsbridge.data.model.Media; | ||
import ca.levimiller.smsbridge.error.NotFoundException; | ||
import ca.levimiller.smsbridge.rest.AttachmentController; | ||
import ca.levimiller.smsbridge.service.FileService; | ||
import java.io.IOException; | ||
import java.net.URISyntaxException; | ||
import java.util.UUID; | ||
import javax.inject.Inject; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.core.io.InputStreamResource; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Slf4j | ||
@Component | ||
public class AttachmentApi implements AttachmentController { | ||
private final MediaRepository mediaRepository; | ||
private final FileService fileService; | ||
|
||
@Inject | ||
public AttachmentApi( | ||
MediaRepository mediaRepository, | ||
@Qualifier("matrixFileService") FileService fileService) { | ||
this.mediaRepository = mediaRepository; | ||
this.fileService = fileService; | ||
} | ||
|
||
@Override | ||
public ResponseEntity<InputStreamResource> getAttachment(UUID mediaUid) { | ||
Media media = mediaRepository.findDistinctByUid(mediaUid) | ||
.orElseThrow(() -> new NotFoundException("Requested attachment not found.")); | ||
try { | ||
return ResponseEntity.ok() | ||
.contentType(MediaType.valueOf(media.getContentType())) | ||
.body(new InputStreamResource(fileService.getFileStream(media.getUrl()))); | ||
} catch (URISyntaxException | IOException e) { | ||
log.error("Error getting attachment from matrix", e); | ||
throw new NotFoundException("Media url malformed or doesn't exist."); | ||
} | ||
} | ||
} |
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
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