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