use of org.opencastproject.util.NotFoundException in project opencast by opencast.
the class MediaInspector method enrichTrack.
/**
* Enriches the track's metadata and can be executed in an asynchronous way.
*
* @param originalTrack
* the original track
* @param override
* <code>true</code> to override existing metadata
* @return the media package element
* @throws MediaInspectionException
*/
private MediaPackageElement enrichTrack(final Track originalTrack, final boolean override, final Map<String, String> options) throws MediaInspectionException {
try {
URI originalTrackUrl = originalTrack.getURI();
MediaPackageElementFlavor flavor = originalTrack.getFlavor();
logger.debug("enrich(" + originalTrackUrl + ") called");
// Get the file from the URL
File file = null;
try {
file = workspace.get(originalTrackUrl);
} catch (NotFoundException e) {
throw new MediaInspectionException("File " + originalTrackUrl + " was not found and can therefore not be " + "inspected", e);
} catch (IOException e) {
throw new MediaInspectionException("Error accessing " + originalTrackUrl, e);
}
// TODO: Try to guess the extension from the container's metadata
if ("".equals(FilenameUtils.getExtension(file.getName()))) {
throw new MediaInspectionException("Can not inspect files without a filename extension");
}
MediaContainerMetadata metadata = getFileMetadata(file, getAccurateFrameCount(options));
if (metadata == null) {
throw new MediaInspectionException("Unable to acquire media metadata for " + originalTrackUrl);
} else {
TrackImpl track = null;
try {
track = (TrackImpl) MediaPackageElementBuilderFactory.newInstance().newElementBuilder().elementFromURI(originalTrackUrl, MediaPackageElement.Type.Track, flavor);
} catch (UnsupportedElementException e) {
throw new MediaInspectionException("Unable to create track element from " + file, e);
}
// init the new track with old
track.setChecksum(originalTrack.getChecksum());
track.setDuration(originalTrack.getDuration());
track.setElementDescription(originalTrack.getElementDescription());
track.setFlavor(flavor);
track.setIdentifier(originalTrack.getIdentifier());
track.setMimeType(originalTrack.getMimeType());
track.setReference(originalTrack.getReference());
track.setSize(file.length());
track.setURI(originalTrackUrl);
for (String tag : originalTrack.getTags()) {
track.addTag(tag);
}
// enrich the new track with basic info
if (track.getDuration() == null || override)
track.setDuration(metadata.getDuration());
if (track.getChecksum() == null || override) {
try {
track.setChecksum(Checksum.create(ChecksumType.DEFAULT_TYPE, file));
} catch (IOException e) {
throw new MediaInspectionException("Unable to read " + file, e);
}
}
// Add the mime type if it's not already present
if (track.getMimeType() == null || override) {
try {
MimeType mimeType = MimeTypes.fromURI(track.getURI());
// The mimetype library doesn't know about audio/video metadata, so the type might be wrong.
if ("audio".equals(mimeType.getType()) && metadata.hasVideoStreamMetadata()) {
mimeType = MimeTypes.parseMimeType("video/" + mimeType.getSubtype());
} else if ("video".equals(mimeType.getType()) && !metadata.hasVideoStreamMetadata()) {
mimeType = MimeTypes.parseMimeType("audio/" + mimeType.getSubtype());
}
track.setMimeType(mimeType);
} catch (UnknownFileTypeException e) {
logger.info("Unable to detect the mimetype for track {} at {}", track.getIdentifier(), track.getURI());
}
}
// find all streams
Dictionary<String, Stream> streamsId2Stream = new Hashtable<String, Stream>();
for (Stream stream : originalTrack.getStreams()) {
streamsId2Stream.put(stream.getIdentifier(), stream);
}
// audio list
try {
addAudioStreamMetadata(track, metadata);
} catch (Exception e) {
throw new MediaInspectionException("Unable to extract audio metadata from " + file, e);
}
// video list
try {
addVideoStreamMetadata(track, metadata);
} catch (Exception e) {
throw new MediaInspectionException("Unable to extract video metadata from " + file, e);
}
logger.info("Successfully inspected track {}", track);
return track;
}
} catch (Exception e) {
logger.warn("Error enriching track " + originalTrack, e);
if (e instanceof MediaInspectionException) {
throw (MediaInspectionException) e;
} else {
throw new MediaInspectionException(e);
}
}
}
use of org.opencastproject.util.NotFoundException in project opencast by opencast.
the class InspectWorkflowOperationHandler method loadDublinCoreCatalog.
/**
* Loads a dublin core catalog from a mediapackage's catalog reference
*
* @param catalog
* the mediapackage's reference to this catalog
* @return the dublin core
* @throws IOException
* if there is a problem loading or parsing the dublin core object
*/
protected DublinCoreCatalog loadDublinCoreCatalog(Catalog catalog) throws IOException {
InputStream in = null;
try {
File f = workspace.get(catalog.getURI());
in = new FileInputStream(f);
return dcService.load(in);
} catch (NotFoundException e) {
throw new IOException("Unable to open catalog " + catalog, e);
} finally {
IOUtils.closeQuietly(in);
}
}
use of org.opencastproject.util.NotFoundException in project opencast by opencast.
the class WorkingFileRepositoryImpl method getFromCollection.
/**
* {@inheritDoc}
*
* @see org.opencastproject.workingfilerepository.api.WorkingFileRepository#getFromCollection(java.lang.String,
* java.lang.String)
*/
@Override
public InputStream getFromCollection(String collectionId, String fileName) throws NotFoundException, IOException {
File f = getFileFromCollection(collectionId, fileName);
if (f == null || !f.isFile()) {
throw new NotFoundException("Unable to locate " + f + " in the working file repository");
}
logger.debug("Attempting to read file {}", f.getAbsolutePath());
return new FileInputStream(f);
}
use of org.opencastproject.util.NotFoundException in project opencast by opencast.
the class WorkingFileRepositoryImpl method copyTo.
/**
* {@inheritDoc}
*
* @see org.opencastproject.workingfilerepository.api.WorkingFileRepository#copyTo(java.lang.String, java.lang.String,
* java.lang.String, java.lang.String, java.lang.String)
*/
@Override
public URI copyTo(String fromCollection, String fromFileName, String toMediaPackage, String toMediaPackageElement, String toFileName) throws NotFoundException, IOException {
File source = getFileFromCollection(fromCollection, fromFileName);
if (source == null)
throw new IllegalArgumentException("Source file " + fromCollection + "/" + fromFileName + " does not exist");
File destDir = getElementDirectory(toMediaPackage, toMediaPackageElement);
if (!destDir.exists()) {
// we needed to create the directory, but couldn't
try {
FileUtils.forceMkdir(destDir);
} catch (IOException e) {
throw new IllegalStateException("could not create mediapackage/element directory '" + destDir.getAbsolutePath() + "' : " + e);
}
}
File destFile;
try {
destFile = new File(destDir, PathSupport.toSafeName(toFileName));
FileSupport.link(source, destFile);
createMd5(destFile);
} catch (Exception e) {
FileUtils.deleteDirectory(destDir);
}
return getURI(toMediaPackage, toMediaPackageElement, toFileName);
}
use of org.opencastproject.util.NotFoundException in project opencast by opencast.
the class WorkingFileRepositoryImpl method deleteFromCollection.
/**
* {@inheritDoc}
*
* @see org.opencastproject.workingfilerepository.api.WorkingFileRepository#deleteFromCollection(java.lang.String,
* java.lang.String,boolean)
*/
@Override
public boolean deleteFromCollection(String collectionId, String fileName, boolean removeCollection) throws IOException {
File f = null;
try {
f = getFileFromCollection(collectionId, fileName);
} catch (NotFoundException e) {
logger.trace("File {}/{} does not exist", collectionId, fileName);
return false;
}
File md5File = getMd5File(f);
if (!f.isFile())
throw new IllegalStateException(f + " is not a regular file");
if (!md5File.isFile())
throw new IllegalStateException(md5File + " is not a regular file");
if (!md5File.delete())
throw new IOException("MD5 hash " + md5File + " cannot be deleted");
if (!f.delete())
throw new IOException(f + " cannot be deleted");
if (removeCollection) {
File parentDirectory = f.getParentFile();
if (parentDirectory.isDirectory() && parentDirectory.list().length == 0) {
logger.debug("Attempting to delete empty collection directory {}", parentDirectory.getAbsolutePath());
try {
FileUtils.forceDelete(parentDirectory);
} catch (IOException e) {
logger.warn("Unable to delete empty collection directory {}", parentDirectory.getAbsolutePath());
return false;
}
}
}
return true;
}
Aggregations