using Gather.Common.Util; using GatherAPI.api.back; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Polly; namespace GatherAPI.Controllers.back { [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; } /// /// 分片上传文件接口 /// /// 请求参数 /// [Authorize] [HttpPost] public async Task 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) { Util.LogHelper.WriteLog(ex.Message + ",行号:" + ex.StackTrace); return Json(Tool.GetJsonWithCode(APICode.Fail, "发生错误,请联系管理员。")); } } /// /// 合并文件 /// /// /// 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 lstFile = new List(); 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)) { await Policy.Handle() .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); } } } }