ImageUpload without multipart

This commit is contained in:
jan
2015-01-23 15:35:13 +01:00
parent c99af6e6a7
commit 91e69dd4a7
5 changed files with 24 additions and 34 deletions
@@ -22,7 +22,7 @@ public interface FileStorageService {
* @param originalFileName
* may be {@code null}
*/
URI storeFile(InputStream is, String originalFileName, long lengthInBytes) throws IOException,
URI storeFile(InputStream is, String fileExtension, long lengthInBytes) throws IOException,
OperationFailedException, InvalidPropertiesException;
/**
@@ -79,13 +79,9 @@ public class AmazonS3FileStorageServiceImpl implements FileStorageService {
return new AmazonS3Client(creds);
}
private static String getKey(String originalFileName) {
private static String getKey(String fileExtension) {
String key = UUID.randomUUID().toString();
if (originalFileName != null) {
String ending = originalFileName.substring(originalFileName.lastIndexOf("."));
key += ending;
// key += "/" + originalFileName;
}
key += fileExtension;
return key;
}
@@ -100,18 +96,18 @@ public class AmazonS3FileStorageServiceImpl implements FileStorageService {
}
@Override
public URI storeFile(InputStream is, String originalFileName, long lengthInBytes)
public URI storeFile(InputStream is, String fileExtension, long lengthInBytes)
throws InvalidPropertiesException, OperationFailedException {
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(lengthInBytes);
String key = getKey(originalFileName);
String key = getKey(fileExtension);
PutObjectRequest request = new PutObjectRequest(bucketName.getValue(), key, is, metadata)
.withCannedAcl(CannedAccessControlList.PublicRead);
AmazonS3Client s3Client = createS3Client();
try {
s3Client.putObject(request);
} catch (AmazonClientException e) {
throw new OperationFailedException("Could not store file " + originalFileName, e);
throw new OperationFailedException("Could not store file", e);
}
URI uri = getUri(key);
logger.info("Stored file " + uri);
@@ -23,9 +23,9 @@ public class LocalFileStorageServiceImpl implements FileStorageService {
private static final String retrievalProtocol = "http";
@Override
public URI storeFile(InputStream is, String originalFileName, long lengthInBytes) throws IOException {
public URI storeFile(InputStream is, String fileExtension, long lengthInBytes) throws IOException {
OutputStream outputStream = null;
String pathToFile = path + "/" + getKey(originalFileName);
String pathToFile = path + "/" + getKey(fileExtension);
outputStream = new FileOutputStream(new File(pathToFile));
@@ -50,13 +50,9 @@ public class LocalFileStorageServiceImpl implements FileStorageService {
return getUri(pathToFile);
}
private static String getKey(String originalFileName) {
private static String getKey(String fileEnding) {
String key = UUID.randomUUID().toString();
if (originalFileName != null) {
String ending = originalFileName.substring(originalFileName.lastIndexOf("."));
key += ending;
// key += "/" + originalFileName;
}
key += fileEnding;
return key;
}