amosavian / FileProvider

FileManager replacement for Local, iCloud and Remote (WebDAV/FTP/Dropbox/OneDrive) files -- Swift
MIT License
64 stars 16 forks source link

FTP giving unexpected status messages; not uploading? #157

Open akirmse opened 5 years ago

akirmse commented 5 years ago

I'm trying to upload to an ftp server that requires anonymous login. Here's my code:

    private static let FTP_SERVER_NAME = "ftpnos.woc.noaa.gov"
    private static let FTP_USERNAME = "anonymous"
    private static let FTP_PASSWORD = "dsworlduser@noaa.gov"

    private func sendDataViaFtp(data: Data, remoteFilename: String, callback: @escaping (_ success: Bool) -> ()) {
        let ftpUrl = URL(string: "ftp://\(SurveyMarkerReporter.FTP_SERVER_NAME)")!
        let credential = URLCredential(user: Self.FTP_USERNAME, password: Self.FTP_PASSWORD, persistence: .forSession)

        if let uploader = FTPFileProvider(baseURL: ftpUrl, mode: .default, credential: credential) {
            uploader.writeContents(path: remoteFilename, contents: data, overwrite: true) { error in
                if let error = error {
                    log.warning("Unable to upload file via FTP: \(error)")
                    callback(false)
                    return
                }
                callback(true)
                return
            }
        } else {
            log.error("Couldn't create FTP uploader")
            callback(false)
        }
    }

I'm getting 220 and 331 status messages back (see logs below), which sound like potentially there isn't really an error, but uploading has not occurred. I don't control this FTP server and can't see what's happening on the server side.

2019-09-21 21:53:36.765 [Warning] [FileProvider.FTP] [SurveyMarkerReporter.swift:67] sendDataViaFtp(data:remoteFilename:callback:) > Unable to upload file via FTP: FileProviderFTPError(code: 220, path: "", serverDescription: Optional("ProFTPD 1.3.3a Server (ftpnos.woc.noaa.gov) [10.21.2.65]"))
2019-09-21 21:53:46.820 [Warning] [FileProvider.FTP] [SurveyMarkerReporter.swift:67] sendDataViaFtp(data:remoteFilename:callback:) > Unable to upload file via FTP: FileProviderFTPError(code: 331, path: "", serverDescription: Optional("Anonymous login ok, send your complete email address as your password"))
2019-09-21 21:53:48.636 [Warning] [FileProvider.FTP] [SurveyMarkerReporter.swift:67] sendDataViaFtp(data:remoteFilename:callback:) > Unable to upload file via FTP: FileProviderFTPError(code: 331, path: "", serverDescription: Optional("Anonymous login ok, send your complete email address as your password"))
2019-09-21 21:53:50.015 [Warning] [FileProvider.FTP] [SurveyMarkerReporter.swift:67] sendDataViaFtp(data:remoteFilename:callback:) > Unable to upload file via FTP: FileProviderFTPError(code: 331, path: "", serverDescription: Optional("Anonymous login ok, send your complete email address as your password"))
akirmse commented 5 years ago

Stepping through the FTPProvider code, I think maybe it's not expecting many lines of response when logging in? After many retries, one upload did eventually succeed, so this is close to working.

Here's a transcript of logging in via command-line ftp:

$ ftp ftpnos.woc.noaa.gov
Connected to kvm-east2-ftpnos.w2.wocvm.noaa.gov.
220-********  WARNING     WARNING    WARNING   WARNING   WARNING ********
220-This is a United States government computer system, which may be
220-accessed and used only for official Government business by authorized
220-personnel.  Unauthorized access or use of this computer system may
220-subject violators to criminal, civil, and/or administrative action.
220-
220-All information on this computer system may be intercepted, recorded,
220-read, copied, and disclosed by and to authorized personnel for official
220-purposes, including criminal investigations. Access or use of this
220-computer system by any person whether authorized or unauthorized,
220-constitutes consent to these terms.
220-********  WARNING     WARNING    WARNING   WARNING   WARNING ********
220-Notice: All files have a storage life of 30 days.  Anything older will 
220-be deleted automatically.  You have been warned.
220-
220 ProFTPD 1.3.3a Server (ftpnos.woc.noaa.gov) [10.21.2.65]
Name (ftpnos.woc.noaa.gov:akirmse): anonymous
331 Anonymous login ok, send your complete email address as your password
Password: 
230 Anonymous access granted, restrictions apply
Remote system type is UNIX.
Using binary mode to transfer files.
akirmse commented 5 years ago

After doing a little more tracing, I think the problem might be that the server sends some of the 220 responses, so FTPProvider immediately sends the login name. But then more of the initial 220 responses arrive, which the provider is incorrectly interpreting as the response to the username login.

I see there are some 0.1 sec sleeps in the code, perhaps an attempt to wait until the server has sent everything? But that doesn't seem very robust.

Pinturaj commented 4 years ago

Not be able to download file. Every time get exception because FTP server

I am using host like ftp://12.2.3.5:22

Please help me.

Thnks