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;
}
Aggregations