Search in sources :

Example 66 with ContentItem

use of ddf.catalog.content.data.ContentItem in project ddf by codice.

the class VideoThumbnailPluginTest method createMockVideoContentItemWithSizeBytes.

private ContentItem createMockVideoContentItemWithSizeBytes(long sizeBytes) throws Exception {
    final ContentItem mockContentItem = createMockVideoContentItem();
    doReturn(sizeBytes).when(mockContentItem).getSize();
    return mockContentItem;
}
Also used : ContentItem(ddf.catalog.content.data.ContentItem)

Example 67 with ContentItem

use of ddf.catalog.content.data.ContentItem in project ddf by codice.

the class VideoThumbnailPluginTest method testProcessMixedContentItems.

/**
 * Tests processing a mix of {@link ContentItem}s where some get thumbnails. Also tests that
 * processing an {@link UpdateStorageResponse} works for different edge cases.
 */
@Test
public void testProcessMixedContentItems() throws Exception {
    // given
    final ContentItem mediumVideoMockContentItem = createMockVideoContentItemFromResource("/medium.mp4");
    final ContentItem throwsIoExceptionVideoMockContentItem = createMockVideoContentItem();
    doThrow(new IOException()).when(throwsIoExceptionVideoMockContentItem).getSize();
    final ContentItem longVideoMockContentItem = createMockVideoContentItemFromResource("/long.mp4");
    final ContentItem corruptedVideoMockContentItem = createMockVideoContentItemFromResource("/corrupted.mp4");
    final ContentItem videoLargerThanDefaultMaxFileSizeMockContentItem = createMockVideoContentItemWithSizeBytes(DEFAULT_MAX_FILE_SIZE_MB * BYTES_PER_MEGABYTE + 1);
    final ContentItem shortVideoMockContentItem = createMockVideoContentItemFromResource("/short.mp4");
    final ContentItem notVideoMockContentItem = createMockContentItemOfMimeType("image/jpeg");
    final UpdateStorageResponse updateStorageResponse = createMockUpdateStorageResponse(mediumVideoMockContentItem, throwsIoExceptionVideoMockContentItem, longVideoMockContentItem, corruptedVideoMockContentItem, videoLargerThanDefaultMaxFileSizeMockContentItem, shortVideoMockContentItem, notVideoMockContentItem);
    // when
    final UpdateStorageResponse processedUpdateResponse = videoThumbnailPlugin.process(updateStorageResponse);
    // then
    final List<ContentItem> processedContentItems = processedUpdateResponse.getUpdatedContentItems();
    assertThat("There should be exactly 7 returned content items", processedContentItems, hasSize(7));
    verifyThumbnailIsGif(mediumVideoMockContentItem, processedContentItems.get(0));
    verifyThumbnailIsNotSet(throwsIoExceptionVideoMockContentItem, processedContentItems.get(1));
    verifyThumbnailIsGif(longVideoMockContentItem, processedContentItems.get(2));
    verifyThumbnailIsNotSet(corruptedVideoMockContentItem, processedContentItems.get(3));
    verifyThumbnailIsNotSet(videoLargerThanDefaultMaxFileSizeMockContentItem, processedContentItems.get(4));
    verifyThumbnailIsPng(shortVideoMockContentItem, processedContentItems.get(5));
    verifyThumbnailIsNotSet(notVideoMockContentItem, processedContentItems.get(6));
}
Also used : UpdateStorageResponse(ddf.catalog.content.operation.UpdateStorageResponse) IOException(java.io.IOException) ContentItem(ddf.catalog.content.data.ContentItem) Test(org.junit.Test)

Example 68 with ContentItem

use of ddf.catalog.content.data.ContentItem in project ddf by codice.

the class VideoThumbnailPluginTest method testProcessVideoWhenErrorRetrievingContentItemSize.

