Search in sources :

Example 6 with BlobEntry

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

the class AuthorDataUtil method getAuthorProfileImage.

public static BlobEntry getAuthorProfileImage(Long authorId, String version, Integer width) throws UnexpectedServerException {
    String coverImagePath = null;
    if (authorId != null && version != null)
        coverImagePath = "author/" + authorId + "/images/profile/" + version;
    else
        coverImagePath = "author/default/images/profile";
    BlobEntry blobEntry = DataAccessorFactory.getBlobAccessor().getBlob(coverImagePath);
    if (width != null)
        blobEntry = ImageUtil.resize(blobEntry, width, width);
    return blobEntry;
}
Also used : BlobEntry(com.pratilipi.data.type.BlobEntry)

Example 7 with BlobEntry

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

the class PratilipiDataUtil method updatePratilipiContent.

public static int updatePratilipiContent(long pratilipiId, int pageNo, PratilipiContentType contentType, Object pageContent, boolean insertNew) throws InvalidArgumentException, InsufficientAccessException, UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
    if (!hasAccessToUpdatePratilipiContent(pratilipi))
        throw new InsufficientAccessException();
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.PRATILIPI_UPDATE, pratilipi);
    BlobAccessor blobAccessor = DataAccessorFactory.getBlobAccessor();
    if (contentType == PratilipiContentType.PRATILIPI) {
        BlobEntry blobEntry = blobAccessor.getBlob(CONTENT_FOLDER + "/" + pratilipiId);
        if (blobEntry == null) {
            blobEntry = blobAccessor.newBlob(CONTENT_FOLDER + "/" + pratilipiId);
            blobEntry.setData("&nbsp".getBytes(Charset.forName("UTF-8")));
            blobEntry.setMimeType("text/html");
        }
        String content = new String(blobEntry.getData(), Charset.forName("UTF-8"));
        PratilipiContentUtil pratilipiContentUtil = new PratilipiContentUtil(content);
        content = pratilipiContentUtil.updateContent(pageNo, (String) pageContent, insertNew);
        int pageCount = pratilipiContentUtil.getPageCount();
        if (content.isEmpty()) {
            content = "&nbsp";
            pageCount = 1;
        }
        blobEntry.setData(content.getBytes(Charset.forName("UTF-8")));
        blobAccessor.createOrUpdateBlob(blobEntry);
        pratilipi.setPageCount(pageCount);
        if (insertNew)
            auditLog.setEventComment("Added new page " + pageNo + " in Pratilpi content.");
        else if (!((String) pageContent).isEmpty())
            auditLog.setEventComment("Updated page " + pageNo + " in Pratilpi content.");
        else
            auditLog.setEventComment("Deleted page " + pageNo + " in Pratilpi content.");
    } else if (contentType == PratilipiContentType.IMAGE) {
        BlobEntry blobEntry = (BlobEntry) pageContent;
        blobEntry.setName(IMAGE_CONTENT_FOLDER + "/" + pratilipiId + "/" + pageNo);
        blobAccessor.createOrUpdateBlob(blobEntry);
        if (pageNo > (int) pratilipi.getPageCount())
            pratilipi.setPageCount(pageNo);
        auditLog.setEventComment("Uploaded page " + pageNo + " in Image content.");
    } else {
        throw new InvalidArgumentException(contentType + " content type is not yet supported.");
    }
    pratilipi.setLastUpdated(new Date());
    pratilipi = dataAccessor.createOrUpdatePratilipi(pratilipi, auditLog);
    return pratilipi.getPageCount();
}
Also used : InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) 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) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) AuditLog(com.pratilipi.data.type.AuditLog) Date(java.util.Date)

Example 8 with BlobEntry

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

the class PratilipiDocUtil method updatePratilipiContent.

