Skip to content

Commit

Permalink
feat: allow nullable content-type (#966)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ryzizub authored Jul 25, 2024
1 parent fe07ede commit a1cc3e5
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
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

0 comments on commit a1cc3e5

Please sign in to comment.