|
17 | 17 |
|
18 | 18 | import com.amazonaws.services.s3.AmazonS3;
|
19 | 19 | import com.amazonaws.services.s3.AmazonS3Client;
|
20 |
| -import com.amazonaws.util.IOUtils; |
21 | 20 |
|
22 | 21 | import java.io.FileNotFoundException;
|
23 | 22 | import java.io.FileOutputStream;
|
@@ -209,26 +208,34 @@ else if (acl == ResourceAccessControlList.PublicReadWrite) {
|
209 | 208 | return accessControl;
|
210 | 209 | }
|
211 | 210 |
|
212 |
| - public String upload(String externalFileName, InputStream input, ResourceAccessControlList acl) { |
213 |
| - byte[] bytes; |
214 |
| - try { |
215 |
| - bytes = IOUtils.toByteArray(input); |
216 |
| - ObjectMetadata metadata = new ObjectMetadata(); |
217 |
| - metadata.setContentLength(bytes.length); |
218 |
| - if (externalFileName.endsWith(".tmp")) { |
219 |
| - metadata.setContentType("image/jpeg"); |
220 |
| - } |
221 |
| - String upload = ""; |
222 |
| - try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes)) { |
223 |
| - client.putObject(new PutObjectRequest(bucket, externalFileName, byteArrayInputStream, metadata).withCannedAcl(internalToAWSACL(acl))); |
224 |
| - upload = getResourceUrl(externalFileName, acl, defaultExpirationMinutes); |
| 211 | + public String upload(String externalFileName, InputStream input, ResourceAccessControlList acl) { |
| 212 | + ExternalProviderHelper.InputStreamWithLength streamInfo = null; |
| 213 | + try { |
| 214 | + streamInfo = ExternalProviderHelper.getInputStreamContentLength(input); |
| 215 | + |
| 216 | + ObjectMetadata metadata = new ObjectMetadata(); |
| 217 | + metadata.setContentLength(streamInfo.contentLength); |
| 218 | + |
| 219 | + if (externalFileName.endsWith(".tmp")) { |
| 220 | + metadata.setContentType("image/jpeg"); |
225 | 221 | }
|
| 222 | + String upload = ""; |
| 223 | + client.putObject(new PutObjectRequest(bucket, externalFileName, streamInfo.inputStream, metadata).withCannedAcl(internalToAWSACL(acl))); |
| 224 | + upload = getResourceUrl(externalFileName, acl, defaultExpirationMinutes); |
226 | 225 | return upload;
|
227 |
| - } catch (IOException ex) { |
228 |
| - logger.error("Error while uploading file to the external provider.", ex); |
229 |
| - return ""; |
230 |
| - } |
231 |
| - } |
| 226 | + } catch (IOException ex) { |
| 227 | + logger.error("Error while uploading file to the external provider.", ex); |
| 228 | + return ""; |
| 229 | + } finally { |
| 230 | + if (streamInfo != null && streamInfo.tempFile != null && streamInfo.tempFile.exists()) { |
| 231 | + try { |
| 232 | + streamInfo.tempFile.delete(); |
| 233 | + } catch (Exception e) { |
| 234 | + logger.warn("Could not delete temporary file: " + streamInfo.tempFile.getAbsolutePath(), e); |
| 235 | + } |
| 236 | + } |
| 237 | + } |
| 238 | + } |
232 | 239 |
|
233 | 240 | public String get(String externalFileName, ResourceAccessControlList acl, int expirationMinutes) {
|
234 | 241 | client.getObjectMetadata(bucket, externalFileName);
|
|
0 commit comments