Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Commit

Permalink
Escape file name before guessing mime type
Browse files Browse the repository at this point in the history
  • Loading branch information
kaloyan-raev committed Apr 3, 2018
1 parent 825e5c3 commit 675c416
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/main/java/io/storj/libstorj/File.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import java.io.Serializable;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

/**
Expand Down Expand Up @@ -175,12 +177,21 @@ public long getSize() {
* @return a guess of the content type based on the file name
*/
public String getMimeType() {
String mime = null;

// prefer the Java util as libstorj returns always 'application/octet-stream'
String mime = URLConnection.guessContentTypeFromName(name);
try {
String escaped = URLEncoder.encode(name, StandardCharsets.UTF_8.toString());
mime = URLConnection.guessContentTypeFromName(escaped);
} catch (Exception e) {
// mime will remain null
}

if (mime == null || mime.isEmpty()) {
// fallback to libstorj
mime = mimeType;
}

return mime;
}

Expand Down

0 comments on commit 675c416

Please sign in to comment.