Skip to content

Commit 43e4943

Browse files
committed
Allows importing binary versions from different storage types.
1 parent e0842d1 commit 43e4943

File tree

4 files changed

+25
-21
lines changed

4 files changed

+25
-21
lines changed

roda-core/roda-core/src/main/java/org/roda/core/storage/DefaultTransactionalStorageService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,8 @@ public Date getCreationDate(StoragePath storagePath) throws GenericException {
604604

605605
@Override
606606
public void importBinaryVersion(StorageService fromService, StoragePath storagePath, String version)
607-
throws AlreadyExistsException, GenericException, RequestNotValidException, AuthorizationDeniedException {
607+
throws AlreadyExistsException, GenericException, RequestNotValidException, AuthorizationDeniedException,
608+
NotFoundException {
608609
stagingStorageService.importBinaryVersion(fromService, storagePath, version);
609610
}
610611

roda-core/roda-core/src/main/java/org/roda/core/storage/StorageService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,5 +401,6 @@ String getStoragePathAsString(StoragePath storagePath, boolean skipStoragePathCo
401401
Date getCreationDate(StoragePath storagePath) throws GenericException;
402402

403403
void importBinaryVersion(StorageService fromService, StoragePath storagePath, String version)
404-
throws AlreadyExistsException, GenericException, RequestNotValidException, AuthorizationDeniedException;
404+
throws AlreadyExistsException, GenericException, RequestNotValidException, AuthorizationDeniedException,
405+
NotFoundException;
405406
}

roda-core/roda-core/src/main/java/org/roda/core/storage/StorageServiceWrapper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,8 @@ public Date getCreationDate(StoragePath storagePath) throws GenericException {
248248

249249
@Override
250250
public void importBinaryVersion(StorageService fromService, StoragePath storagePath, String version)
251-
throws AlreadyExistsException, GenericException, RequestNotValidException, AuthorizationDeniedException {
251+
throws AlreadyExistsException, GenericException, RequestNotValidException, AuthorizationDeniedException,
252+
NotFoundException {
252253
RodaCoreFactory.checkIfWriteIsAllowedAndIfFalseThrowException(nodeType);
253254
storageService.importBinaryVersion(fromService, storagePath, version);
254255
}

roda-core/roda-core/src/main/java/org/roda/core/storage/fs/FileStorageService.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
import org.roda.core.storage.Resource;
5555
import org.roda.core.storage.StorageService;
5656
import org.roda.core.storage.StorageServiceUtils;
57-
import org.roda.core.storage.StorageServiceWrapper;
5857
import org.roda.core.util.IdUtils;
5958
import org.slf4j.Logger;
6059
import org.slf4j.LoggerFactory;
@@ -997,24 +996,26 @@ public Path getHistoryMetadataPath() {
997996

998997
@Override
999998
public void importBinaryVersion(StorageService fromService, StoragePath storagePath, String version)
1000-
throws AlreadyExistsException, GenericException, RequestNotValidException, AuthorizationDeniedException {
1001-
FileStorageService storageService;
1002-
if (fromService instanceof FileStorageService) {
1003-
storageService = (FileStorageService) fromService;
1004-
} else if (fromService instanceof StorageServiceWrapper) {
1005-
storageService = (FileStorageService) ((StorageServiceWrapper) fromService).getWrappedStorageService();
1006-
} else {
1007-
throw new GenericException("Cannot import binary version from " + fromService.getClass().getName());
1008-
}
999+
throws AlreadyExistsException, GenericException, RequestNotValidException, AuthorizationDeniedException,
1000+
NotFoundException {
1001+
try {
1002+
Path targetDataPath = FSUtils.getEntityPath(historyDataPath, storagePath, version);
1003+
Path targetMetadataPath = FSUtils.getBinaryHistoryMetadataPath(historyDataPath, historyMetadataPath,
1004+
targetDataPath);
10091005

1010-
Path sourceDataPath = FSUtils.getEntityPath(storageService.getHistoryDataPath(), storagePath, version);
1011-
Path targetDataPath = FSUtils.getEntityPath(historyDataPath, storagePath, version);
1012-
FSUtils.copy(sourceDataPath, targetDataPath, true);
1006+
BinaryVersion binaryVersion = fromService.getBinaryVersion(storagePath, version);
10131007

1014-
Path sourceMetadataPath = FSUtils.getBinaryHistoryMetadataPath(storageService.getHistoryDataPath(),
1015-
storageService.getHistoryMetadataPath(), sourceDataPath);
1016-
Path targetMetadataPath = FSUtils.getBinaryHistoryMetadataPath(historyDataPath, historyMetadataPath,
1017-
targetDataPath);
1018-
FSUtils.copy(sourceMetadataPath, targetMetadataPath, true);
1008+
// write binary data to target path
1009+
Files.createDirectories(targetDataPath.getParent());
1010+
ContentPayload payload = binaryVersion.getBinary().getContent();
1011+
payload.writeToPath(targetDataPath);
1012+
1013+
// write metadata to target path
1014+
Files.createDirectories(targetMetadataPath.getParent());
1015+
JsonUtils.writeObjectToFile(binaryVersion, targetMetadataPath);
1016+
1017+
} catch (IOException e) {
1018+
throw new GenericException("Could not import binary version", e);
1019+
}
10191020
}
10201021
}

0 commit comments

Comments
 (0)