public LocalDrive(string root, ListConfig list, OperationConfig operation, IPolicy policy, ILogger logger) { this.root = root; this.list = list; this.operation = operation; this.policy = policy; _logger = logger; _epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); }
// List method returns array of files from the target folder public FsObject[] List(string path, ListConfig config = null) { string fullpath = idToPath(path); if (_logger != null) { _logger.LogDebug($"List {fullpath}"); } if (!policy.Comply(fullpath, Operation.Read)) { throw new System.InvalidOperationException("Access Denied"); } if (config == null) { config = list; } return(listFolder(fullpath, config).ToArray()); }
public LocalDrive(string path, DriveConfig config = null) { _epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); root = Path.GetFullPath(path); var p = new CombinedPolicy(); p.Add(new ForceRootPolicy(root)); if (config != null) { _logger = config.Logger; list = config.List; operation = config.Operation; if (config.Policy != null) { p.Add(config.Policy); } } if (list == null) { list = new ListConfig { } } ; if (operation == null) { operation = new OperationConfig { } } ; policy = p; }
private List <FsObject> listFolder(string path, ListConfig config, List <FsObject> res = null) { var files = Directory.GetFileSystemEntries(path); bool needSortData = false; if (config.Nested || res == null) { res = new List <FsObject>(); needSortData = true; } foreach (var file in files) { bool skipFile = false; var id = pathToID(file); var fs = Info(id); if (config.Exclude != null && config.Exclude(fs.Value)) { skipFile = true; } if (config.Include != null && !config.Include(fs.Value)) { skipFile = true; } if (fs.Type != "folder" && (config.SkipFiles || skipFile)) { continue; } if (fs.Type == "folder" && config.SubFolders) { var sub = listFolder( Path.Combine(path, file), config, res); fs.Type = "folder"; if (config.Nested && sub.Count > 0) { fs.Data = sub.ToArray(); } } if (!skipFile) { res.Add(fs); } } // sort files and folders by name, folders first if (needSortData) { res.Sort(delegate(FsObject x, FsObject y){ bool afolder = x.Type == "folder"; bool bfolder = y.Type == "folder"; if ((afolder || bfolder) && x.Type != y.Type) { return(afolder ? -1 : 1); } return(x.Value.ToUpper().CompareTo(y.Value.ToUpper())); }); } return(res); }