flutter_downloader icon indicating copy to clipboard operation
flutter_downloader copied to clipboard

download error if file exists

Open chenhucxc opened this issue 2 years ago • 9 comments

/**
     * Create a file using java.io API
     */
    private File createFileInAppSpecificDir(String filename, String savedDir) {
        File newFile = new File(savedDir, filename);
        try {
            boolean rs = newFile.createNewFile();
            if (rs) {
                return newFile;
            } else {
                logError("It looks like you are trying to save file in public storage but not setting 'saveInPublicStorage' to 'true'");
            }
        } catch (IOException e) {
            e.printStackTrace();
            logError("Create a file using java.io API failed ");
        }
        return null;
    }

should add this

if (newFile.exists()) {
            return newFile;
}

chenhucxc avatar Mar 26 '22 09:03 chenhucxc

This error is also happening on Android when the file is in the directory but I'm trying to download it using the downloader.

@hnvn do you think there's a better solution for this? I think it's better to append a duplication number to the file name ex(photo.png, photo(1).png) since different files can have the same file name and rewriting or retrieving the file with the same name isn't the best solution IMO. I can work on a PR for this fix, I just need your input. :)

Faaatman avatar May 11 '22 09:05 Faaatman

This error is also happening on Android when the file is in the directory but I'm trying to download it using the downloader.

@hnvn do you think there's a better solution for this? I think it's better to append a duplication number to the file name ex(photo.png, photo(1).png) since different files can have the same file name and rewriting or retrieving the file with the same name isn't the best solution IMO. I can work on a PR for this fix, I just need your input. :)

Same problem here, sound nice if you fix this. I agree too, about append a duplication number when the filename is the same

MirzaUkas avatar May 18 '22 10:05 MirzaUkas

Create unique locale file with the remote file name: gist

import 'dart:io';
import 'package:path/path.dart' as path;

class FileUtil {
  static File getUniqueFile(String folderName, final String? fileName) {
    int num = 1;

    String destFileName =
        fileName ?? '${DateTime.now().millisecondsSinceEpoch}';

    String extension = path.extension(destFileName);
    String baseName = path.basenameWithoutExtension(destFileName);

    File file = File(folderName + path.separator + destFileName);
    while (file.existsSync()) {
      destFileName = '$baseName (${num++})$extension';
      file = File(folderName + path.separator + destFileName);
    }
    return file;
  }

  static Future<File> createUniqueFile(
      String folderName, final String? name) async {
    File uniqueFile = FileUtil.getUniqueFile(folderName, name);
    if (!uniqueFile.existsSync()) {
      await uniqueFile.create();
    }
    return uniqueFile;
  }
}

ebwood avatar May 19 '22 09:05 ebwood

any updates on this ? facing the same issue it fails alternately if file exists.

Edit: Thanks @FaKenKoala for your answer, it really helped me ❤. But I think these cases should be handled by plugin.

Kishan-Somaiya avatar Jun 07 '22 07:06 Kishan-Somaiya

It happens when the task status for the file in the Downloader DB is not 'complete,' but you have a fully downloaded file in the local directory. The problem is that the plugin sees the fully downloaded file as a partially downloaded one. If the same file exists, the plugin resumes downloading and sends the size of the byes to the server, which causes HTTP status 416 Range Not Satisfiable.

We could reduce the size of downloadedBytes by a small amount and resume downloading in the setupPartialDownloadedDataHeader method.

claptv avatar Jul 19 '22 09:07 claptv

Same problem form me

arthas1888 avatar Jul 29 '22 16:07 arthas1888

@Faaatman Would be great to see your PR fixing this :)

bartekpacia avatar Aug 17 '22 16:08 bartekpacia

This is worked on in #708 :)

bartekpacia avatar Aug 20 '22 12:08 bartekpacia

you all have this open failed: EACCES (Permission denied) error? I'm not sure that I have the same issue as you but it seems like that

IlyaMax avatar Aug 30 '22 16:08 IlyaMax