use of ai.elimu.model.content.multimedia.Image in project webapp by elimu-ai.
the class ImageEditController method handleRequest.
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String handleRequest(Model model, @PathVariable Long id) {
logger.info("handleRequest");
Image image = imageDao.read(id);
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";
}
use of ai.elimu.model.content.multimedia.Image 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.content.multimedia.Image in project webapp by elimu-ai.
the class StoryBookEditController method handleRequest.
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String handleRequest(Model model, @PathVariable Long id) {
logger.info("handleRequest");
StoryBook storyBook = storyBookDao.read(id);
model.addAttribute("storyBook", storyBook);
model.addAttribute("timeStart", System.currentTimeMillis());
model.addAttribute("contentLicenses", ContentLicense.values());
List<Image> coverImages = imageDao.readAllOrdered();
model.addAttribute("coverImages", coverImages);
model.addAttribute("readingLevels", ReadingLevel.values());
List<StoryBookChapter> storyBookChapters = storyBookChapterDao.readAll(storyBook);
model.addAttribute("storyBookChapters", storyBookChapters);
// Map<StoryBookChapter.id, List<StoryBookParagraph>>
Map<Long, List<StoryBookParagraph>> paragraphsPerStoryBookChapterMap = new HashMap<>();
for (StoryBookChapter storyBookChapter : storyBookChapters) {
paragraphsPerStoryBookChapterMap.put(storyBookChapter.getId(), storyBookParagraphDao.readAll(storyBookChapter));
}
model.addAttribute("paragraphsPerStoryBookChapterMap", paragraphsPerStoryBookChapterMap);
List<String> paragraphs = new ArrayList<>();
for (StoryBookChapter storyBookChapter : storyBookChapters) {
List<StoryBookParagraph> storyBookParagraphs = storyBookParagraphDao.readAll(storyBookChapter);
for (StoryBookParagraph storyBookParagraph : storyBookParagraphs) {
paragraphs.add(storyBookParagraph.getOriginalText());
}
}
model.addAttribute("storyBookContributionEvents", storyBookContributionEventDao.readAll(storyBook));
model.addAttribute("storyBookPeerReviewEvents", storyBookPeerReviewEventDao.readAll(storyBook));
Language language = Language.valueOf(ConfigHelper.getProperty("content.language"));
Map<String, Integer> wordFrequencyMap = WordFrequencyHelper.getWordFrequency(paragraphs, language);
model.addAttribute("wordFrequencyMap", wordFrequencyMap);
Map<String, Word> wordMap = new HashMap<>();
for (Word word : wordDao.readAllOrdered()) {
wordMap.put(word.getText(), word);
}
model.addAttribute("wordMap", wordMap);
model.addAttribute("emojisByWordId", getEmojisByWordId());
Map<String, Integer> letterFrequencyMap = LetterFrequencyHelper.getLetterFrequency(paragraphs, language);
model.addAttribute("letterFrequencyMap", letterFrequencyMap);
Map<String, Letter> letterMap = new HashMap<>();
for (Letter letter : letterDao.readAllOrdered()) {
letterMap.put(letter.getText(), letter);
}
model.addAttribute("letterMap", letterMap);
return "content/storybook/edit";
}
use of ai.elimu.model.content.multimedia.Image in project webapp by elimu-ai.
the class WordEditController method handleRequest.
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String handleRequest(HttpSession session, Model model, @PathVariable Long id) {
logger.info("handleRequest");
Word word = wordDao.read(id);
if (word.getLetterSoundCorrespondences().isEmpty()) {
autoSelectLetterSoundCorrespondences(word);
// TODO: display information message to the Contributor that the letter-sound correspondences were auto-selected, and that they should be verified
}
model.addAttribute("word", word);
model.addAttribute("timeStart", System.currentTimeMillis());
// TODO: sort by letter(s) text
model.addAttribute("letterSoundCorrespondences", letterSoundCorrespondenceDao.readAllOrderedByUsage());
model.addAttribute("rootWords", wordDao.readAllOrdered());
model.addAttribute("emojisByWordId", getEmojisByWordId());
model.addAttribute("wordTypes", WordType.values());
model.addAttribute("spellingConsistencies", SpellingConsistency.values());
model.addAttribute("wordContributionEvents", wordContributionEventDao.readAll(word));
model.addAttribute("wordPeerReviewEvents", wordPeerReviewEventDao.readAll(word));
List<Audio> audios = audioDao.readAll(word);
model.addAttribute("audios", audios);
// Generate Audio for this Word (if it has not been done already)
if (audios.isEmpty()) {
Calendar timeStart = Calendar.getInstance();
Language language = Language.valueOf(ConfigHelper.getProperty("content.language"));
try {
byte[] audioBytes = GoogleCloudTextToSpeechHelper.synthesizeText(word.getText(), language);
logger.info("audioBytes: " + audioBytes);
if (audioBytes != null) {
Audio audio = new Audio();
audio.setTimeLastUpdate(Calendar.getInstance());
audio.setContentType(AudioFormat.MP3.getContentType());
audio.setWord(word);
audio.setTitle("word_" + word.getText());
audio.setTranscription(word.getText());
audio.setBytes(audioBytes);
// TODO: Convert from byte[] to File, and extract audio duration
audio.setDurationMs(null);
audio.setAudioFormat(AudioFormat.MP3);
audioDao.create(audio);
audios.add(audio);
model.addAttribute("audios", audios);
AudioContributionEvent audioContributionEvent = new AudioContributionEvent();
audioContributionEvent.setContributor((Contributor) session.getAttribute("contributor"));
audioContributionEvent.setTime(Calendar.getInstance());
audioContributionEvent.setAudio(audio);
audioContributionEvent.setRevisionNumber(audio.getRevisionNumber());
audioContributionEvent.setComment("Google Cloud Text-to-Speech (🤖 auto-generated comment)️");
audioContributionEvent.setTimeSpentMs(System.currentTimeMillis() - timeStart.getTimeInMillis());
audioContributionEvent.setPlatform(Platform.WEBAPP);
audioContributionEventDao.create(audioContributionEvent);
}
} catch (Exception ex) {
logger.error(ex);
}
}
// Look up variants of the same wordByTextMatch
model.addAttribute("wordInflections", wordDao.readInflections(word));
// Look up Multimedia content that has been labeled with this Word
// TODO: labeled Audios
List<Emoji> labeledEmojis = emojiDao.readAllLabeled(word);
model.addAttribute("labeledEmojis", labeledEmojis);
List<Image> labeledImages = imageDao.readAllLabeled(word);
model.addAttribute("labeledImages", labeledImages);
// TODO: labeled Videos
// Look up StoryBook Paragraphs that contain this Word
List<StoryBookParagraph> storyBookParagraphsContainingWord = storyBookParagraphDao.readAllContainingWord(word.getText());
model.addAttribute("storyBookParagraphsContainingWord", storyBookParagraphsContainingWord);
return "content/word/edit";
}
use of ai.elimu.model.content.multimedia.Image in project webapp by elimu-ai.
the class StoryBookCreateController method handleSubmit.
@RequestMapping(method = RequestMethod.POST)
public String handleSubmit(@Valid StoryBook storyBook, BindingResult result, Model model, HttpServletRequest request, HttpSession session) {
logger.info("handleSubmit");
StoryBook existingStoryBook = storybookDao.readByTitle(storyBook.getTitle());
if (existingStoryBook != null) {
result.rejectValue("title", "NonUnique");
}
if (result.hasErrors()) {
model.addAttribute("storybook", storyBook);
model.addAttribute("timeStart", System.currentTimeMillis());
model.addAttribute("contentLicenses", ContentLicense.values());
List<Image> coverImages = imageDao.readAllOrdered();
model.addAttribute("coverImages", coverImages);
model.addAttribute("readingLevels", ReadingLevel.values());
return "content/storybook/create";
} else {
storyBook.setTimeLastUpdate(Calendar.getInstance());
storybookDao.create(storyBook);
StoryBookContributionEvent storyBookContributionEvent = new StoryBookContributionEvent();
storyBookContributionEvent.setContributor((Contributor) session.getAttribute("contributor"));
storyBookContributionEvent.setTime(Calendar.getInstance());
storyBookContributionEvent.setStoryBook(storyBook);
storyBookContributionEvent.setRevisionNumber(storyBook.getRevisionNumber());
storyBookContributionEvent.setComment(StringUtils.abbreviate(request.getParameter("contributionComment"), 1000));
storyBookContributionEvent.setTimeSpentMs(System.currentTimeMillis() - Long.valueOf(request.getParameter("timeStart")));
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 created: " + contentUrl, "\"" + storyBookContributionEvent.getStoryBook().getTitle() + "\"", "Comment: \"" + storyBookContributionEvent.getComment() + "\"", null, embedThumbnailUrl);
return "redirect:/content/storybook/list#" + storyBook.getId();
}
}
Aggregations