using System.IO;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.StaticFiles;

namespace YGNT.File.Core
{
    public static class FileHelper
    {
        public static string GetContextType(string suffix)
        {
            //获取文件的ContentType
            var provider = new FileExtensionContentTypeProvider();
            provider.TryGetContentType(suffix, out var contentType);
            if (string.IsNullOrEmpty(contentType))
                contentType = "application/octet-stream";
            return contentType;
        }

        /// <summary>
        /// 获得上传文件目录
        /// </summary>
        /// <param name="webHostEnvironment"></param>
        /// <returns></returns>
        public static string GetFilesPath(this IWebHostEnvironment webHostEnvironment)
        {
            var webRootPath = webHostEnvironment.WebRootPath;
            var path = Path.Combine(webRootPath, "Files");
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);
            return path;
        }
    }
}