コード例 #1
0
 public WsFilesController(IFileProvider fileProvider)
 {
     _fileProvider = fileProvider;
     _filesHelper  = new FilesHelper(fileProvider);
 }
コード例 #2
0
        private IEnumerable <object> GetChildren(Site site, string path, IFileInfo parent, string nameFilter, bool jsonModels = true, Fields fields = null)
        {
            long start = -1, finish = -1;
            var  dirs  = new SortedList <string, object>();
            var  files = new Dictionary <string, object>();

            //
            // Virtual Directories
            foreach (var vdir in FilesHelper.GetVdirs(site, path))
            {
                if (string.IsNullOrEmpty(nameFilter) || vdir.Name.IndexOf(nameFilter, StringComparison.OrdinalIgnoreCase) != -1)
                {
                    if (!dirs.ContainsKey(vdir.Name))
                    {
                        dirs.Add(vdir.Name, vdir);
                    }
                }
            }

            //
            // Directories
            foreach (var d in _fileProvider.GetDirectories(parent, string.IsNullOrEmpty(nameFilter) ? "*" : $"*{nameFilter}*"))
            {
                if (!dirs.ContainsKey(d.Name) && !d.Attributes.HasFlag(FileAttributes.Hidden) && !d.Attributes.HasFlag(FileAttributes.System))
                {
                    dirs.Add(d.Name, d);
                }
            }

            foreach (var item in dirs)
            {
                files.Add(item.Key, item.Value);
            }

            //
            // Files
            foreach (var f in _fileProvider.GetFiles(parent, string.IsNullOrEmpty(nameFilter) ? "*" : $"*{nameFilter}*"))
            {
                if (!files.ContainsKey(f.Name) && !f.Attributes.HasFlag(FileAttributes.Hidden) && !f.Attributes.HasFlag(FileAttributes.System))
                {
                    files.Add(f.Name, f);
                }
            }

            if (Context.Request.Headers.ContainsKey(HeaderNames.Range) && !Context.Request.Headers.TryGetRange(out start, out finish, files.Count, _units))
            {
                throw new InvalidRangeException();
            }

            if (start != -1)
            {
                Context.Response.Headers.SetContentRange(start, finish, files.Count);
                files = files.Where((f, i) => i >= start && i <= finish).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
            }

            if (jsonModels)
            {
                var parentModel = _filesHelper.DirectoryToJsonModelRef(site, path, fields.Filter("parent"));

                foreach (var key in files.Keys.ToList())
                {
                    if (files[key] is Vdir)
                    {
                        files[key] = _filesHelper.VdirToJsonModelRef((Vdir)files[key], fields ?? FilesHelper.RefFields, parentModel);
                    }
                    else
                    {
                        IFileInfo file = (IFileInfo)files[key];
                        files[key] = file.Type == Administration.Files.FileType.File ? _filesHelper.FileToJsonModelRef(site, Path.Combine(path, file.Name), fields ?? FilesHelper.RefFields, parentModel)
                                                                                    : _filesHelper.DirectoryToJsonModelRef(site, Path.Combine(path, file.Name), fields ?? FilesHelper.RefFields, parentModel);
                    }
                }
            }

            return(files.Values);
        }