Skip to content

Draft: probe content type of files using different strategy (i.e. inspect first bytes) #18949

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1643,21 +1643,25 @@ public class ApiClient {
* @return The guessed Content-Type
*/
public String guessContentTypeFromFile(File file) {
String contentType = URLConnection.guessContentTypeFromName(file.getName());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the PR

can you please add some tests in https://github.com/OpenAPITools/openapi-generator/blob/5a18e9897bd8b2b4a5013a41bccaf93710dd3883/samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/ApiClientTest.java to test guessContentTypeFromFile with different file types to confirm the enhancement is good?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added them, but it seems that the default implementation for FileTypeDetector relies on file extension (commented test).

I'm not very knowledgeable on java and the description on how to change the implementation, so I'd need some help from someone who is in order to change the implementation to accomodate the commented test, but I'd say that, as long as we use this API, users can at least do it.

if (contentType == null) {
try {
String contentType = Files.probeContentType(file.toPath());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shall we use URLConnection.guessContentTypeFromStream instead as suggested in https://stackoverflow.com/a/19712111/677735?

if (contentType == null) {
return "application/octet-stream";
} else {
return contentType;
}
} catch(IOException error) {
return "application/octet-stream";
} else {
return contentType;
}
}

/**
* Add a Content-Disposition Header for the given key and file to the MultipartBody Builder.
*
* @param mpBuilder MultipartBody.Builder
* @param mpBuilder MultipartBody.Builder
* @param key The key of the Header element
* @param file The file to add to the Header
*/
*/
private void addPartToMultiPartBuilder(MultipartBody.Builder mpBuilder, String key, File file) {
Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + key + "\"; filename=\"" + file.getName() + "\"");
MediaType mediaType = MediaType.parse(guessContentTypeFromFile(file));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1383,21 +1383,25 @@ public RequestBody buildRequestBodyMultipart(Map<String, Object> formParams) {
* @return The guessed Content-Type
*/
public String guessContentTypeFromFile(File file) {
String contentType = URLConnection.guessContentTypeFromName(file.getName());
if (contentType == null) {
try {
String contentType = Files.probeContentType(file.toPath());
if (contentType == null) {
return "application/octet-stream";
} else {
return contentType;
}
} catch(IOException error) {
return "application/octet-stream";
} else {
return contentType;
}
}

/**
* Add a Content-Disposition Header for the given key and file to the MultipartBody Builder.
*
* @param mpBuilder MultipartBody.Builder
* @param mpBuilder MultipartBody.Builder
* @param key The key of the Header element
* @param file The file to add to the Header
*/
*/
private void addPartToMultiPartBuilder(MultipartBody.Builder mpBuilder, String key, File file) {
Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + key + "\"; filename=\"" + file.getName() + "\"");
MediaType mediaType = MediaType.parse(guessContentTypeFromFile(file));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1406,21 +1406,25 @@ public RequestBody buildRequestBodyMultipart(Map<String, Object> formParams) {
* @return The guessed Content-Type
*/
public String guessContentTypeFromFile(File file) {
String contentType = URLConnection.guessContentTypeFromName(file.getName());
if (contentType == null) {
try {
String contentType = Files.probeContentType(file.toPath());
if (contentType == null) {
return "application/octet-stream";
} else {
return contentType;
}
} catch(IOException error) {
return "application/octet-stream";
} else {
return contentType;
}
}

/**
* Add a Content-Disposition Header for the given key and file to the MultipartBody Builder.
*
* @param mpBuilder MultipartBody.Builder
* @param mpBuilder MultipartBody.Builder
* @param key The key of the Header element
* @param file The file to add to the Header
*/
*/
private void addPartToMultiPartBuilder(MultipartBody.Builder mpBuilder, String key, File file) {
Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + key + "\"; filename=\"" + file.getName() + "\"");
MediaType mediaType = MediaType.parse(guessContentTypeFromFile(file));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1491,21 +1491,25 @@ public RequestBody buildRequestBodyMultipart(Map<String, Object> formParams) {
* @return The guessed Content-Type
*/
public String guessContentTypeFromFile(File file) {
String contentType = URLConnection.guessContentTypeFromName(file.getName());
if (contentType == null) {
try {
String contentType = Files.probeContentType(file.toPath());
if (contentType == null) {
return "application/octet-stream";
} else {
return contentType;
}
} catch(IOException error) {
return "application/octet-stream";
} else {
return contentType;
}
}

/**
* Add a Content-Disposition Header for the given key and file to the MultipartBody Builder.
*
* @param mpBuilder MultipartBody.Builder
* @param mpBuilder MultipartBody.Builder
* @param key The key of the Header element
* @param file The file to add to the Header
*/
*/
private void addPartToMultiPartBuilder(MultipartBody.Builder mpBuilder, String key, File file) {
Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + key + "\"; filename=\"" + file.getName() + "\"");
MediaType mediaType = MediaType.parse(guessContentTypeFromFile(file));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1484,21 +1484,25 @@ public RequestBody buildRequestBodyMultipart(Map<String, Object> formParams) {
* @return The guessed Content-Type
*/
public String guessContentTypeFromFile(File file) {
String contentType = URLConnection.guessContentTypeFromName(file.getName());
if (contentType == null) {
try {
String contentType = Files.probeContentType(file.toPath());
if (contentType == null) {
return "application/octet-stream";
} else {
return contentType;
}
} catch(IOException error) {
return "application/octet-stream";
} else {
return contentType;
}
}

/**
* Add a Content-Disposition Header for the given key and file to the MultipartBody Builder.
*
* @param mpBuilder MultipartBody.Builder
* @param mpBuilder MultipartBody.Builder
* @param key The key of the Header element
* @param file The file to add to the Header
*/
*/
private void addPartToMultiPartBuilder(MultipartBody.Builder mpBuilder, String key, File file) {
Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + key + "\"; filename=\"" + file.getName() + "\"");
MediaType mediaType = MediaType.parse(guessContentTypeFromFile(file));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1479,21 +1479,25 @@ public RequestBody buildRequestBodyMultipart(Map<String, Object> formParams) {
* @return The guessed Content-Type
*/
public String guessContentTypeFromFile(File file) {
String contentType = URLConnection.guessContentTypeFromName(file.getName());
if (contentType == null) {
try {
String contentType = Files.probeContentType(file.toPath());
if (contentType == null) {
return "application/octet-stream";
} else {
return contentType;
}
} catch(IOException error) {
return "application/octet-stream";
} else {
return contentType;
}
}

/**
* Add a Content-Disposition Header for the given key and file to the MultipartBody Builder.
*
* @param mpBuilder MultipartBody.Builder
* @param mpBuilder MultipartBody.Builder
* @param key The key of the Header element
* @param file The file to add to the Header
*/
*/
private void addPartToMultiPartBuilder(MultipartBody.Builder mpBuilder, String key, File file) {
Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + key + "\"; filename=\"" + file.getName() + "\"");
MediaType mediaType = MediaType.parse(guessContentTypeFromFile(file));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1482,21 +1482,25 @@ public RequestBody buildRequestBodyMultipart(Map<String, Object> formParams) {
* @return The guessed Content-Type
*/
public String guessContentTypeFromFile(File file) {
String contentType = URLConnection.guessContentTypeFromName(file.getName());
if (contentType == null) {
try {
String contentType = Files.probeContentType(file.toPath());
if (contentType == null) {
return "application/octet-stream";
} else {
return contentType;
}
} catch(IOException error) {
return "application/octet-stream";
} else {
return contentType;
}
}

/**
* Add a Content-Disposition Header for the given key and file to the MultipartBody Builder.
*
* @param mpBuilder MultipartBody.Builder
* @param mpBuilder MultipartBody.Builder
* @param key The key of the Header element
* @param file The file to add to the Header
*/
*/
private void addPartToMultiPartBuilder(MultipartBody.Builder mpBuilder, String key, File file) {
Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + key + "\"; filename=\"" + file.getName() + "\"");
MediaType mediaType = MediaType.parse(guessContentTypeFromFile(file));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1485,21 +1485,25 @@ public RequestBody buildRequestBodyMultipart(Map<String, Object> formParams) {
* @return The guessed Content-Type
*/
public String guessContentTypeFromFile(File file) {
String contentType = URLConnection.guessContentTypeFromName(file.getName());
if (contentType == null) {
try {
String contentType = Files.probeContentType(file.toPath());
if (contentType == null) {
return "application/octet-stream";
} else {
return contentType;
}
} catch(IOException error) {
return "application/octet-stream";
} else {
return contentType;
}
}

/**
* Add a Content-Disposition Header for the given key and file to the MultipartBody Builder.
*
* @param mpBuilder MultipartBody.Builder
* @param mpBuilder MultipartBody.Builder
* @param key The key of the Header element
* @param file The file to add to the Header
*/
*/
private void addPartToMultiPartBuilder(MultipartBody.Builder mpBuilder, String key, File file) {
Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + key + "\"; filename=\"" + file.getName() + "\"");
MediaType mediaType = MediaType.parse(guessContentTypeFromFile(file));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1479,21 +1479,25 @@ public RequestBody buildRequestBodyMultipart(Map<String, Object> formParams) {
* @return The guessed Content-Type
*/
public String guessContentTypeFromFile(File file) {
String contentType = URLConnection.guessContentTypeFromName(file.getName());
if (contentType == null) {
try {
String contentType = Files.probeContentType(file.toPath());
if (contentType == null) {
return "application/octet-stream";
} else {
return contentType;
}
} catch(IOException error) {
return "application/octet-stream";
} else {
return contentType;
}
}

/**
* Add a Content-Disposition Header for the given key and file to the MultipartBody Builder.
*
* @param mpBuilder MultipartBody.Builder
* @param mpBuilder MultipartBody.Builder
* @param key The key of the Header element
* @param file The file to add to the Header
*/
*/
private void addPartToMultiPartBuilder(MultipartBody.Builder mpBuilder, String key, File file) {
Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + key + "\"; filename=\"" + file.getName() + "\"");
MediaType mediaType = MediaType.parse(guessContentTypeFromFile(file));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1479,21 +1479,25 @@ public RequestBody buildRequestBodyMultipart(Map<String, Object> formParams) {
* @return The guessed Content-Type
*/
public String guessContentTypeFromFile(File file) {
String contentType = URLConnection.guessContentTypeFromName(file.getName());
if (contentType == null) {
try {
String contentType = Files.probeContentType(file.toPath());
if (contentType == null) {
return "application/octet-stream";
} else {
return contentType;
}
} catch(IOException error) {
return "application/octet-stream";
} else {
return contentType;
}
}

/**
* Add a Content-Disposition Header for the given key and file to the MultipartBody Builder.
*
* @param mpBuilder MultipartBody.Builder
* @param mpBuilder MultipartBody.Builder
* @param key The key of the Header element
* @param file The file to add to the Header
*/
*/
private void addPartToMultiPartBuilder(MultipartBody.Builder mpBuilder, String key, File file) {
Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + key + "\"; filename=\"" + file.getName() + "\"");
MediaType mediaType = MediaType.parse(guessContentTypeFromFile(file));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1544,21 +1544,25 @@ public RequestBody buildRequestBodyMultipart(Map<String, Object> formParams) {
* @return The guessed Content-Type
*/
public String guessContentTypeFromFile(File file) {
String contentType = URLConnection.guessContentTypeFromName(file.getName());
if (contentType == null) {
try {
String contentType = Files.probeContentType(file.toPath());
if (contentType == null) {
return "application/octet-stream";
} else {
return contentType;
}
} catch(IOException error) {
return "application/octet-stream";
} else {
return contentType;
}
}

/**
* Add a Content-Disposition Header for the given key and file to the MultipartBody Builder.
*
* @param mpBuilder MultipartBody.Builder
* @param mpBuilder MultipartBody.Builder
* @param key The key of the Header element
* @param file The file to add to the Header
*/
*/
private void addPartToMultiPartBuilder(MultipartBody.Builder mpBuilder, String key, File file) {
Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + key + "\"; filename=\"" + file.getName() + "\"");
MediaType mediaType = MediaType.parse(guessContentTypeFromFile(file));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import static org.junit.jupiter.api.Assertions.*;

import java.util.*;
import java.io.IOException;
import java.io.File;
import java.nio.file.Files;

import okhttp3.OkHttpClient;
import org.junit.jupiter.api.*;
Expand Down Expand Up @@ -81,6 +84,30 @@ public void testSelectHeaderContentType() {
assertNull(apiClient.selectHeaderContentType(contentTypes));
}

@Test
public void testguessContentTypeFromFile() throws IOException {
byte[] b = {
//ccurve.png
-119, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82,
0, 0, 0, 15, 0, 0, 0, 15, 8, 6, 0, 0, 0, 59, -42, -107,
74, 0, 0, 0, 64, 73, 68, 65, 84, 120, -38, 99, 96, -64, 14, -2,
99, -63, 68, 1, 100, -59, -1, -79, -120, 17, -44, -8, 31, -121, 28, 81,
26, -1, -29, 113, 13, 78, -51, 100, -125, -1, -108, 24, 64, 86, -24, -30,
11, 101, -6, -37, 76, -106, -97, 25, 104, 17, 96, -76, 77, 97, 20, -89,
109, -110, 114, 21, 0, -82, -127, 56, -56, 56, 76, -17, -42, 0, 0, 0,
0, 73, 69, 78, 68, -82, 66, 96, -126
};

File jpegFile = File.createTempFile("image", ".png");
jpegFile.deleteOnExit();
Files.write(jpegFile.toPath(), b);
assertEquals("image/png", apiClient.guessContentTypeFromFile(jpegFile));
// File otherFile = File.createTempFile("image", ".txt");
// otherFile.deleteOnExit();
// Files.write(otherFile.toPath(), b);
// assertEquals("image/png", apiClient.guessContentTypeFromFile(otherFile));
}

@Test
public void testGetAuthentications() {
Map<String, Authentication> auths = apiClient.getAuthentications();
Expand Down
Loading