Skip to content
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

feat: allow nullable content-type #966

Merged
merged 7 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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 @@ -9,7 +9,7 @@ data class DownloadTask(
var filename: String?,
var savedDir: String,
var headers: String,
var mimeType: String,
var mimeType: String?,
var resumable: Boolean,
var showNotification: Boolean,
var openFileFromNotification: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,14 @@ class DownloadWorker(context: Context, params: WorkerParameters) :
break
}
httpConn!!.connect()
val contentType: String
val contentType: String?
if ((responseCode == HttpURLConnection.HTTP_OK || isResume && responseCode == HttpURLConnection.HTTP_PARTIAL) && !isStopped) {
contentType = httpConn.contentType
val contentLength: Long =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) httpConn.contentLengthLong else httpConn.contentLength.toLong()
log("Content-Type = $contentType")
if (contentType != null){
log("Content-Type = $contentType")
}
log("Content-Length = $contentLength")
val charset = getCharsetFromContentType(contentType)
log("Charset = $charset")
Expand Down Expand Up @@ -517,7 +519,7 @@ class DownloadWorker(context: Context, params: WorkerParameters) :
* Create a file inside the Download folder using MediaStore API
*/
@RequiresApi(Build.VERSION_CODES.Q)
private fun createFileInPublicDownloadsDir(filename: String?, mimeType: String): Uri? {
private fun createFileInPublicDownloadsDir(filename: String?, mimeType: String?): Uri? {
val collection: Uri = MediaStore.Downloads.EXTERNAL_CONTENT_URI
val values = ContentValues()
values.put(MediaStore.Downloads.DISPLAY_NAME, filename)
Expand Down Expand Up @@ -770,7 +772,7 @@ class DownloadWorker(context: Context, params: WorkerParameters) :
return contentType?.split(";")?.toTypedArray()?.get(0)?.trim { it <= ' ' }
}

private fun isImageOrVideoFile(contentType: String): Boolean {
private fun isImageOrVideoFile(contentType: String?): Boolean {
val newContentType = getContentTypeWithoutCharset(contentType)
return newContentType != null && (newContentType.startsWith("image/") || newContentType.startsWith("video"))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ class TaskDao(private val dbHelper: TaskDbHelper) {
val db = dbHelper.writableDatabase
val values = ContentValues()
values.put(TaskEntry.COLUMN_NAME_FILE_NAME, filename)
values.put(TaskEntry.COLUMN_NAME_MIME_TYPE, mimeType)
values.put(TaskEntry.COLUMN_NAME_MIME_TYPE, mimeType ?: "unknown")
db.beginTransaction()
try {
db.update(
Expand Down
Loading