Search in sources :

Example 6 with CoverImageException

use of org.opencastproject.coverimage.CoverImageException in project opencast by opencast.

the class CoverImageWorkflowOperationHandlerBase method start.

@Override
public WorkflowOperationResult start(final WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
    MediaPackage mediaPackage = workflowInstance.getMediaPackage();
    WorkflowOperationInstance operation = workflowInstance.getCurrentOperation();
    logger.info("Cover Image Workflow started for media package '{}'", mediaPackage.getIdentifier());
    // User XML metadata from operation configuration, fallback to default metadata
    String xml = operation.getConfiguration(XML_METADATA);
    if (xml == null) {
        xml = getMetadataXml(mediaPackage);
        logger.debug("Metadata was not part of operation configuration, using Dublin Core as fallback");
    }
    logger.debug("Metadata set to: {}", xml);
    String xsl = loadXsl(operation);
    logger.debug("XSL for transforming metadata to SVG loaded: {}", xsl);
    // Read image dimensions
    int width = getIntConfiguration(operation, WIDTH);
    logger.debug("Image width set to {}px", width);
    int height = getIntConfiguration(operation, HEIGHT);
    logger.debug("Image height set to {}px", height);
    // Read optional poster image flavor
    String posterImgUri = getPosterImageFileUrl(operation.getConfiguration(POSTERIMAGE_URL));
    if (posterImgUri == null)
        posterImgUri = getPosterImageFileUrl(mediaPackage, operation.getConfiguration(POSTERIMAGE_FLAVOR));
    if (posterImgUri == null) {
        logger.debug("No optional poster image set");
    } else {
        logger.debug("Poster image found at '{}'", posterImgUri);
    }
    // Read target flavor
    String targetFlavor = operation.getConfiguration(TARGET_FLAVOR);
    if (StringUtils.isBlank(targetFlavor)) {
        logger.warn("Required configuration key '{}' is blank", TARGET_FLAVOR);
        throw new WorkflowOperationException("Configuration key '" + TARGET_FLAVOR + "' must be set");
    }
    try {
        MediaPackageElementFlavor.parseFlavor(targetFlavor);
    } catch (IllegalArgumentException e) {
        logger.warn("Given target flavor '{}' is not a valid flavor", targetFlavor);
        throw new WorkflowOperationException(e);
    }
    Job generate;
    try {
        generate = getCoverImageService().generateCoverImage(xml, xsl, String.valueOf(width), String.valueOf(height), posterImgUri, targetFlavor);
        logger.debug("Job for cover image generation created");
        if (!waitForStatus(generate).isSuccess()) {
            throw new WorkflowOperationException("'Cover image' job did not successfuly end");
        }
        generate = serviceRegistry.getJob(generate.getId());
        Attachment coverImage = (Attachment) MediaPackageElementParser.getFromXml(generate.getPayload());
        URI attachmentUri = getWorkspace().moveTo(coverImage.getURI(), mediaPackage.getIdentifier().compact(), UUID.randomUUID().toString(), COVERIMAGE_FILENAME);
        coverImage.setURI(attachmentUri);
        coverImage.setMimeType(MimeTypes.PNG);
        // Add tags
        final String targetTags = StringUtils.trimToNull(operation.getConfiguration(TARGET_TAGS));
        if (targetTags != null) {
            for (String tag : asList(targetTags)) {
                logger.trace("Tagging image with '{}'", tag);
                if (StringUtils.trimToNull(tag) != null)
                    coverImage.addTag(tag);
            }
        }
        mediaPackage.add(coverImage);
    } catch (MediaPackageException e) {
        throw new WorkflowOperationException(e);
    } catch (NotFoundException e) {
        throw new WorkflowOperationException(e);
    } catch (ServiceRegistryException e) {
        throw new WorkflowOperationException(e);
    } catch (CoverImageException e) {
        throw new WorkflowOperationException(e);
    } catch (IllegalArgumentException e) {
        throw new WorkflowOperationException(e);
    } catch (IOException e) {
        throw new WorkflowOperationException(e);
    }
    logger.info("Cover Image Workflow finished successfully for media package '{}' within {}ms", mediaPackage.getIdentifier(), generate.getQueueTime());
    return createResult(mediaPackage, Action.CONTINUE, generate.getQueueTime());
}
Also used : MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) NotFoundException(org.opencastproject.util.NotFoundException) FileNotFoundException(java.io.FileNotFoundException) Attachment(org.opencastproject.mediapackage.Attachment) IOException(java.io.IOException) URI(java.net.URI) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) CoverImageException(org.opencastproject.coverimage.CoverImageException) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) Job(org.opencastproject.job.api.Job)

Example 7 with CoverImageException

use of org.opencastproject.coverimage.CoverImageException in project opencast by opencast.

the class AbstractCoverImageService method generateCoverImageInternal.

protected Attachment generateCoverImageInternal(Job job, String xml, String xsl, int width, int height, String posterImage, String targetFlavor) throws CoverImageException {
    URI result;
    File tempSvg = null;
    File tempPng = null;
    StringReader xmlReader = null;
    try {
        Document xslDoc = parseXsl(xsl);
        // Create temp SVG file for transformation result
        tempSvg = createTempFile(job, ".svg");
        Result svg = new StreamResult(tempSvg);
        // Load Metadata (from resources)
        xmlReader = new StringReader(xml);
        Source xmlSource = new StreamSource(xmlReader);
        // Transform XML metadata with stylesheet to SVG
        transformSvg(svg, xmlSource, xslDoc, width, height, posterImage);
        // Rasterize SVG to PNG
        tempPng = createTempFile(job, ".png");
        rasterizeSvg(tempSvg, tempPng);
        FileInputStream in = null;
        try {
            in = new FileInputStream(tempPng);
            result = workspace.putInCollection(COVERIMAGE_WORKSPACE_COLLECTION, job.getId() + "_coverimage.png", in);
            log.debug("Put the cover image into the workspace ({})", result);
        } catch (FileNotFoundException e) {
            // should never happen...
            throw new CoverImageException(e);
        } catch (IOException e) {
            log.warn("Error while putting resulting image into workspace collection '{}': {}", COVERIMAGE_WORKSPACE_COLLECTION, e);
            throw new CoverImageException("Error while putting resulting image into workspace collection", e);
        } finally {
            IOUtils.closeQuietly(in);
        }
    } finally {
        FileUtils.deleteQuietly(tempSvg);
        FileUtils.deleteQuietly(tempPng);
        log.debug("Removed temporary files");
        IOUtils.closeQuietly(xmlReader);
    }
    return (Attachment) MediaPackageElementBuilderFactory.newInstance().newElementBuilder().elementFromURI(result, Type.Attachment, MediaPackageElementFlavor.parseFlavor(targetFlavor));
}
Also used : StreamResult(javax.xml.transform.stream.StreamResult) StreamSource(javax.xml.transform.stream.StreamSource) FileNotFoundException(java.io.FileNotFoundException) Attachment(org.opencastproject.mediapackage.Attachment) IOException(java.io.IOException) Document(org.w3c.dom.Document) URI(java.net.URI) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) FileInputStream(java.io.FileInputStream) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) CoverImageException(org.opencastproject.coverimage.CoverImageException) StringReader(java.io.StringReader) File(java.io.File)

Aggregations

CoverImageException (org.opencastproject.coverimage.CoverImageException)7 IOException (java.io.IOException)4 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 URI (java.net.URI)2 DOMSource (javax.xml.transform.dom.DOMSource)2 Job (org.opencastproject.job.api.Job)2 Attachment (org.opencastproject.mediapackage.Attachment)2 Document (org.w3c.dom.Document)2 InputSource (org.xml.sax.InputSource)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Result (javax.xml.transform.Result)1 Source (javax.xml.transform.Source)1 Transformer (javax.xml.transform.Transformer)1