Handle InputStreamPayload in filesystem putBlob

We must save input to subsequently calculate its MD5.  Also tidy up
testWritePayloadOnFile.  Closes #1093.  Test case written by Tom
Manning.
This commit is contained in:
Andrew Gaul 2012-09-29 11:03:00 -07:00 committed by Adrian Cole
parent 269915ce31
commit 554d6cfd24
2 changed files with 30 additions and 12 deletions

View File

@ -34,7 +34,7 @@ import javax.inject.Named;
import javax.inject.Provider; import javax.inject.Provider;
import com.google.common.base.Throwables; import com.google.common.base.Throwables;
import com.google.common.io.Closeables; import com.google.common.io.ByteStreams;
import com.google.common.io.Files; import com.google.common.io.Files;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
@ -201,14 +201,13 @@ public class FilesystemStorageStrategyImpl implements LocalStorageStrategy {
filesystemContainerNameValidator.validate(containerName); filesystemContainerNameValidator.validate(containerName);
filesystemBlobKeyValidator.validate(blobKey); filesystemBlobKeyValidator.validate(blobKey);
File outputFile = getFileForBlobKey(containerName, blobKey); File outputFile = getFileForBlobKey(containerName, blobKey);
FileOutputStream output = null;
try { try {
Files.createParentDirs(outputFile); Files.createParentDirs(outputFile);
if (payload.getRawContent() instanceof File) if (payload.getRawContent() instanceof File)
Files.copy((File) payload.getRawContent(), outputFile); Files.copy((File) payload.getRawContent(), outputFile);
else { else {
output = new FileOutputStream(outputFile); payload = Payloads.newPayload(ByteStreams.toByteArray(payload));
payload.writeTo(output); Files.copy(payload, outputFile);
} }
Payloads.calculateMD5(payload, crypto.md5()); Payloads.calculateMD5(payload, crypto.md5());
String eTag = CryptoStreams.hex(payload.getContentMetadata().getContentMD5()); String eTag = CryptoStreams.hex(payload.getContentMetadata().getContentMD5());
@ -219,7 +218,6 @@ public class FilesystemStorageStrategyImpl implements LocalStorageStrategy {
} }
throw ex; throw ex;
} finally { } finally {
Closeables.closeQuietly(output);
payload.release(); payload.release();
} }
} }

View File

@ -44,6 +44,7 @@ import org.jclouds.filesystem.predicates.validators.internal.FilesystemBlobKeyVa
import org.jclouds.filesystem.predicates.validators.internal.FilesystemContainerNameValidatorImpl; import org.jclouds.filesystem.predicates.validators.internal.FilesystemContainerNameValidatorImpl;
import org.jclouds.filesystem.utils.TestUtils; import org.jclouds.filesystem.utils.TestUtils;
import org.jclouds.io.payloads.FilePayload; import org.jclouds.io.payloads.FilePayload;
import org.jclouds.io.payloads.InputStreamPayload;
import org.testng.annotations.AfterMethod; import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -347,17 +348,36 @@ public class FilesystemStorageStrategyImplTest {
} }
public void testWritePayloadOnFile() throws IOException { public void testWritePayloadOnFile() throws IOException {
String blobKey; String blobKey = TestUtils.createRandomBlobKey("writePayload-", ".img");
File sourceFile; File sourceFile = TestUtils.getImageForBlobPayload();
FilePayload filePayload; FilePayload filePayload = new FilePayload(sourceFile);
blobKey = TestUtils.createRandomBlobKey("writePayload-", ".img");
sourceFile = TestUtils.getImageForBlobPayload();
filePayload = new FilePayload(sourceFile);
Blob blob = storageStrategy.newBlob(blobKey); Blob blob = storageStrategy.newBlob(blobKey);
blob.setPayload(filePayload); blob.setPayload(filePayload);
// write files // write files
storageStrategy.putBlob(CONTAINER_NAME, blob); storageStrategy.putBlob(CONTAINER_NAME, blob);
// verify that the files is equal
File blobFullPath = new File(TARGET_CONTAINER_NAME, blobKey);
InputSupplier<FileInputStream> expectedInput =
Files.newInputStreamSupplier(sourceFile);
InputSupplier<FileInputStream> actualInput =
Files.newInputStreamSupplier(blobFullPath);
assertTrue(ByteStreams.equal(expectedInput, actualInput),
"Files are not equal");
}
public void testWritePayloadOnFileInputStream() throws IOException {
String blobKey = TestUtils.createRandomBlobKey("writePayload-", ".img");
File sourceFile = TestUtils.getImageForBlobPayload();
InputStreamPayload fileInputStreamPayload = new InputStreamPayload(
new FileInputStream(sourceFile));
Blob blob = storageStrategy.newBlob(blobKey);
blob.setPayload(fileInputStreamPayload);
// write files
storageStrategy.putBlob(CONTAINER_NAME, blob);
// verify that the files is equal // verify that the files is equal
File blobFullPath = new File(TARGET_CONTAINER_NAME, blobKey); File blobFullPath = new File(TARGET_CONTAINER_NAME, blobKey);
InputSupplier<FileInputStream> expectedInput = InputSupplier<FileInputStream> expectedInput =