public async Task <BaseResponse> AddAppVersionAsync(string account, string path, AppVersionAddDto req) { //验证版本号是否相同 var app = await _avr.Find(a => a.VersionNo == req.VersionNo).FirstOrDefaultAsync(); if (app != null) { return(new BaseResponse { Success = false, Message = "已存在相同版本号的升级文件" }); } try { var entity = _mapper.Map <AppVersionModel>(req); entity.Create = account; entity.Address = path; await _avr.AddAsync(entity); _log.LogInformation($"{account}添加标示为{entity.Id}升级文件成功"); return(new HandleResponse <int> { Success = true, Message = "添加文件成功", Key = entity.Id }); } catch (Exception ex) { _log.LogError($"{account}添加升级文件{req.VersionNo}失败,失败原因:{ex.Message}->{ex.StackTrace}->{ex.InnerException}"); return(new BaseResponse { Success = false, Message = "添加失败文件失败" }); } }
public async Task <ActionResult <BaseResponse> > AddAppVersion([FromForm] AppVersionAddDto req) { //超级管理员有权限 var GroupId = User.Claims.FirstOrDefault(a => a.Type == "GroupId").Value; var isAdmin = User.Claims.FirstOrDefault(a => a.Type == "IsAdmin").Value.ToLower() == "true" ? true : false; string Code = User.Claims.FirstOrDefault(a => a.Type == "Code").Value; string Account = User.Claims.FirstOrDefault(a => a.Type == "Account").Value; if (!(isAdmin && Code == _config["Group"])) { return(Unauthorized("用户没有权限添加升级文件")); } //文件后缀 var fileExtension = Path.GetExtension(req.file.FileName); //暂时只支持apk文件 const string fileFilt = ".apk"; if (fileExtension == null) { return(new BaseResponse { Success = false, Message = "上传的文件没有后缀" }); } if (fileFilt.IndexOf(fileExtension.ToLower(), StringComparison.Ordinal) <= -1) { return(new BaseResponse { Success = false, Message = "请上传后缀名为apk" + "的升级文件" }); } //判断文件大小 long length = req.file.Length; if (length > 1024 * 1024 * 50) //50M { return(new BaseResponse { Success = false, Message = "上传的文件不能大于30M" }); } //类型图片保存的相对路径:Files+组织编号+TypeFiles+TypeId+文件名称 string webRootPath = _webHostEnvironment.WebRootPath; //wwwroot文件夹 string ext = DateTime.Now.ToString("yyyyMMddhhmmss") + fileExtension; //头像名称修改为用户编号加后缀名 string userPath = Path.Combine(GroupId, "appFiles"); //保存位置 userPath = Path.Combine(_config["StoredFilesPath"], userPath); string path = Path.Combine(userPath, ext); //文件保存地址(相对路径) var filePath = Path.Combine(webRootPath, userPath); //物理路径,不包含文件名称 //如果路径不存在,创建路径 if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } filePath = Path.Combine(filePath, ext);//文件的物理路径 try { using (var stream = System.IO.File.Create(filePath)) { await req.file.CopyToAsync(stream); } var br = await _app.AddAppVersionAsync(Account, path, req); //br = await _us.UpdateUserImageAsync(um.Id, um.Account, path); if (!br.Success) { //删除已存在的logo if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } } return(br); } catch (Exception ex) { //删除已存在的logo if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } _log.LogError($"{Account}上传升级文件失败,失败原因:{ex.Message}->{ex.StackTrace}->{ex.InnerException}"); return(new BaseResponse { Success = false, Message = "上传升级文件失败" }); } }