Skip to content

Commit

Permalink
feat: 5MB limit to image and make uploading image optional
Browse files Browse the repository at this point in the history
  • Loading branch information
peageon committed May 27, 2024
1 parent 7d38af9 commit 0035914
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.runningmate.backend;

import com.runningmate.backend.exception.ErrorMessageResponseDTO;
import com.runningmate.backend.exception.ExistsConflictException;
import com.runningmate.backend.exception.FieldExistsException;
import com.runningmate.backend.exception.ResourceNotFoundException;
import com.runningmate.backend.exception.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
Expand Down Expand Up @@ -33,6 +30,11 @@ public ResponseEntity<ErrorMessageResponseDTO> handleResourceNotFoundException(R
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ErrorMessageResponseDTO(e.getMessage()));
}

@ExceptionHandler(InvalidFileTypeException.class)
public ResponseEntity<ErrorMessageResponseDTO> handleInvalidFileTypeException(InvalidFileTypeException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ErrorMessageResponseDTO(e.getMessage()));
}

@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<Map<String, String>> handleValidationExceptions(MethodArgumentNotValidException e) {
Map<String, String> errors = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.runningmate.backend.exception;

public class InvalidFileTypeException extends RuntimeException{
public InvalidFileTypeException(String fileType) {
super("Invalid File Type Given: " + fileType);
}

public InvalidFileTypeException(String fileType, String extension) {
super("File type of " + fileType + " was given but file's extension is " + extension);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.WriteChannel;
import com.google.cloud.storage.*;
import com.runningmate.backend.exception.InvalidFileTypeException;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ResourceUtils;
Expand All @@ -13,10 +14,13 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@Service
public class GcsFileStorageService {
private static final List<String> ACCEPTED_CONTENT_TYPES = Arrays.asList("image/jpeg", "image/png", "image/gif");

@Value("${spring.cloud.gcp.storage.credentials.location}")
private String keyFileName;
Expand All @@ -25,6 +29,10 @@ public class GcsFileStorageService {
private String bucketName;

public String storeFile(MultipartFile file) throws RuntimeException, IOException {
if (!validateFile(file)) {
return null;
}

InputStream keyFile = ResourceUtils.getURL(keyFileName).openStream();

String fileName = UUID.randomUUID().toString();
Expand All @@ -44,4 +52,18 @@ public String storeFile(MultipartFile file) throws RuntimeException, IOException
throw new RuntimeException("Failed to store file in GCS", e); // throws 500
}
}

private boolean validateFile(MultipartFile file) {
if (file == null || file.isEmpty()) {
return false;
}
if (!ACCEPTED_CONTENT_TYPES.contains(file.getContentType())) {
throw new InvalidFileTypeException(file.getContentType());
}
String filename = file.getOriginalFilename();
if (filename != null && !filename.toLowerCase().matches(".*\\.(jpg|jpeg|png|gif)$")) {
throw new InvalidFileTypeException(file.getContentType(), filename.toLowerCase());
}
return true;
}
}

0 comments on commit 0035914

Please sign in to comment.