public static void updatePratilipiContent(Long pratilipiId) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    BlobAccessor blobAccessor = DataAccessorFactory.getBlobAccessor();
    DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
    PratilipiContentDoc pcDoc = DataAccessorFactory.getDocAccessor().newPratilipiContentDoc();
    Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
    if (!pratilipi.isOldContent()) {
        return;
    } else if (pratilipi.getContentType() == PratilipiContentType.PRATILIPI) {
        BlobEntry blobEntry = blobAccessor.getBlob("pratilipi-content/pratilipi/" + pratilipiId);
        if (blobEntry == null)
            return;
        String contentHtml = new String(blobEntry.getData(), Charset.forName("UTF-8"));
        List<Object[]> pageletList = _createPageletList(pratilipi, Jsoup.parse(contentHtml).body());
        if (pageletList.size() > 0) {
            PratilipiContentDoc.Chapter chapter = null;
            if (pageletList.get(0)[0] != PratilipiContentDoc.PageletType.HEAD)
                chapter = pcDoc.addChapter(pratilipi.getTitle() == null ? pratilipi.getTitleEn() : pratilipi.getTitle());
            for (Object[] pagelet : pageletList) {
                if (pagelet[0] == PratilipiContentDoc.PageletType.HEAD) {
                    chapter = pcDoc.addChapter((String) pagelet[1]);
                } else {
                    PratilipiContentDoc.Page page = chapter.getPage(1);
                    if (page == null)
                        page = chapter.addPage();
                    page.addPagelet((PratilipiContentDoc.PageletType) pagelet[0], pagelet[1], (PratilipiContentDoc.AlignmentType) pagelet[2]);
                }
            }
        }
    } else if (pratilipi.getContentType() == PratilipiContentType.IMAGE) {
        for (int i = 1; i <= pratilipi.getPageCount(); i++) {
            BlobEntry blobEntry = blobAccessor.getBlob("pratilipi/" + pratilipiId + "/images/" + i);
            if (pratilipi.getId() == 5639838220943360L && i <= 5)
                // Skipping first 5 pages as per Shally's request
                continue;
            else if (pratilipi.getId() == 5749258686824448L && i <= 4)
                // Skipping first 4 pages as per Shally's request
                continue;
            else if (pratilipi.getId() == 5486454792781824L && i <= 1)
                // Skipping first page as per Shally's request
                continue;
            else if (blobEntry == null && pratilipi.getId() == 5768181499035648L)
                // Skipping missing pages as per Dileepan's request
                continue;
            JsonObject imgData = new JsonObject();
            imgData.addProperty("name", i + "");
            imgData.addProperty("height", ImageUtil.getHeight(blobEntry));
            imgData.addProperty("width", ImageUtil.getWidth(blobEntry));
            pcDoc.addChapter(null).addPage().addPagelet(PratilipiContentDoc.PageletType.IMAGE, imgData);
        }
    } else {
        throw new UnexpectedServerException("ContentType " + pratilipi.getContentType() + " is not supported !");
    }
    docAccessor.save(pratilipiId, pcDoc);
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) DocAccessor(com.pratilipi.data.DocAccessor) BlobEntry(com.pratilipi.data.type.BlobEntry) Chapter(com.pratilipi.data.type.PratilipiContentDoc.Chapter) JsonObject(com.google.gson.JsonObject) Page(com.pratilipi.data.type.Page) AlignmentType(com.pratilipi.data.type.PratilipiContentDoc.AlignmentType) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) PageletType(com.pratilipi.data.type.PratilipiContentDoc.PageletType) BlobAccessor(com.pratilipi.data.BlobAccessor) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) JsonObject(com.google.gson.JsonObject) UserPratilipi(com.pratilipi.data.type.UserPratilipi) Pratilipi(com.pratilipi.data.type.Pratilipi) PratilipiContentDoc(com.pratilipi.data.type.PratilipiContentDoc)

Example 9 with BlobEntry

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

the class PratilipiDocUtil method _createImageData.

private static JsonObject _createImageData(Long pratilipiId, Node imageNode) throws UnexpectedServerException {
    BlobAccessor blobAccessor = DataAccessorFactory.getBlobAccessor();
    String imageName = null;
    BlobEntry blobEntry = null;
    String imageUrl = imageNode.attr("src");
    if (imageUrl.indexOf("pratilipiId=" + pratilipiId) == -1 || imageUrl.indexOf("name=") == -1) {
        imageName = imageUrl.replaceAll("[:/.?=&+]+", "_");
        String fileName = _createImageFullName(pratilipiId, imageName);
        blobEntry = blobAccessor.getBlob(fileName);
        if (blobEntry == null) {
            blobEntry = HttpUtil.doGet(imageUrl);
            if (!blobEntry.getMimeType().startsWith("image/")) {
                logger.log(Level.SEVERE, "Ignoring image " + imageUrl);
                return null;
            }
            blobEntry.setName(fileName);
            blobAccessor.createOrUpdateBlob(blobEntry);
        }
    } else {
        imageName = imageUrl.substring(imageUrl.indexOf("name=") + 5);
        if (imageName.indexOf('&') != -1)
            imageName = imageName.substring(0, imageName.indexOf('&'));
        imageName = imageName.replace("%20", " ");
        blobEntry = blobAccessor.getBlob(_createImageFullName(pratilipiId, imageName));
    }
    int width = ImageUtil.getWidth(blobEntry);
    int height = ImageUtil.getHeight(blobEntry);
    int setWidth = width;
    if (imageNode.hasAttr("width") && !imageNode.attr("width").trim().isEmpty())
        setWidth = Integer.parseInt(imageNode.attr("width").trim());
    JsonObject imgData = new JsonObject();
    imgData.addProperty("name", imageName);
    imgData.addProperty("width", width);
    imgData.addProperty("height", height);
    imgData.addProperty("ratio", (double) setWidth / width);
    return imgData;
}
Also used : BlobEntry(com.pratilipi.data.type.BlobEntry) BlobAccessor(com.pratilipi.data.BlobAccessor) JsonObject(com.google.gson.JsonObject)

