-
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.
Signed-off-by: pierresomny <[email protected]>
- Loading branch information
1 parent
e0b0a93
commit 43d50d1
Showing
13 changed files
with
270 additions
and
5 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
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
14 changes: 14 additions & 0 deletions
14
src/main/java/com/sfeiropensource/schoolapp/exception/BadRequestException.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,14 @@ | ||
package com.sfeiropensource.schoolapp.exception; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class BadRequestException extends RuntimeException { | ||
|
||
private final String message; | ||
|
||
public BadRequestException(String message) { | ||
super(message); | ||
this.message = message; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/sfeiropensource/schoolapp/exception/FileWriteException.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,14 @@ | ||
package com.sfeiropensource.schoolapp.exception; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class FileWriteException extends RuntimeException { | ||
|
||
private final String message; | ||
|
||
public FileWriteException(String message) { | ||
super(message); | ||
this.message = message; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/sfeiropensource/schoolapp/exception/GCPFileUploadException.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,14 @@ | ||
package com.sfeiropensource.schoolapp.exception; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class GCPFileUploadException extends RuntimeException { | ||
|
||
private final String message; | ||
|
||
public GCPFileUploadException(String message) { | ||
super(message); | ||
this.message = message; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/sfeiropensource/schoolapp/exception/InvalidFileTypeException.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,14 @@ | ||
package com.sfeiropensource.schoolapp.exception; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class InvalidFileTypeException extends RuntimeException { | ||
|
||
private final String message; | ||
|
||
public InvalidFileTypeException(String message) { | ||
super(message); | ||
this.message = message; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/sfeiropensource/schoolapp/model/FileDto.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,11 @@ | ||
package com.sfeiropensource.schoolapp.model; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
public class FileDto { | ||
private String fileName; | ||
private String fileUrl; | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/com/sfeiropensource/schoolapp/service/GcpBucketService.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,50 @@ | ||
package com.sfeiropensource.schoolapp.service; | ||
|
||
import com.sfeiropensource.schoolapp.exception.BadRequestException; | ||
import com.sfeiropensource.schoolapp.exception.GCPFileUploadException; | ||
import com.sfeiropensource.schoolapp.model.FileDto; | ||
import com.sfeiropensource.schoolapp.utils.DataBucketUtil; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.File; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
@Slf4j | ||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class GcpBucketService { | ||
|
||
private final DataBucketUtil dataBucketUtil; | ||
|
||
public void uploadFiles(MultipartFile file) { | ||
|
||
log.info("Start file uploading service"); | ||
|
||
String originalFileName = file.getOriginalFilename(); | ||
if (originalFileName == null) { | ||
throw new BadRequestException("Original file name is null"); | ||
} | ||
Path path = new File(originalFileName).toPath(); | ||
|
||
try { | ||
String contentType = Files.probeContentType(path); | ||
FileDto fileDto = dataBucketUtil.uploadFile(file, originalFileName, contentType); | ||
|
||
if (fileDto != null) { | ||
log.info("File uploaded successfully, file name: {} and url: {}", fileDto.getFileName(), fileDto.getFileUrl()); | ||
} | ||
} catch (Exception e) { | ||
log.error("Error occurred while uploading. Error: ", e); | ||
throw new GCPFileUploadException("Error occurred while uploading"); | ||
} | ||
|
||
// fileRepository.saveAll(inputFiles); | ||
log.info("File details successfully saved in the database"); | ||
} | ||
} |
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
103 changes: 103 additions & 0 deletions
103
src/main/java/com/sfeiropensource/schoolapp/utils/DataBucketUtil.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,103 @@ | ||
package com.sfeiropensource.schoolapp.utils; | ||
|
||
import com.google.auth.oauth2.GoogleCredentials; | ||
import com.google.cloud.storage.Blob; | ||
import com.google.cloud.storage.Bucket; | ||
import com.google.cloud.storage.Storage; | ||
import com.google.cloud.storage.StorageOptions; | ||
import com.sfeiropensource.schoolapp.exception.FileWriteException; | ||
import com.sfeiropensource.schoolapp.exception.GCPFileUploadException; | ||
import com.sfeiropensource.schoolapp.exception.InvalidFileTypeException; | ||
import com.sfeiropensource.schoolapp.model.FileDto; | ||
import org.apache.commons.io.FileUtils; | ||
import org.apache.coyote.BadRequestException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.core.io.ClassPathResource; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.InputStream; | ||
|
||
@Component | ||
public class DataBucketUtil { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(DataBucketUtil.class); | ||
|
||
@Value("${gcp.config.file}") | ||
private String gcpConfigFile; | ||
|
||
@Value("${gcp.project.id}") | ||
private String gcpProjectId; | ||
|
||
@Value("${gcp.bucket.id}") | ||
private String gcpBucketId; | ||
|
||
@Value("${gcp.dir.name}") | ||
private String gcpDirectoryName; | ||
|
||
|
||
public FileDto uploadFile(MultipartFile multipartFile, String fileName, String contentType) { | ||
|
||
try { | ||
|
||
LOGGER.debug("Start file uploading process on GCS"); | ||
byte[] fileData = FileUtils.readFileToByteArray(convertFile(multipartFile)); | ||
|
||
InputStream inputStream = new ClassPathResource(gcpConfigFile).getInputStream(); | ||
|
||
StorageOptions options = StorageOptions.newBuilder().setProjectId(gcpProjectId) | ||
.setCredentials(GoogleCredentials.fromStream(inputStream)).build(); | ||
|
||
Storage storage = options.getService(); | ||
Bucket bucket = storage.get(gcpBucketId, Storage.BucketGetOption.fields()); | ||
|
||
Blob blob = bucket.create(gcpDirectoryName + "/" + fileName + "-[replace by random ID]" + checkFileExtension(fileName), fileData, contentType); | ||
|
||
if (blob != null) { | ||
LOGGER.debug("File successfully uploaded to GCS"); | ||
return new FileDto(blob.getName(), blob.getMediaLink()); | ||
} | ||
|
||
} catch (Exception e) { | ||
LOGGER.error("An error occurred while uploading data. Exception: ", e); | ||
throw new GCPFileUploadException("An error occurred while storing data to GCS"); | ||
} | ||
throw new GCPFileUploadException("An error occurred while storing data to GCS"); | ||
} | ||
|
||
private File convertFile(MultipartFile file) { | ||
|
||
try { | ||
if (file.getOriginalFilename() == null) { | ||
throw new BadRequestException("Original file name is null"); | ||
} | ||
File convertedFile = new File(file.getOriginalFilename()); | ||
FileOutputStream outputStream = new FileOutputStream(convertedFile); | ||
outputStream.write(file.getBytes()); | ||
outputStream.close(); | ||
LOGGER.debug("Converting multipart file : {}", convertedFile); | ||
return convertedFile; | ||
} catch (Exception e) { | ||
throw new FileWriteException("An error has occurred while converting the file"); | ||
} | ||
} | ||
|
||
private String checkFileExtension(String fileName) { | ||
if (fileName != null && fileName.contains(".")) { | ||
String[] extensionList = {".png", ".jpeg", ".pdf", ".doc", ".mp3"}; | ||
|
||
for (String extension : extensionList) { | ||
if (fileName.endsWith(extension)) { | ||
LOGGER.debug("Accepted file type : {}", extension); | ||
return extension; | ||
} | ||
} | ||
} | ||
LOGGER.error("Not a permitted file type"); | ||
throw new InvalidFileTypeException("Not a permitted file type"); | ||
} | ||
} |