@@ -148,7 +148,8 @@ class DownloadWorker(context: Context, params: WorkerParameters) :
148
148
dbHelper = TaskDbHelper .getInstance(applicationContext)
149
149
taskDao = TaskDao (dbHelper!! )
150
150
val url: String =
151
- inputData.getString(ARG_URL ) ? : throw IllegalArgumentException (" Argument '$ARG_URL ' should not be null" )
151
+ inputData.getString(ARG_URL )
152
+ ? : throw IllegalArgumentException (" Argument '$ARG_URL ' should not be null" )
152
153
val filename: String? =
153
154
inputData.getString(ARG_FILE_NAME ) // ?: throw IllegalArgumentException("Argument '$ARG_FILE_NAME' should not be null")
154
155
val savedDir: String = inputData.getString(ARG_SAVED_DIR )
@@ -170,9 +171,9 @@ class DownloadWorker(context: Context, params: WorkerParameters) :
170
171
val task = taskDao?.loadTask(id.toString())
171
172
log(
172
173
" DownloadWorker{url=$url ,filename=$filename ,savedDir=$savedDir ,header=$headers ,isResume=$isResume ,status=" + (
173
- task?.status
174
- ? : " GONE"
175
- )
174
+ task?.status
175
+ ? : " GONE"
176
+ )
176
177
)
177
178
178
179
// Task has been deleted or cancelled
@@ -209,7 +210,14 @@ class DownloadWorker(context: Context, params: WorkerParameters) :
209
210
taskDao = null
210
211
Result .success()
211
212
} catch (e: Exception ) {
212
- updateNotification(applicationContext, filename ? : url, DownloadStatus .FAILED , - 1 , null , true )
213
+ updateNotification(
214
+ applicationContext,
215
+ filename ? : url,
216
+ DownloadStatus .FAILED ,
217
+ - 1 ,
218
+ null ,
219
+ true
220
+ )
213
221
taskDao?.updateTask(id.toString(), DownloadStatus .FAILED , lastProgress)
214
222
e.printStackTrace()
215
223
dbHelper = null
@@ -306,14 +314,16 @@ class DownloadWorker(context: Context, params: WorkerParameters) :
306
314
log(" Open connection to $url " )
307
315
httpConn.connectTimeout = actualTimeout
308
316
httpConn.readTimeout = actualTimeout
309
- httpConn.instanceFollowRedirects = false // Make the logic below easier to detect redirections
317
+ httpConn.instanceFollowRedirects =
318
+ false // Make the logic below easier to detect redirections
310
319
httpConn.setRequestProperty(" User-Agent" , " Mozilla/5.0..." )
311
320
312
321
// setup request headers if it is set
313
322
setupHeaders(httpConn, headers)
314
323
// try to continue downloading a file from its partial downloaded data.
315
324
if (isResume) {
316
- downloadedBytes = setupPartialDownloadedDataHeader(httpConn, actualFilename, savedDir)
325
+ downloadedBytes =
326
+ setupPartialDownloadedDataHeader(httpConn, actualFilename, savedDir)
317
327
}
318
328
responseCode = httpConn.responseCode
319
329
when (responseCode) {
@@ -469,7 +479,14 @@ class DownloadWorker(context: Context, params: WorkerParameters) :
469
479
}
470
480
} catch (e: IOException ) {
471
481
taskDao!! .updateTask(id.toString(), DownloadStatus .FAILED , lastProgress)
472
- updateNotification(context, actualFilename ? : fileURL, DownloadStatus .FAILED , - 1 , null , true )
482
+ updateNotification(
483
+ context,
484
+ actualFilename ? : fileURL,
485
+ DownloadStatus .FAILED ,
486
+ - 1 ,
487
+ null ,
488
+ true
489
+ )
473
490
e.printStackTrace()
474
491
} finally {
475
492
if (outputStream != null ) {
@@ -497,17 +514,33 @@ class DownloadWorker(context: Context, params: WorkerParameters) :
497
514
private fun createFileInAppSpecificDir (filename : String , savedDir : String ): File ? {
498
515
val newFile = File (savedDir, filename)
499
516
try {
500
- val rs: Boolean = newFile.createNewFile()
501
- if (rs) {
502
- return newFile
503
- } else {
504
- logError(" It looks like you are trying to save file in public storage but not setting 'saveInPublicStorage' to 'true'" )
517
+ if (newFile.exists()) {
518
+ val deleted = newFile.delete()
519
+ if (! deleted) {
520
+ logError(" Unable to delete existing file: ${newFile.absolutePath} " )
521
+ return null
522
+ }
505
523
}
524
+
525
+ val created: Boolean = newFile.createNewFile()
526
+ if (! created) {
527
+ logError(
528
+ """
529
+ Unable to create new file: ${newFile.absolutePath} .
530
+ Are are trying to save file in public storage
531
+ but not setting 'saveInPublicStorage' to 'true'?
532
+ """ .trimIndent()
533
+ )
534
+ return null
535
+ }
536
+
537
+ return newFile
538
+
506
539
} catch (e: IOException ) {
507
540
e.printStackTrace()
508
541
logError(" Create a file using java.io API failed " )
542
+ return null
509
543
}
510
- return null
511
544
}
512
545
513
546
/* *
@@ -602,15 +635,18 @@ class DownloadWorker(context: Context, params: WorkerParameters) :
602
635
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
603
636
// Create the NotificationChannel
604
637
val res = applicationContext.resources
605
- val channelName: String = res.getString(R .string.flutter_downloader_notification_channel_name)
606
- val channelDescription: String = res.getString(R .string.flutter_downloader_notification_channel_description)
638
+ val channelName: String =
639
+ res.getString(R .string.flutter_downloader_notification_channel_name)
640
+ val channelDescription: String =
641
+ res.getString(R .string.flutter_downloader_notification_channel_description)
607
642
val importance: Int = NotificationManager .IMPORTANCE_LOW
608
643
val channel = NotificationChannel (CHANNEL_ID , channelName, importance)
609
644
channel.description = channelDescription
610
645
channel.setSound(null , null )
611
646
612
647
// Add the channel
613
- val notificationManager: NotificationManagerCompat = NotificationManagerCompat .from(context)
648
+ val notificationManager: NotificationManagerCompat =
649
+ NotificationManagerCompat .from(context)
614
650
notificationManager.createNotificationChannel(channel)
615
651
}
616
652
}
@@ -769,7 +805,9 @@ class DownloadWorker(context: Context, params: WorkerParameters) :
769
805
770
806
private fun isImageOrVideoFile (contentType : String ): Boolean {
771
807
val newContentType = getContentTypeWithoutCharset(contentType)
772
- return newContentType != null && (newContentType.startsWith(" image/" ) || newContentType.startsWith(" video" ))
808
+ return newContentType != null && (newContentType.startsWith(" image/" ) || newContentType.startsWith(
809
+ " video"
810
+ ))
773
811
}
774
812
775
813
private fun isExternalStoragePath (filePath : String? ): Boolean {
0 commit comments