mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-09-25 06:58:39 +00:00
ImageUpload without multipart
This commit is contained in:
@@ -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;
|
||||
|
||||
/**
|
||||
|
||||
+5
-9
@@ -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);
|
||||
|
||||
+4
-8
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user