public async Task <FullPath> ParsePathAsync(string target) { if (string.IsNullOrEmpty(target)) { return(null); } int underscoreIndex = target.IndexOf('_'); string pathHash = target.Substring(underscoreIndex + 1); string volumePrefix = target.Substring(0, underscoreIndex + 1); var root = Roots.First(r => r.VolumeId == volumePrefix); var rootDirectory = new DirectoryInfo(root.RootDirectory); string path = HttpEncoder.DecodePath(pathHash); string dirUrl = path != rootDirectory.Name ? path : string.Empty; var dir = new FileSystemDirectory(root.RootDirectory + dirUrl); if (await dir.ExistsAsync) { return(new FullPath(root, dir, target)); } else { var file = new FileSystemFile(root.RootDirectory + dirUrl); return(new FullPath(root, file, target)); } }
public async Task <JsonResult> MakeDirAsync(FullPath path, string name, IEnumerable <string> dirs) { var response = new AddResponseModel(); if (!string.IsNullOrEmpty(name)) { var newDir = new FileSystemDirectory(Path.Combine(path.Directory.FullName, name)); await newDir.CreateAsync(); response.Added.Add(await BaseModel.CreateAsync(newDir, path.RootVolume)); } foreach (string dir in dirs) { string dirName = dir.StartsWith("/") ? dir.Substring(1) : dir; var newDir = new FileSystemDirectory(Path.Combine(path.Directory.FullName, dirName)); await newDir.CreateAsync(); response.Added.Add(await BaseModel.CreateAsync(newDir, path.RootVolume)); string relativePath = newDir.FullName.Substring(path.RootVolume.RootDirectory.Length); response.Hashes.Add($"/{dirName}", path.RootVolume.VolumeId + HttpEncoder.EncodePath(relativePath)); } return(await Json(response)); }
public async Task <JsonResult> MakeDirAsync(FullPath path, string name) { var newDir = new FileSystemDirectory(Path.Combine(path.Directory.FullName, name)); await newDir.CreateAsync(); var response = new AddResponseModel(); response.Added.Add(await BaseModel.Create(this, newDir, path.RootVolume)); return(await Json(response)); }
public async Task <JsonResult> PasteAsync(FullPath dest, IEnumerable <FullPath> paths, bool isCut, IEnumerable <string> renames, string suffix) { var response = new ReplaceResponseModel(); foreach (var src in paths) { if (src.IsDirectory) { var newDir = new FileSystemDirectory(Path.Combine(dest.Directory.FullName, src.Directory.Name)); if (await newDir.ExistsAsync) { Directory.Delete(newDir.FullName, true); } if (isCut) { await RemoveThumbsAsync(src); Directory.Move(src.Directory.FullName, newDir.FullName); response.Removed.Add(src.HashedTarget); } else { DirectoryCopy(src.Directory.FullName, newDir.FullName, true); } response.Added.Add(await BaseModel.CreateAsync(newDir, dest.RootVolume)); } else { var newFile = new FileSystemFile(Path.Combine(dest.Directory.FullName, src.File.Name)); if (await newFile.ExistsAsync) { await newFile.DeleteAsync(); } if (isCut) { await RemoveThumbsAsync(src); File.Move(src.File.FullName, newFile.FullName); response.Removed.Add(src.HashedTarget); } else { File.Copy(src.File.FullName, newFile.FullName); } response.Added.Add(await BaseModel.CreateAsync(newFile, dest.RootVolume)); } } return(await Json(response)); }
public async Task <JsonResult> RenameAsync(FullPath path, string name) { var response = new ReplaceResponseModel(); response.Removed.Add(path.HashedTarget); await RemoveThumbs(path); if (path.IsDirectory) { var newPath = new FileSystemDirectory(Path.Combine(path.Directory.Parent.FullName, name)); Directory.Move(path.Directory.FullName, newPath.FullName); response.Added.Add(await BaseModel.Create(this, newPath, path.RootVolume)); } else { var newPath = new FileSystemFile(Path.Combine(path.File.DirectoryName, name)); File.Move(path.File.FullName, newPath.FullName); response.Added.Add(await BaseModel.Create(this, newPath, path.RootVolume)); } return(await Json(response)); }
public async Task <FullPath> ParsePathAsync(string target) { if (string.IsNullOrEmpty(target)) { return(null); } string volumePrefix = null; string pathHash = null; for (int i = 0; i < target.Length; i++) { if (target[i] == '_') { pathHash = target.Substring(i + 1); volumePrefix = target.Substring(0, i + 1); break; } } var root = Roots.First(r => r.VolumeId == volumePrefix); var rootDirectory = new DirectoryInfo(root.RootDirectory); string path = HttpEncoder.DecodePath(pathHash); string dirUrl = path != rootDirectory.Name ? path : string.Empty; var dir = new FileSystemDirectory(root.RootDirectory + dirUrl); if (await dir.ExistsAsync) { return(new FullPath(root, dir, target)); } else { var file = new FileSystemFile(root.RootDirectory + dirUrl); return(new FullPath(root, file, target)); } }
public async Task <JsonResult> ExtractAsync(FullPath fullPath, bool newFolder) { var response = new AddResponseModel(); if (fullPath.IsDirectory || fullPath.File.Extension.ToLower() != ".zip") { throw new NotSupportedException("Only .zip files are currently supported."); } string rootPath = fullPath.File.Directory.FullName; if (newFolder) { rootPath = Path.Combine(rootPath, Path.GetFileNameWithoutExtension(fullPath.File.Name)); var rootDir = new FileSystemDirectory(rootPath); if (!await rootDir.ExistsAsync) { await rootDir.CreateAsync(); } response.Added.Add(await BaseModel.CreateAsync(rootDir, fullPath.RootVolume)); } using (var archive = ZipFile.OpenRead(fullPath.File.FullName)) { string separator = Path.DirectorySeparatorChar.ToString(); foreach (ZipArchiveEntry entry in archive.Entries) { try { //Replce zip entry path separator by system path separator string file = Path.Combine(rootPath, entry.FullName) .Replace("/", separator).Replace("\\", separator); if (file.EndsWith(separator)) //directory { var dir = new FileSystemDirectory(file); if (!await dir.ExistsAsync) { await dir.CreateAsync(); } if (!newFolder) { response.Added.Add(await BaseModel.CreateAsync(dir, fullPath.RootVolume)); } } else { entry.ExtractToFile(file, true); if (!newFolder) { response.Added.Add(await BaseModel.CreateAsync(new FileSystemFile(file), fullPath.RootVolume)); } } } catch //(Exception ex) { //throw new Exception(entry.FullName, ex); } } } return(await Json(response)); }