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

Duplicate file download failed same file name #900

Open
dineshnaikb opened this issue Oct 9, 2023 · 4 comments
Open

Duplicate file download failed same file name #900

dineshnaikb opened this issue Oct 9, 2023 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@dineshnaikb
Copy link

Describe the bug

To Reproduce

Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on download file, same name
  3. Scroll down to '....'
  4. See error

Expected behavior

Screenshots

Device information:

  • Device: (Redmi,realme)
  • OS: android 13
  • plugin version 1.11.3
@dineshnaikb dineshnaikb added the bug Something isn't working label Oct 9, 2023
@salmaahhmed
Copy link
Collaborator

@Hackerdash can you post some logs ?

@salmaahhmed
Copy link
Collaborator

@Hackerdash can you use the latest version and see if your issue is fixed? otherwise will close the issue since i cannot reproduce

@salmaahhmed salmaahhmed self-assigned this Oct 20, 2023
@ggmanuilov
Copy link

I have encountered the problem of not downloading a file with the same name, if one has already been downloaded before.
Solved it like this:

  Future<String> getFilenamePath(
      Directory directory, String originalFileName) async {
    String fileName = originalFileName;
    var i = 0;
    while (true) {
      String fullPath = directory.path + Platform.pathSeparator + fileName;
      if (await File(fullPath).exists()) {
        i++;
        List splits = originalFileName.split('.');
        fileName = ["${splits[0]}.$i", splits[1]].join('.');
      } else {
        break;
      }
    }

    return fileName;
  }

Future<void> startDownload(String url, String cookies) async {
    final uri = Uri.parse(url);
    Directory directory = await getApplicationDocumentsDirectory();
    if (Platform.isAndroid) {
      // In this direcory we search already download file.
      directory = Directory('/storage/emulated/0/Download');
    }
    String filename = uri.pathSegments.last;
    final fileName = await getFilenamePath(directory, filename);

    await FlutterDownloader.enqueue(
      url: url,
      fileName: fileName,
      savedDir: directory.path,
      showNotification: true,
      openFileFromNotification: true,
      // saveInPublicStorage: false here we set up custom path to Download,
      // need for get unique name of file,
      // else download was falid.
      saveInPublicStorage: false,
    );
  }

I'll be only too happy to get rid of this ugly code, but it works.

If there is a more successful implementation, I will be glad to consider it

@dineshnaikb
Copy link
Author

dineshnaikb commented Apr 9, 2024

@ggmanuilov
@salmaahhmed
log

D/EGL_emulation( 9647): eglMakeCurrent: 0xddf1c100: ver 2 0 (tinfo 0xc3aef6f0)
D/DownloadWorker( 9647): DownloadWorker{url=https:urlxxxxxxxxx.pdf,filename=Pulp_Paper_and_Packaging_Guidelines_June_21_1685614275 (1)_1700124406_1703223729_1703331896 (1).pdf,savedDir=/data/user/0/com.xxxxxxx.xxx/app_flutter,header={},isResume=false,status=ENQUEUED
D/DownloadWorker( 9647): Update notification: {notificationId: 2, title: Pulp_Paper_and_Packaging_Guidelines_June_21_1685614275 (1)_1700124406_1703223729_1703331896 (1).pdf, status: RUNNING, progress: 0}
D/DownloadWorker( 9647): Open connection to urlxxxxxxxxx
D/DownloadWorker( 9647): Headers = {}
I/trustAllHosts( 9647): checkServerTrusted
D/DownloadWorker( 9647): Content-Type = application/octet-stream
D/DownloadWorker( 9647): Content-Length = 113741
D/DownloadWorker( 9647): Charset = null
D/DownloadWorker( 9647): fileName = Pulp_Paper_and_Packaging_Guidelines_June_21_1685614275 (1)_1700124406_1703223729_1703331896 (1).pdf
D/DownloadWorker( 9647): Update too frequently!!!!, this should be dropped
I/chatty  ( 9647): uid=10147(com.sdcampus.app) pool-14-thread- identical 8 lines
D/DownloadWorker( 9647): Update too frequently!!!!, this should be dropped
D/DownloadWorker( 9647): Update too frequently!!!!, but it is the final update, we should sleep a second to ensure the update call can be processed
D/DownloadWorker( 9647): Update notification: {notificationId: 2, title: Pulp_Paper_and_Packaging_Guidelines_June_21_1685614275 (1)_1700124406_1703223729_1703331896 (1).pdf, status: FAILED, progress: -1}
W/System.err( 9647): java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.xxxxxx.xxx/app_flutter/Pulp_Paper_and_Packaging_Guidelines_June_21_1685614275 (1)_1700124406_1703223729_1703331896 (1).pdf
W/System.err( 9647): 	at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:849)
W/System.err( 9647): 	at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:452)
W/System.err( 9647): 	at vn.hunghd.flutterdownloader.IntentUtils.buildIntent(IntentUtils.kt:19)
W/System.err( 9647): 	at vn.hunghd.flutterdownloader.IntentUtils.validatedFileIntent(IntentUtils.kt:36)
W/System.err( 9647): 	at vn.hunghd.flutterdownloader.DownloadWorker.downloadFile(DownloadWorker.kt:446)
W/System.err( 9647): 	at vn.hunghd.flutterdownloader.DownloadWorker.doWork(DownloadWorker.kt:206)
W/System.err( 9647): 	at androidx.work.Worker$1.run(Worker.java:86)
W/System.err( 9647): 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
W/System.err( 9647): 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
W/System.err( 9647): 	at java.lang.Thread.run(Thread.java:919)
I/WM-WorkerWrapper( 9647): Worker result FAILURE for Work [ id=c4cce9a1-42e9-4322-a02d-332031b477de, tags={ flutter_download_task, vn.hunghd.flutterdownloader.DownloadWorker } ]
D/EGL_emulation( 9647): eglMakeCurrent: 0xddf1c100: ver 2 0 (tinfo 0xc3aef6f0)

getApplicationDocumentsDirectory always show failed notification

image

I have encountered the problem of not downloading a file with the same name, if one has already been downloaded before. Solved it like this:

  Future<String> getFilenamePath(
      Directory directory, String originalFileName) async {
    String fileName = originalFileName;
    var i = 0;
    while (true) {
      String fullPath = directory.path + Platform.pathSeparator + fileName;
      if (await File(fullPath).exists()) {
        i++;
        List splits = originalFileName.split('.');
        fileName = ["${splits[0]}.$i", splits[1]].join('.');
      } else {
        break;
      }
    }

    return fileName;
  }

Future<void> startDownload(String url, String cookies) async {
    final uri = Uri.parse(url);
    Directory directory = await getApplicationDocumentsDirectory();
    if (Platform.isAndroid) {
      // In this direcory we search already download file.
      directory = Directory('/storage/emulated/0/Download');
    }
    String filename = uri.pathSegments.last;
    final fileName = await getFilenamePath(directory, filename);

    await FlutterDownloader.enqueue(
      url: url,
      fileName: fileName,
      savedDir: directory.path,
      showNotification: true,
      openFileFromNotification: true,
      // saveInPublicStorage: false here we set up custom path to Download,
      // need for get unique name of file,
      // else download was falid.
      saveInPublicStorage: false,
    );
  }

I'll be only too happy to get rid of this ugly code, but it works.

If there is a more successful implementation, I will be glad to consider it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants