Commit 695d75c5 authored by Matija Obreza's avatar Matija Obreza
Browse files

Handle images properly

parent fe867e9f
......@@ -151,6 +151,7 @@ public class FilesystemStorageServiceImpl implements BytesStorageService {
*/
@Override
public byte[] get(final String path, final String filename) throws IOException {
LOG.trace("Retrieveing bytes of {} {}", path, filename);
final File destinationDir = new File(repoDir, path);
final File destinationFile = new File(destinationDir, filename);
......@@ -163,6 +164,8 @@ public class FilesystemStorageServiceImpl implements BytesStorageService {
try (FileInputStream inputStream = new FileInputStream(destinationFile)) {
data = IOUtils.toByteArray(inputStream);
}
} else {
LOG.warn("Repository bytes not found at {}", destinationFile.getAbsolutePath());
}
return data;
......
......@@ -113,6 +113,12 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
throw new InvalidRepositoryFileDataException();
}
if (contentType.startsWith("image/")) {
// Handle images differently
LOG.trace("It's an image! contentType={}", contentType);
return addImage(repositoryPath, originalFilename, contentType, bytes, copyMetaData(metaData, new RepositoryImage()));
}
RepositoryFile repositoryFile = new RepositoryFile();
if (metaData != null) {
......@@ -468,25 +474,31 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
/**
* Copy meta data.
*
* @param sourceFile the source file
* @param resultFile the result file
* @param source the source file
* @param result the result file
* @return
*/
private void copyMetaData(final RepositoryFile sourceFile, final RepositoryFile resultFile) {
resultFile.setContentType(sourceFile.getContentType());
resultFile.setDescription(sourceFile.getDescription());
resultFile.setOriginalFilename(sourceFile.getOriginalFilename());
resultFile.setAccessRights(sourceFile.getAccessRights());
resultFile.setBibliographicCitation(sourceFile.getBibliographicCitation());
resultFile.setCreated(sourceFile.getCreated());
resultFile.setCreatedDate(sourceFile.getCreatedDate());
resultFile.setCreator(sourceFile.getCreator());
resultFile.setExtent(sourceFile.getExtent());
resultFile.setLastModifiedDate(sourceFile.getLastModifiedDate());
resultFile.setLicense(sourceFile.getLicense());
resultFile.setPath(sourceFile.getPath());
resultFile.setRightsHolder(sourceFile.getRightsHolder());
resultFile.setSubject(sourceFile.getSubject());
resultFile.setTitle(sourceFile.getTitle());
private <T extends RepositoryFile> T copyMetaData(final RepositoryFile source, final T result) {
if (source == null) {
return result;
}
result.setContentType(source.getContentType());
result.setDescription(source.getDescription());
result.setOriginalFilename(source.getOriginalFilename());
result.setAccessRights(source.getAccessRights());
result.setBibliographicCitation(source.getBibliographicCitation());
result.setCreated(source.getCreated());
result.setCreatedDate(source.getCreatedDate());
result.setCreator(source.getCreator());
result.setExtent(source.getExtent());
result.setLastModifiedDate(source.getLastModifiedDate());
result.setLicense(source.getLicense());
result.setPath(source.getPath());
result.setRightsHolder(source.getRightsHolder());
result.setSubject(source.getSubject());
result.setTitle(source.getTitle());
return result;
}
/*
......
......@@ -191,14 +191,14 @@ public class ImageGalleryTest {
// Add an image to the gallery path
final TestImage image1 = new TestImage("10x10.png", "image/png");
final RepositoryImage repoImage1 = fileRepoService.addImage(initialPath, image1.getOriginalFilename(), image1.getContentType(), image1.getImageBytes(), null);
final RepositoryImage repoImage1 = (RepositoryImage) fileRepoService.addFile(initialPath, image1.getOriginalFilename(), image1.getContentType(), image1.getImageBytes(), null);
// Expect the image to be included in the list!
assertThat("Image gallery must include the image!", imageGalleryService.loadImageGallery(imageGallery.getPath()).getImages(), contains(repoImage1));
// Add an image to the gallery path
final TestImage image2 = new TestImage("11x10.png", "image/png");
final RepositoryImage repoImage2 = fileRepoService.addImage(initialPath, image2.getOriginalFilename(), image2.getContentType(), image2.getImageBytes(), null);
final RepositoryImage repoImage2 = (RepositoryImage) fileRepoService.addFile(initialPath, image2.getOriginalFilename(), image2.getContentType(), image2.getImageBytes(), null);
assertThat("Image gallery must include both images!", imageGalleryService.loadImageGallery(imageGallery.getPath()).getImages(), contains(repoImage1, repoImage2));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment