Пример #1
0
 public static Stream Write(string path)
 {
     var diskpath = Paths.Map(path);
     if (Paths.IsWritable(path)) return new FileStream(diskpath, FileMode.Create, FileAccess.Write);
     else if (CanUseFtp) {
         var pipe = new PipeStream();
         Tasks.Do(() => {
             using (pipe)
             using (var ftp = new FtpClient()) {
                 string dir, name;
                 ftp.Split(path, out dir, out name);
                 ftp.ChangeDirectory(dir);
                 ftp.PutFile(pipe, name, Ftp.FileAction.Create);
                 ftp.Close();
             }
         });
         return pipe;
     }
     throw new IOException(string.Format("Path {0} is write protected.", path));
 }
Пример #2
0
        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.");
            }
        }