CompetitionAPI_dotnet/CompetitionAPI/Controllers/back/study/UploadController.cs

134 lines
5.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Competition.Common.Util;
using CompetitionAPI.api.back;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.POIFS.Crypt.Dsig;
using Polly;
using CompetitionAPI.Util;
namespace CompetitionAPI.Controllers.back.study
{
[Route("api/[controller]")]
[ApiController]
public class UploadController : Controller
{
private readonly IWebHostEnvironment _webHostEnvironment;
public IConfiguration Configuration { get; }
public UploadController(IWebHostEnvironment webHostEnvironment, IConfiguration configuration)
{
_webHostEnvironment = webHostEnvironment;
Configuration = configuration;
}
/// <summary>
/// 分片上传文件接口
/// </summary>
/// <param name="file">请求参数</param>
/// <returns></returns>
[Authorize]
[HttpPost]
[APIFilter]
public async Task<JsonResult> RuleUploadFile([FromQuery] SliceFileInfo file)
{
try
{
string path = System.IO.Path.Combine(_webHostEnvironment.WebRootPath, "Upload", file.FolderName);
var files = Request.Form.Files;
var buffer = new byte[file.Size];
var fileName = file.Name.Replace("(", "").Replace(")", "").Replace("", "").Replace("", "");
path = path + "//" + fileName + "//";
if (!System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
string filepath = path + "//" + file.Name.Replace("(", "").Replace(")", "").Replace("", "").Replace("", "") + "^" + file.Number;
using (var stream = new FileStream(filepath, FileMode.Append))
{
await files[0].CopyToAsync(stream);
}
var filesList = Directory.GetFiles(Path.GetDirectoryName(path));
//当顺序号等于分片总数量 合并文件
if ((file.Number + 1) == file.Count || filesList.Length == file.Count)
{
await MergeFile(file);
return Json(Tool.GetJsonWithCode(APICode.Success, "/Upload/" + file.FolderName + "/" + file.Name.Replace("(", "").Replace(")", "").Replace("", "").Replace("", "") + "/" + fileName.Split("~")[0].ToString()));
}
else
{
return Json(Tool.GetJsonWithCode(APICode.Success, "成功"));
}
}
catch (Exception ex)
{
LogHelper.WriteLog(ex.Message + ",行号:" + ex.StackTrace);
return Json(Tool.GetJsonWithCode(APICode.Fail, "发生错误,请联系管理员。"));
}
}
/// <summary>
/// 合并文件
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
private async Task MergeFile(SliceFileInfo file)
{
string path = System.IO.Path.Combine(_webHostEnvironment.WebRootPath, "Upload", file.FolderName);
var fileName = file.Name.Replace("(", "").Replace(")", "").Replace("", "").Replace("", "");
path = path + "//" + fileName + "//";
string baseFileName = path + fileName.Split("~")[0].ToString();
if (!System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
var filesList = Directory.GetFiles(Path.GetDirectoryName(path));
if (filesList.Length != file.Count)
{
return;
}
List<FileSort> lstFile = new List<FileSort>();
foreach (var item in filesList)
{
lstFile.Add(new FileSort()
{
Name = item,
NumBer = Convert.ToInt32(item.Substring(item.IndexOf('^') + 1))
});
}
lstFile = lstFile.OrderBy(x => x.NumBer).ToList();
using (var fileStream = new FileStream(baseFileName, FileMode.Create))
{
//foreach (var fileSort in filesList)
//{
// using (FileStream fileChunk = new FileStream(fileSort, FileMode.Open))
// {
// await fileChunk.CopyToAsync(fileStream);
// }
//}
await Policy.Handle<IOException>()
.RetryForeverAsync()
.ExecuteAsync(async () =>
{
foreach (var fileSort in lstFile)
{
using (FileStream fileChunk = new FileStream(fileSort.Name, FileMode.Open))
{
await fileChunk.CopyToAsync(fileStream);
}
}
});
}
//删除分片文件
foreach (var dirfile in filesList)
{
System.IO.File.Delete(dirfile);
}
}
}
}