using NCC.Common.Configuration; using NCC.Common.Enum; using NCC.Common.Extension; using NCC.Common.FileManage; using NCC.Common.Filter; using NCC.Common.Helper; using NCC.Dependency; using NCC.DynamicApiController; using NCC.Extend.Entitys.Dto.DocumentPreview; using NCC.FriendlyException; using Mapster; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; using System.IO; using System.Linq; namespace NCC.Extend { ///     /// 文件预览     /// 版 本:V1.20.15     /// 版 权:Wesley(https://www.NCCsoft.com)     /// 作 者:NCC开发平台组     /// 日 期:2022-03-16     /// [ApiDescriptionSettings(Tag = "Extend", Name = "DocumentPreview", Order = 600)] [Route("api/extend/[controller]")] public class DocumentPreview : IDynamicApiController, ITransient { #region Get /// /// 获取文档列表 /// /// /// [HttpGet("")] public dynamic GetList_Api([FromQuery] KeywordInput input) { var output = GetList(input); return output; } /// /// 文件在线预览 /// /// /// /// [HttpGet("{fileId}/Preview")] public dynamic DocumentPreview_Api(string fileId, [FromQuery] DocumentPreviewPreviewInput input) { var output = new DocumentPreviewPreviewOutput(); var filePath = FileVariable.DocumentPreviewFilePath; var files = FileHelper.GetAllFiles(filePath); if (fileId.ToInt() > files.Count) throw NCCException.Oh(ErrorCode.D8000); var file = files[fileId.ToInt()]; if (file != null) { var domain = App.Configuration["NCC_APP:Domain"]; var yozoUrl= App.Configuration["NCC_APP:YOZO:domain"]; var yozoKey = App.Configuration["NCC_APP:YOZO:domainKey"]; var httpContext = App.HttpContext; output.fileName = file.Name; output.filePath = domain + "/api/Extend/DocumentPreview/down/" + file.Name; var url = string.Format("{0}?k={1}&url={2}", yozoUrl, yozoKey, output.filePath, input.noCache, input.watermark, input.isCopy, input.pageStart, input.pageEnd, input.type); if (input.previewType.Equals("localPreview")) { url = "http://" + httpContext.GetLocalIpAddressToIPv4() + ":" + httpContext.Connection.LocalPort + "/api/Extend/DocumentPreview/down/" + file.Name; } return url; } else throw NCCException.Oh(ErrorCode.D8000); } /// /// 下载 /// /// [HttpGet("down/{fileName}")] [AllowAnonymous] public void FileDown(string fileName) { var filePath = FileVariable.DocumentPreviewFilePath + fileName; var systemFilePath = FileVariable.SystemFilePath + fileName; if (FileHelper.Exists(filePath)) FileHelper.DownloadFile(filePath, fileName); else FileHelper.DownloadFile(systemFilePath, fileName); } /// /// 本地预览 /// /// /// [HttpGet("PreviewLocal/{fileId}")] [AllowAnonymous] public dynamic PreviewLocalFile(string fileId) { var filePath = FileVariable.DocumentPreviewFilePath; var fileInfo = FileHelper.GetAllFiles(filePath)[fileId.ToInt()]; var fileName = filePath + fileInfo.Name; if (fileInfo.Extension == ".xlsx" || fileInfo.Extension == ".xls") { PDFHelper.AsposeExcelToPDF(filePath + fileInfo.Name); } else if (fileInfo.Extension == ".docx" || fileInfo.Extension == ".doc") { PDFHelper.AsposeWordToPDF(filePath + fileInfo.Name); } else if (fileInfo.Extension == ".pptx" || fileInfo.Extension == ".ppt") { PDFHelper.PPTToPDF(filePath + fileInfo.Name); } //输出pdf文件 return new FileStreamResult(new FileStream(fileName, FileMode.Open), "application/pdf") { FileDownloadName = fileInfo.Name }; } #endregion #region PrivateMethod /// /// 文档列表 /// /// /// private List GetList(KeywordInput input) { var filePath = FileVariable.DocumentPreviewFilePath; var files = FileHelper.GetAllFiles(filePath); List data = new List(); if (files != null) { for (int i = 0; i < files.Count; i++) { var item = files[i]; FileModel fileModel = new FileModel(); fileModel.FileId = i.ToString(); fileModel.FileName = item.Name; fileModel.FileType = FileHelper.GetFileType(item); fileModel.FileSize = FileHelper.GetFileSize(item.FullName).ToString(); fileModel.FileTime = item.LastWriteTime; data.Add(fileModel); } data = data.FindAll(x => "xlsx".Equals(x.FileType) || "xls".Equals(x.FileType) || "docx".Equals(x.FileType) || "doc".Equals(x.FileType) || "pptx".Equals(x.FileType) || "ppt".Equals(x.FileType)); } if (!input.keyword.IsNullOrEmpty()) { data = data.FindAll(x => x.FileName.Contains(input.keyword)); } var output = data.OrderByDescending(x=>x.FileTime).Adapt>(); return output; } #endregion } }