Search in sources :

Example 1 with AudioFormat

use of ai.elimu.model.v2.enums.content.AudioFormat in project webapp by elimu-ai.

the class AudioContributionsRestController method handleUploadWordRecordingRequest.

@RequestMapping(value = "/words", method = RequestMethod.POST)
public String handleUploadWordRecordingRequest(HttpServletRequest request, HttpServletResponse response, @RequestParam("file") MultipartFile multipartFile) {
    logger.info("handleUploadWordRecordingRequest");
    JSONObject jsonObject = new JSONObject();
    String providerIdGoogle = request.getHeader("providerIdGoogle");
    logger.info("providerIdGoogle: " + providerIdGoogle);
    if (StringUtils.isBlank(providerIdGoogle)) {
        jsonObject.put("result", "error");
        jsonObject.put("errorMessage", "Missing providerIdGoogle");
        response.setStatus(HttpStatus.BAD_REQUEST.value());
        String jsonResponse = jsonObject.toString();
        logger.info("jsonResponse: " + jsonResponse);
        return jsonResponse;
    }
    String timeSpentMsAsString = request.getHeader("timeSpentMs");
    logger.info("timeSpentMsAsString: " + timeSpentMsAsString);
    if (StringUtils.isBlank(timeSpentMsAsString)) {
        jsonObject.put("result", "error");
        jsonObject.put("errorMessage", "Missing timeSpentMs");
        response.setStatus(HttpStatus.BAD_REQUEST.value());
        String jsonResponse = jsonObject.toString();
        logger.info("jsonResponse: " + jsonResponse);
        return jsonResponse;
    }
    Long timeSpentMs = Long.valueOf(timeSpentMsAsString);
    // Lookup the Contributor by ID
    Contributor contributor = contributorDao.readByProviderIdGoogle(providerIdGoogle);
    logger.info("contributor: " + contributor);
    if (contributor == null) {
        jsonObject.put("result", "error");
        jsonObject.put("errorMessage", "The Contributor was not found.");
        response.setStatus(HttpStatus.UNPROCESSABLE_ENTITY.value());
        String jsonResponse = jsonObject.toString();
        logger.info("jsonResponse: " + jsonResponse);
        return jsonResponse;
    }
    // Expected format: "word_5.mp3"
    String originalFilename = multipartFile.getOriginalFilename();
    logger.info("originalFilename: " + originalFilename);
    AudioFormat audioFormat = CrowdsourceHelper.extractAudioFormatFromFilename(originalFilename);
    logger.info("audioFormat: " + audioFormat);
    Long wordIdExtractedFromFilename = CrowdsourceHelper.extractWordIdFromFilename(originalFilename);
    logger.info("wordIdExtractedFromFilename: " + wordIdExtractedFromFilename);
    Word word = wordDao.read(wordIdExtractedFromFilename);
    logger.info("word: " + word);
    if (word == null) {
        jsonObject.put("result", "error");
        jsonObject.put("errorMessage", "A Word with ID " + wordIdExtractedFromFilename + " was not found.");
        response.setStatus(HttpStatus.UNPROCESSABLE_ENTITY.value());
        String jsonResponse = jsonObject.toString();
        logger.info("jsonResponse: " + jsonResponse);
        return jsonResponse;
    }
    String contentType = multipartFile.getContentType();
    logger.info("contentType: " + contentType);
    try {
        byte[] bytes = multipartFile.getBytes();
        logger.info("bytes.length: " + bytes.length);
        // Store a backup of the original CSV file on the filesystem (in case it will be needed for debugging)
        // TODO
        // Convert from MultipartFile to File, and extract audio duration
        String tmpDir = System.getProperty("java.io.tmpdir");
        File tmpDirElimuAi = new File(tmpDir, "elimu-ai");
        tmpDirElimuAi.mkdir();
        File file = new File(tmpDirElimuAi, multipartFile.getOriginalFilename());
        logger.info("file: " + file);
        multipartFile.transferTo(file);
        Long durationMs = AudioMetadataExtractionHelper.getDurationInMilliseconds(file);
        logger.info("durationMs: " + durationMs);
        // Store the audio recording in the database
        Audio audio = new Audio();
        audio.setTimeLastUpdate(Calendar.getInstance());
        audio.setContentType(contentType);
        audio.setWord(word);
        audio.setTitle(word.getText().toLowerCase());
        audio.setTranscription(word.getText().toLowerCase());
        audio.setBytes(bytes);
        audio.setDurationMs(durationMs);
        audio.setAudioFormat(audioFormat);
        audioDao.create(audio);
        AudioContributionEvent audioContributionEvent = new AudioContributionEvent();
        audioContributionEvent.setContributor(contributor);
        audioContributionEvent.setTime(Calendar.getInstance());
        audioContributionEvent.setTimeSpentMs(timeSpentMs);
        audioContributionEvent.setPlatform(Platform.CROWDSOURCE_APP);
        audioContributionEvent.setAudio(audio);
        audioContributionEvent.setRevisionNumber(audio.getRevisionNumber());
        audioContributionEventDao.create(audioContributionEvent);
        String contentUrl = "http://" + EnvironmentContextLoaderListener.PROPERTIES.getProperty("content.language").toLowerCase() + ".elimu.ai/content/multimedia/audio/edit/" + audio.getId();
        DiscordHelper.sendChannelMessage("Audio created: " + contentUrl, "\"" + audio.getTranscription() + "\"", "Comment: \"" + audioContributionEvent.getComment() + "\"", null, null);
    } catch (Exception ex) {
        logger.error(ex);
        jsonObject.put("result", "error");
        jsonObject.put("errorMessage", ex.getMessage());
        response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
    }
    String jsonResponse = jsonObject.toString();
    logger.info("jsonResponse: " + jsonResponse);
    return jsonResponse;
}
Also used : Word(ai.elimu.model.content.Word) JSONObject(org.json.JSONObject) Contributor(ai.elimu.model.contributor.Contributor) AudioContributionEvent(ai.elimu.model.contributor.AudioContributionEvent) AudioFormat(ai.elimu.model.v2.enums.content.AudioFormat) Audio(ai.elimu.model.content.multimedia.Audio) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with AudioFormat

use of ai.elimu.model.v2.enums.content.AudioFormat in project webapp by elimu-ai.

the class CrowdsourceHelper method extractAudioFormatFromFilename.

/**
 * E.g. "word_5.mp3" --> MP3
 */
public static AudioFormat extractAudioFormatFromFilename(String filename) {
    String audioFormatLowerCase = filename.substring(filename.length() - 3, filename.length());
    String audioFormatUpperCase = audioFormatLowerCase.toUpperCase();
    AudioFormat audioFormat = AudioFormat.valueOf(audioFormatUpperCase);
    return audioFormat;
}
Also used : AudioFormat(ai.elimu.model.v2.enums.content.AudioFormat)

Aggregations

AudioFormat (ai.elimu.model.v2.enums.content.AudioFormat)2 Word (ai.elimu.model.content.Word)1 Audio (ai.elimu.model.content.multimedia.Audio)1 AudioContributionEvent (ai.elimu.model.contributor.AudioContributionEvent)1 Contributor (ai.elimu.model.contributor.Contributor)1 File (java.io.File)1 JSONObject (org.json.JSONObject)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 MultipartFile (org.springframework.web.multipart.MultipartFile)1