@Test
public void testProcessVideoWhenErrorRetrievingContentItemSize() throws Exception {
    // given
    final ContentItem mockContentItem = createMockVideoContentItem();
    doThrow(new IOException()).when(mockContentItem).getSize();
    // when
    final CreateStorageResponse processedCreateResponse = videoThumbnailPlugin.process(createMockCreateStorageResponse(mockContentItem));
    // then
    final List<ContentItem> processedContentItems = processedCreateResponse.getCreatedContentItems();
    assertThat("There should be exactly 1 returned content item", processedContentItems, hasSize(1));
    verifyThumbnailIsNotSet(mockContentItem, processedContentItems.get(0));
}
Also used : CreateStorageResponse(ddf.catalog.content.operation.CreateStorageResponse) IOException(java.io.IOException) ContentItem(ddf.catalog.content.data.ContentItem) Test(org.junit.Test)

Example 69 with ContentItem

use of ddf.catalog.content.data.ContentItem in project ddf by codice.

the class CreateOperations method populateMetacardMap.

private void populateMetacardMap(Map<String, Metacard> metacardMap, CreateStorageResponse createStorageResponse) throws IOException {
    for (ContentItem contentItem : createStorageResponse.getCreatedContentItems()) {
        if (StringUtils.isBlank(contentItem.getQualifier())) {
            Metacard metacard = metacardMap.get(contentItem.getId());
            Metacard overrideMetacard = contentItem.getMetacard();
            Metacard updatedMetacard = OverrideAttributesSupport.overrideMetacard(metacard, overrideMetacard, true);
            updatedMetacard.setAttribute(new AttributeImpl(Metacard.RESOURCE_URI, contentItem.getUri()));
            updatedMetacard.setAttribute(new AttributeImpl(Metacard.RESOURCE_SIZE, String.valueOf(contentItem.getSize())));
            metacardMap.put(contentItem.getId(), updatedMetacard);
        }
    }
}
Also used : Metacard(ddf.catalog.data.Metacard) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) ContentItem(ddf.catalog.content.data.ContentItem)

Example 70 with ContentItem

use of ddf.catalog.content.data.ContentItem in project ddf by codice.

the class OperationsMetacardSupport method generateMetacardAndContentItems.

