Skip to content

Commit b37b170

Browse files
committed
Implemented better way to delete temporary files
(cherry picked from commit c61bbd9)
1 parent 0e8b523 commit b37b170

File tree

6 files changed

+24
-77
lines changed

6 files changed

+24
-77
lines changed

gxcloudstorage-awss3-v1/src/main/java/com/genexus/db/driver/ExternalProviderS3V1.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,7 @@ else if (acl == ResourceAccessControlList.PublicReadWrite) {
219219
}
220220

221221
public String upload(String externalFileName, InputStream input, ResourceAccessControlList acl) {
222-
ExternalProviderHelper.InputStreamWithLength streamInfo = null;
223-
try {
224-
streamInfo = ExternalProviderHelper.getInputStreamContentLength(input);
225-
222+
try (ExternalProviderHelper.InputStreamWithLength streamInfo = ExternalProviderHelper.getInputStreamContentLength(input)) {
226223
ObjectMetadata metadata = new ObjectMetadata();
227224
metadata.setContentLength(streamInfo.contentLength);
228225
metadata.setContentType((externalFileName.endsWith(".tmp") && "application/octet-stream".equals(streamInfo.detectedContentType)) ? "image/jpeg" : streamInfo.detectedContentType);
@@ -234,14 +231,6 @@ public String upload(String externalFileName, InputStream input, ResourceAccessC
234231
} catch (IOException ex) {
235232
logger.error("Error while uploading file to the external provider.", ex);
236233
return "";
237-
} finally {
238-
if (streamInfo != null && streamInfo.tempFile != null && streamInfo.tempFile.exists()) {
239-
try {
240-
streamInfo.tempFile.delete();
241-
} catch (Exception e) {
242-
logger.warn("Could not delete temporary file: " + streamInfo.tempFile.getAbsolutePath(), e);
243-
}
244-
}
245234
}
246235
}
247236

gxcloudstorage-awss3-v2/src/main/java/com/genexus/db/driver/ExternalProviderS3V2.java

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -624,10 +624,7 @@ else if (acl == ResourceAccessControlList.PublicReadWrite)
624624
}
625625

626626
private String uploadWithACL(String externalFileName, InputStream input, ResourceAccessControlList acl) {
627-
ExternalProviderHelper.InputStreamWithLength streamInfo = null;
628-
try {
629-
streamInfo = ExternalProviderHelper.getInputStreamContentLength(input);
630-
627+
try (ExternalProviderHelper.InputStreamWithLength streamInfo = ExternalProviderHelper.getInputStreamContentLength(input)) {
631628
PutObjectRequest.Builder putObjectRequestBuilder = PutObjectRequest.builder()
632629
.bucket(bucket)
633630
.key(externalFileName)
@@ -645,15 +642,6 @@ private String uploadWithACL(String externalFileName, InputStream input, Resourc
645642
} catch (IOException ex) {
646643
logger.error("Error while uploading file to the external provider.", ex);
647644
return "";
648-
} finally {
649-
// Clean up the temporary file if it was created
650-
if (streamInfo != null && streamInfo.tempFile != null && streamInfo.tempFile.exists()) {
651-
try {
652-
streamInfo.tempFile.delete();
653-
} catch (Exception e) {
654-
logger.warn("Could not delete temporary file: " + streamInfo.tempFile.getAbsolutePath(), e);
655-
}
656-
}
657645
}
658646
}
659647

@@ -759,10 +747,7 @@ private String uploadWithoutACL(String localFile, String externalFileName) {
759747
}
760748

761749
private String uploadWithoutACL(String externalFileName, InputStream input) {
762-
ExternalProviderHelper.InputStreamWithLength streamInfo = null;
763-
try {
764-
streamInfo = ExternalProviderHelper.getInputStreamContentLength(input);
765-
750+
try (ExternalProviderHelper.InputStreamWithLength streamInfo = ExternalProviderHelper.getInputStreamContentLength(input)) {
766751
PutObjectRequest.Builder putObjectRequestBuilder = PutObjectRequest.builder()
767752
.bucket(bucket)
768753
.key(externalFileName)
@@ -778,15 +763,6 @@ private String uploadWithoutACL(String externalFileName, InputStream input) {
778763
} catch (IOException ex) {
779764
logger.error("Error while uploading file to the external provider.", ex);
780765
return "";
781-
} finally {
782-
// Clean up the temporary file if it was created
783-
if (streamInfo != null && streamInfo.tempFile != null && streamInfo.tempFile.exists()) {
784-
try {
785-
streamInfo.tempFile.delete();
786-
} catch (Exception e) {
787-
logger.warn("Could not delete temporary file: " + streamInfo.tempFile.getAbsolutePath(), e);
788-
}
789-
}
790766
}
791767
}
792768

gxcloudstorage-azureblob/src/main/java/com/genexus/db/driver/ExternalProviderAzureStorage.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,7 @@ public String upload(String localFile, String externalFileName, ResourceAccessCo
139139
}
140140

141141
public String upload(String externalFileName, InputStream input, ResourceAccessControlList acl) {
142-
ExternalProviderHelper.InputStreamWithLength streamInfo = null;
143-
try {
144-
streamInfo = ExternalProviderHelper.getInputStreamContentLength(input);
145-
142+
try (ExternalProviderHelper.InputStreamWithLength streamInfo = ExternalProviderHelper.getInputStreamContentLength(input)) {
146143
CloudBlockBlob blob = getCloudBlockBlob(externalFileName, acl);
147144
blob.getProperties().setContentType((externalFileName.endsWith(".tmp") && "application/octet-stream".equals(streamInfo.detectedContentType)) ? "image/jpeg" : streamInfo.detectedContentType);
148145
try (BlobOutputStream blobOutputStream = blob.openOutputStream()) {
@@ -163,15 +160,6 @@ public String upload(String externalFileName, InputStream input, ResourceAccessC
163160
logger.error("Error uploading file", ex);
164161
return "";
165162
}
166-
finally {
167-
if (streamInfo != null && streamInfo.tempFile != null && streamInfo.tempFile.exists()) {
168-
try {
169-
streamInfo.tempFile.delete();
170-
} catch (Exception e) {
171-
logger.warn("Could not delete temporary file: " + streamInfo.tempFile.getAbsolutePath(), e);
172-
}
173-
}
174-
}
175163
}
176164

177165
public String get(String externalFileName, ResourceAccessControlList acl, int expirationMinutes) {

gxcloudstorage-common/src/main/java/com/genexus/db/driver/ExternalProviderHelper.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
public class ExternalProviderHelper {
1010

11+
public static final int BUFFER_MARK_LIMIT = 128 * 1024;
12+
1113
public static String getServicePropertyValue(GXService s, String propName, boolean isSecure){
1214
String value = s.getProperties().get(propName);
1315
if (value != null){
@@ -38,14 +40,14 @@ public static InputStreamWithLength getInputStreamContentLength(InputStream inpu
3840
}
3941
long size = tempFile.length();
4042
InputStream bufferedInput = new BufferedInputStream(new FileInputStream(tempFile));
41-
bufferedInput.mark(128 * 1024);
43+
bufferedInput.mark(BUFFER_MARK_LIMIT);
4244
Tika tika = new Tika();
4345
String detectedContentType = tika.detect(bufferedInput);
4446
bufferedInput.reset();
4547
return new InputStreamWithLength(bufferedInput, size, tempFile, detectedContentType);
4648
}
4749

48-
public static class InputStreamWithLength {
50+
public static class InputStreamWithLength implements AutoCloseable {
4951
public final InputStream inputStream;
5052
public final long contentLength;
5153
public final File tempFile; // nullable
@@ -57,5 +59,18 @@ public InputStreamWithLength(InputStream inputStream, long contentLength, File t
5759
this.tempFile = tempFile;
5860
this.detectedContentType = detectedContentType;
5961
}
62+
63+
@Override
64+
public void close() throws IOException {
65+
try {
66+
if (inputStream != null) {
67+
inputStream.close();
68+
}
69+
} finally {
70+
if (tempFile != null && tempFile.exists()) {
71+
tempFile.delete();
72+
}
73+
}
74+
}
6075
}
6176
}

gxcloudstorage-googlecloudstorage/src/main/java/com/genexus/db/driver/ExternalProviderGoogle.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,7 @@ private void setBlobAcl(BlobId blobId, ResourceAccessControlList acl) {
169169
}
170170

171171
public String upload(String externalFileName, InputStream input, ResourceAccessControlList acl) {
172-
ExternalProviderHelper.InputStreamWithLength streamInfo = null;
173-
try {
174-
streamInfo = ExternalProviderHelper.getInputStreamContentLength(input);
175-
172+
try (ExternalProviderHelper.InputStreamWithLength streamInfo = ExternalProviderHelper.getInputStreamContentLength(input)) {
176173
BlobId blobId = BlobId.of(bucket, externalFileName);
177174
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
178175

@@ -183,14 +180,6 @@ public String upload(String externalFileName, InputStream input, ResourceAccessC
183180
} catch (IOException ex) {
184181
handleIOException(ex);
185182
return "";
186-
} finally {
187-
if (streamInfo != null && streamInfo.tempFile != null && streamInfo.tempFile.exists()) {
188-
try {
189-
streamInfo.tempFile.delete();
190-
} catch (Exception e) {
191-
logger.warn("Could not delete temporary file: " + streamInfo.tempFile.getAbsolutePath(), e);
192-
}
193-
}
194183
}
195184
}
196185

gxcloudstorage-ibmcos/src/main/java/com/genexus/db/driver/ExternalProviderIBM.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,7 @@ else if (acl == ResourceAccessControlList.PublicReadWrite) {
149149
}
150150

151151
public String upload(String externalFileName, InputStream input, ResourceAccessControlList acl) {
152-
ExternalProviderHelper.InputStreamWithLength streamInfo = null;
153-
try {
154-
streamInfo = ExternalProviderHelper.getInputStreamContentLength(input);
152+
try (ExternalProviderHelper.InputStreamWithLength streamInfo = ExternalProviderHelper.getInputStreamContentLength(input)) {
155153
ObjectMetadata metadata = new ObjectMetadata();
156154
metadata.setContentLength(streamInfo.contentLength);
157155
metadata.setContentType((externalFileName.endsWith(".tmp") && "application/octet-stream".equals(streamInfo.detectedContentType)) ? "image/jpeg" : streamInfo.detectedContentType);
@@ -162,15 +160,7 @@ public String upload(String externalFileName, InputStream input, ResourceAccessC
162160
} catch (IOException ex) {
163161
logger.error("Error while uploading file to the external provider.", ex);
164162
return "";
165-
} finally {
166-
if (streamInfo != null && streamInfo.tempFile != null && streamInfo.tempFile.exists()) {
167-
try {
168-
streamInfo.tempFile.delete();
169-
} catch (Exception e) {
170-
logger.warn("Could not delete temporary file: " + streamInfo.tempFile.getAbsolutePath(), e);
171-
}
172-
}
173-
}
163+
}
174164
}
175165

176166
public String get(String externalFileName, ResourceAccessControlList acl, int expirationMinutes) {

0 commit comments

Comments
 (0)