Search in sources :

Example 1 with MediaPackageSerializer

use of org.opencastproject.mediapackage.MediaPackageSerializer in project opencast by opencast.

the class ZipWorkflowOperationHandler method zip.

/**
 * Creates a zip archive of all elements in a mediapackage.
 *
 * @param mediaPackage
 *          the mediapackage to zip
 *
 * @return the zip file
 *
 * @throws IOException
 *           If an IO exception occurs
 * @throws NotFoundException
 *           If a file referenced in the mediapackage can not be found
 * @throws MediaPackageException
 *           If the mediapackage can not be serialized to xml
 * @throws WorkflowOperationException
 *           If the mediapackage is invalid
 */
protected File zip(MediaPackage mediaPackage, List<MediaPackageElementFlavor> flavorsToZip, boolean compress) throws IOException, NotFoundException, MediaPackageException, WorkflowOperationException {
    if (mediaPackage == null) {
        throw new WorkflowOperationException("Invalid mediapackage");
    }
    // Create the temp directory
    File mediaPackageDir = new File(tempStorageDir, mediaPackage.getIdentifier().compact());
    FileUtils.forceMkdir(mediaPackageDir);
    // Link or copy each matching element's file from the workspace to the temp directory
    MediaPackageSerializer serializer = new DefaultMediaPackageSerializerImpl(mediaPackageDir);
    MediaPackage clone = (MediaPackage) mediaPackage.clone();
    for (MediaPackageElement element : clone.getElements()) {
        // remove the element if it doesn't match the flavors to zip
        boolean remove = true;
        for (MediaPackageElementFlavor flavor : flavorsToZip) {
            if (flavor.matches(element.getFlavor())) {
                remove = false;
                break;
            }
        }
        if (remove) {
            clone.remove(element);
            continue;
        }
        File elementDir = new File(mediaPackageDir, element.getIdentifier());
        FileUtils.forceMkdir(elementDir);
        File workspaceFile = workspace.get(element.getURI());
        File linkedFile = FileSupport.link(workspaceFile, new File(elementDir, workspaceFile.getName()), true);
        try {
            element.setURI(serializer.encodeURI(linkedFile.toURI()));
        } catch (URISyntaxException e) {
            throw new MediaPackageException("unable to serialize a mediapackage element", e);
        }
    }
    // Add the manifest
    FileUtils.writeStringToFile(new File(mediaPackageDir, "manifest.xml"), MediaPackageParser.getAsXml(clone), "UTF-8");
    // Zip the directory
    File zip = new File(tempStorageDir, clone.getIdentifier().compact() + ".zip");
    int compressValue = compress ? ZipUtil.DEFAULT_COMPRESSION : ZipUtil.NO_COMPRESSION;
    long startTime = System.currentTimeMillis();
    ZipUtil.zip(new File[] { mediaPackageDir }, zip, true, compressValue);
    long stopTime = System.currentTimeMillis();
    logger.debug("Zip file creation took {} seconds", (stopTime - startTime) / 1000);
    // Remove the directory
    FileUtils.forceDelete(mediaPackageDir);
    // Return the zip
    return zip;
}
Also used : DefaultMediaPackageSerializerImpl(org.opencastproject.mediapackage.DefaultMediaPackageSerializerImpl) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) MediaPackageSerializer(org.opencastproject.mediapackage.MediaPackageSerializer) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) MediaPackage(org.opencastproject.mediapackage.MediaPackage) URISyntaxException(java.net.URISyntaxException) File(java.io.File) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor)

Aggregations

File (java.io.File)1 URISyntaxException (java.net.URISyntaxException)1 DefaultMediaPackageSerializerImpl (org.opencastproject.mediapackage.DefaultMediaPackageSerializerImpl)1 MediaPackage (org.opencastproject.mediapackage.MediaPackage)1 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)1 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)1 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)1 MediaPackageSerializer (org.opencastproject.mediapackage.MediaPackageSerializer)1 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)1