Search in sources :

Example 21 with BlobEntry

use of com.pratilipi.data.type.BlobEntry in project pratilipi by Pratilipi.

the class PratilipiDataUtil method getPratilipiCover.

public static BlobEntry getPratilipiCover(Long pratilipiId, Integer width) throws UnexpectedServerException {
    Pratilipi pratilipi = pratilipiId == null ? null : DataAccessorFactory.getDataAccessor().getPratilipi(pratilipiId);
    BlobEntry blobEntry = null;
    if (pratilipi == null || pratilipi.getCoverImage() == null) {
        blobEntry = DataAccessorFactory.getBlobAccessor().getBlob("pratilipi/default/images/cover");
    } else {
        blobEntry = DataAccessorFactory.getBlobAccessor().getBlob("pratilipi/" + pratilipiId + "/images/" + pratilipi.getCoverImage());
        if (blobEntry == null)
            blobEntry = DataAccessorFactory.getBlobAccessor().getBlob("pratilipi-cover/original/" + pratilipiId);
    }
    if (width != null)
        blobEntry = ImageUtil.resize(blobEntry, width, (int) (1.5 * width));
    return blobEntry;
}
Also used : BlobEntry(com.pratilipi.data.type.BlobEntry) UserPratilipi(com.pratilipi.data.type.UserPratilipi) Pratilipi(com.pratilipi.data.type.Pratilipi)

Example 22 with BlobEntry

use of com.pratilipi.data.type.BlobEntry in project pratilipi by Pratilipi.

the class PratilipiDataUtil method updatePratilipiIndex.

public static void updatePratilipiIndex(Long pratilipiId) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
    if (pratilipi.isOldContent() && pratilipi.getContentType() == PratilipiContentType.PRATILIPI) {
        BlobAccessor blobAccessor = DataAccessorFactory.getBlobAccessor();
        BlobEntry blobEntry = blobAccessor.getBlob(CONTENT_FOLDER + "/" + pratilipiId);
        String index = null;
        Integer pageCount = 0;
        if (blobEntry != null) {
            String content = new String(blobEntry.getData(), Charset.forName("UTF-8"));
            PratilipiContentUtil pratilipiContentUtil = new PratilipiContentUtil(content);
            index = pratilipiContentUtil.generateIndex();
            pageCount = pratilipiContentUtil.getPageCount();
        }
        AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.PRATILIPI_UPDATE, pratilipi);
        boolean isChanged = false;
        if ((pratilipi.getIndex() == null && index != null) || (pratilipi.getIndex() != null && index == null) || (pratilipi.getIndex() != null && index != null && !pratilipi.getIndex().equals(index))) {
            pratilipi.setIndex(index);
            isChanged = true;
        }
        if (pratilipi.getPageCount() != pageCount) {
            pratilipi.setPageCount(pageCount);
            isChanged = true;
        }
        if (isChanged)
            pratilipi = dataAccessor.createOrUpdatePratilipi(pratilipi, auditLog);
    } else {
    // throw new InvalidArgumentException( "Index generation for " + pratilipi.getContentType() + " content type is not yet supported." );
    }
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) BlobEntry(com.pratilipi.data.type.BlobEntry) PratilipiContentUtil(com.pratilipi.common.util.PratilipiContentUtil) BlobAccessor(com.pratilipi.data.BlobAccessor) UserPratilipi(com.pratilipi.data.type.UserPratilipi) Pratilipi(com.pratilipi.data.type.Pratilipi) AuditLog(com.pratilipi.data.type.AuditLog)

Example 23 with BlobEntry

use of com.pratilipi.data.type.BlobEntry in project pratilipi by Pratilipi.

the class InitDataUtil method getInitBanner.

public static BlobEntry getInitBanner(Language language, String name, Integer width) throws InvalidArgumentException, UnexpectedServerException {
    BlobEntry blobEntry = DataAccessorFactory.getBlobAccessor().getBlob("init/banners/" + language.getCode() + "/" + name);
    if (blobEntry == null) {
        JsonObject errorMessages = new JsonObject();
        errorMessages.addProperty("name", "Invalid banner name.");
        throw new InvalidArgumentException(errorMessages);
    }
    if (width != null)
        blobEntry.setData(ImageUtil.resize(blobEntry.getData(), width));
    return blobEntry;
}
Also used : InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) BlobEntry(com.pratilipi.data.type.BlobEntry) JsonObject(com.google.gson.JsonObject)

Example 24 with BlobEntry

use of com.pratilipi.data.type.BlobEntry in project pratilipi by Pratilipi.

the class BlobAccessorWithMemcache method memcacheGet.

private BlobEntry memcacheGet(String fileName) {
    BlobEntry blobEntry = memcache.get(PREFIX + fileName);
    if (blobEntry == null)
        return null;
    if (blobEntry.getData().length == blobEntry.getDataLength())
        return blobEntry;
    int dataLength = (int) blobEntry.getDataLength();
    List<String> keyList = new ArrayList<String>((int) Math.ceil((double) dataLength / SEGMENT_SIZE));
    for (int i = 1; i < (int) Math.ceil((double) dataLength / SEGMENT_SIZE); i++) keyList.add(PREFIX + fileName + "?" + i);
    Map<String, Serializable> keySegmentMap = memcache.getAll(keyList);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        baos.write(blobEntry.getData());
        for (String key : keyList) {
            byte[] dataSegment = (byte[]) keySegmentMap.get(key);
            if (dataSegment == null)
                return null;
            baos.write(dataSegment);
        }
    } catch (IOException e) {
        logger.log(Level.SEVERE, "Failed to load and stitch blob data.", e);
        return null;
    }
    byte[] blobData = baos.toByteArray();
    if (blobData.length != dataLength) {
        logger.log(Level.SEVERE, "Blob size (" + blobData.length + ") did not match expected size (" + dataLength + ")");
        return null;
    }
    blobEntry.setData(blobData);
    return blobEntry;
}
Also used : Serializable(java.io.Serializable) BlobEntry(com.pratilipi.data.type.BlobEntry) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 25 with BlobEntry

use of com.pratilipi.data.type.BlobEntry in project pratilipi by Pratilipi.

the class BlobAccessorWithMemcache method getBlob.

@Override
public BlobEntry getBlob(String fileName) throws UnexpectedServerException {
    BlobEntry blobEntry = memcacheGet(fileName);
    if (blobEntry == null) {
        blobEntry = blobAccessor.getBlob(fileName);
        memcachePut(blobEntry);
    }
    return blobEntry;
}
Also used : BlobEntry(com.pratilipi.data.type.BlobEntry)

Aggregations

BlobEntry (com.pratilipi.data.type.BlobEntry)31 BlobAccessor (com.pratilipi.data.BlobAccessor)12 DataAccessor (com.pratilipi.data.DataAccessor)8 UnexpectedServerException (com.pratilipi.common.exception.UnexpectedServerException)7 Pratilipi (com.pratilipi.data.type.Pratilipi)7 GenericResponse (com.pratilipi.api.shared.GenericResponse)6 UserPratilipi (com.pratilipi.data.type.UserPratilipi)6 SimpleDateFormat (java.text.SimpleDateFormat)6 JsonObject (com.google.gson.JsonObject)5 Post (com.pratilipi.api.annotation.Post)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 DateFormat (java.text.DateFormat)5 Date (java.util.Date)5 Gson (com.google.gson.Gson)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 GsonBuilder (com.google.gson.GsonBuilder)3 Get (com.pratilipi.api.annotation.Get)3 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)3 PratilipiContentUtil (com.pratilipi.common.util.PratilipiContentUtil)3