Skip to content

Commit 9740b91

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 88603bd commit 9740b91

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

common/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<dependency>
3333
<groupId>commons-io</groupId>
3434
<artifactId>commons-io</artifactId>
35-
<version>2.11.0</version>
35+
<version>2.12.0</version>
3636
</dependency>
3737
<dependency>
3838
<groupId>org.bouncycastle</groupId>

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

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

216216
ObjectMetadata metadata = new ObjectMetadata();
217217
metadata.setContentLength(streamInfo.contentLength);
218+
metadata.setContentType((externalFileName.endsWith(".tmp") && "application/octet-stream".equals(streamInfo.detectedContentType)) ? "image/jpeg" : streamInfo.detectedContentType);
218219

219-
if (externalFileName.endsWith(".tmp")) {
220-
metadata.setContentType("image/jpeg");
221-
}
222220
String upload = "";
223221
client.putObject(new PutObjectRequest(bucket, externalFileName, streamInfo.inputStream, metadata).withCannedAcl(internalToAWSACL(acl)));
224222
upload = getResourceUrl(externalFileName, acl, defaultExpirationMinutes);

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
}

0 commit comments

Comments
 (0)