Пример #1
0
        /// <summary>
        /// Get files in the passed directory
        /// </summary>
        /// <param name="directoryPath">Path to the files directory</param>
        /// <param name="type">Type of the files</param>
        /// <returns>A task that represents the completion of the operation</returns>
        protected virtual async Task GetFilesAsync(string directoryPath, string type)
        {
            directoryPath = GetVirtualPath(directoryPath);
            List <string> files = GetFiles(GetFullPath(directoryPath), type);

            await HttpContext.Response.WriteAsync("[");

            for (int i = 0; i < files.Count; i++)
            {
                int    width        = 0;
                int    height       = 0;
                string physicalPath = files[i];

                if (GetFileType(_fileProvider.GetFileExtension(files[i])) == "image")
                {
                    using (FileStream stream = new FileStream(physicalPath, FileMode.Open))
                    {
                        using (Image image = Image.FromStream(stream))
                        {
                            width  = image.Width;
                            height = image.Height;
                        }
                    }
                }

                await HttpContext.Response.WriteAsync($"{{\"p\":\"{directoryPath.TrimEnd('/')}/{_fileProvider.GetFileName(physicalPath)}\",\"t\":\"{Math.Ceiling(GetTimestamp(_fileProvider.GetLastWriteTime(physicalPath)))}\",\"s\":\"{_fileProvider.FileLength(physicalPath)}\",\"w\":\"{width}\",\"h\":\"{height}\"}}");

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

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