-
-
Save lcbasu/aa99e847fef2534471ec12866a02ae8c to your computer and use it in GitHub Desktop.
Revisions
-
m-cakir revised this gist
Jun 12, 2020 . No changes.There are no files selected for viewing
-
m-cakir revised this gist
Oct 13, 2019 . 1 changed file with 7 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,11 @@ * * * * * // STEP 1: Download Firebase config file and store it as "firebase.json" under the src/main/resources folder * // https://support.google.com/firebase/answer/7015592?hl=en * * // STEP 2: Make data public on Google Cloud * // https://cloud.google.com/storage/docs/access-control/making-data-public * firebase.bucket-name=MY_FIREBASE_PROJECT_BUCKET_NAME -
m-cakir revised this gist
Oct 13, 2019 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,6 @@ * * // first, make data public on Google Cloud * // https://cloud.google.com/storage/docs/access-control/making-data-public * firebase.bucket-name=MY_FIREBASE_PROJECT_BUCKET_NAME firebase.image-url=https://storage.googleapis.com/${firebase.bucket-name}/%s -
m-cakir revised this gist
Oct 13, 2019 . 2 changed files with 43 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,35 @@ * * * * * * @Autowired IImageService imageService; @PostMapping public ResponseEntity create(@RequestParam(name = "file") MultipartFile[] files) { for (MultipartFile file : files) { try { String fileName = imageService.save(file); String imageUrl = imageService.getImageUrl(fileName); // do whatever you want with that } catch (Exception e) { // throw internal error; } } return ResponseEntity.ok().build(); } * * * * * This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,8 @@ * * * firebase.bucket-name=MY_FIREBASE_PROJECT_BUCKET_NAME firebase.image-url=https://storage.googleapis.com/${firebase.bucket-name}/%s * * * -
m-cakir revised this gist
Oct 13, 2019 . 1 changed file with 109 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,109 @@ import com.google.auth.oauth2.GoogleCredentials; import com.google.cloud.storage.Blob; import com.google.cloud.storage.Bucket; import com.google.firebase.FirebaseApp; import com.google.firebase.FirebaseOptions; import com.google.firebase.cloud.StorageClient; import lombok.Data; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.context.event.EventListener; import org.springframework.core.io.ClassPathResource; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; import org.springframework.web.multipart.MultipartFile; import java.awt.image.BufferedImage; import java.io.IOException; @Service public class FirebaseImageService implements IImageService { @Autowired Properties properties; @EventListener public void init(ApplicationReadyEvent event) { // initialize Firebase try { ClassPathResource serviceAccount = new ClassPathResource("firebase.json"); FirebaseOptions options = new FirebaseOptions.Builder() .setCredentials(GoogleCredentials.fromStream(serviceAccount.getInputStream())) .setStorageBucket(properties.getBucketName()) .build(); FirebaseApp.initializeApp(options); } catch (Exception ex) { ex.printStackTrace(); } } @Override public String getImageUrl(String name) { return String.format(properties.imageUrl, name); } @Override public String save(MultipartFile file) throws IOException { Bucket bucket = StorageClient.getInstance().bucket(); String name = generateFileName(file.getOriginalFilename()); bucket.create(name, file.getBytes(), file.getContentType()); return name; } @Override public String save(BufferedImage bufferedImage, String originalFileName) throws IOException { byte[] bytes = getByteArrays(bufferedImage, getExtension(originalFileName)); Bucket bucket = StorageClient.getInstance().bucket(); String name = generateFileName(originalFileName); bucket.create(name, bytes); return name; } @Override public void delete(String name) throws IOException { Bucket bucket = StorageClient.getInstance().bucket(); if (StringUtils.isEmpty(name)) { throw new IOException("invalid file name"); } Blob blob = bucket.get(name); if (blob == null) { throw new IOException("file not found"); } blob.delete(); } @Data @Configuration @ConfigurationProperties(prefix = "firebase") public class Properties { private String bucketName; private String imageUrl; } } -
m-cakir revised this gist
Oct 13, 2019 . 1 changed file with 47 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,47 @@ import org.springframework.util.StringUtils; import org.springframework.web.multipart.MultipartFile; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.UUID; public interface IImageService { String getImageUrl(String name); String save(MultipartFile file) throws IOException; String save(BufferedImage bufferedImage, String originalFileName) throws IOException; void delete(String name) throws IOException; default String getExtension(String originalFileName) { return StringUtils.getFilenameExtension(originalFileName); } default String generateFileName(String originalFileName) { return UUID.randomUUID().toString() + getExtension(originalFileName); } default byte[] getByteArrays(BufferedImage bufferedImage, String format) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { ImageIO.write(bufferedImage, format, baos); baos.flush(); return baos.toByteArray(); } catch (IOException e) { throw e; } finally { baos.close(); } } } -
m-cakir created this gist
Oct 13, 2019 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,25 @@ * * * * * <dependency> <groupId>com.google.firebase</groupId> <artifactId>firebase-admin</artifactId> <version>6.10.0</version> </dependency> <dependency> <groupId>com.google.cloud</groupId> <artifactId>google-cloud-storage</artifactId> <version>1.96.0</version> </dependency> <dependency> <groupId>com.google.api-client</groupId> <artifactId>google-api-client</artifactId> <version>1.30.4</version> </dependency> * * * * *