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

FTPFileProvider is downloading file twice #152

Open
Nathipha opened this issue May 13, 2019 · 2 comments
Open

FTPFileProvider is downloading file twice #152

Nathipha opened this issue May 13, 2019 · 2 comments

Comments

@Nathipha
Copy link

Nathipha commented May 13, 2019

I'm using your library to log into my FTP server and download a file but
func copyItem(path: String, toLocalURL destURL: URL, completionHandler: SimpleCompletionHandler) -> Progress?
(only function for downloading I've found) doesn't support overwriting.
So while the first try to download the file to the app's "Documents" folder always succeeds, the second one always fails because the file already exists.

My code:

    private func download() {
        print("start download") //Only called once!
        let foldername = "myfolder"
        let filename = "mytestfile.txf"
        let server = "192.0.0.1"
        let username = "testuser"
        let password = "testpw"       

        let credential = URLCredential(user: username, password: password, persistence: .permanent)
        let ftpProvider = FTPFileProvider(baseURL: server, mode: FTPFileProvider.Mode.passive, credential: credential, cache: URLCache())
        
        ftpProvider?.delegate = self as FileProviderDelegate
        
        let fileManager = FileManager.default
        let source = "/\(foldername)/\(filename)"
        let dest = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent(filename)
        let destPath = dest.path

        if fileManager.fileExists(atPath: destPath) {
            print("file already exists!")
            
            do {
                try fileManager.removeItem(atPath: destPath)
            } catch {
                print("error removing!") //TODO: Error
            }
            print("still exists: \(fileManager.fileExists(atPath: destPath))")
        } else {
            print("file doesn't already exist!")
        }
        
        let progress = ftpProvider?.copyItem(path: source, toLocalURL: dest, completionHandler: nil)
        progressBar.observedProgress = progress
    }

As you can see, I added my own check for already existing files but unfortunately this doesn't help with the second try.

Why is it even trying to download the file twice? Is there maybe some hidden function for downloading that does support overwriting?
How can I fix this?

@Nathipha
Copy link
Author

Update: I created a simple "sample.txt" at the root of my ftp server (text inside :
"Hello world from sample.txt!"), then tried to just read the file to later save it myself. For this I'm using this code from the "Sample-iOS.swift" file here.

ftpProvider?.contents(path: source, completionHandler: {
    contents, error in
    if let contents = contents {
        print(String(data: contents, encoding: .utf8))
    }
})

But it also does this twice! The output for the "sample.txt" file is:

Optional("Hello world from sample.txt!")
Fetching on sample.txt succeed.
Optional("Hello world from sample.txt!Hello world from sample.txt!")
Fetching on sample.txt succeed.

Why is it calling this twice too? I'm only calling my function once and "start download" is also only printed once.

@Nathipha
Copy link
Author

Update 2: I did some more investigating and found out what's called twice in the contents function:

It's the whole self.ftpDownload section!
And inside FTPHelper.ftpLogin the whole self.ftpRetrieve section is called twice.
And inside FTPHelper.ftpRetrieve the whole self.attributesOfItem section is called twice.
And probably so on...

ftpProvider?.copyItem uses the same ftpDownload func, so at least I know why both contents() and copyItem() are affected.

The same question remains though: Why is it calling these functions twice and how do I fix this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant