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 (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaloyan-raev authored Apr 3, 2018
1 parent afabc67 commit 513c5d5
Show file tree
Hide file tree
Showing 2 changed files with 43 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
31 changes: 31 additions & 0 deletions src/test/java/io/storj/libstorj/FileTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (C) 2018 Kaloyan Raev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.storj.libstorj;

import org.junit.Assert;
import org.junit.Test;

public class FileTest {

@Test
public void testMimeType() {
File file = new File("file-id", "bucket-id", "A#BC.pdf", null, true, 1, null, null,
null, null);
Assert.assertEquals("application/pdf", file.getMimeType());
}

}

0 comments on commit 513c5d5

Please sign in to comment.