Skip to content

Commit

Permalink
Remove full file read before guessing content-type, add exception on …
Browse files Browse the repository at this point in the history
…class instantiation.
  • Loading branch information
HardNorth committed Jan 11, 2024
1 parent f6b6804 commit fb4cdf9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
18 changes: 7 additions & 11 deletions src/main/java/com/epam/reportportal/utils/MimeTypeDetector.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,34 @@
*/
package com.epam.reportportal.utils;

import com.epam.reportportal.utils.files.Utils;
import com.google.common.io.ByteSource;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
* Utility stuff to detect mime type of binary data
*
* @author Andrei Varabyeu
*/
public class MimeTypeDetector {
private static final String UNKNOWN_TYPE = "application/octet-stream";
private static final String EXTENSION_DELIMITER = ".";

private static final Map<String, String> ADDITIONAL_EXTENSION_MAPPING = new HashMap<String, String>() {{
put(".properties", "text/plain");
}};
private static final Map<String, String> ADDITIONAL_EXTENSION_MAPPING =
Collections.unmodifiableMap(new HashMap<String, String>() {{
put(".properties", "text/plain");
}});

private MimeTypeDetector() {
//statics only
throw new IllegalStateException("Static only class. No instances should exist for the class!");
}

private static String detectByExtensionInternal(String name) {
Expand All @@ -57,9 +55,7 @@ private static String detectByExtensionInternal(String name) {

@Nonnull
public static String detect(@Nonnull final File file) throws IOException {
String type = URLConnection
.guessContentTypeFromStream(new ByteArrayInputStream(Utils.readInputStreamToBytes(new FileInputStream(
file))));
String type = URLConnection.guessContentTypeFromStream(new FileInputStream(file));
if (type == null) {
type = Files.probeContentType(file.toPath());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class HttpRequestUtils {
private static final String DEFAULT_TYPE = "application/octet-stream";

private HttpRequestUtils() {
throw new IllegalStateException("Static only class");
throw new IllegalStateException("Static only class. No instances should exist for the class!");
}

public static List<MultipartBody.Part> buildLogMultiPartRequest(List<SaveLogRQ> rqs) {
Expand Down

0 comments on commit fb4cdf9

Please sign in to comment.