Search in sources :

Example 6 with BlobAccessor

use of com.pratilipi.data.BlobAccessor 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 7 with BlobAccessor

use of com.pratilipi.data.BlobAccessor 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;
    // Getting imageUrl from the src attribute
    String imageUrl = imageNode.attr("src");
    // If pratilipiId is missing or name is missing from the sourceUrl
    if (imageUrl.indexOf("pratilipiId=" + pratilipiId) == -1 || imageUrl.indexOf("name=") == -1) {
        // Converting all special characters to underscore
        imageName = imageUrl.replaceAll("[:/.?=&+]+", "_");
        // Getting the fileName
        String fileName = _createImageFullName(pratilipiId, imageName);
        // Trying to get image from its name
        blobEntry = blobAccessor.getBlob(fileName);
        if (blobEntry == null) {
            // If it doesn't exist in storage, make a http call to the image url and get the image
            blobEntry = HttpUtil.doGet(imageUrl);
            // Check for the mime type
            if (!blobEntry.getMimeType().startsWith("image/")) {
                logger.log(Level.SEVERE, "Ignoring image " + imageUrl);
                return null;
            }
            // Setting the fileName
            blobEntry.setName(fileName);
            // Saving the image
            blobAccessor.createOrUpdateBlob(blobEntry);
        }
    // The image already exists
    } else {
        // Getting the image by name
        imageName = imageUrl.substring(imageUrl.indexOf("name=") + 5);
        // Stripping out all other query parameters
        if (imageName.indexOf('&') != -1)
            imageName = imageName.substring(0, imageName.indexOf('&'));
        // space check
        imageName = imageName.replace("%20", " ");
        // getting image by name
        blobEntry = blobAccessor.getBlob(_createImageFullName(pratilipiId, imageName));
    }
    // At this point, there will be an image
    // width and height are the original width and height
    int width = ImageUtil.getWidth(blobEntry);
    int height = ImageUtil.getHeight(blobEntry);
    // If the image is resized from frontend, 1000*1000px to 500*500px, it is available in the 'width' attribute on the img tag
    int setWidth = width;
    if (imageNode.hasAttr("width") && !imageNode.attr("width").trim().isEmpty())
        setWidth = Integer.parseInt(imageNode.attr("width").trim());
    // Creating the jsonObject
    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 8 with BlobAccessor

use of com.pratilipi.data.BlobAccessor in project pratilipi by Pratilipi.

the class AuthorDataUtil method saveAuthorCoverImage.

public static String saveAuthorCoverImage(Long authorId, BlobEntry blobEntry) throws InsufficientAccessException, UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Author author = dataAccessor.getAuthor(authorId);
    if (!hasAccessToUpdateAuthorData(author, null))
        throw new InsufficientAccessException();
    String coverImageName = new Date().getTime() + "";
    BlobAccessor blobAccessor = DataAccessorFactory.getBlobAccessor();
    blobEntry.setName("author/" + authorId + "/images/cover/" + coverImageName);
    blobAccessor.createOrUpdateBlob(blobEntry);
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.AUTHOR_UPDATE, author);
    author.setCoverImage(coverImageName);
    author.setLastUpdated(new Date());
    author = dataAccessor.createOrUpdateAuthor(author, auditLog);
    return createAuthorCoverImageUrl(author);
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) BlobAccessor(com.pratilipi.data.BlobAccessor) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException)

Example 9 with BlobAccessor

use of com.pratilipi.data.BlobAccessor in project pratilipi by Pratilipi.

the class PratilipiDataUtil method savePratilipiCover.

public static String savePratilipiCover(Long pratilipiId, BlobEntry blobEntry) throws InsufficientAccessException, UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
    if (!hasAccessToUpdatePratilipiData(pratilipi, null))
        throw new InsufficientAccessException();
    String coverImageName = new Date().getTime() + "";
    BlobAccessor blobAccessor = DataAccessorFactory.getBlobAccessor();
    blobEntry.setName("pratilipi/" + pratilipiId + "/images/" + coverImageName);
    blobAccessor.createOrUpdateBlob(blobEntry);
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.PRATILIPI_UPDATE, pratilipi);
    pratilipi.setCoverImage(coverImageName);
    pratilipi.setLastUpdated(new Date());
    pratilipi = dataAccessor.createOrUpdatePratilipi(pratilipi, auditLog);
    return createPratilipiCoverUrl(pratilipi);
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) BlobAccessor(com.pratilipi.data.BlobAccessor) Pratilipi(com.pratilipi.data.type.Pratilipi) UserPratilipi(com.pratilipi.data.type.UserPratilipi) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) Date(java.util.Date) AuditLog(com.pratilipi.data.type.AuditLog)

Example 10 with BlobAccessor

use of com.pratilipi.data.BlobAccessor 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) BlobAccessor(com.pratilipi.data.BlobAccessor) Pratilipi(com.pratilipi.data.type.Pratilipi) UserPratilipi(com.pratilipi.data.type.UserPratilipi) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) AuditLog(com.pratilipi.data.type.AuditLog) Date(java.util.Date)

Aggregations

BlobAccessor (com.pratilipi.data.BlobAccessor)19 DataAccessor (com.pratilipi.data.DataAccessor)13 BlobEntry (com.pratilipi.data.type.BlobEntry)12 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)7 Date (java.util.Date)7 Pratilipi (com.pratilipi.data.type.Pratilipi)6 SimpleDateFormat (java.text.SimpleDateFormat)6 UserPratilipi (com.pratilipi.data.type.UserPratilipi)5 DateFormat (java.text.DateFormat)5 AuditLog (com.pratilipi.data.type.AuditLog)4 Gson (com.google.gson.Gson)3 JsonObject (com.google.gson.JsonObject)3 Get (com.pratilipi.api.annotation.Get)3 GenericResponse (com.pratilipi.api.shared.GenericResponse)3 GsonBuilder (com.google.gson.GsonBuilder)2 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)2 UnexpectedServerException (com.pratilipi.common.exception.UnexpectedServerException)2 DocAccessor (com.pratilipi.data.DocAccessor)2 Author (com.pratilipi.data.type.Author)2 Page (com.pratilipi.data.type.Page)2