using Hotline.Share.Enums.Article;
using Hotline.Share.Tools;
using Microsoft.AspNetCore.Mvc;

namespace Hotline.Application.Tools;
public static class StreamExtensions
{
    public static FileStreamResult GetPDFFile(this Stream stream, string fileName)
    {
        return stream.GetFileStreamResult(EFileType.pdf, fileName);
    }

    public static FileStreamResult GetExcelFile(this Stream stream, string fileName)
    {
        return stream.GetFileStreamResult(EFileType.excel, fileName);
    }

    public static FileStreamResult GetWordFile(this Stream stream, string fileName)
    {
        return stream.GetFileStreamResult(EFileType.word, fileName);
    }

    /// <summary>
    /// 生成流文件http响应信息
    /// </summary>
    /// <param name="stream">文件流</param>
    /// <param name="fileType">文件类型</param>
    /// <param name="fileName">文件名字</param>
    /// <param name="hasTail">是否自动生成时间后缀</param>
    /// <returns></returns>
    public static FileStreamResult GetFileStreamResult(this Stream stream, EFileType fileType, string fileName, bool hasTail = true)
    {
        var tail = DateTime.Now.ToString("yyyyMMddhhmmss") + fileType.GetFileExtension();
        var tailName = fileName + tail;
        if (hasTail)
        {
            fileName = tailName;
        }
        else
        {
            fileName += fileType.GetFileExtension();
        }
        return new FileStreamResult(stream, fileType.GetContentType())
        {
            FileDownloadName = fileName
        };
    }

}
