Search in sources :

Example 16 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 17 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)

Example 18 with BlobEntry

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

the class PratilipiDocUtil method getContentImage.

public static BlobEntry getContentImage(long pratilipiId, String name, Integer width) throws InsufficientAccessException, UnexpectedServerException {
    Pratilipi pratilipi = DataAccessorFactory.getDataAccessor().getPratilipi(pratilipiId);
    if (!PratilipiDataUtil.hasAccessToReadPratilipiContent(pratilipi))
        throw new InsufficientAccessException();
    BlobEntry blobEntry = DataAccessorFactory.getBlobAccessor().getBlob("pratilipi/" + pratilipiId + "/images/" + name);
    if (width != null)
        blobEntry = ImageUtil.resize(blobEntry, width);
    return blobEntry;
}
Also used : BlobEntry(com.pratilipi.data.type.BlobEntry) UserPratilipi(com.pratilipi.data.type.UserPratilipi) Pratilipi(com.pratilipi.data.type.Pratilipi) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException)

Example 19 with BlobEntry

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

the class PratilipiDocUtil method _createPageletList.

private static List<Object[]> _createPageletList(Pratilipi pratilipi, Node node) throws UnexpectedServerException {
    List<Object[]> pageletList = new LinkedList<>();
    Object[] currPagelet = null;
    for (Node childNode : node.childNodes()) {
        if (childNode.nodeName().equals("body") || childNode.nodeName().equals("div") || childNode.nodeName().equals("p")) {
            currPagelet = null;
            List<Object[]> pList = _createPageletList(pratilipi, childNode);
            if (pList.size() == 0) {
                pageletList.add(new Object[] { PratilipiContentDoc.PageletType.HTML, "<br/>", null });
            } else {
                AlignmentType alignment = _getAlignment(childNode);
                if (alignment != null)
                    for (Object[] pagelet : pList) if (pagelet[2] == null && (pagelet[0] == PratilipiContentDoc.PageletType.TEXT || pagelet[0] == PratilipiContentDoc.PageletType.HTML))
                        pagelet[2] = alignment;
                pageletList.addAll(pList);
            }
        } else if (childNode.nodeName().equals("h1") || childNode.nodeName().equals("h2")) {
            String text = _extractText(childNode);
            if (text == null)
                continue;
            if (currPagelet != null && currPagelet[0] == PratilipiContentDoc.PageletType.HEAD) {
                currPagelet[1] = currPagelet[1] + " - " + text;
            } else {
                currPagelet = new Object[] { PratilipiContentDoc.PageletType.HEAD, text, null };
                pageletList.add(currPagelet);
            }
        } else if (childNode.nodeName().equals("img")) {
            currPagelet = null;
            BlobAccessor blobAccessor = DataAccessorFactory.getBlobAccessor();
            BlobEntry blobEntry = null;
            String imageUrl = childNode.attr("src");
            String imageName = null;
            if (imageUrl.indexOf("name=") != -1) {
                imageName = imageUrl.substring(imageUrl.indexOf("name=") + 5);
                if (imageName.indexOf('&') != -1)
                    imageName = imageName.substring(0, imageName.indexOf('&'));
                imageName = imageName.replace("%20", " ");
                String fileName = _createImageFullName(pratilipi.getId(), imageName);
                blobEntry = blobAccessor.getBlob(fileName);
                if (blobEntry == null) {
                    // Copying from old resource location
                    blobEntry = blobAccessor.getBlob("pratilipi-resource/" + pratilipi.getId() + "/" + imageName);
                    if (blobEntry != null) {
                        blobEntry.setName(fileName);
                        blobAccessor.createOrUpdateBlob(blobEntry);
                    }
                }
                if (blobEntry == null && imageUrl.indexOf("pratilipiId=") != -1) {
                    // Copying from old resource location of another Pratilipi
                    String pratilipiIdStr = imageUrl.substring(imageUrl.indexOf("pratilipiId=") + 12);
                    if (pratilipiIdStr.indexOf('&') != -1)
                        pratilipiIdStr = pratilipiIdStr.substring(0, pratilipiIdStr.indexOf('&'));
                    blobEntry = blobAccessor.getBlob("pratilipi-resource/" + pratilipiIdStr + "/" + imageName);
                    if (blobEntry != null) {
                        blobEntry.setName(fileName);
                        blobAccessor.createOrUpdateBlob(blobEntry);
                    }
                }
                if (blobEntry == null)
                    continue;
            } else if (imageUrl.startsWith("http")) {
                imageName = imageUrl.replaceAll("[:/.?=&+]+", "_");
                String fileName = _createImageFullName(pratilipi.getId(), imageName);
                blobEntry = blobAccessor.getBlob(fileName);
                if (blobEntry == null) {
                    blobEntry = HttpUtil.doGet(imageUrl);
                    if (!blobEntry.getMimeType().startsWith("image/"))
                        continue;
                    blobEntry.setName(fileName);
                    blobAccessor.createOrUpdateBlob(blobEntry);
                }
            } else if (imageUrl.startsWith("data:") && imageUrl.indexOf("base64") != -1) {
                imageName = UUID.randomUUID().toString();
                String mimeType = imageUrl.substring(5, imageUrl.indexOf(';'));
                String base64String = imageUrl.substring(imageUrl.indexOf("base64,") + 7);
                blobEntry = blobAccessor.newBlob(_createImageFullName(pratilipi.getId(), imageName), Base64.decodeBase64(base64String), mimeType);
                blobAccessor.createOrUpdateBlob(blobEntry);
            } else if (imageUrl.startsWith("file:///") || imageUrl.startsWith("C:")) {
                continue;
            }
            JsonObject imgData = new JsonObject();
            imgData.addProperty("name", imageName);
            imgData.addProperty("height", ImageUtil.getHeight(blobEntry));
            imgData.addProperty("width", ImageUtil.getWidth(blobEntry));
            pageletList.add(new Object[] { PratilipiContentDoc.PageletType.IMAGE, imgData, null });
        } else if (childNode.nodeName().equals("br")) {
            if (currPagelet != null && currPagelet[0] == PratilipiContentDoc.PageletType.HTML)
                currPagelet[1] = currPagelet[1] + "<br/>";
        } else {
            String text = _extractText(childNode);
            if (text == null)
                continue;
            if (childNode.nodeName().equals("b") || childNode.nodeName().equals("strong") || childNode.nodeName().equals("h3") || childNode.nodeName().equals("h4") || childNode.nodeName().equals("h5") || childNode.nodeName().equals("h6"))
                text = "<b>" + text + "</b>";
            if (currPagelet == null || currPagelet[0] != PratilipiContentDoc.PageletType.HTML) {
                currPagelet = new Object[] { PratilipiContentDoc.PageletType.HTML, text, null };
                pageletList.add(currPagelet);
            } else {
                currPagelet[1] = currPagelet[1] + " " + text;
            }
        }
    }
    return pageletList;
}
Also used : AlignmentType(com.pratilipi.data.type.PratilipiContentDoc.AlignmentType) Node(org.jsoup.nodes.Node) TextNode(org.jsoup.nodes.TextNode) BlobEntry(com.pratilipi.data.type.BlobEntry) BlobAccessor(com.pratilipi.data.BlobAccessor) JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject) LinkedList(java.util.LinkedList)

Example 20 with BlobEntry

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

the class PratilipiDataUtil method getPratilipiContent.

public static String getPratilipiContent(long pratilipiId, Integer chapterNo) throws InvalidArgumentException, InsufficientAccessException, UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
    if (!hasAccessToReadPratilipiContent(pratilipi))
        throw new InsufficientAccessException();
    BlobEntry blobEntry = DataAccessorFactory.getBlobAccessor().getBlob(CONTENT_FOLDER + "/" + pratilipiId);
    if (blobEntry == null)
        return null;
    String contentHtml = new String(blobEntry.getData(), Charset.forName("UTF-8"));
    PratilipiContentUtil pratilipiContentUtil = new PratilipiContentUtil(contentHtml);
    return pratilipiContentUtil.getContent(chapterNo);
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) BlobEntry(com.pratilipi.data.type.BlobEntry) PratilipiContentUtil(com.pratilipi.common.util.PratilipiContentUtil) UserPratilipi(com.pratilipi.data.type.UserPratilipi) Pratilipi(com.pratilipi.data.type.Pratilipi) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException)

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