Search in sources :

Example 1 with BlobstoreService

use of com.google.appengine.api.blobstore.BlobstoreService in project java-docs-samples by GoogleCloudPlatform.

the class ImagesServlet method doGet.

// [END gcs]
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    // [START original_image]
    // Read the image.jpg resource into a ByteBuffer.
    FileInputStream fileInputStream = new FileInputStream(new File("WEB-INF/image.jpg"));
    FileChannel fileChannel = fileInputStream.getChannel();
    ByteBuffer byteBuffer = ByteBuffer.allocate((int) fileChannel.size());
    fileChannel.read(byteBuffer);
    byte[] imageBytes = byteBuffer.array();
    // Write the original image to Cloud Storage
    gcsService.createOrReplace(new GcsFilename(bucket, "image.jpeg"), new GcsFileOptions.Builder().mimeType("image/jpeg").build(), ByteBuffer.wrap(imageBytes));
    // [END original_image]
    // [START resize]
    // Get an instance of the imagesService we can use to transform images.
    ImagesService imagesService = ImagesServiceFactory.getImagesService();
    // Make an image directly from a byte array, and transform it.
    Image image = ImagesServiceFactory.makeImage(imageBytes);
    Transform resize = ImagesServiceFactory.makeResize(100, 50);
    Image resizedImage = imagesService.applyTransform(resize, image);
    // Write the transformed image back to a Cloud Storage object.
    gcsService.createOrReplace(new GcsFilename(bucket, "resizedImage.jpeg"), new GcsFileOptions.Builder().mimeType("image/jpeg").build(), ByteBuffer.wrap(resizedImage.getImageData()));
    // [END resize]
    // [START rotate]
    // Make an image from a Cloud Storage object, and transform it.
    BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
    BlobKey blobKey = blobstoreService.createGsBlobKey("/gs/" + bucket + "/image.jpeg");
    Image blobImage = ImagesServiceFactory.makeImageFromBlob(blobKey);
    Transform rotate = ImagesServiceFactory.makeRotate(90);
    Image rotatedImage = imagesService.applyTransform(rotate, blobImage);
    // Write the transformed image back to a Cloud Storage object.
    gcsService.createOrReplace(new GcsFilename(bucket, "rotatedImage.jpeg"), new GcsFileOptions.Builder().mimeType("image/jpeg").build(), ByteBuffer.wrap(rotatedImage.getImageData()));
    // [END rotate]
    // Output some simple HTML to display the images we wrote to Cloud Storage
    // in the browser.
    PrintWriter out = resp.getWriter();
    out.println("<html><body>\n");
    out.println("<img src='//storage.cloud.google.com/" + bucket + "/image.jpeg' alt='AppEngine logo' />");
    out.println("<img src='//storage.cloud.google.com/" + bucket + "/resizedImage.jpeg' alt='AppEngine logo resized' />");
    out.println("<img src='//storage.cloud.google.com/" + bucket + "/rotatedImage.jpeg' alt='AppEngine logo rotated' />");
    out.println("</body></html>\n");
}
Also used : BlobstoreService(com.google.appengine.api.blobstore.BlobstoreService) FileChannel(java.nio.channels.FileChannel) ImagesService(com.google.appengine.api.images.ImagesService) Image(com.google.appengine.api.images.Image) ByteBuffer(java.nio.ByteBuffer) FileInputStream(java.io.FileInputStream) BlobKey(com.google.appengine.api.blobstore.BlobKey) GcsFileOptions(com.google.appengine.tools.cloudstorage.GcsFileOptions) Transform(com.google.appengine.api.images.Transform) File(java.io.File) GcsFilename(com.google.appengine.tools.cloudstorage.GcsFilename) PrintWriter(java.io.PrintWriter)

Example 2 with BlobstoreService

use of com.google.appengine.api.blobstore.BlobstoreService in project cryptonomica by Cryptonomica.

the class CloudStorageService method serveFileFromCloudStorage.

/*
    *  server file from Cloud Storage
    * */
public static void serveFileFromCloudStorage(String bucketName, String objectName, String contentType, HttpServletResponse resp) throws IOException {
    // GcsFilename fileName = getFileName(req);
    GcsFilename fileName = new GcsFilename(bucketName, objectName);
    // resp.setContentType("video/webm");
    resp.setContentType(contentType);
    if (SERVE_USING_BLOBSTORE_API) {
        BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
        BlobKey blobKey = blobstoreService.createGsBlobKey("/gs/" + fileName.getBucketName() + "/" + fileName.getObjectName());
        blobstoreService.serve(blobKey, resp);
    } else {
        GcsInputChannel readChannel = gcsService.openPrefetchingReadChannel(fileName, 0, BUFFER_SIZE);
        copy(Channels.newInputStream(readChannel), resp.getOutputStream());
    }
}
Also used : BlobstoreService(com.google.appengine.api.blobstore.BlobstoreService) BlobKey(com.google.appengine.api.blobstore.BlobKey)

