Skip to content

Commit a1cc3e5

Browse files
authored
feat: allow nullable content-type (#966)
* feat: allow empty content-type on android * feat: add nullable places for content type * feat: add nullable mimeType do worker * fix: add unknown type * fix: add unknown type * chore: fix lint issues
1 parent fe07ede commit a1cc3e5

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

android/src/main/kotlin/vn/hunghd/flutterdownloader/DownloadTask.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ data class DownloadTask(
99
var filename: String?,
1010
var savedDir: String,
1111
var headers: String,
12-
var mimeType: String,
12+
var mimeType: String?,
1313
var resumable: Boolean,
1414
var showNotification: Boolean,
1515
var openFileFromNotification: Boolean,

android/src/main/kotlin/vn/hunghd/flutterdownloader/DownloadWorker.kt

+6-4
Original file line numberDiff line numberDiff line change
@@ -338,12 +338,14 @@ class DownloadWorker(context: Context, params: WorkerParameters) :
338338
break
339339
}
340340
httpConn!!.connect()
341-
val contentType: String
341+
val contentType: String?
342342
if ((responseCode == HttpURLConnection.HTTP_OK || isResume && responseCode == HttpURLConnection.HTTP_PARTIAL) && !isStopped) {
343343
contentType = httpConn.contentType
344344
val contentLength: Long =
345345
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) httpConn.contentLengthLong else httpConn.contentLength.toLong()
346-
log("Content-Type = $contentType")
346+
if (contentType != null) {
347+
log("Content-Type = $contentType")
348+
}
347349
log("Content-Length = $contentLength")
348350
val charset = getCharsetFromContentType(contentType)
349351
log("Charset = $charset")
@@ -517,7 +519,7 @@ class DownloadWorker(context: Context, params: WorkerParameters) :
517519
* Create a file inside the Download folder using MediaStore API
518520
*/
519521
@RequiresApi(Build.VERSION_CODES.Q)
520-
private fun createFileInPublicDownloadsDir(filename: String?, mimeType: String): Uri? {
522+
private fun createFileInPublicDownloadsDir(filename: String?, mimeType: String?): Uri? {
521523
val collection: Uri = MediaStore.Downloads.EXTERNAL_CONTENT_URI
522524
val values = ContentValues()
523525
values.put(MediaStore.Downloads.DISPLAY_NAME, filename)
@@ -770,7 +772,7 @@ class DownloadWorker(context: Context, params: WorkerParameters) :
770772
return contentType?.split(";")?.toTypedArray()?.get(0)?.trim { it <= ' ' }
771773
}
772774

773-
private fun isImageOrVideoFile(contentType: String): Boolean {
775+
private fun isImageOrVideoFile(contentType: String?): Boolean {
774776
val newContentType = getContentTypeWithoutCharset(contentType)
775777
return newContentType != null && (newContentType.startsWith("image/") || newContentType.startsWith("video"))
776778
}

android/src/main/kotlin/vn/hunghd/flutterdownloader/TaskDao.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ class TaskDao(private val dbHelper: TaskDbHelper) {
199199
val db = dbHelper.writableDatabase
200200
val values = ContentValues()
201201
values.put(TaskEntry.COLUMN_NAME_FILE_NAME, filename)
202-
values.put(TaskEntry.COLUMN_NAME_MIME_TYPE, mimeType)
202+
values.put(TaskEntry.COLUMN_NAME_MIME_TYPE, mimeType ?: "unknown")
203203
db.beginTransaction()
204204
try {
205205
db.update(

0 commit comments

Comments
 (0)