Example #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));
 }
Example #2
0
 public static void Delete(IEnumerable<string> paths)
 {
     paths.Each(path => {
         if (path.Contains("*")) All(path).Each(p => Delete(p));
         path = Paths.Normalize(path);
         var ftppath = path.Substring(1);
         var diskpath = Paths.Map(path);
         if (Directory.Exists(diskpath)) {
             if (Paths.IsWritable(path)) {
                 try {
                     Directory.Delete(diskpath, true);
                 } catch {
                     var info = new DirectoryInfo(diskpath);
                     var children = info.EnumerateFileSystemInfos("*", SearchOption.AllDirectories).ToList();
                     foreach (var file in children.OfType<FileInfo>()) File.Delete(file.FullName);
                     children.Reverse();
                     foreach (var dir in children.OfType<DirectoryInfo>()) Directory.Delete(dir.FullName);
                 }
             } else if (CanUseFtp) {
                 using (var ftp = new FtpClient()) {
                     ftp.DeleteDirectoryRecursive(ftppath);
                     ftp.Close();
                 }
             } else throw new IOException(string.Format("Path {0} is write protected.", path));
         } else if (File.Exists(diskpath)) {
             if (Paths.IsWritable(path)) File.Delete(diskpath);
             else if (CanUseFtp) {
                 using (var ftp = new FtpClient()) {
                     string dir, name;
                     ftp.Split(path, out dir, out name);
                     ftp.ChangeDirectory(dir);
                     ftp.DeleteFile(name);
                     ftp.Close();
                 }
             } else throw new IOException(string.Format("Path {0} is write protected.", path));
         }
     });
 }
Example #3
0
 public static void Save(Stream src, string path)
 {
     if (Paths.IsWritable(path)) {
         SaveRaw(src, Paths.Map(path));
     } else if (CanUseFtp) {
         using (var ftp = new FtpClient()) {
             string dir, name;
             ftp.Split(path, out dir, out name);
             ftp.ChangeDirectory(dir);
             ftp.PutFile(src, name, Ftp.FileAction.Create);
             ftp.Close();
         }
     } else throw new IOException(string.Format("Path {0} is write protected.", path));
 }
Example #4
0
 public static void CreateDirectory(IEnumerable<string> paths)
 {
     foreach (var path in paths) {
         var diskpath = Paths.Map(path);
         if (!Directory.Exists(diskpath)) {
             if (Paths.IsWritable(path)) Directory.CreateDirectory(diskpath);
             else if (CanUseFtp) {
                 using (var ftp = new FtpClient()) {
                     string dir, name;
                     ftp.Split(path, out dir, out name);
                     if (!dir.IsNullOrEmpty()) ftp.ChangeDirectory(dir);
                     ftp.MakeDirectory(name);
                     ftp.Close();
                 }
             } else throw new IOException(string.Format("Path {0} is write protected.", path));
         }
     }
 }