Search in sources :

Example 1 with VideoRevisionEvent

use of ai.elimu.model.contributor.VideoRevisionEvent in project webapp by elimu-ai.

the class VideoCreateController method handleSubmit.

@RequestMapping(method = RequestMethod.POST)
public String handleSubmit(HttpSession session, /*@Valid*/
Video video, @RequestParam("bytes") MultipartFile multipartFile, @RequestParam("thumbnail") MultipartFile multipartFileThumbnail, BindingResult result, Model model) {
    logger.info("handleSubmit");
    Contributor contributor = (Contributor) session.getAttribute("contributor");
    if (StringUtils.isBlank(video.getTitle())) {
        result.rejectValue("title", "NotNull");
    } else {
        Video existingVideo = videoDao.read(video.getTitle(), video.getLocale());
        if (existingVideo != 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);
            if (originalFileName.toLowerCase().endsWith(".m4v")) {
                video.setVideoFormat(VideoFormat.M4V);
            } else if (originalFileName.toLowerCase().endsWith(".mp4")) {
                video.setVideoFormat(VideoFormat.MP4);
            } else {
                result.rejectValue("bytes", "typeMismatch");
            }
            if (video.getVideoFormat() != null) {
                String contentType = multipartFile.getContentType();
                logger.info("contentType: " + contentType);
                video.setContentType(contentType);
                video.setBytes(bytes);
            // TODO: convert to a default video format?
            }
        }
        byte[] bytesThumbnail = multipartFileThumbnail.getBytes();
        if (multipartFileThumbnail.isEmpty() || (bytesThumbnail == null) || (bytesThumbnail.length == 0)) {
            result.rejectValue("thumbnail", "NotNull");
        } else {
            String originalFileName = multipartFileThumbnail.getOriginalFilename();
            logger.info("originalFileName: " + originalFileName);
            if (!originalFileName.toLowerCase().endsWith(".png")) {
                result.rejectValue("thumbnail", "typeMismatch");
            } else {
                video.setThumbnail(bytesThumbnail);
                int width = ImageHelper.getWidth(bytesThumbnail);
                logger.info("width: " + width + "px");
                if (width < ImageHelper.MINIMUM_WIDTH) {
                    result.rejectValue("thumbnail", "image.too.small");
                    video.setBytes(null);
                } else {
                    if (width > ImageHelper.MINIMUM_WIDTH) {
                        bytesThumbnail = ImageHelper.scaleImage(bytesThumbnail, ImageHelper.MINIMUM_WIDTH);
                        video.setBytes(bytesThumbnail);
                    }
                }
            }
        }
    } catch (IOException e) {
        logger.error(e);
    }
    if (result.hasErrors()) {
        model.addAttribute("contentLicenses", ContentLicense.values());
        model.addAttribute("literacySkills", LiteracySkill.values());
        model.addAttribute("numeracySkills", NumeracySkill.values());
        return "content/multimedia/video/create";
    } else {
        video.setTitle(video.getTitle().toLowerCase());
        video.setTimeLastUpdate(Calendar.getInstance());
        videoDao.create(video);
        VideoRevisionEvent videoRevisionEvent = new VideoRevisionEvent();
        videoRevisionEvent.setContributor(contributor);
        videoRevisionEvent.setCalendar(Calendar.getInstance());
        videoRevisionEvent.setVideo(video);
        videoRevisionEvent.setTitle(video.getTitle());
        videoRevisionEventDao.create(videoRevisionEvent);
        if (EnvironmentContextLoaderListener.env == Environment.PROD) {
            String text = URLEncoder.encode(contributor.getFirstName() + " just added a new Video:\n" + "• Language: \"" + video.getLocale().getLanguage() + "\"\n" + "• Title: \"" + video.getTitle() + "\"\n" + "See ") + "http://elimu.ai/content/multimedia/video/edit/" + video.getId();
            String iconUrl = contributor.getImageUrl();
            String imageUrl = "http://elimu.ai/video/" + video.getId() + "/thumbnail.png";
            SlackApiHelper.postMessage(SlackApiHelper.getChannelId(Team.CONTENT_CREATION), text, iconUrl, imageUrl);
        }
        return "redirect:/content/multimedia/video/list#" + video.getId();
    }
}
Also used : VideoRevisionEvent(ai.elimu.model.contributor.VideoRevisionEvent) Video(ai.elimu.model.content.multimedia.Video) Contributor(ai.elimu.model.Contributor) IOException(java.io.IOException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with VideoRevisionEvent

use of ai.elimu.model.contributor.VideoRevisionEvent in project webapp by elimu-ai.

the class VideoEditController method handleSubmit.

@RequestMapping(value = "/{id}", method = RequestMethod.POST)
public String handleSubmit(HttpSession session, Video video, @RequestParam("bytes") MultipartFile multipartFile, BindingResult result, Model model) {
    logger.info("handleSubmit");
    Contributor contributor = (Contributor) session.getAttribute("contributor");
    if (StringUtils.isBlank(video.getTitle())) {
        result.rejectValue("title", "NotNull");
    } else {
        Video existingVideo = videoDao.read(video.getTitle(), video.getLocale());
        if ((existingVideo != null) && !existingVideo.getId().equals(video.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);
            if (originalFileName.toLowerCase().endsWith(".m4v")) {
                video.setVideoFormat(VideoFormat.M4V);
            } else if (originalFileName.toLowerCase().endsWith(".mp4")) {
                video.setVideoFormat(VideoFormat.MP4);
            } else {
                result.rejectValue("bytes", "typeMismatch");
            }
            if (video.getVideoFormat() != null) {
                String contentType = multipartFile.getContentType();
                logger.info("contentType: " + contentType);
                video.setContentType(contentType);
                video.setBytes(bytes);
            // TODO: convert to a default video format?
            }
        }
    } catch (IOException e) {
        logger.error(e);
    }
    if (result.hasErrors()) {
        model.addAttribute("video", video);
        model.addAttribute("contentLicenses", ContentLicense.values());
        model.addAttribute("literacySkills", LiteracySkill.values());
        model.addAttribute("numeracySkills", NumeracySkill.values());
        model.addAttribute("videoRevisionEvents", videoRevisionEventDao.readAll(video));
        model.addAttribute("letters", letterDao.readAllOrdered(contributor.getLocale()));
        model.addAttribute("numbers", numberDao.readAllOrdered(contributor.getLocale()));
        model.addAttribute("words", wordDao.readAllOrdered(contributor.getLocale()));
        return "content/multimedia/video/edit";
    } else {
        video.setTitle(video.getTitle().toLowerCase());
        video.setTimeLastUpdate(Calendar.getInstance());
        video.setRevisionNumber(video.getRevisionNumber() + 1);
        videoDao.update(video);
        VideoRevisionEvent videoRevisionEvent = new VideoRevisionEvent();
        videoRevisionEvent.setContributor(contributor);
        videoRevisionEvent.setCalendar(Calendar.getInstance());
        videoRevisionEvent.setVideo(video);
        videoRevisionEvent.setTitle(video.getTitle());
        videoRevisionEventDao.create(videoRevisionEvent);
        if (EnvironmentContextLoaderListener.env == Environment.PROD) {
            String text = URLEncoder.encode(contributor.getFirstName() + " just updated a Video:\n" + "• Language: \"" + video.getLocale().getLanguage() + "\"\n" + "• Title: \"" + video.getTitle() + "\"\n" + "• Revision number: #" + video.getRevisionNumber() + "\n" + "See ") + "http://elimu.ai/content/multimedia/video/edit/" + video.getId();
            String iconUrl = contributor.getImageUrl();
            String imageUrl = "http://elimu.ai/video/" + video.getId() + "/thumbnail.png";
            SlackApiHelper.postMessage(SlackApiHelper.getChannelId(Team.CONTENT_CREATION), text, iconUrl, imageUrl);
        }
        return "redirect:/content/multimedia/video/list#" + video.getId();
    }
}
Also used : VideoRevisionEvent(ai.elimu.model.contributor.VideoRevisionEvent) Video(ai.elimu.model.content.multimedia.Video) Contributor(ai.elimu.model.Contributor) IOException(java.io.IOException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Contributor (ai.elimu.model.Contributor)2 Video (ai.elimu.model.content.multimedia.Video)2 VideoRevisionEvent (ai.elimu.model.contributor.VideoRevisionEvent)2 IOException (java.io.IOException)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2