DocumentPreview.cs
6.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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
{
/// <summary>
/// 文件预览
/// 版 本:V1.20.15
/// 版 权:Wesley(https://www.NCCsoft.com)
/// 作 者:NCC开发平台组
/// 日 期:2022-03-16
/// </summary>
[ApiDescriptionSettings(Tag = "Extend", Name = "DocumentPreview", Order = 600)]
[Route("api/extend/[controller]")]
public class DocumentPreview : IDynamicApiController, ITransient
{
#region Get
/// <summary>
/// 获取文档列表
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet("")]
public dynamic GetList_Api([FromQuery] KeywordInput input)
{
var output = GetList(input);
return output;
}
/// <summary>
/// 文件在线预览
/// </summary>
/// <param name="fileId"></param>
/// <param name="input"></param>
/// <returns></returns>
[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);
}
/// <summary>
/// 下载
/// </summary>
/// <param name="fileName"></param>
[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);
}
/// <summary>
/// 本地预览
/// </summary>
/// <param name="fileId"></param>
/// <returns></returns>
[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
/// <summary>
/// 文档列表
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
private List<DocumentPreviewListOutput> GetList(KeywordInput input)
{
var filePath = FileVariable.DocumentPreviewFilePath;
var files = FileHelper.GetAllFiles(filePath);
List<FileModel> data = new List<FileModel>();
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<List<DocumentPreviewListOutput>>();
return output;
}
#endregion
}
}