Example 3 with BlobstoreService

use of com.google.appengine.api.blobstore.BlobstoreService in project teammates by TEAMMATES.

the class ImageResult method send.

@Override
public void send(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    if (blobKey.isEmpty()) {
        resp.sendRedirect(Const.SystemParams.DEFAULT_PROFILE_PICTURE_PATH);
    } else {
        resp.setContentType("image/png");
        BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
        blobstoreService.serve(new BlobKey(blobKey), resp);
    }
}
Also used : BlobstoreService(com.google.appengine.api.blobstore.BlobstoreService) BlobKey(com.google.appengine.api.blobstore.BlobKey)

Example 4 with BlobstoreService

use of com.google.appengine.api.blobstore.BlobstoreService in project teammates by TEAMMATES.

the class PublicImageServlet method doPost.

@SuppressWarnings("unchecked")
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) {
    String url = HttpRequestHelper.getRequestedUrl(req);
    UserType userType = new GateKeeper().getCurrentUser();
    Map<String, String[]> requestParameters = req.getParameterMap();
    String blobKey = HttpRequestHelper.getValueFromParamMap(requestParameters, Const.ParamsNames.BLOB_KEY);
    Assumption.assertPostParamNotNull(Const.ParamsNames.BLOB_KEY, blobKey);
    try {
        if (blobKey.isEmpty()) {
            String message = "Failed to serve image with URL : blobKey is missing";
            Map<String, String[]> params = HttpRequestHelper.getParameterMap(req);
            log.info(new LogMessageGenerator().generateBasicActivityLogMessage(url, params, message, userType));
            resp.sendError(1, "No image found");
        } else {
            resp.setContentType("image/png");
            BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
            blobstoreService.serve(new BlobKey(blobKey), resp);
            // TODO : restrict image request to those "public" files only
            String message = "Public image request with URL: <br>" + "<a href=\"" + url + "\" target=\"_blank\" rel=\"noopener noreferrer\" >" + url + "</a>";
            Map<String, String[]> params = HttpRequestHelper.getParameterMap(req);
            log.info(new LogMessageGenerator().generateBasicActivityLogMessage(url, params, message, userType));
        }
    } catch (IOException ioe) {
        Map<String, String[]> params = HttpRequestHelper.getParameterMap(req);
        log.warning(new LogMessageGenerator().generateActionFailureLogMessage(url, params, ioe, userType));
    } catch (Exception e) {
        log.severe("Exception occured while performing " + Const.PublicActionNames.PUBLIC_IMAGE_SERVE_ACTION + ": " + TeammatesException.toStringWithStackTrace(e));
    }
}
Also used : BlobstoreService(com.google.appengine.api.blobstore.BlobstoreService) BlobKey(com.google.appengine.api.blobstore.BlobKey) LogMessageGenerator(teammates.common.util.LogMessageGenerator) GateKeeper(teammates.logic.api.GateKeeper) IOException(java.io.IOException) UserType(teammates.common.datatransfer.UserType) Map(java.util.Map) IOException(java.io.IOException) TeammatesException(teammates.common.exception.TeammatesException)

Aggregations

BlobKey (com.google.appengine.api.blobstore.BlobKey)4 BlobstoreService (com.google.appengine.api.blobstore.BlobstoreService)4 Image (com.google.appengine.api.images.Image)1 ImagesService (com.google.appengine.api.images.ImagesService)1 Transform (com.google.appengine.api.images.Transform)1 GcsFileOptions (com.google.appengine.tools.cloudstorage.GcsFileOptions)1 GcsFilename (com.google.appengine.tools.cloudstorage.GcsFilename)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 ByteBuffer (java.nio.ByteBuffer)1 FileChannel (java.nio.channels.FileChannel)1 Map (java.util.Map)1 UserType (teammates.common.datatransfer.UserType)1 TeammatesException (teammates.common.exception.TeammatesException)1 LogMessageGenerator (teammates.common.util.LogMessageGenerator)1 GateKeeper (teammates.logic.api.GateKeeper)1