diff --git a/src/main/java/de/mediathekview/mlib/tool/FileSizeDeterminer.java b/src/main/java/de/mediathekview/mlib/tool/FileSizeDeterminer.java index 50ebbdbf..28743906 100644 --- a/src/main/java/de/mediathekview/mlib/tool/FileSizeDeterminer.java +++ b/src/main/java/de/mediathekview/mlib/tool/FileSizeDeterminer.java @@ -8,97 +8,42 @@ import org.jetbrains.annotations.NotNull; import java.io.IOException; -import java.util.Optional; import static jakarta.ws.rs.core.HttpHeaders.CONTENT_LENGTH; import static jakarta.ws.rs.core.HttpHeaders.CONTENT_TYPE; public class FileSizeDeterminer { private static final Logger LOG = LogManager.getLogger(FileSizeDeterminer.class); - private static final int BYTE_TO_MIB = 1024; - private static final int BYTE_TO_MB = 1000; - private static final String PROTOCOL_RTMP = "rtmp"; - private static final String FILE_TYPE_M3U8 = "m3u8"; - private final String url; private final OkHttpClient client; // - private Optional fileSizeInByte = Optional.empty(); - private Optional responsePath = Optional.empty(); - private Optional responseContentType = Optional.empty(); - - - /* - * get the file size of the url in byte - */ - public long getFileSizeInByte() { - if (fileSizeInByte.isEmpty()) { - getFileSizeForBuilder(); - } - return fileSizeInByte.orElse(-1L); - } - - /* - * get the path of the response which may differ to request url for redirects - */ - public String getResponsePath() { - if (responsePath.isEmpty()) { - getFileSizeInByte(); - } - return responsePath.orElse(""); - } - - /* - * get the content type of the reponse message - */ - - public String getResponseContentType() { - if (responseContentType.isEmpty()) { - getFileSizeInByte(); - } - return responseContentType.orElse(""); - } - - + - /** - * Builds the determiner with the default read- and connect timeout of 30 seconds. - * - * @param aUrl The url of the file. - */ - public FileSizeDeterminer(final String aUrl) { - this(aUrl, 30L, 30L); + public FileSizeDeterminer() { + this(30L,30L); } - /** - * @param aUrl The url of the file. - * @param connectTimeoutInSeconds The connection timeout in seconds. - * @param readTimeoutInSeconds The read timeout in seconds. - */ public FileSizeDeterminer( - final String aUrl, final long connectTimeoutInSeconds, final long readTimeoutInSeconds) { - url = aUrl; + final long connectTimeoutInSeconds, final long readTimeoutInSeconds) { client = new OkHttpClientBuilder() .withConnectTimeout(connectTimeoutInSeconds) .withReadTimeout(readTimeoutInSeconds) .build(); } - - /** @return The file size in bytes. */ - public Long getFileSizeForBuilder() { - return getFileSizeByRequest(RequestType.HEAD); + public RespoonseInfo getRequestInfo(final String url) { + return getRequestInfo(url, RequestType.HEAD); } - private Long getFileSizeByRequest(final RequestType requestType) { - // Cant determine the file size of rtmp and m3u8. - if (!url.startsWith(PROTOCOL_RTMP) && !url.endsWith(FILE_TYPE_M3U8)) { + public RespoonseInfo getRequestInfo(final String url, final RequestType requestType) { try (final Response response = - client.newCall(createRequestBuilderForRequestType(requestType).build()).execute()) { - final String contentLengthHeader = response.header(CONTENT_LENGTH); - responseContentType = Optional.of(response.header(CONTENT_TYPE, "")); - responsePath = Optional.of(response.request().url().encodedPath()); - fileSizeInByte = Optional.of(parseContentLength(contentLengthHeader)); - return fileSizeInByte.get(); + client.newCall(createRequestBuilderForRequestType(url, requestType).build()).execute()) { + RespoonseInfo respoonseInfo = new RespoonseInfo( + parseContentLength(response.header(CONTENT_LENGTH)), + response.code(), + response.header(CONTENT_TYPE, ""), + response.request().url().encodedPath() + ); + return respoonseInfo; } catch (final IOException ioException) { LOG.error( "Something went wrong determining the file size of \"{}\" with {} request.", @@ -106,15 +51,14 @@ private Long getFileSizeByRequest(final RequestType requestType) { requestType); if (requestType.equals(RequestType.HEAD)) { LOG.info("Retrying the file size determination with GET request."); - return getFileSizeByRequest(RequestType.GET); + return getRequestInfo(url, RequestType.GET); } } - } - return -1L; + return null; } @NotNull - private Request.Builder createRequestBuilderForRequestType(final RequestType requestType) { + private Request.Builder createRequestBuilderForRequestType(final String url, final RequestType requestType) { final Request.Builder requestBuilder; switch (requestType) { case GET: @@ -129,11 +73,11 @@ private Request.Builder createRequestBuilderForRequestType(final RequestType req return requestBuilder; } - /** @return The file size in MiB. */ - public Long getFileSizeInMiB() { - return getFileSizeForBuilder() / BYTE_TO_MIB; + private enum RequestType { + GET, + HEAD } - + @NotNull private Long parseContentLength(final String contentLengthHeader) { try { @@ -146,14 +90,33 @@ private Long parseContentLength(final String contentLengthHeader) { return -1L; } } - - /** @return The file size in MB. */ - public Long getFileSizeInMB() { - return getFileSizeForBuilder() / BYTE_TO_MB; + + public class RespoonseInfo { + private Long size; + private int code; + private String contentType; + private String path; + + public RespoonseInfo(Long size, int code, String contentType, String path) { + super(); + this.size = size; + this.code = code; + this.contentType = contentType; + this.path = path; + } + + public Long getSize() { + return size; + } + public int getCode() { + return code; + } + public String getContentType() { + return contentType; + } + public String getPath() { + return path; + } } - private enum RequestType { - GET, - HEAD - } } diff --git a/src/test/java/de/mediathekview/mlib/tool/FileSizeDeterminerTest.java b/src/test/java/de/mediathekview/mlib/tool/FileSizeDeterminerTest.java index 4c1d50f7..62fae6ff 100644 --- a/src/test/java/de/mediathekview/mlib/tool/FileSizeDeterminerTest.java +++ b/src/test/java/de/mediathekview/mlib/tool/FileSizeDeterminerTest.java @@ -9,6 +9,7 @@ import static com.github.tomakehurst.wiremock.client.WireMock.*; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; import static jakarta.ws.rs.core.HttpHeaders.CONTENT_LENGTH; +import static jakarta.ws.rs.core.HttpHeaders.CONTENT_TYPE; import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; class FileSizeDeterminerTest { @@ -21,7 +22,7 @@ public static void setUpWiremock() { wireMockServer.stubFor( head(urlEqualTo("/" + TEST_FILE_NAME)) .willReturn( - aResponse().withStatus(200).withHeader(CONTENT_LENGTH, "5643"))); + aResponse().withStatus(200).withHeader(CONTENT_LENGTH, "5643").withHeader(CONTENT_TYPE, "text/html"))); } @BeforeEach @@ -36,20 +37,20 @@ public void stopWireMock() { @Test void testGetFileSize() { - assertThat(getClassUnderTest().getFileSizeForBuilder()).isEqualTo(5643L); + assertThat(getClassUnderTest().getRequestInfo(wireMockServer.baseUrl() + TEST_FILE_URL).getSize()).isEqualTo(5643L); } - + @Test - void testGetFileSizeMiB() { - assertThat(getClassUnderTest().getFileSizeInMiB()).isEqualTo(5L); + void testGetStatusCode() { + assertThat(getClassUnderTest().getRequestInfo(wireMockServer.baseUrl() + TEST_FILE_URL).getCode()).isEqualTo(200); } - + @Test - void testGetFileSizeMB() { - assertThat(getClassUnderTest().getFileSizeInMB()).isEqualTo(5L); + void testGetContentType() { + assertThat(getClassUnderTest().getRequestInfo(wireMockServer.baseUrl() + TEST_FILE_URL).getContentType()).isEqualTo("text/html"); } private FileSizeDeterminer getClassUnderTest() { - return new FileSizeDeterminer(wireMockServer.baseUrl() + TEST_FILE_URL); + return new FileSizeDeterminer(); } }