- // Define the strings.
- var formattedFileSize: String
- var fileNameString: String
-
- // Populate the file size and name strings.
- if (urlString.startsWith("data:")) { // The URL contains the entire data of an image.
- // Remove `data:` from the beginning of the URL.
- val urlWithoutData = urlString.substring(5)
-
- // Get the URL MIME type, which ends with a `;`.
- val urlMimeType = urlWithoutData.substring(0, urlWithoutData.indexOf(";"))
-
- // Get the Base64 data, which begins after a `,`.
- val base64DataString = urlWithoutData.substring(urlWithoutData.indexOf(",") + 1)
-
- // Calculate the file size of the data URL. Each Base64 character represents 6 bits.
- formattedFileSize = NumberFormat.getInstance().format(base64DataString.length * 3L / 4) + " " + context.getString(R.string.bytes)
-
- // Set the file name according to the MIME type.
- fileNameString = context.getString(R.string.file) + "." + MimeTypeMap.getSingleton().getExtensionFromMimeType(urlMimeType)
- } else { // The URL refers to the location of the data.
- // Initialize the formatted file size string.
- formattedFileSize = context.getString(R.string.unknown_size)
-
- // Because everything relating to requesting data from a webserver can throw errors, the entire section must catch exceptions.
- try {
- // Convert the URL string to a URL.
- val url = URL(urlString)
-
- // Instantiate the proxy helper.
- val proxyHelper = ProxyHelper()
-
- // Get the current proxy.
- val proxy = proxyHelper.getCurrentProxy(context)
-
- // Open a connection to the URL. No data is actually sent at this point.
- val httpUrlConnection = url.openConnection(proxy) as HttpURLConnection
-
- // Add the user agent to the header property.
- httpUrlConnection.setRequestProperty("User-Agent", userAgent)
-
- // Add the cookies if they are enabled.
- if (cookiesEnabled) {
- // Get the cookies for the current domain.
- val cookiesString = CookieManager.getInstance().getCookie(url.toString())
-
- // Add the cookies if they are not null.
- if (cookiesString != null)
- httpUrlConnection.setRequestProperty("Cookie", cookiesString)
- }
-
- // The actual network request is in a `try` bracket so that `disconnect()` is run in the `finally` section even if an error is encountered in the main block.
- try {
- // Get the status code. This initiates a network connection.
- val responseCode = httpUrlConnection.responseCode
-
- // Check the response code.
- if (responseCode >= 400) { // The response code is an error message.
- // Set the formatted file size to indicate a bad URL.
- formattedFileSize = context.getString(R.string.invalid_url)
-
- // Set the file name according to the URL.
- fileNameString = getFileNameFromUrl(context, urlString, null)
- } else { // The response code is not an error message.
- // Get the headers.
- val contentLengthString = httpUrlConnection.getHeaderField("Content-Length")
- val contentDispositionString = httpUrlConnection.getHeaderField("Content-Disposition")
- var contentTypeString = httpUrlConnection.contentType
-
- // Remove anything after the MIME type in the content type string.
- if (contentTypeString.contains(";"))
- contentTypeString = contentTypeString.substring(0, contentTypeString.indexOf(";"))
-
- // Only process the content length string if it isn't null.
- if (contentLengthString != null) {
- // Convert the content length string to a long.
- val fileSize = contentLengthString.toLong()
-
- // Format the file size.
- formattedFileSize = NumberFormat.getInstance().format(fileSize) + " " + context.getString(R.string.bytes)
- }
-
- // Get the file name string from the content disposition.
- fileNameString = getFileNameFromHeaders(context, contentDispositionString, contentTypeString, urlString)
- }
- } finally {
- // Disconnect the HTTP URL connection.
- httpUrlConnection.disconnect()
- }
- } catch (exception: Exception) {
- // Set the formatted file size to indicate a bad URL.
- formattedFileSize = context.getString(R.string.invalid_url)
-
- // Set the file name according to the URL.
- fileNameString = getFileNameFromUrl(context, urlString, null)
- }
- }