private async Task MergeFileAsync(string rootDir, string fileName, string tempDir) { List <string> allowPicSuffixAr = new List <string> { ".jpg", ".png", ".jpeg", ".gif", ".bmp" }; List <string> allowViewSuffixAr = new List <string> { ".mp4", ".mkv", ".mov", ".m4v", ".wmv", ".avi", ".flv" }; long bytesLength = 0; string suffix = Path.GetExtension(fileName); int isImage = 1; string fileAppSettingPath = ""; if (allowViewSuffixAr.IndexOf(suffix) != -1) { isImage = 0; fileAppSettingPath = Appsettings.app(new string[] { "UploadFilePath", "VideoPath" }); } if (allowPicSuffixAr.IndexOf(suffix) != -1) { isImage = 1; fileAppSettingPath = Appsettings.app(new string[] { "UploadFilePath", "PicPath" }); } var yearDir = DateTime.Now.ToString("yyyy"); var monthDir = DateTime.Now.ToString("MM"); var dayDir = DateTime.Now.ToString("dd"); string envPath = Path.Combine(rootDir, fileAppSettingPath, yearDir, monthDir, dayDir); var dir = tempDir; var files = Directory.GetFiles(dir); var finalDir = envPath; if (!Directory.Exists(finalDir)) { Directory.CreateDirectory(finalDir); } fileName = "$" + fileName; var finalPath = Path.Combine(finalDir, fileName); using (var fs = new FileStream(finalPath, FileMode.Create)) { var fileParts = files.OrderBy(x => x.Length).ThenBy(x => x); foreach (var part in fileParts) { var bytes = await System.IO.File.ReadAllBytesAsync(part); bytesLength += bytes.Length; await fs.WriteAsync(bytes, 0, bytes.Length); bytes = null; System.IO.File.Delete(part); } await fs.FlushAsync(); Image img = Image.FromStream(fs); fs.Close(); Directory.Delete(dir); FileProxyDto dto = new FileProxyDto(); if (allowViewSuffixAr.IndexOf(suffix) != -1) { //vidoe } if (allowPicSuffixAr.IndexOf(suffix) != -1) { //pic //dto.dpi = img.Width + "*" + img.Height; //using (var graphic = Graphics.FromImage(img)) //{ // var font = new Font(FontFamily.GenericSansSerif, 800, FontStyle.Bold, GraphicsUnit.Pixel); // var color = Color.FromArgb(128, 255, 255, 255); // var brush = new SolidBrush(color); // var point = new Point((img.Width / 2) - ((img.Width / 2) / 2) - 20, (img.Height / 2) - ((img.Height / 2) / 2)); // graphic.DrawString("", font, brush, point); //} //finalPath = finalPath.Replace(fileName, "$" + fileName); //img.Save(finalPath); } dto.status = new StatusProxyDto(); dto.name = fileName; dto.size = bytesLength + ""; dto.classifyName = ""; dto.isImage = isImage; dto.ext = fileName.Substring(fileName.LastIndexOf(".") + 1); dto.statusKey = "cms.goods.init"; dto.dpi = img.Width + "*" + img.Height; dto.url = "/" + finalPath.Substring(finalPath.IndexOf("uploadFiles")).Replace("\\", "/"); RestRequest request = new RestRequest("/MIS/CMS/MemberAction/Upload", Method.POST); string token = _accessor.HttpContext.Request.Headers["Authorization"]; token = token.Replace("Bearer ", ""); dto.memberId = AuthHelper.GetClaimFromToken(token).Id; dto.memberName = dto.ownerName = AuthHelper.GetClaimFromToken(token).Name; _logger.LogInformation(dto.memberId + "||" + dto.memberName); _client.AddDefaultHeader("Authorization", "Bearer " + token); request.AddJsonBody(dto); var res = await _client.ExecuteAsync(request); } }
public async Task <ApiResponse <SingleFileResponseDto> > Upload_HeaderLogo_Single_File(IFormFile file, [FromServices] IWebHostEnvironment environment) { ApiResponse <SingleFileResponseDto> response = new ApiResponse <SingleFileResponseDto>(); string suffix = Path.GetExtension(file.FileName); List <string> allowPicSuffixAr = new List <string> { ".jpg", ".png", ".jpeg", ".gif", ".bmp" }; List <string> allowViewSuffixAr = new List <string> { ".mp4", ".mkv", ".mov", ".m4v", ".wmv", ".avi", ".flv" }; string fileAppSettingPath = ""; if (allowViewSuffixAr.IndexOf(suffix) != -1) { fileAppSettingPath = Appsettings.app(new string[] { "UploadFilePath", "VideoPath" }); } if (allowPicSuffixAr.IndexOf(suffix) != -1) { fileAppSettingPath = Appsettings.app(new string[] { "UploadFilePath", "PicPath" }); } var yearDir = DateTime.Now.ToString("yyyy"); var monthDir = DateTime.Now.ToString("MM"); var dayDir = DateTime.Now.ToString("dd"); var uploadFIle = file; string returnToRelativePath = Path.Combine(fileAppSettingPath, yearDir, monthDir, dayDir).Replace("\\", "/"); string uploadsFolder = Path.Combine(environment.WebRootPath, fileAppSettingPath, yearDir, monthDir, dayDir).Replace("\\", "/"); string uniqueFileName = Guid.NewGuid().ToString() + "_" + uploadFIle.FileName; if (!Directory.Exists(uploadsFolder)) { Directory.CreateDirectory(uploadsFolder); } FileProxyDto dto = new FileProxyDto(); var filePathWithFileName = Path.Combine(uploadsFolder, uniqueFileName); using (var stream = new FileStream(filePathWithFileName, FileMode.Create)) { await file.CopyToAsync(stream); } string dpi = ""; if (allowViewSuffixAr.IndexOf(suffix) != -1) { //vidoe } if (allowPicSuffixAr.IndexOf(suffix) != -1) { //pic Image img = Image.FromFile(filePathWithFileName); dpi = img.Width + "*" + img.Height; } //dto.status = new StatusProxyDto(); //dto.name = filePathWithFileName.Substring(filePathWithFileName.LastIndexOf("\\")+1); //dto.size = file.Length + ""; //dto.classifyName = ""; //dto.isImage = 1; //dto.ext = suffix; //dto.statusKey = "cms.goods.init"; //dto.url = "/" + filePathWithFileName.Substring(filePathWithFileName.IndexOf("uploadFiles")).Replace("\\", "/"); //RestRequest request = new RestRequest("/MIS/CMS/MemberAction/Upload", Method.POST); //string token = _accessor.HttpContext.Request.Headers["Authorization"]; //token = token.Replace("Bearer ", ""); //dto.memberId = AuthHelper.GetClaimFromToken(token).Id; //dto.memberName = dto.ownerName = AuthHelper.GetClaimFromToken(token).Name; //_logger.LogInformation(dto.memberId + "||" + dto.memberName); //_client.AddDefaultHeader("Authorization", "Bearer " + token); //request.AddJsonBody(dto); //var res = await _client.ExecuteAsync(request); response.Data = new SingleFileResponseDto { RelativePath = Appsettings.app(new string[] { "UploadFilePath", "Uri" }) + returnToRelativePath + "/" + uniqueFileName, FileName = uploadFIle.FileName, Dpi = dpi }; return(response); }