Skip to content

Commit e131aea

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 fea969c commit e131aea

File tree

9 files changed

+33
-26
lines changed

9 files changed

+33
-26
lines changed

common/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
<dependency>
3939
<groupId>commons-io</groupId>
4040
<artifactId>commons-io</artifactId>
41-
<version>2.11.0</version>
41+
<version>2.12.0</version>
4242
</dependency>
4343
<dependency>
4444
<groupId>org.bouncycastle</groupId>

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,8 @@ public String upload(String externalFileName, InputStream input, ResourceAccessC
225225

226226
ObjectMetadata metadata = new ObjectMetadata();
227227
metadata.setContentLength(streamInfo.contentLength);
228+
metadata.setContentType((externalFileName.endsWith(".tmp") && "application/octet-stream".equals(streamInfo.detectedContentType)) ? "image/jpeg" : streamInfo.detectedContentType);
228229

229-
if (externalFileName.endsWith(".tmp")) {
230-
metadata.setContentType("image/jpeg");
231-
}
232230
String upload = "";
233231
client.putObject(new PutObjectRequest(bucket, externalFileName, streamInfo.inputStream, metadata).withCannedAcl(internalToAWSACL(acl)));
234232
upload = getResourceUrl(externalFileName, acl, defaultExpirationMinutes);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ private String uploadWithoutACL(String externalFileName, InputStream input) {
743743
PutObjectRequest.Builder putObjectRequestBuilder = PutObjectRequest.builder()
744744
.bucket(bucket)
745745
.key(externalFileName)
746-
.contentType(externalFileName.endsWith(".tmp") ? "image/jpeg" : null);
746+
.contentType((externalFileName.endsWith(".tmp") && "application/octet-stream".equals(streamInfo.detectedContentType)) ? "image/jpeg" : streamInfo.detectedContentType);
747747
PutObjectRequest putObjectRequest = putObjectRequestBuilder.build();
748748

749749
PutObjectResponse response = client.putObject(putObjectRequest, RequestBody.fromInputStream(streamInfo.inputStream, streamInfo.contentLength));

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,16 +139,16 @@ public String upload(String localFile, String externalFileName, ResourceAccessCo
139139
}
140140

141141
public String upload(String externalFileName, InputStream input, ResourceAccessControlList acl) {
142-
142+
ExternalProviderHelper.InputStreamWithLength streamInfo = null;
143143
try {
144+
streamInfo = ExternalProviderHelper.getInputStreamContentLength(input);
145+
144146
CloudBlockBlob blob = getCloudBlockBlob(externalFileName, acl);
145-
if (externalFileName.endsWith(".tmp")) {
146-
blob.getProperties().setContentType("image/jpeg");
147-
}
147+
blob.getProperties().setContentType((externalFileName.endsWith(".tmp") && "application/octet-stream".equals(streamInfo.detectedContentType)) ? "image/jpeg" : streamInfo.detectedContentType);
148148
try (BlobOutputStream blobOutputStream = blob.openOutputStream()) {
149149
byte[] buffer = new byte[8192];
150150
int bytesRead;
151-
while ((bytesRead = input.read(buffer)) != -1) {
151+
while ((bytesRead = streamInfo.inputStream.read(buffer)) != -1) {
152152
blobOutputStream.write(buffer, 0, bytesRead);
153153
}
154154
}
@@ -163,6 +163,15 @@ public String upload(String externalFileName, InputStream input, ResourceAccessC
163163
logger.error("Error uploading file", ex);
164164
return "";
165165
}
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+
}
166175
}
167176

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

gxcloudstorage-common/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
<artifactId>log4j-api</artifactId>
3535
<version>${log4j.version}</version>
3636
</dependency>
37+
<dependency>
38+
<groupId>org.apache.tika</groupId>
39+
<artifactId>tika-core</artifactId>
40+
<version>3.2.0</version>
41+
</dependency>
3742
</dependencies>
3843

3944
</project>

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.genexus.util.GXService;
55

66
import java.io.*;
7+
import org.apache.tika.Tika;
78

89
public class ExternalProviderHelper {
910

@@ -36,19 +37,25 @@ public static InputStreamWithLength getInputStreamContentLength(InputStream inpu
3637
}
3738
}
3839
long size = tempFile.length();
39-
InputStream newInput = new FileInputStream(tempFile);
40-
return new InputStreamWithLength(newInput, size, tempFile);
40+
InputStream bufferedInput = new BufferedInputStream(new FileInputStream(tempFile));
41+
bufferedInput.mark(128 * 1024);
42+
Tika tika = new Tika();
43+
String detectedContentType = tika.detect(bufferedInput);
44+
bufferedInput.reset();
45+
return new InputStreamWithLength(bufferedInput, size, tempFile, detectedContentType);
4146
}
4247

4348
public static class InputStreamWithLength {
4449
public final InputStream inputStream;
4550
public final long contentLength;
4651
public final File tempFile; // nullable
52+
public final String detectedContentType;
4753

48-
public InputStreamWithLength(InputStream inputStream, long contentLength, File tempFile) {
54+
public InputStreamWithLength(InputStream inputStream, long contentLength, File tempFile, String detectedContentType) {
4955
this.inputStream = inputStream;
5056
this.contentLength = contentLength;
5157
this.tempFile = tempFile;
58+
this.detectedContentType = detectedContentType;
5259
}
5360
}
5461
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,7 @@ public String upload(String externalFileName, InputStream input, ResourceAccessC
154154
streamInfo = ExternalProviderHelper.getInputStreamContentLength(input);
155155
ObjectMetadata metadata = new ObjectMetadata();
156156
metadata.setContentLength(streamInfo.contentLength);
157-
if (externalFileName.endsWith(".tmp")) {
158-
metadata.setContentType("image/jpeg");
159-
}
157+
metadata.setContentType((externalFileName.endsWith(".tmp") && "application/octet-stream".equals(streamInfo.detectedContentType)) ? "image/jpeg" : streamInfo.detectedContentType);
160158
String upload = "";
161159
client.putObject(new PutObjectRequest(bucket, externalFileName, streamInfo.inputStream, metadata).withCannedAcl(internalToAWSACL(acl)));
162160
upload = getResourceUrl(externalFileName, acl, defaultExpirationMinutes);

java/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,6 @@
5959
<artifactId>javax.jms</artifactId>
6060
<version>3.1.2.2</version>
6161
</dependency>
62-
<dependency>
63-
<groupId>commons-io</groupId>
64-
<artifactId>commons-io</artifactId>
65-
<version>2.11.0</version>
66-
</dependency>
6762
<dependency>
6863
<groupId>commons-net</groupId>
6964
<artifactId>commons-net</artifactId>

wrappercommon/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@
2929
<artifactId>log4j-core</artifactId>
3030
<version>${log4j.version}</version>
3131
</dependency>
32-
<dependency>
33-
<groupId>commons-io</groupId>
34-
<artifactId>commons-io</artifactId>
35-
<version>2.11.0</version>
36-
</dependency>
3732
<dependency>
3833
<groupId>org.apache.ws.security</groupId>
3934
<artifactId>wss4j</artifactId>

0 commit comments

Comments
 (0)