package com.google.sticknotesbackend.servlets;

import com.google.cloud.storage.Acl;
import com.google.cloud.storage.Acl.Role;
import com.google.cloud.storage.Acl.User;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import org.apache.commons.io.IOUtils;

/**
 * Gets a base64 JSON array
 */
@WebServlet("api/file-upload/")
@MultipartConfig
public class ImageUploadServlet extends HttpServlet {
  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String projectId = "notesboard";
    String bucketName = "notesboard-file-uploads";
    // get the uploaded file
    Part filePart = request.getPart("file"); // Retrieves <input type="file" name="file">
    String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString(); // MSIE fix.
    InputStream fileContent = filePart.getInputStream();
    // generate filename for the uploaded file by prepending UUID to the filename
    fileName = UUID.randomUUID().toString() + fileName;
    // get bytes
    byte[] bytes = IOUtils.toByteArray(fileContent);
    // get storage service
    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    BlobId blobId = BlobId.of(bucketName, fileName);
    BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
    // upload file
    String linkToFile = storage.create(blobInfo, bytes).getMediaLink();
    response.getWriter().print(linkToFile);
  }
}

@@ -1939,28 +1939,8 @@
 %0A   
- String linkToFile =
  sto
@@ -1971,24 +1971,136 @@
 tes)
-.getMediaLink();
+;%0A    // generate a link to the file%0A    String linkToFile = %22https://storage.cloud.google.com/%22 + bucketName + %22/%22 + fileName; 
 %0A   

Fix uploaded file link