Search in sources :

Example 1 with GcsFileMetadata

use of com.google.appengine.tools.cloudstorage.GcsFileMetadata in project pratilipi by Pratilipi.

the class BlobAccessorGcsImpl method getBlob.

@Override
public BlobEntry getBlob(String fileName) throws UnexpectedServerException {
    GcsFilename gcsFileName = new GcsFilename(bucketName, fileName);
    try {
        GcsFileMetadata gcsFileMetadata = gcsService.getMetadata(gcsFileName);
        if (gcsFileMetadata == null)
            return null;
        if (gcsFileMetadata.getLength() == 0)
            return null;
        GcsInputChannel gcsInputChannel = gcsService.openReadChannel(gcsFileName, 0);
        ByteBuffer byteBuffer = ByteBuffer.allocate((int) gcsFileMetadata.getLength());
        gcsInputChannel.read(byteBuffer);
        if (byteBuffer.position() != gcsFileMetadata.getLength()) {
            logger.log(Level.SEVERE, "Byte buffer size of " + byteBuffer.position() + " is not same as content lenght of " + gcsFileMetadata.getLength());
            throw new UnexpectedServerException();
        }
        return new BlobEntryGcsImpl(byteBuffer, gcsFileMetadata);
    } catch (IOException ex) {
        logger.log(Level.INFO, "Failed to fetch blob with name '" + fileName + "'", ex);
        throw new UnexpectedServerException();
    }
}
Also used : UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) BlobEntryGcsImpl(com.pratilipi.data.type.gcs.BlobEntryGcsImpl) IOException(java.io.IOException) GcsInputChannel(com.google.appengine.tools.cloudstorage.GcsInputChannel) GcsFileMetadata(com.google.appengine.tools.cloudstorage.GcsFileMetadata) ByteBuffer(java.nio.ByteBuffer) GcsFilename(com.google.appengine.tools.cloudstorage.GcsFilename)

Example 2 with GcsFileMetadata

use of com.google.appengine.tools.cloudstorage.GcsFileMetadata in project iosched by google.

the class CloudFileManager method readFileAsJsonObject.

public JsonObject readFileAsJsonObject(GcsFilename file) throws IOException {
    GcsFileMetadata metadata = gcsService.getMetadata(file);
    if (metadata == null) {
        if (SystemProperty.environment.value() == SystemProperty.Environment.Value.Development) {
            // In the development server, try to fetch files on cloud storage via HTTP
            Logger.getAnonymousLogger().info("fetching " + file.getObjectName() + " at " + Config.CLOUD_STORAGE_BASE_URL + file.getObjectName());
            return RemoteJsonHelper.fetchJsonFromPublicURL(Config.CLOUD_STORAGE_BASE_URL + file.getObjectName());
        }
        return null;
    }
    GcsInputChannel readChannel = null;
    try {
        readChannel = gcsService.openReadChannel(file, 0);
        JsonElement element = new JsonParser().parse(Channels.newReader(readChannel, DEFAULT_CHARSET_NAME));
        return element.getAsJsonObject();
    } finally {
        if (readChannel != null) {
            readChannel.close();
        }
    }
}
Also used : JsonElement(com.google.gson.JsonElement) GcsInputChannel(com.google.appengine.tools.cloudstorage.GcsInputChannel) GcsFileMetadata(com.google.appengine.tools.cloudstorage.GcsFileMetadata) JsonParser(com.google.gson.JsonParser)

Example 3 with GcsFileMetadata

use of com.google.appengine.tools.cloudstorage.GcsFileMetadata in project activityinfo by bedatadriven.

the class GcsBlobServlet method doGet.

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        BlobId blobId = new BlobId(request.getParameter("blobId"));
        ResourceId resourceId = ResourceId.valueOf(request.getParameter("resourceId"));
        Preconditions.checkState(!Strings.isNullOrEmpty(blobId.asString()));
        Preconditions.checkState(!Strings.isNullOrEmpty(resourceId.asString()));
        AuthenticatedUser user = authProvider.get();
        service.assertNotAnonymousUser(user);
        service.assertHasAccess(user, blobId, resourceId);
        service.assertBlobExists(blobId);
        GcsFileMetadata metadata = GcsServiceFactory.createGcsService().getMetadata(new GcsFilename(service.getBucketName(), blobId.asString()));
        response.setHeader("Content-Disposition", metadata.getOptions().getContentDisposition());
        response.setContentType(metadata.getOptions().getMimeType());
        BlobstoreServiceFactory.getBlobstoreService().serve(service.blobKey(blobId), response);
    } catch (WebApplicationException e) {
        sendError(response, e);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) ResourceId(org.activityinfo.model.resource.ResourceId) BlobId(org.activityinfo.store.spi.BlobId) AuthenticatedUser(org.activityinfo.legacy.shared.AuthenticatedUser) GcsFileMetadata(com.google.appengine.tools.cloudstorage.GcsFileMetadata) GcsFilename(com.google.appengine.tools.cloudstorage.GcsFilename)

Aggregations

GcsFileMetadata (com.google.appengine.tools.cloudstorage.GcsFileMetadata)3 GcsFilename (com.google.appengine.tools.cloudstorage.GcsFilename)2 GcsInputChannel (com.google.appengine.tools.cloudstorage.GcsInputChannel)2 JsonElement (com.google.gson.JsonElement)1 JsonParser (com.google.gson.JsonParser)1 UnexpectedServerException (com.pratilipi.common.exception.UnexpectedServerException)1 BlobEntryGcsImpl (com.pratilipi.data.type.gcs.BlobEntryGcsImpl)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 AuthenticatedUser (org.activityinfo.legacy.shared.AuthenticatedUser)1 ResourceId (org.activityinfo.model.resource.ResourceId)1 BlobId (org.activityinfo.store.spi.BlobId)1