use of ai.elimu.model.contributor.ImageContributionEvent in project webapp by elimu-ai.
the class ImageEditController method handleSubmit.
@RequestMapping(value = "/{id}", method = RequestMethod.POST)
public String handleSubmit(HttpServletRequest request, HttpSession session, Image image, @RequestParam("bytes") MultipartFile multipartFile, BindingResult result, Model model) {
logger.info("handleSubmit");
if (StringUtils.isBlank(image.getTitle())) {
result.rejectValue("title", "NotNull");
} else {
Image existingImage = imageDao.read(image.getTitle());
if ((existingImage != null) && !existingImage.getId().equals(image.getId())) {
result.rejectValue("title", "NonUnique");
}
}
try {
byte[] bytes = multipartFile.getBytes();
if (multipartFile.isEmpty() || (bytes == null) || (bytes.length == 0)) {
result.rejectValue("bytes", "NotNull");
} else {
String originalFileName = multipartFile.getOriginalFilename();
logger.info("originalFileName: " + originalFileName);
byte[] headerBytes = Arrays.copyOfRange(bytes, 0, 6);
// "GIF87a"
byte[] gifHeader87a = { 71, 73, 70, 56, 55, 97 };
// "GIF89a"
byte[] gifHeader89a = { 71, 73, 70, 56, 57, 97 };
if (Arrays.equals(gifHeader87a, headerBytes) || Arrays.equals(gifHeader89a, headerBytes)) {
image.setImageFormat(ImageFormat.GIF);
} else if (originalFileName.toLowerCase().endsWith(".png")) {
image.setImageFormat(ImageFormat.PNG);
} else if (originalFileName.toLowerCase().endsWith(".jpg") || originalFileName.toLowerCase().endsWith(".jpeg")) {
image.setImageFormat(ImageFormat.JPG);
} else if (originalFileName.toLowerCase().endsWith(".gif")) {
image.setImageFormat(ImageFormat.GIF);
} else {
result.rejectValue("bytes", "typeMismatch");
}
if (image.getImageFormat() != null) {
String contentType = multipartFile.getContentType();
logger.info("contentType: " + contentType);
image.setContentType(contentType);
image.setBytes(bytes);
if (image.getImageFormat() != ImageFormat.GIF) {
int width = ImageHelper.getWidth(bytes);
logger.info("width: " + width + "px");
if (width < ImageHelper.MINIMUM_WIDTH) {
result.rejectValue("bytes", "image.too.small");
image.setBytes(null);
} else {
if (width > ImageHelper.MINIMUM_WIDTH) {
bytes = ImageHelper.scaleImage(bytes, ImageHelper.MINIMUM_WIDTH);
image.setBytes(bytes);
}
}
}
}
}
} catch (IOException e) {
logger.error(e);
}
if (result.hasErrors()) {
model.addAttribute("image", image);
model.addAttribute("contentLicenses", ContentLicense.values());
model.addAttribute("literacySkills", LiteracySkill.values());
model.addAttribute("numeracySkills", NumeracySkill.values());
model.addAttribute("timeStart", System.currentTimeMillis());
model.addAttribute("imageContributionEvents", imageContributionEventDao.readAll(image));
model.addAttribute("letters", letterDao.readAllOrdered());
model.addAttribute("numbers", numberDao.readAllOrdered());
model.addAttribute("words", wordDao.readAllOrdered());
model.addAttribute("emojisByWordId", getEmojisByWordId());
Audio audio = audioDao.readByTranscription(image.getTitle());
model.addAttribute("audio", audio);
return "content/multimedia/image/edit";
} else {
image.setTitle(image.getTitle().toLowerCase());
image.setTimeLastUpdate(Calendar.getInstance());
image.setRevisionNumber(image.getRevisionNumber() + 1);
imageDao.update(image);
ImageContributionEvent imageContributionEvent = new ImageContributionEvent();
imageContributionEvent.setContributor((Contributor) session.getAttribute("contributor"));
imageContributionEvent.setTime(Calendar.getInstance());
imageContributionEvent.setImage(image);
imageContributionEvent.setRevisionNumber(image.getRevisionNumber());
imageContributionEvent.setComment(StringUtils.abbreviate(request.getParameter("contributionComment"), 1000));
imageContributionEvent.setTimeSpentMs(System.currentTimeMillis() - Long.valueOf(request.getParameter("timeStart")));
imageContributionEvent.setPlatform(Platform.WEBAPP);
imageContributionEventDao.create(imageContributionEvent);
String contentUrl = "http://" + EnvironmentContextLoaderListener.PROPERTIES.getProperty("content.language").toLowerCase() + ".elimu.ai/content/multimedia/image/edit/" + image.getId();
String embedThumbnailUrl = "http://" + EnvironmentContextLoaderListener.PROPERTIES.getProperty("content.language").toLowerCase() + ".elimu.ai/image/" + image.getId() + "_r" + image.getRevisionNumber() + "." + image.getImageFormat().toString().toLowerCase();
DiscordHelper.sendChannelMessage("Image edited: " + contentUrl, "\"" + image.getTitle() + "\"", "Comment: \"" + imageContributionEvent.getComment() + "\"", null, embedThumbnailUrl);
return "redirect:/content/multimedia/image/list#" + image.getId();
}
}
use of ai.elimu.model.contributor.ImageContributionEvent in project webapp by elimu-ai.
the class ImageCreateController method handleSubmit.
@RequestMapping(method = RequestMethod.POST)
public String handleSubmit(HttpServletRequest request, HttpSession session, /*@Valid*/
Image image, @RequestParam("bytes") MultipartFile multipartFile, BindingResult result, Model model) {
logger.info("handleSubmit");
if (StringUtils.isBlank(image.getTitle())) {
result.rejectValue("title", "NotNull");
} else {
Image existingImage = imageDao.read(image.getTitle());
if (existingImage != null) {
result.rejectValue("title", "NonUnique");
}
}
try {
byte[] bytes = multipartFile.getBytes();
if (multipartFile.isEmpty() || (bytes == null) || (bytes.length == 0)) {
result.rejectValue("bytes", "NotNull");
} else {
String originalFileName = multipartFile.getOriginalFilename();
logger.info("originalFileName: " + originalFileName);
byte[] headerBytes = Arrays.copyOfRange(bytes, 0, 6);
// "GIF87a"
byte[] gifHeader87a = { 71, 73, 70, 56, 55, 97 };
// "GIF89a"
byte[] gifHeader89a = { 71, 73, 70, 56, 57, 97 };
if (Arrays.equals(gifHeader87a, headerBytes) || Arrays.equals(gifHeader89a, headerBytes)) {
image.setImageFormat(ImageFormat.GIF);
} else if (originalFileName.toLowerCase().endsWith(".png")) {
image.setImageFormat(ImageFormat.PNG);
} else if (originalFileName.toLowerCase().endsWith(".jpg") || originalFileName.toLowerCase().endsWith(".jpeg")) {
image.setImageFormat(ImageFormat.JPG);
} else if (originalFileName.toLowerCase().endsWith(".gif")) {
image.setImageFormat(ImageFormat.GIF);
} else {
result.rejectValue("bytes", "typeMismatch");
}
if (image.getImageFormat() != null) {
String contentType = multipartFile.getContentType();
logger.info("contentType: " + contentType);
image.setContentType(contentType);
image.setBytes(bytes);
if (image.getImageFormat() != ImageFormat.GIF) {
int width = ImageHelper.getWidth(bytes);
logger.info("width: " + width + "px");
if (width < ImageHelper.MINIMUM_WIDTH) {
result.rejectValue("bytes", "image.too.small");
image.setBytes(null);
} else {
if (width > ImageHelper.MINIMUM_WIDTH) {
bytes = ImageHelper.scaleImage(bytes, ImageHelper.MINIMUM_WIDTH);
image.setBytes(bytes);
}
}
}
}
}
} catch (IOException e) {
logger.error(e);
}
if (result.hasErrors()) {
model.addAttribute("contentLicenses", ContentLicense.values());
model.addAttribute("literacySkills", LiteracySkill.values());
model.addAttribute("numeracySkills", NumeracySkill.values());
model.addAttribute("timeStart", System.currentTimeMillis());
return "content/multimedia/image/create";
} else {
image.setTitle(image.getTitle().toLowerCase());
try {
int[] dominantColor = ImageColorHelper.getDominantColor(image.getBytes());
image.setDominantColor("rgb(" + dominantColor[0] + "," + dominantColor[1] + "," + dominantColor[2] + ")");
} catch (NullPointerException ex) {
// javax.imageio.IIOException: Unsupported Image Type
}
image.setTimeLastUpdate(Calendar.getInstance());
imageDao.create(image);
// Label Image with Word of matching title
Word matchingWord = wordDao.readByText(image.getTitle());
if (matchingWord != null) {
Set<Word> labeledWords = new HashSet<>();
if (!labeledWords.contains(matchingWord)) {
labeledWords.add(matchingWord);
image.setWords(labeledWords);
imageDao.update(image);
}
}
ImageContributionEvent imageContributionEvent = new ImageContributionEvent();
imageContributionEvent.setContributor((Contributor) session.getAttribute("contributor"));
imageContributionEvent.setTime(Calendar.getInstance());
imageContributionEvent.setImage(image);
imageContributionEvent.setRevisionNumber(image.getRevisionNumber());
imageContributionEvent.setComment(StringUtils.abbreviate(request.getParameter("contributionComment"), 1000));
imageContributionEvent.setTimeSpentMs(System.currentTimeMillis() - Long.valueOf(request.getParameter("timeStart")));
imageContributionEvent.setPlatform(Platform.WEBAPP);
imageContributionEventDao.create(imageContributionEvent);
String contentUrl = "http://" + EnvironmentContextLoaderListener.PROPERTIES.getProperty("content.language").toLowerCase() + ".elimu.ai/content/multimedia/image/edit/" + image.getId();
String embedThumbnailUrl = "http://" + EnvironmentContextLoaderListener.PROPERTIES.getProperty("content.language").toLowerCase() + ".elimu.ai/image/" + image.getId() + "_r" + image.getRevisionNumber() + "." + image.getImageFormat().toString().toLowerCase();
DiscordHelper.sendChannelMessage("Image created: " + contentUrl, "\"" + image.getTitle() + "\"", "Comment: \"" + imageContributionEvent.getComment() + "\"", null, embedThumbnailUrl);
return "redirect:/content/multimedia/image/list#" + image.getId();
}
}
use of ai.elimu.model.contributor.ImageContributionEvent in project webapp by elimu-ai.
the class StoryBookCreateFromEPubController method storeImageContributionEvent.
private void storeImageContributionEvent(Image image, HttpSession session, HttpServletRequest request) {
logger.info("storeImageContributionEvent");
ImageContributionEvent imageContributionEvent = new ImageContributionEvent();
imageContributionEvent.setContributor((Contributor) session.getAttribute("contributor"));
imageContributionEvent.setTime(Calendar.getInstance());
imageContributionEvent.setImage(image);
imageContributionEvent.setRevisionNumber(image.getRevisionNumber());
imageContributionEvent.setComment("Extracted from ePUB file (🤖 auto-generated comment)");
imageContributionEvent.setTimeSpentMs(System.currentTimeMillis() - Long.valueOf(request.getParameter("timeStart")));
imageContributionEvent.setPlatform(Platform.WEBAPP);
imageContributionEventDao.create(imageContributionEvent);
String contentUrl = "http://" + EnvironmentContextLoaderListener.PROPERTIES.getProperty("content.language").toLowerCase() + ".elimu.ai/content/multimedia/image/edit/" + image.getId();
String embedThumbnailUrl = "http://" + EnvironmentContextLoaderListener.PROPERTIES.getProperty("content.language").toLowerCase() + ".elimu.ai/image/" + image.getId() + "_r" + image.getRevisionNumber() + "." + image.getImageFormat().toString().toLowerCase();
DiscordHelper.sendChannelMessage("Image created: " + contentUrl, "\"" + image.getTitle() + "\"", "Comment: \"" + imageContributionEvent.getComment() + "\"", null, embedThumbnailUrl);
}
use of ai.elimu.model.contributor.ImageContributionEvent in project webapp by elimu-ai.
the class StoryBookChapterDeleteController method handleRequest.
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String handleRequest(HttpSession session, @PathVariable Long storyBookId, @PathVariable Long id) {
logger.info("handleRequest");
Contributor contributor = (Contributor) session.getAttribute("contributor");
logger.info("contributor.getRoles(): " + contributor.getRoles());
if (!contributor.getRoles().contains(Role.EDITOR)) {
// TODO: return HttpStatus.FORBIDDEN
throw new IllegalAccessError("Missing role for access");
}
StoryBookChapter storyBookChapterToBeDeleted = storyBookChapterDao.read(id);
logger.info("storyBookChapterToBeDeleted: " + storyBookChapterToBeDeleted);
logger.info("storyBookChapterToBeDeleted.getSortOrder(): " + storyBookChapterToBeDeleted.getSortOrder());
// Delete the chapter's paragraphs
List<StoryBookParagraph> storyBookParagraphs = storyBookParagraphDao.readAll(storyBookChapterToBeDeleted);
logger.info("storyBookParagraphs.size(): " + storyBookParagraphs.size());
for (StoryBookParagraph storyBookParagraphToBeDeleted : storyBookParagraphs) {
// Delete the paragraph's reference from corresponding audios (if any)
List<Audio> paragraphAudios = audioDao.readAll(storyBookParagraphToBeDeleted);
for (Audio paragraphAudio : paragraphAudios) {
paragraphAudio.setStoryBookParagraph(null);
audioDao.update(paragraphAudio);
}
logger.info("Deleting StoryBookParagraph with ID " + storyBookParagraphToBeDeleted.getId());
storyBookParagraphDao.delete(storyBookParagraphToBeDeleted);
}
// Delete the chapter
logger.info("Deleting StoryBookChapter with ID " + storyBookChapterToBeDeleted.getId());
storyBookChapterDao.delete(storyBookChapterToBeDeleted);
// Delete the chapter's image (if any)
Image chapterImage = storyBookChapterToBeDeleted.getImage();
logger.info("chapterImage: " + chapterImage);
if (chapterImage != null) {
// Remove content labels
chapterImage.setLiteracySkills(null);
chapterImage.setNumeracySkills(null);
chapterImage.setLetters(null);
chapterImage.setNumbers(null);
chapterImage.setWords(null);
imageDao.update(chapterImage);
// Remove contribution events
for (ImageContributionEvent imageContributionEvent : imageContributionEventDao.readAll(chapterImage)) {
logger.warn("Deleting ImageContributionEvent from the database");
imageContributionEventDao.delete(imageContributionEvent);
}
logger.warn("Deleting the chapter image from the database");
imageDao.delete(chapterImage);
}
// Update the StoryBook's metadata
StoryBook storyBook = storyBookChapterToBeDeleted.getStoryBook();
storyBook.setTimeLastUpdate(Calendar.getInstance());
storyBook.setRevisionNumber(storyBook.getRevisionNumber() + 1);
storyBook.setPeerReviewStatus(PeerReviewStatus.PENDING);
storyBookDao.update(storyBook);
// Store contribution event
StoryBookContributionEvent storyBookContributionEvent = new StoryBookContributionEvent();
storyBookContributionEvent.setContributor(contributor);
storyBookContributionEvent.setTime(Calendar.getInstance());
storyBookContributionEvent.setStoryBook(storyBook);
storyBookContributionEvent.setRevisionNumber(storyBook.getRevisionNumber());
storyBookContributionEvent.setComment("Deleted storybook chapter " + (storyBookChapterToBeDeleted.getSortOrder() + 1) + " (🤖 auto-generated comment)");
storyBookContributionEvent.setTimeSpentMs(0L);
storyBookContributionEvent.setPlatform(Platform.WEBAPP);
storyBookContributionEventDao.create(storyBookContributionEvent);
String contentUrl = "http://" + EnvironmentContextLoaderListener.PROPERTIES.getProperty("content.language").toLowerCase() + ".elimu.ai/content/storybook/edit/" + storyBook.getId();
String embedThumbnailUrl = null;
if (storyBook.getCoverImage() != null) {
embedThumbnailUrl = "http://" + EnvironmentContextLoaderListener.PROPERTIES.getProperty("content.language").toLowerCase() + ".elimu.ai/image/" + storyBook.getCoverImage().getId() + "_r" + storyBook.getCoverImage().getRevisionNumber() + "." + storyBook.getCoverImage().getImageFormat().toString().toLowerCase();
}
DiscordHelper.sendChannelMessage("Storybook chapter deleted: " + contentUrl, "\"" + storyBookContributionEvent.getStoryBook().getTitle() + "\"", "Comment: \"" + storyBookContributionEvent.getComment() + "\"", null, embedThumbnailUrl);
// Update the sorting order of the remaining chapters
List<StoryBookChapter> storyBookChapters = storyBookChapterDao.readAll(storyBook);
logger.info("storyBookChapters.size(): " + storyBookChapters.size());
for (StoryBookChapter storyBookChapter : storyBookChapters) {
logger.info("storyBookChapter.getId(): " + storyBookChapter.getId() + ", storyBookChapter.getSortOrder(): " + storyBookChapter.getSortOrder());
if (storyBookChapter.getSortOrder() > storyBookChapterToBeDeleted.getSortOrder()) {
// Reduce sort order by 1
storyBookChapter.setSortOrder(storyBookChapter.getSortOrder() - 1);
storyBookChapterDao.update(storyBookChapter);
logger.info("storyBookChapter.getSortOrder() (after update): " + storyBookChapter.getSortOrder());
}
}
// Refresh the REST API cache
storyBooksJsonService.refreshStoryBooksJSONArray();
return "redirect:/content/storybook/edit/" + storyBookId;
}
Aggregations