예제 #1
0
        /// <summary>
        /// Get all available directories as a directory tree
        /// </summary>
        /// <param name="type">Type of the file</param>
        /// <returns>A task that represents the completion of the operation</returns>
        protected virtual async Task GetDirectoriesAsync(string type)
        {
            string rootDirectoryPath = GetFullPath(GetVirtualPath(null));

            if (!_fileProvider.DirectoryExists(rootDirectoryPath))
            {
                throw new Exception("Invalid files root directory. Check your configuration.");
            }

            ArrayList allDirectories = GetDirectories(rootDirectoryPath);

            allDirectories.Insert(0, rootDirectoryPath);

            string localPath = GetFullPath(null);
            await HttpContext.Response.WriteAsync("[");

            for (int i = 0; i < allDirectories.Count; i++)
            {
                string directoryPath = (string)allDirectories[i];
                await HttpContext.Response.WriteAsync($"{{\"p\":\"/{directoryPath.Replace(localPath, string.Empty).Replace("\\", "/").TrimStart('/')}\",\"f\":\"{GetFiles(directoryPath, type).Count}\",\"d\":\"{_fileProvider.GetDirectories(directoryPath).Length}\"}}");

                if (i < allDirectories.Count - 1)
                {
                    await HttpContext.Response.WriteAsync(",");
                }
            }

            await HttpContext.Response.WriteAsync("]");
        }