Skip to content

Commit

Permalink
[#2078] Add endpoint for uploading media (#2147)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismatix authored Jul 13, 2021
1 parent 56e7482 commit a88c16f
Show file tree
Hide file tree
Showing 50 changed files with 343 additions and 1,715 deletions.
6 changes: 2 additions & 4 deletions backend/media/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ app_deps = [
"//backend:base_app",
"//backend/model/message",
"//backend/model/metadata",
"//lib/java/mapping",
"//lib/java/uuid",
"//lib/java/url",
"//lib/java/spring/kafka/core:spring-kafka-core",
"//lib/java/spring/kafka/streams:spring-kafka-streams",
"@maven//:javax_xml_bind_jaxb_api",
"//lib/java/spring/web:spring-web",
"@maven//:org_springframework_retry_spring_retry",
"@maven//:org_aspectj_aspectjweaver",
"@maven//:com_amazonaws_aws_java_sdk_core",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package co.airy.core.media;

import co.airy.core.media.services.MediaUpload;
import co.airy.log.AiryLoggerFactory;
import co.airy.spring.web.payload.RequestErrorResponsePayload;
import co.airy.uuid.UUIDv5;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.slf4j.Logger;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

@RestController
public class MediaController {
private static final Logger log = AiryLoggerFactory.getLogger(MediaController.class);
private final MediaUpload mediaUpload;

public MediaController(MediaUpload mediaUpload) {
this.mediaUpload = mediaUpload;
}


@PostMapping(value = "/media.uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<?> mediaUpload(@RequestParam("file") MultipartFile multipartFile) {
final String originalFileName = multipartFile.getOriginalFilename();

if (originalFileName == null) {
return ResponseEntity.unprocessableEntity().body(new RequestErrorResponsePayload("Request is missing original file name"));
}

try {
final InputStream is = multipartFile.getInputStream();
String fileName = UUIDv5.fromFile(is).toString();

final String originalFileExtension = originalFileName.contains(".") ? originalFileName.substring(originalFileName.lastIndexOf(".")) : "";
fileName = fileName.concat(originalFileExtension);

return ResponseEntity.ok(new MediaUploadResponsePayload(mediaUpload.uploadMedia(is, fileName)));
} catch (Exception e) {
log.error("Media upload failed:", e);
return ResponseEntity.badRequest().body(new RequestErrorResponsePayload(String.format("Media Upload failed with error: %s", e.getMessage())));
}
}
}

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
class MediaUploadResponsePayload {
private String mediaUrl;
}

This file was deleted.

This file was deleted.

87 changes: 0 additions & 87 deletions backend/media/src/main/java/co/airy/core/media/Stores.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
public class AwsConfig {

@Bean
public AmazonS3 amazonS3Client(@Value("${storage.s3.key}") final String mediaS3Key,
@Value("${storage.s3.secret}") final String mediaS3Secret,
@Value("${storage.s3.region}") final String region) {
public AmazonS3 amazonS3Client(@Value("${s3.key}") final String mediaS3Key,
@Value("${s3.secret}") final String mediaS3Secret,
@Value("${s3.region}") final String region) {
AWSCredentials credentials = new BasicAWSCredentials(
mediaS3Key,
mediaS3Secret
Expand Down
Loading

0 comments on commit a88c16f

Please sign in to comment.