init commit
This commit is contained in:
425
20220330_Vote/Ewide.RoadFlow/Serivce/Files/FilesService.cs
Normal file
425
20220330_Vote/Ewide.RoadFlow/Serivce/Files/FilesService.cs
Normal file
@@ -0,0 +1,425 @@
|
||||
using Furion.DependencyInjection;
|
||||
using Furion.DynamicApiController;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Ewide.RoadFlowLite.Serivce.Files
|
||||
{
|
||||
/// <summary>
|
||||
/// 文件读写
|
||||
/// </summary>
|
||||
[Route("/api/roadflow/Files/")]
|
||||
[ApiDescriptionSettings("RoadFlow")]
|
||||
public class FilesService: IDynamicApiController, ITransient
|
||||
{
|
||||
|
||||
/*
|
||||
/// <summary>
|
||||
/// 保存CKEDITOR编辑器上传的文件
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string SaveCKEditorFiles()
|
||||
{
|
||||
var files = Request.Form.Files;
|
||||
JObject jObject = new JObject();
|
||||
if (files.Count == 0)
|
||||
{
|
||||
jObject.Add("number", -1);
|
||||
jObject.Add("message", localizer["NotUploadFile"].Value);
|
||||
return new JObject() { { "error", jObject } }.ToString();
|
||||
}
|
||||
var file = files[0];
|
||||
string extName = Path.GetExtension(file.FileName).TrimStart('.');
|
||||
if (!IsUpload(extName))
|
||||
{
|
||||
jObject.Add("number", -1);
|
||||
jObject.Add("message", localizer["CannotUploadThisType"].Value);
|
||||
return new JObject() { { "error", jObject } }.ToString();
|
||||
}
|
||||
Guid userId = Current.GetUserId(Request);
|
||||
if (userId.IsEmptyGuid())
|
||||
{
|
||||
jObject.Add("number", -1);
|
||||
jObject.Add("message", localizer["UserNotLogin"].Value);
|
||||
return new JObject() { { "error", jObject } }.ToString();
|
||||
}
|
||||
DateTime date = DateExtensions.Now;
|
||||
string attachmentPath = "/files/" + userId.ToLowerString() + "/uploads/";
|
||||
string dateString = date.Year.ToString() + "/" + date.ToString("MM") + "/" + date.ToString("dd");
|
||||
string saveDir = rootPath + attachmentPath + dateString + "/";
|
||||
string fileName = file.FileName.Replace(" ", "");
|
||||
string newFileName = GetUploadFileName(saveDir, fileName);
|
||||
if (!Directory.Exists(saveDir))
|
||||
{
|
||||
Directory.CreateDirectory(saveDir);
|
||||
}
|
||||
using (FileStream fs = System.IO.File.Create(saveDir + newFileName))
|
||||
{
|
||||
file.CopyTo(fs);
|
||||
fs.Flush();
|
||||
}
|
||||
JObject jObject1 = new JObject
|
||||
{
|
||||
{ "fileName", newFileName },
|
||||
{ "uploaded", 1 },
|
||||
{ "url", (Request.IsHttps ? "https://" : "http://") + Request.Host.Value + Url.Content("~/RoadFlowWeb/Files/Show?file=") + (attachmentPath + dateString + "/" + newFileName).DESEncrypt() }
|
||||
};
|
||||
return jObject1.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 保存控件上传的文件
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string SaveFiles()
|
||||
{
|
||||
DateTime dateTime = DateExtensions.Now;
|
||||
string year = dateTime.ToString("yyyy");
|
||||
string month = dateTime.ToString("MM");
|
||||
string day = dateTime.ToString("dd");
|
||||
var files = Request.Form.Files;
|
||||
string filetype = Request.Forms("filetype");
|
||||
JObject jObject = new JObject();
|
||||
if (files.Count > 0)
|
||||
{
|
||||
var file = files[0];
|
||||
string extName = Path.GetExtension(file.FileName).TrimStart('.');
|
||||
if (!IsUpload(extName))
|
||||
{
|
||||
jObject.Add("error", localizer["CannotUploadThisType"].Value);
|
||||
return jObject.ToString();
|
||||
}
|
||||
if (!filetype.IsNullOrWhiteSpace() && !("," + filetype + ",").ContainsIgnoreCase("," + extName + ","))
|
||||
{
|
||||
jObject.Add("error", localizer["CannotUploadThisType"].Value);
|
||||
return jObject.ToString();
|
||||
}
|
||||
Guid userId = Current.GetUserId(Request);
|
||||
if (userId.IsEmptyGuid())
|
||||
{
|
||||
jObject.Add("error", localizer["UserNotLogin"].Value);
|
||||
return jObject.ToString();
|
||||
}
|
||||
string attachmentPath = "/files/" + userId.ToLowerString() + "/uploads/";
|
||||
string saveDir = rootPath + attachmentPath + year + "/" + month + "/" + day + "/";
|
||||
string fileName = file.FileName.Replace(" ", "");
|
||||
string newFileName = GetUploadFileName(saveDir, fileName);
|
||||
if (!Directory.Exists(saveDir))
|
||||
{
|
||||
Directory.CreateDirectory(saveDir);
|
||||
}
|
||||
using (FileStream fs = System.IO.File.Create(saveDir + newFileName))
|
||||
{
|
||||
file.CopyTo(fs);
|
||||
//fs.Flush();
|
||||
}
|
||||
jObject.Add("id", (attachmentPath + year + "/" + month + "/" + day + "/" + newFileName).DESEncrypt());
|
||||
jObject.Add("size", file.Length.ToFileSize());
|
||||
}
|
||||
return jObject.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 保存用户上传的签章图片
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string SaveUserSign()
|
||||
{
|
||||
var files = Request.Form.Files;
|
||||
JObject jObject = new JObject();
|
||||
if (files.Count > 0)
|
||||
{
|
||||
var file = files[0];
|
||||
string extName = Path.GetExtension(file.FileName).TrimStart('.');
|
||||
if (!IsUpload(extName))
|
||||
{
|
||||
jObject.Add("error", localizer["CannotUploadThisType"].Value);
|
||||
return jObject.ToString();
|
||||
}
|
||||
Guid userId = Current.GetUserId(Request);
|
||||
if (userId.IsEmptyGuid())
|
||||
{
|
||||
jObject.Add("error", localizer["UserNotLogin"].Value);
|
||||
return jObject.ToString();
|
||||
}
|
||||
string saveDir = Config.ContentRootPath + "/wwwroot/roadflow-files/user-signs/" + userId.ToLowerString() + "/";
|
||||
if (!Directory.Exists(saveDir))
|
||||
{
|
||||
Directory.CreateDirectory(saveDir);
|
||||
}
|
||||
using (FileStream fs = System.IO.File.Create(saveDir + "default.png"))
|
||||
{
|
||||
file.CopyTo(fs);
|
||||
fs.Flush();
|
||||
}
|
||||
jObject.Add("id", "/roadflow-files/user-signs/" + userId.ToLowerString() + "/" + "default.png?v=" + GuidExtensions.NewGuid().ToLowerNString());
|
||||
}
|
||||
return jObject.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 保存用户上传的头像图片
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string SaveUserHeader()
|
||||
{
|
||||
var files = Request.Form.Files;
|
||||
JObject jObject = new JObject();
|
||||
if (files.Count > 0)
|
||||
{
|
||||
var file = files[0];
|
||||
string extName = Path.GetExtension(file.FileName).TrimStart('.');
|
||||
if (!IsUpload(extName))
|
||||
{
|
||||
jObject.Add("error", localizer["CannotUploadThisType"].Value);
|
||||
return jObject.ToString();
|
||||
}
|
||||
Guid userId = Current.GetUserId(Request);
|
||||
if (userId.IsEmptyGuid())
|
||||
{
|
||||
jObject.Add("error", localizer["UserNotLogin"].Value);
|
||||
return jObject.ToString();
|
||||
}
|
||||
string saveDir = Config.ContentRootPath + "/wwwroot/roadflow-files/user-headers/" + userId.ToLowerString() + "/";
|
||||
if (!Directory.Exists(saveDir))
|
||||
{
|
||||
Directory.CreateDirectory(saveDir);
|
||||
}
|
||||
string fileName = file.FileName.Replace(" ", "");
|
||||
string newFileName = GetUploadFileName(saveDir, fileName);
|
||||
using (FileStream fs = System.IO.File.Create(saveDir + newFileName))
|
||||
{
|
||||
file.CopyTo(fs);
|
||||
fs.Flush();
|
||||
}
|
||||
jObject.Add("id", "/roadflow-files/user-headers/" + userId.ToLowerString() + "/" + newFileName + "?v=" + GuidExtensions.NewGuid().ToLowerNString());
|
||||
}
|
||||
return jObject.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 保存文件管理上传的文件
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
|
||||
public string SaveFileManaeFiles()
|
||||
{
|
||||
var files = Request.Form.Files;
|
||||
JObject jObject = new JObject();
|
||||
if (files.Count > 0)
|
||||
{
|
||||
var file = files[0];
|
||||
string extName = Path.GetExtension(file.FileName).TrimStart('.');
|
||||
if (!IsUpload(extName))
|
||||
{
|
||||
jObject.Add("error", localizer["CannotUploadThisType"].Value);
|
||||
return jObject.ToString();
|
||||
}
|
||||
Guid userId = Current.GetUserId(Request);
|
||||
if (userId.IsEmptyGuid())
|
||||
{
|
||||
jObject.Add("error", localizer["UserNotLogin"].Value);
|
||||
return jObject.ToString();
|
||||
}
|
||||
string dir = Request.Forms("dir").DESDecrypt();
|
||||
string saveDir = dir.IsNullOrWhiteSpace() ? rootPath + "/files/" + userId.ToLowerString() + "/files/" : dir + "/";
|
||||
string fileName = file.FileName.Replace(" ", "");
|
||||
string newFileName = GetUploadFileName(saveDir, fileName);
|
||||
if (!Directory.Exists(saveDir))
|
||||
{
|
||||
Directory.CreateDirectory(saveDir);
|
||||
}
|
||||
using (FileStream fs = System.IO.File.Create(saveDir + newFileName))
|
||||
{
|
||||
file.CopyTo(fs);
|
||||
fs.Flush();
|
||||
}
|
||||
jObject.Add("id", (saveDir + newFileName).DESEncrypt());
|
||||
jObject.Add("size", file.Length.ToFileSize());
|
||||
}
|
||||
return jObject.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查文件是否可以上传
|
||||
/// </summary>
|
||||
/// <param name="extName"></param>
|
||||
/// <returns></returns>
|
||||
private bool IsUpload(string extName)
|
||||
{
|
||||
return RoadFlow.Utility.Config.UploadFileExtNames.IsNullOrWhiteSpace()
|
||||
? !",exe,msi,bat,cshtml,html,asp,aspx,ashx,ascx,cs,dll,js,vbs,css,".ContainsIgnoreCase("," + extName + ",")
|
||||
: ("," + RoadFlow.Utility.Config.UploadFileExtNames + ",").ContainsIgnoreCase("," + extName + ",");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 得到上传文件名,如果重名要重新命名
|
||||
/// </summary>
|
||||
/// <param name="saveDir"></param>
|
||||
/// <param name="fileName"></param>
|
||||
/// <returns></returns>
|
||||
private string GetUploadFileName(string saveDir, string fileName)
|
||||
{
|
||||
if (System.IO.File.Exists(saveDir + fileName))
|
||||
{
|
||||
string extName = Path.GetExtension(fileName);
|
||||
string fName = Path.GetFileNameWithoutExtension(fileName) + "_" + Tools.GetRandomString(3).ToLower();
|
||||
return GetUploadFileName(saveDir, fName + extName);
|
||||
}
|
||||
return fileName.Replace(" ", "");//去掉文件名中的空格(LibreOffice转为pdf时文件名有空格转换不了)
|
||||
}
|
||||
|
||||
private (string, string) GetHeadType(string extName)
|
||||
{
|
||||
if (extName.IsNullOrWhiteSpace())
|
||||
{
|
||||
return ("attachment", "application/octet-stream");
|
||||
}
|
||||
string ext = extName.Trim().ToLower();
|
||||
if (",jpg,jpeg,png,gif,tif,tiff,".Contains("," + ext + ","))
|
||||
{
|
||||
return ("inline", "image/" + ("jpg".Equals(ext) ? "jpeg" : ext));
|
||||
}
|
||||
else if (",txt,inf,log,ini,conf,cnf,".Contains("," + ext + ","))
|
||||
{
|
||||
return ("inline", "text/plain;charset=gb2312");//text/plain;charset=utf-8
|
||||
}
|
||||
else if (",pdf,".Contains("," + ext + ","))
|
||||
{
|
||||
return ("inline", "application/pdf");
|
||||
}
|
||||
else if (",json,".Contains("," + ext + ","))
|
||||
{
|
||||
return ("inline", "application/json");
|
||||
}
|
||||
else if (",doc,docx,dot,".Contains("," + ext + ","))
|
||||
{
|
||||
return ("attachment", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
|
||||
}
|
||||
else if (",xls,xlsx,".Contains("," + ext + ","))
|
||||
{
|
||||
return ("attachment", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
||||
}
|
||||
else if (",ppt,pptx,pps,pot,ppa,".Contains("," + ext + ","))
|
||||
{
|
||||
return ("attachment", "application/vnd.openxmlformats-officedocument.presentationml.presentation");
|
||||
}
|
||||
return ("attachment", "application/octet-stream");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断是否是office文件
|
||||
/// </summary>
|
||||
/// <param name="file"></param>
|
||||
/// <returns></returns>
|
||||
private bool IsOfficeFile(string file)
|
||||
{
|
||||
string extName = Path.GetExtension(file);
|
||||
return ",.doc,.docx,.xls,.xlsx,.ppt,.pptx,.wps,.dps,.et,".ContainsIgnoreCase("," + extName + ",");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 显示文件
|
||||
/// </summary>
|
||||
[WebValidate]
|
||||
public IActionResult Show()
|
||||
{
|
||||
string fileId = Request.Querys("file");
|
||||
if (fileId.IsNullOrWhiteSpace())
|
||||
{
|
||||
fileId = Request.Querys("id");
|
||||
}
|
||||
|
||||
if (fileId.IsNullOrWhiteSpace())
|
||||
{
|
||||
return new ContentResult() { Content = "文件不存在!", ContentType = "text/html;charset=utf-8" };
|
||||
}
|
||||
string file = fileId.DESDecrypt();
|
||||
bool fullPath = "1".Equals(Request.Querys("fullpath"));//是否是完整路径(个人文件管理中传过来的路径就是文件完整路径)
|
||||
|
||||
FileInfo tmpFile = new FileInfo(fullPath ? file : rootPath + file);
|
||||
if (!tmpFile.Exists)
|
||||
{
|
||||
return new ContentResult() { Content = "文件不存在!", ContentType = "text/html;charset=utf-8" };
|
||||
}
|
||||
//检查如果路径不是规定的路径,否则不能访问
|
||||
if (fullPath && !RoadFlow.Business.UserFile.HasAccess(tmpFile.DirectoryName, Guid.Empty))
|
||||
{
|
||||
return new ContentResult() { Content = "不能访问!", ContentType = "text/html;charset=utf-8" };
|
||||
}
|
||||
|
||||
bool isDownload = "1".Equals(Request.Querys("download"));//是否是下载文件
|
||||
|
||||
#region 如果是office文件转换为pdf显示
|
||||
if (!isDownload && IsOfficeFile(file))
|
||||
{
|
||||
string tempDirectory = tmpFile.DirectoryName.Replace("\\", "/").Substring(rootPath.Replace("\\", "/").Length).TrimStart('/');
|
||||
string pdfDirectory = rootPath + "/tmpfiles/" + tempDirectory + "/";
|
||||
string pdfFileName = pdfDirectory + Path.GetFileNameWithoutExtension(tmpFile.FullName) + ".pdf";
|
||||
if (!System.IO.File.Exists(pdfFileName))
|
||||
{
|
||||
pdfFileName = RoadFlow.Utility.DocExtensions.ToPdf(tmpFile.FullName, pdfDirectory);
|
||||
if (!pdfFileName.IsNullOrWhiteSpace() && System.IO.File.Exists(pdfFileName))
|
||||
{
|
||||
tmpFile = new FileInfo(pdfFileName);
|
||||
file = pdfFileName;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
tmpFile = new FileInfo(pdfFileName);
|
||||
file = pdfFileName;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
var tmpContentType = isDownload ? ("attachment", "application/octet-stream") : GetHeadType(Path.GetExtension(file).TrimStart('.'));
|
||||
string fileName = tmpFile.Name.UrlEncode();
|
||||
Response.Headers.Add("Accept-Ranges", new StringValues("bytes"));
|
||||
Response.Headers.Add("Server-FileName", new StringValues(fileName));
|
||||
//Response.Headers.Add("Content-Encoding", new StringValues("utf-8"));
|
||||
//Response.Headers.Add("Vary", new StringValues("Accept-Encoding"));
|
||||
Response.ContentType = tmpContentType.Item2;
|
||||
Response.Headers.Add("Content-Disposition", new StringValues(tmpContentType.Item1 + ";filename=" + fileName));
|
||||
Response.Headers.Add("Content-Length", new StringValues(tmpFile.Length.ToString()));
|
||||
|
||||
//using (var tmpRead = tmpFile.OpenRead())
|
||||
//{
|
||||
// var tmpByte = new byte[2048];
|
||||
// var i = tmpRead.Read(tmpByte, 0, tmpByte.Length);
|
||||
// while (i > 0)
|
||||
// {
|
||||
// Response.Body.WriteAsync(tmpByte, 0, i);
|
||||
// Response.Body.FlushAsync();
|
||||
// i = tmpRead.Read(tmpByte, 0, tmpByte.Length);
|
||||
// }
|
||||
//}
|
||||
//Response.Body.FlushAsync();
|
||||
//Response.Body.DisposeAsync();
|
||||
//return new EmptyResult();
|
||||
return File(tmpFile.OpenRead(), tmpContentType.Item2);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 验证码
|
||||
/// </summary>
|
||||
public void ValidCode()
|
||||
{
|
||||
string guid = Request.Querys("guid");
|
||||
string bgImg = RoadFlow.Utility.Tools.GetWebRootPath() + "/vcodebg.png";
|
||||
System.IO.MemoryStream ms = RoadFlow.Utility.Tools.GetValidateImg(out string code, bgImg);
|
||||
RoadFlow.Cache.IO.Insert("rf_vue_validcode_" + guid, code, DateExtensions.Now.AddMinutes(30));
|
||||
Response.Clear();
|
||||
Response.ContentType = "image/png";
|
||||
Response.Body.WriteAsync(ms.GetBuffer(), 0, (int)ms.Length);
|
||||
Response.Body.DisposeAsync();
|
||||
}
|
||||
|
||||
|
||||
*/
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user