public void Move( [FromQuery] string from, [FromQuery] string to) { if (string.IsNullOrWhiteSpace(from)) { from = ""; } if (string.IsNullOrWhiteSpace(to)) { to = ""; } var rootDirectory = DirectoryEntity.FromPath(Startup.Configuration[Startup.RootDirectory]); string fromFullPath = System.IO.Path.Combine(Startup.Configuration[Startup.RootDirectory], from); string toFullPath = System.IO.Path.Combine(Startup.Configuration[Startup.RootDirectory], to); // get the file attributes for file or directory FileAttributes fromAttr = System.IO.File.GetAttributes(fromFullPath); if (fromAttr == FileAttributes.Directory) { DirectoryEntity.FromPath(fromFullPath).Move(toFullPath); } else { FileEntity.FromPath(fromFullPath).Move(toFullPath); } }
public PagingWithUrl <IFileSystemItem> List( string path, FileSystemItemType?type, string q, int skip = 0, int take = 10) { if (string.IsNullOrWhiteSpace(path)) { path = ""; } var rootDirectory = DirectoryEntity.FromPath(Startup.Configuration[Startup.RootDirectory]); string fullPath = System.IO.Path.Combine(Startup.Configuration[Startup.RootDirectory], path); IEnumerable <IFileSystemItem> fullResult; if (!string.IsNullOrWhiteSpace(q)) { fullResult = DirectoryEntity.FromPath(fullPath).Search(q); } else { fullResult = DirectoryEntity.FromPath(fullPath).GetChildren(); } fullResult = fullResult.OrderBy(x => x.Type) // 目錄優先 .Where(x => !type.HasValue || x.Type == type.Value) // 類型過濾 .Select(x => { x.RelativePath = x.Path.Substring(rootDirectory.Path.Length).Replace('\\', '/'); if (x.RelativePath[0] == '/') { x.RelativePath = x.RelativePath.Substring(1); } if (x is FileEntity file) { file.DownloadUrl = $"{Request.Scheme}://{Request.Host}/api/File/download?path={Uri.EscapeDataString(x.RelativePath)}&token={Uri.EscapeDataString(BuildToken(file))}"; } return(x); });; return(new PagingWithUrl <IFileSystemItem>(fullResult, skip, take).Process(x => { var builder = new UriBuilder(Request.GetDisplayUrl()); var queryBuilder = new QueryBuilder(); if (type.HasValue) { queryBuilder.Add("type", type.Value.ToString()); } queryBuilder.Add("skip", (skip + take).ToString()); queryBuilder.Add("take", take.ToString()); builder.Query = queryBuilder.ToString(); x.Next = builder.ToString(); })); }
public IFileSystemItem CreateDirectory(string path) { if (string.IsNullOrWhiteSpace(path)) { path = ""; } var rootDirectory = DirectoryEntity.FromPath(Startup.Configuration[Startup.RootDirectory]); string fullPath = System.IO.Path.Combine(Startup.Configuration[Startup.RootDirectory], path); return(DirectoryEntity.FromDirectoryInfo(Directory.CreateDirectory(fullPath))); }
public IEnumerable <IFileSystemItem> Upload(string path, [FromForm] IFormFileCollection files) { if (string.IsNullOrWhiteSpace(path)) { path = ""; } var rootDirectory = DirectoryEntity.FromPath(Startup.Configuration[Startup.RootDirectory]); string fullPath = System.IO.Path.Combine(Startup.Configuration[Startup.RootDirectory], path); DirectoryEntity targetDirectory = DirectoryEntity.FromPath(fullPath); return(Request.Form.Files.Select(x => { return targetDirectory.CreateFile(x.FileName, x.OpenReadStream()); })); }
public void Delete([FromBody] IEnumerable <string> paths) { foreach (var path in paths) { if (string.IsNullOrWhiteSpace(path)) { continue; // 根目錄禁止刪除 } string fullPath = System.IO.Path.Combine(Startup.Configuration[Startup.RootDirectory], path); FileAttributes attr = System.IO.File.GetAttributes(fullPath); if (attr == FileAttributes.Directory) { DirectoryEntity.FromPath(fullPath).Delete(); } else { FileEntity.FromPath(fullPath).Delete(); } } }