void generateMetacardAndContentItems(List<ContentItem> incomingContentItems, Map<String, Metacard> metacardMap, List<ContentItem> contentItems, Map<String, Map<String, Path>> tmpContentPaths) throws IngestException {
    for (ContentItem contentItem : incomingContentItems) {
        try {
            Path tmpPath = null;
            String fileName;
            long size;
            try (InputStream inputStream = contentItem.getInputStream()) {
                fileName = contentItem.getFilename();
                if (inputStream == null) {
                    throw new IngestException("Could not copy bytes of content message.  Message was NULL.");
                }
                if (!InputValidation.isFileNameClientSideSafe(fileName)) {
                    throw new IngestException("Ignored filename found.");
                }
                String sanitizedFilename = InputValidation.sanitizeFilename(fileName);
                tmpPath = Files.createTempFile(FilenameUtils.getBaseName(sanitizedFilename), FilenameUtils.getExtension(sanitizedFilename));
                Files.copy(inputStream, tmpPath, StandardCopyOption.REPLACE_EXISTING);
                size = Files.size(tmpPath);
                final String key = contentItem.getId();
                Map<String, Path> pathAndQualifiers = tmpContentPaths.get(key);
                if (pathAndQualifiers == null) {
                    pathAndQualifiers = new HashMap<>();
                    pathAndQualifiers.put(contentItem.getQualifier(), tmpPath);
                    tmpContentPaths.put(key, pathAndQualifiers);
                } else {
                    pathAndQualifiers.put(contentItem.getQualifier(), tmpPath);
                }
            } catch (IOException e) {
                if (tmpPath != null) {
                    FileUtils.deleteQuietly(tmpPath.toFile());
                }
                throw new IngestException("Could not copy bytes of content message.", e);
            }
            String mimeTypeRaw = contentItem.getMimeTypeRawData();
            mimeTypeRaw = guessMimeType(mimeTypeRaw, fileName, tmpPath);
            if (!InputValidation.isMimeTypeClientSideSafe(mimeTypeRaw)) {
                throw new IngestException("Unsupported mime type.");
            }
            // If any sanitization was done, rename file name to sanitized file name.
            if (!InputValidation.sanitizeFilename(fileName).equals(fileName)) {
                fileName = InputValidation.sanitizeFilename(fileName);
            } else {
                fileName = updateFileExtension(mimeTypeRaw, fileName);
            }
            Metacard metacard;
            boolean qualifiedContent = StringUtils.isNotEmpty(contentItem.getQualifier());
            if (qualifiedContent) {
                metacard = contentItem.getMetacard();
            } else {
                metacard = metacardFactory.generateMetacard(mimeTypeRaw, contentItem.getId(), fileName, tmpPath);
            }
            metacardMap.put(metacard.getId(), metacard);
            ContentItem generatedContentItem = new ContentItemImpl(metacard.getId(), qualifiedContent ? contentItem.getQualifier() : "", com.google.common.io.Files.asByteSource(tmpPath.toFile()), mimeTypeRaw, fileName, size, metacard);
            contentItems.add(generatedContentItem);
        } catch (Exception e) {
            tmpContentPaths.values().stream().flatMap(id -> id.values().stream()).forEach(path -> FileUtils.deleteQuietly(path.toFile()));
            tmpContentPaths.clear();
            throw new IngestException("Could not create metacard.", e);
        }
    }
}
Also used : Path(java.nio.file.Path) DefaultProbDetector(org.apache.tika.detect.DefaultProbDetector) StringUtils(org.apache.commons.lang.StringUtils) ContentItemImpl(ddf.catalog.content.data.impl.ContentItemImpl) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) MediaType(org.apache.tika.mime.MediaType) DefaultAttributeValueRegistry(ddf.catalog.data.DefaultAttributeValueRegistry) StandardCopyOption(java.nio.file.StandardCopyOption) Metadata(org.apache.tika.metadata.Metadata) ContentItem(ddf.catalog.content.data.ContentItem) Charset(java.nio.charset.Charset) Metacard(ddf.catalog.data.Metacard) Map(java.util.Map) TikaInputStream(org.apache.tika.io.TikaInputStream) Path(java.nio.file.Path) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) Logger(org.slf4j.Logger) Files(java.nio.file.Files) IngestException(ddf.catalog.source.IngestException) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) AttributeInjector(ddf.catalog.data.AttributeInjector) Detector(org.apache.tika.detect.Detector) FrameworkProperties(ddf.catalog.impl.FrameworkProperties) MimeTypeResolutionException(ddf.mime.MimeTypeResolutionException) InputStreamReader(java.io.InputStreamReader) MetacardType(ddf.catalog.data.MetacardType) List(java.util.List) Attribute(ddf.catalog.data.Attribute) InputValidation(org.codice.ddf.platform.util.InputValidation) BufferedReader(java.io.BufferedReader) FilenameUtils(org.apache.commons.io.FilenameUtils) InputStream(java.io.InputStream) TikaInputStream(org.apache.tika.io.TikaInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) IngestException(ddf.catalog.source.IngestException) IOException(java.io.IOException) MimeTypeResolutionException(ddf.mime.MimeTypeResolutionException) Metacard(ddf.catalog.data.Metacard) IngestException(ddf.catalog.source.IngestException) ContentItem(ddf.catalog.content.data.ContentItem) ContentItemImpl(ddf.catalog.content.data.impl.ContentItemImpl)

Aggregations

ContentItem (ddf.catalog.content.data.ContentItem)92 Test (org.junit.Test)51 Metacard (ddf.catalog.data.Metacard)44 ContentItemImpl (ddf.catalog.content.data.impl.ContentItemImpl)31 CreateStorageResponse (ddf.catalog.content.operation.CreateStorageResponse)30 ArrayList (java.util.ArrayList)29 IOException (java.io.IOException)21 HashMap (java.util.HashMap)21 ByteSource (com.google.common.io.ByteSource)20 URI (java.net.URI)20 UpdateStorageRequest (ddf.catalog.content.operation.UpdateStorageRequest)17 CreateStorageRequestImpl (ddf.catalog.content.operation.impl.CreateStorageRequestImpl)16 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)16 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)15 InputStream (java.io.InputStream)14 StorageException (ddf.catalog.content.StorageException)13 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)13 UpdateStorageResponse (ddf.catalog.content.operation.UpdateStorageResponse)12 Map (java.util.Map)12 StorageProvider (ddf.catalog.content.StorageProvider)11