Search in sources :

Example 21 with NotFoundException

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);
        }
    }
}
Also used : MediaContainerMetadata(org.opencastproject.inspection.ffmpeg.api.MediaContainerMetadata) TrackImpl(org.opencastproject.mediapackage.track.TrackImpl) Hashtable(java.util.Hashtable) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) URI(java.net.URI) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) MimeType(org.opencastproject.util.MimeType) MediaAnalyzerException(org.opencastproject.inspection.ffmpeg.api.MediaAnalyzerException) UnsupportedElementException(org.opencastproject.mediapackage.UnsupportedElementException) MediaInspectionException(org.opencastproject.inspection.api.MediaInspectionException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) UnknownFileTypeException(org.opencastproject.util.UnknownFileTypeException) MediaInspectionException(org.opencastproject.inspection.api.MediaInspectionException) UnsupportedElementException(org.opencastproject.mediapackage.UnsupportedElementException) UnknownFileTypeException(org.opencastproject.util.UnknownFileTypeException) Stream(org.opencastproject.mediapackage.Stream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) File(java.io.File)

Example 22 with NotFoundException

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);
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 23 with NotFoundException

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);
}
Also used : NotFoundException(org.opencastproject.util.NotFoundException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 24 with NotFoundException

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);
}
Also used : IOException(java.io.IOException) File(java.io.File) URISyntaxException(java.net.URISyntaxException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) AtomicMoveNotSupportedException(java.nio.file.AtomicMoveNotSupportedException)

Example 25 with NotFoundException

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;
}
Also used : NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) File(java.io.File)

Aggregations

NotFoundException (org.opencastproject.util.NotFoundException)382 IOException (java.io.IOException)137 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)130 SchedulerException (org.opencastproject.scheduler.api.SchedulerException)79 MediaPackage (org.opencastproject.mediapackage.MediaPackage)69 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)67 EntityManager (javax.persistence.EntityManager)55 SeriesException (org.opencastproject.series.api.SeriesException)53 Path (javax.ws.rs.Path)52 WebApplicationException (javax.ws.rs.WebApplicationException)52 RestQuery (org.opencastproject.util.doc.rest.RestQuery)51 ConfigurationException (org.osgi.service.cm.ConfigurationException)51 URI (java.net.URI)50 SchedulerConflictException (org.opencastproject.scheduler.api.SchedulerConflictException)50 SchedulerTransactionLockException (org.opencastproject.scheduler.api.SchedulerTransactionLockException)49 Date (java.util.Date)48 Test (org.junit.Test)47 File (java.io.File)46 HttpResponse (org.apache.http.HttpResponse)46 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)46