Skip to content

Commit c61bbd9

Browse files
committed
Implemented better way to delete temporary files
1 parent 4b034f7 commit c61bbd9

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
@@ -601,10 +601,7 @@ else if (acl == ResourceAccessControlList.PublicReadWrite)
601601
}
602602

603603
private String uploadWithACL(String externalFileName, InputStream input, ResourceAccessControlList acl) {
604-
ExternalProviderHelper.InputStreamWithLength streamInfo = null;
605-
try {
606-
streamInfo = ExternalProviderHelper.getInputStreamContentLength(input);
607-
604+
try (ExternalProviderHelper.InputStreamWithLength streamInfo = ExternalProviderHelper.getInputStreamContentLength(input)) {
608605
PutObjectRequest.Builder putObjectRequestBuilder = PutObjectRequest.builder()
609606
.bucket(bucket)
610607
.key(externalFileName)
@@ -622,15 +619,6 @@ private String uploadWithACL(String externalFileName, InputStream input, Resourc
622619
} catch (IOException ex) {
623620
logger.error("Error while uploading file to the external provider.", ex);
624621
return "";
625-
} finally {
626-
// Clean up the temporary file if it was created
627-
if (streamInfo != null && streamInfo.tempFile != null && streamInfo.tempFile.exists()) {
628-
try {
629-
streamInfo.tempFile.delete();
630-
} catch (Exception e) {
631-
logger.warn("Could not delete temporary file: " + streamInfo.tempFile.getAbsolutePath(), e);
632-
}
633-
}
634622
}
635623
}
636624

@@ -736,10 +724,7 @@ private String uploadWithoutACL(String localFile, String externalFileName) {
736724
}
737725

738726
private String uploadWithoutACL(String externalFileName, InputStream input) {
739-
ExternalProviderHelper.InputStreamWithLength streamInfo = null;
740-
try {
741-
streamInfo = ExternalProviderHelper.getInputStreamContentLength(input);
742-
727+
try (ExternalProviderHelper.InputStreamWithLength streamInfo = ExternalProviderHelper.getInputStreamContentLength(input)) {
743728
PutObjectRequest.Builder putObjectRequestBuilder = PutObjectRequest.builder()
744729
.bucket(bucket)
745730
.key(externalFileName)
@@ -755,15 +740,6 @@ private String uploadWithoutACL(String externalFileName, InputStream input) {
755740
} catch (IOException ex) {
756741
logger.error("Error while uploading file to the external provider.", ex);
757742
return "";
758-
} finally {
759-
// Clean up the temporary file if it was created
760-
if (streamInfo != null && streamInfo.tempFile != null && streamInfo.tempFile.exists()) {
761-
try {
762-
streamInfo.tempFile.delete();
763-
} catch (Exception e) {
764-
logger.warn("Could not delete temporary file: " + streamInfo.tempFile.getAbsolutePath(), e);
765-
}
766-
}
767743
}
768744
}
769745

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)