185 lines
6.0 KiB
C#
185 lines
6.0 KiB
C#
using Common.Models.Enums;
|
|
using Common.Models.UnqTables;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Web.Http;
|
|
using Common.Utils;
|
|
using Org.BouncyCastle.Asn1.Cms;
|
|
using SqlSugar;
|
|
|
|
namespace Server.Controllers.FunctionSetting
|
|
{
|
|
public class ResourcesController : DefaultController
|
|
{
|
|
/// <summary>
|
|
/// 上传资源,迁移到ComControoller下面了
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
|
|
/// <summary>
|
|
/// 获取文件列表
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpPost, ErrorFilter]
|
|
public WebResult GetList()
|
|
{
|
|
var Keyword = GetString("Keyword");
|
|
var PageIndex = GetInt("PageIndex", true);
|
|
var PageSize = GetInt("PageSize", true);
|
|
var fileType = GetEnum<ResourcesType>("FileType");
|
|
var fileUse = GetEnum<ResourcesUse>("FileUse");
|
|
|
|
if (PageSize > 100) PageSize = 100;
|
|
var tNumber = 0;
|
|
var exp = Expressionable.Create<Resources>();
|
|
if (fileType != ResourcesType.未知)
|
|
{
|
|
exp.And(w => w.FileType == fileType);
|
|
}
|
|
if (fileUse != ResourcesUse.未知)
|
|
{
|
|
exp.And(w => w.FileUse == fileUse);
|
|
}
|
|
if (!string.IsNullOrEmpty(Keyword))
|
|
{
|
|
exp.And(f => f.Filename.Contains(Keyword) || f.Filename.Contains(Keyword));
|
|
}
|
|
var DataList = Db.Queryable<Resources>().Where(exp.ToExpression()).OrderBy(o => o.CreateTime, OrderByType.Desc).ToPageList(PageIndex, PageSize, ref tNumber);
|
|
var res = new PageResult<Resources>(DataList, tNumber, PageSize, PageIndex);
|
|
return PutData(res);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除文件
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpPost, ErrorFilter]
|
|
public WebResult Remove()
|
|
{
|
|
var ids = GetIntList("Ids");
|
|
var items = Db.Queryable<Resources>().In(ids).ToList();
|
|
if (items.Count==0)
|
|
{
|
|
return PutData("删除失败,未发现任何有关文件");
|
|
}
|
|
var db = Db;
|
|
|
|
bool isNotRemove = false;
|
|
db.UseTran(() =>
|
|
{
|
|
foreach (var item in items)
|
|
{
|
|
if (item.FileUse == ResourcesUse.凭证 )
|
|
{
|
|
//如果不是超级管理员
|
|
if (Session == null || !Session.IsCreator)
|
|
{
|
|
//这个不能被删除
|
|
isNotRemove = true;
|
|
return;
|
|
}
|
|
}
|
|
db.Deleteable(item).ExecuteCommand();
|
|
}
|
|
}, ex => throw ex);
|
|
|
|
|
|
if (isNotRemove)
|
|
{
|
|
return PutData("操作失败,凭证文件非管理员不允许删除");
|
|
}
|
|
else
|
|
{
|
|
//删除历史文件
|
|
foreach (var item in items)
|
|
{
|
|
Util.DeleteFile(Util.MapFile(item.FileId,defaultResourcesPath));
|
|
}
|
|
}
|
|
return PutSuccess;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置文件名
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpPost, ErrorFilter]
|
|
public WebResult SetFilename()
|
|
{
|
|
var FileName = GetString("FileName");
|
|
var id = GetLong("Id");
|
|
var item = Db.Queryable<Resources>().Where(w => w.Id == id).First();
|
|
if (item == null)
|
|
{
|
|
return PutData("设置失败,文件不存在");
|
|
}
|
|
item.Filename = FileName;
|
|
Db.Updateable(item).ExecuteCommand();
|
|
return PutSuccess;
|
|
}
|
|
|
|
private const string defaultResourcesPath = "网站\\resources";
|
|
[HttpPost, ErrorFilter]
|
|
public WebResult ReplaceFile()
|
|
{
|
|
var Id = GetInt("Id",true);
|
|
var resources = Db.Queryable<Resources>().Single(f=>f.Id == Id);
|
|
if (resources == null) return PutData("替换失败,该资源不存在或已被删除");
|
|
|
|
//替换只允许修改文件
|
|
var provider = new MultipartMemoryStreamProvider();
|
|
var r = Request.Content.ReadAsMultipartAsync(provider).Result;
|
|
System.Net.Http.StreamContent item = null;
|
|
foreach (var content in r.Contents)
|
|
{
|
|
switch (content.Headers.ContentDisposition.Name.Replace("\"", ""))
|
|
{
|
|
case "File":
|
|
item = content as System.Net.Http.StreamContent;
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
using (var ms = item.ReadAsStreamAsync().Result)
|
|
{
|
|
|
|
var oldFilename = Util.MapFile(resources.FileId, defaultResourcesPath);
|
|
|
|
//重新修改文件ID
|
|
resources.FileId = Guid.NewGuid().ToString("N");
|
|
resources.UpdateTime = DateTime.Now;
|
|
|
|
//以随机数为文件名,防止同名文件被覆盖
|
|
var fileName = Util.MapFile(resources.FileId, defaultResourcesPath);
|
|
try
|
|
{
|
|
using (var filems = File.OpenWrite(fileName))
|
|
{
|
|
ms.CopyTo(filems);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//如果新增失败,删掉无效文件
|
|
Util.DeleteFile(fileName);
|
|
return PutData(ex);
|
|
}
|
|
|
|
Db.Updateable(resources).ExecuteCommand();
|
|
|
|
|
|
//更新成功,删掉老文件
|
|
Util.DeleteFile(oldFilename);
|
|
}
|
|
|
|
return PutData(resources);
|
|
}
|
|
}
|
|
}
|