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;
}
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;
}
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);
}
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);
}
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(" ".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 = " ";
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();
}
Aggregations