Merge pull request #858 from andrewgaul/filesystem-putblob-inputstreampayload

Handle InputStreamPayload in filesystem putBlob
This commit is contained in:
Adrian Cole 2012-09-29 12:36:46 -07:00
commit f96638ff44
2 changed files with 30 additions and 12 deletions

View File

@ -34,7 +34,7 @@ import javax.inject.Named;
import javax.inject.Provider;
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 org.apache.commons.io.FileUtils;
@ -201,14 +201,13 @@ public class FilesystemStorageStrategyImpl implements LocalStorageStrategy {
filesystemContainerNameValidator.validate(containerName);
filesystemBlobKeyValidator.validate(blobKey);
File outputFile = getFileForBlobKey(containerName, blobKey);
FileOutputStream output = null;
try {
Files.createParentDirs(outputFile);
if (payload.getRawContent() instanceof File)
Files.copy((File) payload.getRawContent(), outputFile);
else {
output = new FileOutputStream(outputFile);
payload.writeTo(output);
payload = Payloads.newPayload(ByteStreams.toByteArray(payload));
Files.copy(payload, outputFile);
}
Payloads.calculateMD5(payload, crypto.md5());
String eTag = CryptoStreams.hex(payload.getContentMetadata().getContentMD5());
@ -219,7 +218,6 @@ public class FilesystemStorageStrategyImpl implements LocalStorageStrategy {
}
throw ex;
} finally {
Closeables.closeQuietly(output);
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.utils.TestUtils;
import org.jclouds.io.payloads.FilePayload;
import org.jclouds.io.payloads.InputStreamPayload;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
@ -347,17 +348,36 @@ public class FilesystemStorageStrategyImplTest {
}
public void testWritePayloadOnFile() throws IOException {
String blobKey;
File sourceFile;
FilePayload filePayload;
blobKey = TestUtils.createRandomBlobKey("writePayload-", ".img");
sourceFile = TestUtils.getImageForBlobPayload();
filePayload = new FilePayload(sourceFile);
String blobKey = TestUtils.createRandomBlobKey("writePayload-", ".img");
File sourceFile = TestUtils.getImageForBlobPayload();
FilePayload filePayload = new FilePayload(sourceFile);
Blob blob = storageStrategy.newBlob(blobKey);
blob.setPayload(filePayload);
// write files
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
File blobFullPath = new File(TARGET_CONTAINER_NAME, blobKey);
InputSupplier<FileInputStream> expectedInput =