Пример #1
0
        public async Task <BaseResponse> AddTypeUpdateFile(int typeId, TypeUpdateFileAddViewModel req, string account, string url)
        {
            //验证类型是否可以添加
            var t = await _tr.FindAsync(typeId);

            if (t.Status == TypeStatus.Root)
            {
                return(new BaseResponse {
                    Success = false, Message = "目录节点类型不能添加具体数据"
                });
            }
            //是否存在重名
            var ret = await _tu.Find(a => a.TypeId == typeId && a.Name == req.Name).FirstOrDefaultAsync();

            if (ret != null)
            {
                return(new BaseResponse {
                    Success = false, Message = "已存在相同名称的升级文件"
                });
            }
            try
            {
                var dto = _mapper.Map <TypeUpdateFileModel>(req);
                dto.Create = account;
                dto.Url    = url;
                dto.TypeId = typeId;
                await _tu.AddAsync(dto);

                _log.LogInformation($"{account}添加Id为{dto.Id}名称为{dto.Name}的升级文件成功");
                return(new HandleResponse <int> {
                    Success = true, Message = "添加升级文件成功", Key = dto.Id
                });
            }
            catch (Exception ex)
            {
                _log.LogError($"{account}添加升级文件{req.Name}失败,失败原因:{ex.Message}->{ex.StackTrace}->{ex.InnerException}");
                return(new BaseResponse {
                    Success = false, Message = "添加失败文件失败,请联系管理员"
                });
            }
        }
Пример #2
0
        public async Task <ActionResult <BaseResponse> > AddTypeUpdateFile(int typeId, [FromForm] TypeUpdateFileAddViewModel req)
        {
            //string user = User.Identity.Name;
            //if (string.IsNullOrWhiteSpace(user))
            //{
            //    return Unauthorized("用户凭证缺失");
            //}
            //UserMessage um = JsonConvert.DeserializeObject<UserMessage>(user);
            //string GroupId;
            //int status;
            //var ret = _ts.IsExist(typeId, out GroupId, out status);
            //if (status == 0)
            //{
            //    return new BaseResponse { Success = false, Message = "目录节点类型不能添加具体数据" };
            //}
            //if (!ret)
            //{
            //    return new BaseResponse { Success = false, Message = "输入的类型不存在" };
            //}
            //if (!(um.IsAdmin && (um.GroupId == GroupId || um.Code == _config["Group"])))
            //{
            //    return Unauthorized("用户没有权限");
            //}
            string Account = User.Claims.FirstOrDefault(a => a.Type == "Account").Value;
            string GroupId = User.Claims.FirstOrDefault(a => a.Type == "GroupId").Value;
            //文件后缀
            var fileExtension = Path.GetExtension(req.file.FileName);
            //判断后缀是否是图片
            const string fileFilt = ".bin";

            if (fileExtension == null)
            {
                return(new BaseResponse {
                    Success = false, Message = "上传的文件没有后缀"
                });
            }
            if (fileFilt.IndexOf(fileExtension.ToLower(), StringComparison.Ordinal) <= -1)
            {
                return(new BaseResponse {
                    Success = false, Message = "请上传后缀名为bin的升级文件"
                });
            }
            //判断文件大小
            long length = req.file.Length;

            if (length > 1024 * 1024 * 5) //2M
            {
                return(new BaseResponse {
                    Success = false, Message = "上传的文件不能大于5M"
                });
            }
            //类型图片保存的相对路径:Files+组织编号+TypeFiles+TypeId+文件名称
            string webRootPath = _webHostEnvironment.WebRootPath;                      //wwwroot文件夹
            //string contentRootPath = _webHostEnvironment.ContentRootPath;//根目录
            string ext      = DateTime.Now.ToString("yyyyMMddhhmmss") + fileExtension; //头像名称修改为用户编号加后缀名
            string userPath = Path.Combine(GroupId, "TypeFiles", typeId.ToString());   //用户头像保存位置

            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 _tu.AddTypeUpdateFile(typeId, req, Account, path);

                //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 = "上传类型更新文件失败,请联系管理员处理"
                });
            }
        }