Skip to content

Commit 828ac94

Browse files
committed
Tries to determine the content-type of files uploaded to external storage. Previously, if the file extension was .tmp, it was always set to image/jpeg, which caused issues—such as when uploading a PDF: upon downloading it, the file couldn't be opened properly.
1 parent 2fcf17f commit 828ac94

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,17 @@ public String upload(String localFile, String externalFileName, ResourceAccessCo
138138
}
139139

140140
public String upload(String externalFileName, InputStream input, ResourceAccessControlList acl) {
141-
141+
ExternalProviderHelper.InputStreamWithLength streamInfo = null;
142142
try {
143+
streamInfo = ExternalProviderHelper.getInputStreamContentLength(input);
144+
143145
CloudBlockBlob blob = getCloudBlockBlob(externalFileName, acl);
144-
if (externalFileName.endsWith(".tmp")) {
145-
blob.getProperties().setContentType("image/jpeg");
146-
}
146+
blob.getProperties().setContentType((externalFileName.endsWith(".tmp") && "application/octet-stream".equals(streamInfo.detectedContentType)) ? "image/jpeg" : streamInfo.detectedContentType);
147147
try (BlobOutputStream blobOutputStream = blob.openOutputStream()) {
148-
int next = input.read();
149-
while (next != -1) {
150-
blobOutputStream.write(next);
151-
next = input.read();
148+
byte[] buffer = new byte[8192];
149+
int bytesRead;
150+
while ((bytesRead = streamInfo.inputStream.read(buffer)) != -1) {
151+
blobOutputStream.write(buffer, 0, bytesRead);
152152
}
153153
}
154154
return getResourceUrl(externalFileName, acl, DEFAULT_EXPIRATION_MINUTES);
@@ -162,6 +162,15 @@ public String upload(String externalFileName, InputStream input, ResourceAccessC
162162
logger.error("Error uploading file", ex);
163163
return "";
164164
}
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+
}
165174
}
166175

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

0 commit comments

Comments
 (0)