同步操作将从 李玉宝/OpenAuth.Core 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
using System;using System.Collections.Generic;using System.IO;using Infrastructure;using Microsoft.AspNetCore.Http;using Microsoft.Extensions.Logging;using Microsoft.Extensions.Options;using OpenAuth.Repository.Domain;using OpenAuth.Repository.Interface;namespace OpenAuth.App{/// <summary>/// 文件/// </summary>public class FileApp : BaseApp<UploadFile>{private ILogger<FileApp> _logger;private string _filePath;private string _dbFilePath; //数据库中的文件路径private string _dbThumbnail; //数据库中的缩略图路径public FileApp( IOptions<AppSetting> setOptions, IUnitWork unitWork, IRepository<UploadFile> repository, ILogger<FileApp> logger):base(unitWork, repository){_logger = logger;_filePath = setOptions.Value.UploadPath;if (string.IsNullOrEmpty(_filePath)){_filePath = AppContext.BaseDirectory;}}public List<UploadFile> Add(IFormFileCollection files){var result = new List<UploadFile>();foreach (var file in files){result.Add(Add(file));}return result;}public UploadFile Add(IFormFile file){if (file != null){_logger.LogInformation("收到新文件: " + file.FileName);_logger.LogInformation("收到新文件: " + file.Length);}else{_logger.LogWarning("收到新文件为空");}if (file != null && file.Length > 0 && file.Length < 10485760){using (var binaryReader = new BinaryReader(file.OpenReadStream())){var fileName = Path.GetFileName(file.FileName);var data = binaryReader.ReadBytes((int)file.Length);UploadFile(fileName, data);var filedb = new UploadFile{FilePath = _dbFilePath,Thumbnail = _dbThumbnail,FileName = fileName,FileSize = file.Length,FileType = Path.GetExtension(fileName),Extension = Path.GetExtension(fileName)};Repository.Add(filedb);return filedb;}}else{throw new Exception("文件过大");}}private void UploadFile(string fileName, byte[] fileBuffers){string folder = DateTime.Now.ToString("yyyyMMdd");//判断文件是否为空if (string.IsNullOrEmpty(fileName)){throw new Exception("文件名不能为空");}//判断文件是否为空if (fileBuffers.Length < 1){throw new Exception("文件不能为空");}var uploadPath = _filePath + "/" + folder + "/";_logger.LogInformation("文件写入:" + uploadPath);if (!Directory.Exists(uploadPath)){Directory.CreateDirectory(uploadPath);}var ext = Path.GetExtension(fileName).ToLower();string newName = GenerateId.GenerateOrderNumber() + ext;using (var fs = new FileStream(uploadPath + newName, FileMode.Create)){fs.Write(fileBuffers, 0, fileBuffers.Length);fs.Close();//生成缩略图if (ext.Contains(".jpg") || ext.Contains(".jpeg") || ext.Contains(".png") || ext.Contains(".bmp") || ext.Contains(".gif")){string thumbnailName = GenerateId.GenerateOrderNumber() + ext;ImgHelper.MakeThumbnail(uploadPath + newName, uploadPath + thumbnailName);_dbThumbnail = folder + "/" + thumbnailName;}_dbFilePath = folder + "/" + newName;}}}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。