public static void Upload(Stream stream, Uri url) { using (stream) { switch (url.Scheme) { case "ftp": case "ftps": if (Providers.HasProvider(typeof(Ftp.FtpClient))) { Ftp.FtpSecurityProtocol protocol; if (url.Scheme == "ftp") protocol = Ftp.FtpSecurityProtocol.None; else protocol = Ftp.FtpSecurityProtocol.Tls1OrSsl3Explicit; var path = url.PathAndQuery; if (path.Contains('?')) path = path.Substring(0, path.IndexOf('?')); var user = url.UserInfo; var password = "******"; if (user.IsNullOrEmpty()) { user = "******"; } else if (user.Contains(":")) { int p = user.IndexOf(':'); password = user.Substring(p+1); user = user.Substring(0, p); } using (var ftp = new Ftp.FtpClient(url.Host, url.Port, protocol)) { ftp.Open(user, password); ftp.PutFile(stream, path, Ftp.FileAction.Create); } } else throw new NotSupportedException("There is no FTP Provider available."); break; default: if (url.IsFile) Save(stream, url.LocalPath); throw new NotSupportedException("Only ftp & ftps url's are supported."); } } }
public static Stream Download(Uri url, Action<AdvancedWebClient> setup = null, string toFile = null) { if (toFile != null) Save(Download(url, setup, null), toFile); switch (url.Scheme) { case "http": case "https": if (Paths.IsLocal(url)) { var path = Uri.UnescapeDataString(url.AbsoluteUri.Replace(Paths.Home, "~")); return OpenVirtual(path); } else { var web = new AdvancedWebClient(true); if (setup != null) setup(web); return web.OpenRead(url.ToString()); } case "ftp": case "ftps": if (Providers.HasProvider(typeof(Ftp.FtpClient))) { Ftp.FtpSecurityProtocol protocol; if (url.Scheme == "ftp") protocol = Ftp.FtpSecurityProtocol.None; else protocol = Ftp.FtpSecurityProtocol.Tls1OrSsl3Explicit; var path = url.PathAndQuery; if (path.Contains('?')) path = path.Substring(0, path.IndexOf('?')); var user = url.UserInfo; var password = "******"; if (user.IsNullOrEmpty()) { user = "******"; } else if (user.Contains(":")) { int p = user.IndexOf(':'); password = user.Substring(p+1); user = user.Substring(0, p); } var stream = new PipeStream(); Tasks.Do(() => { using (var ftp = new Ftp.FtpClient(url.Host, url.Port, protocol)) { ftp.Open(user, password); ftp.GetFile(path, stream, false); } }); return stream; } else throw new NotSupportedException("There is no FTP Provider available."); default: if (url.IsFile) return Read(url.LocalPath); throw new NotSupportedException("Only http, https, ftp & ftps url's are supported."); } }