Skip to content

Instantly share code, notes, and snippets.

@lcbasu
Forked from m-cakir/FirebaseImageService.java
Created June 17, 2020 01:32
Show Gist options
  • Save lcbasu/aa99e847fef2534471ec12866a02ae8c to your computer and use it in GitHub Desktop.
Save lcbasu/aa99e847fef2534471ec12866a02ae8c to your computer and use it in GitHub Desktop.

Revisions

  1. @m-cakir m-cakir revised this gist Jun 12, 2020. No changes.
  2. @m-cakir m-cakir revised this gist Oct 13, 2019. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion application.properties
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,11 @@
    *
    * // first, make data public on Google Cloud
    *
    *
    *
    * // 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
  3. @m-cakir m-cakir revised this gist Oct 13, 2019. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion application.properties
    Original 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
  4. @m-cakir m-cakir revised this gist Oct 13, 2019. 2 changed files with 43 additions and 0 deletions.
    35 changes: 35 additions & 0 deletions ImageController.java
    Original 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();
    }
    *
    *
    *
    *
    *
    8 changes: 8 additions & 0 deletions application.properties
    Original 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
    *
    *
    *
  5. @m-cakir m-cakir revised this gist Oct 13, 2019. 1 changed file with 109 additions and 0 deletions.
    109 changes: 109 additions & 0 deletions FirebaseImageService.java
    Original 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;
    }

    }
  6. @m-cakir m-cakir revised this gist Oct 13, 2019. 1 changed file with 47 additions and 0 deletions.
    47 changes: 47 additions & 0 deletions IImageService.java
    Original 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();
    }
    }

    }
  7. @m-cakir m-cakir created this gist Oct 13, 2019.
    25 changes: 25 additions & 0 deletions pom.xml
    Original 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>
    *
    *
    *
    *
    *