mirror of
https://github.com/amir1376/ab-download-manager.git
synced 2025-02-20 11:43:24 +08:00
improve name suggestion from response info
This commit is contained in:
parent
c8067e3881
commit
9c459213ba
@ -4,6 +4,7 @@ import ir.amirab.downloader.connection.DownloaderClient
|
|||||||
import ir.amirab.downloader.connection.response.ResponseInfo
|
import ir.amirab.downloader.connection.response.ResponseInfo
|
||||||
import ir.amirab.downloader.downloaditem.DownloadCredentials
|
import ir.amirab.downloader.downloaditem.DownloadCredentials
|
||||||
import com.abdownloadmanager.utils.isValidUrl
|
import com.abdownloadmanager.utils.isValidUrl
|
||||||
|
import ir.amirab.util.UrlUtils
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.asStateFlow
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
import kotlinx.coroutines.flow.update
|
import kotlinx.coroutines.flow.update
|
||||||
|
@ -26,17 +26,17 @@ class OkHttpDownloaderClient(
|
|||||||
?.filter {
|
?.filter {
|
||||||
//OkHttp handles this header and if we override it,
|
//OkHttp handles this header and if we override it,
|
||||||
//makes redirected links to have this "Host" instead of their own!, and cause error
|
//makes redirected links to have this "Host" instead of their own!, and cause error
|
||||||
!it.key.equals("Host",true)
|
!it.key.equals("Host", true)
|
||||||
}
|
}
|
||||||
?.forEach { (k, v) ->
|
?.forEach { (k, v) ->
|
||||||
header(k, v)
|
header(k, v)
|
||||||
}
|
}
|
||||||
val username = downloadCredentials.username
|
val username = downloadCredentials.username
|
||||||
val password = downloadCredentials.password
|
val password = downloadCredentials.password
|
||||||
if (username?.isNotBlank() == true && password?.isNotBlank() == true) {
|
if (username?.isNotBlank() == true && password?.isNotBlank() == true) {
|
||||||
header("Authorization", Credentials.basic(username, password))
|
header("Authorization", Credentials.basic(username, password))
|
||||||
}
|
}
|
||||||
downloadCredentials.userAgent?.let { userAgent->
|
downloadCredentials.userAgent?.let { userAgent ->
|
||||||
header("User-Agent", userAgent)
|
header("User-Agent", userAgent)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -65,6 +65,7 @@ class OkHttpDownloaderClient(
|
|||||||
return ResponseInfo(
|
return ResponseInfo(
|
||||||
statusCode = response.code,
|
statusCode = response.code,
|
||||||
message = response.message,
|
message = response.message,
|
||||||
|
requestUrl = response.request.url.toString(),
|
||||||
requestHeaders = response.request.headers.associate { (key, value) ->
|
requestHeaders = response.request.headers.associate { (key, value) ->
|
||||||
key.lowercase() to value
|
key.lowercase() to value
|
||||||
},
|
},
|
||||||
@ -88,7 +89,7 @@ class OkHttpDownloaderClient(
|
|||||||
}
|
}
|
||||||
).await()
|
).await()
|
||||||
val body = runCatching {
|
val body = runCatching {
|
||||||
requireNotNull(response.body){
|
requireNotNull(response.body) {
|
||||||
"body is null"
|
"body is null"
|
||||||
}
|
}
|
||||||
}.onFailure {
|
}.onFailure {
|
||||||
|
@ -3,10 +3,13 @@ package ir.amirab.downloader.connection.response
|
|||||||
import ir.amirab.downloader.connection.response.headers.getContentRange
|
import ir.amirab.downloader.connection.response.headers.getContentRange
|
||||||
import ir.amirab.downloader.connection.response.headers.extractFileNameFromContentDisposition
|
import ir.amirab.downloader.connection.response.headers.extractFileNameFromContentDisposition
|
||||||
import ir.amirab.downloader.exception.UnSuccessfulResponseException
|
import ir.amirab.downloader.exception.UnSuccessfulResponseException
|
||||||
|
import ir.amirab.downloader.utils.FileNameUtil
|
||||||
|
import ir.amirab.util.UrlUtils
|
||||||
|
|
||||||
data class ResponseInfo(
|
data class ResponseInfo(
|
||||||
val statusCode: Int,
|
val statusCode: Int,
|
||||||
val message: String,
|
val message: String,
|
||||||
|
val requestUrl: String,
|
||||||
val requestHeaders: Map<String, String> = linkedMapOf(),
|
val requestHeaders: Map<String, String> = linkedMapOf(),
|
||||||
val responseHeaders: Map<String, String> = linkedMapOf(),
|
val responseHeaders: Map<String, String> = linkedMapOf(),
|
||||||
) {
|
) {
|
||||||
@ -37,9 +40,24 @@ data class ResponseInfo(
|
|||||||
statusCode == 206
|
statusCode == 206
|
||||||
}
|
}
|
||||||
val fileName: String? by lazy {
|
val fileName: String? by lazy {
|
||||||
responseHeaders["content-disposition"]?.let {
|
val foundName = run {
|
||||||
extractFileNameFromContentDisposition(it)
|
val nameFromHeader = responseHeaders["content-disposition"]?.let {
|
||||||
|
extractFileNameFromContentDisposition(it)
|
||||||
|
}
|
||||||
|
if (nameFromHeader!=null){
|
||||||
|
return@lazy nameFromHeader
|
||||||
|
}
|
||||||
|
UrlUtils.extractNameFromLink(requestUrl).orEmpty()
|
||||||
}
|
}
|
||||||
|
var valueToReturn = foundName
|
||||||
|
if (isWebPage()){
|
||||||
|
valueToReturn = FileNameUtil.replaceExtension(
|
||||||
|
valueToReturn,
|
||||||
|
"html",
|
||||||
|
true
|
||||||
|
)
|
||||||
|
}
|
||||||
|
valueToReturn
|
||||||
}
|
}
|
||||||
|
|
||||||
// It is good to use these properties to check file is valid
|
// It is good to use these properties to check file is valid
|
||||||
@ -52,6 +70,10 @@ data class ResponseInfo(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun ResponseInfo.isWebPage(): Boolean {
|
||||||
|
return responseHeaders["content-type"].orEmpty().contains("text/html")
|
||||||
|
}
|
||||||
|
|
||||||
fun ResponseInfo.expectSuccess() = apply {
|
fun ResponseInfo.expectSuccess() = apply {
|
||||||
if (!isSuccessFul) {
|
if (!isSuccessFul) {
|
||||||
throw UnSuccessfulResponseException(statusCode, message)
|
throw UnSuccessfulResponseException(statusCode, message)
|
||||||
|
@ -4,6 +4,7 @@ import kotlinx.coroutines.currentCoroutineContext
|
|||||||
import kotlinx.coroutines.flow.*
|
import kotlinx.coroutines.flow.*
|
||||||
import kotlinx.coroutines.isActive
|
import kotlinx.coroutines.isActive
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
import java.security.cert.Extension
|
||||||
|
|
||||||
object FileNameUtil {
|
object FileNameUtil {
|
||||||
private fun getExtensionOrNull(name: String): String? {
|
private fun getExtensionOrNull(name: String): String? {
|
||||||
@ -33,4 +34,14 @@ object FileNameUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun replaceExtension(filename: String, newExtension: String, appendIfNotExists: Boolean=true): String {
|
||||||
|
val ext = getExtensionOrNull(filename) ?: if (appendIfNotExists) {
|
||||||
|
return "$filename.$newExtension"
|
||||||
|
} else {
|
||||||
|
return filename
|
||||||
|
}
|
||||||
|
val filenameWithoutExtension = filename.substring(0, filename.length - ext.length)
|
||||||
|
return "$filenameWithoutExtension$newExtension"
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package com.abdownloadmanager.desktop.utils
|
package ir.amirab.util
|
||||||
|
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
import java.net.URLDecoder
|
import java.net.URLDecoder
|
Loading…
x
Reference in New Issue
Block a user