Example 10 with BlobEntry

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

the class PratilipiDocUtil method updatePratilipiGoogleAnalyticsPageViews.

public static List<Long> updatePratilipiGoogleAnalyticsPageViews(int year, int month, int day) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    BlobAccessor blobAccessor = DataAccessorFactory.getBlobAccessor();
    Gson gson = new Gson();
    String dateStr = year + (month < 10 ? "-0" + month : "-" + month) + (day < 10 ? "-0" + day : "-" + day);
    String fileName = "pratilipi-google-analytics/page-views/" + dateStr;
    BlobEntry blobEntry = blobAccessor.getBlob(fileName);
    if (blobEntry == null) {
        try {
            blobEntry = blobAccessor.newBlob(fileName, "{}".getBytes("UTF-8"), "application/json");
        } catch (UnsupportedEncodingException e) {
            logger.log(Level.SEVERE, e.getMessage());
            throw new UnexpectedServerException();
        }
    }
    @SuppressWarnings("serial") Map<String, Integer> oldPageViewsMap = gson.fromJson(new String(blobEntry.getData(), Charset.forName("UTF-8")), new TypeToken<Map<String, Integer>>() {
    }.getType());
    Map<String, Integer> newPageViewsMap = GoogleAnalyticsApi.getPageViews(dateStr);
    Map<String, Integer> diffPageViewsMap = new HashMap<>();
    for (Entry<String, Integer> entry : newPageViewsMap.entrySet()) if (!entry.getValue().equals(oldPageViewsMap.get(entry.getKey())))
        diffPageViewsMap.put(entry.getKey(), entry.getValue());
    Map<Long, Integer> pageViewsMap = new HashMap<>();
    Map<Long, Integer> readPageViewsMap = new HashMap<>();
    for (Entry<String, Integer> entry : diffPageViewsMap.entrySet()) {
        String uri = entry.getKey();
        if (!uri.startsWith("/read?id=")) {
            if (uri.indexOf('?') != -1)
                uri = uri.substring(0, uri.indexOf('?'));
            Page page = dataAccessor.getPage(uri);
            if (page != null && page.getType() == PageType.PRATILIPI) {
                Long pratilpiId = page.getPrimaryContentId();
                if (pageViewsMap.get(pratilpiId) == null)
                    pageViewsMap.put(pratilpiId, entry.getValue());
                else
                    pageViewsMap.put(pratilpiId, pageViewsMap.get(pratilpiId) + entry.getValue());
            }
        } else {
            // Reader
            String patilipiIdStr = uri.indexOf('&') == -1 ? uri.substring("/read?id=".length()) : uri.substring("/read?id=".length(), uri.indexOf('&'));
            try {
                Long pratilpiId = Long.parseLong(patilipiIdStr);
                if (readPageViewsMap.get(pratilpiId) == null)
                    readPageViewsMap.put(pratilpiId, entry.getValue());
                else
                    readPageViewsMap.put(pratilpiId, readPageViewsMap.get(pratilpiId) + entry.getValue());
            } catch (NumberFormatException e) {
                logger.log(Level.SEVERE, "Exception while processing reader uri " + uri, e);
            }
        }
    }
    for (Entry<Long, Integer> entry : pageViewsMap.entrySet()) {
        if (readPageViewsMap.get(entry.getKey()) == null) {
            updatePratilipiGoogleAnalyticsPageViews(entry.getKey(), year, month, day, entry.getValue(), 0);
        } else {
            updatePratilipiGoogleAnalyticsPageViews(entry.getKey(), year, month, day, entry.getValue(), readPageViewsMap.get(entry.getKey()));
            readPageViewsMap.remove(entry.getKey());
        }
    }
    for (Entry<Long, Integer> entry : readPageViewsMap.entrySet()) updatePratilipiGoogleAnalyticsPageViews(entry.getKey(), year, month, day, 0, entry.getValue());
    if (diffPageViewsMap.size() > 0) {
        try {
            blobEntry.setData(gson.toJson(newPageViewsMap).getBytes("UTF-8"));
            blobAccessor.createOrUpdateBlob(blobEntry);
        } catch (UnsupportedEncodingException e) {
            logger.log(Level.SEVERE, e.getMessage());
            throw new UnexpectedServerException();
        }
    }
    ArrayList<Long> updatedPratilipiIdList = new ArrayList<>(pageViewsMap.size() + readPageViewsMap.size());
    updatedPratilipiIdList.addAll(pageViewsMap.keySet());
    updatedPratilipiIdList.addAll(readPageViewsMap.keySet());
    return updatedPratilipiIdList;
}
Also used : HashMap(java.util.HashMap) DataAccessor(com.pratilipi.data.DataAccessor) BlobEntry(com.pratilipi.data.type.BlobEntry) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Page(com.pratilipi.data.type.Page) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) TypeToken(com.google.common.reflect.TypeToken) BlobAccessor(com.pratilipi.data.BlobAccessor)

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