Beispiel #1
0
        public async Task <BaseResponse> AddProjectImageAsync(ProjectImageAddDto req, string url, string account, int projectId)
        {
            try
            {
                var entity = _mapper.Map <ProjectImageModel>(req);
                entity.Create    = account;
                entity.url       = url;
                entity.ProjectId = projectId;
                await _pi.AddAsync(entity);

                _log.LogInformation($"{account}添加图片成功,图片标示为{entity.Id}");
                return(new BResponse <int> {
                    Success = true, Message = "添加图片成功", Data = entity.Id
                });
            }
            catch (Exception ex)
            {
                _log.LogError($"{account}添加图片失败,失败原因:{ex.Message}->{ex.StackTrace}->{ex.InnerException}");
                return(new BaseResponse {
                    Success = false, Message = "添加图片失败,请联系管理员"
                });
            }
        }
Beispiel #2
0
        public async Task <ActionResult <BaseResponse> > AddProjectImage(string GroupId, int projectId, [FromForm] ProjectImageAddDto req)
        {
            var    GId     = 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;
            string Roles   = User.Claims.FirstOrDefault(a => a.Type == "Role").Value;

            #region 验证用户权限
            var pathId = await _ps.GetPathId(projectId);

            if (pathId == null)
            {
                return(new NotFoundResult());
            }
            else
            {
                pathId = $"{pathId}/{projectId}";
            }
            if (GId != GroupId)
            {
                if (!(isAdmin && Code == _config["Group"]))
                {
                    return(new ContentResult {
                        Content = "用户没有权限", ContentType = "text/plain", StatusCode = 401
                    });
                }
            }
            else
            {
                if (!isAdmin)
                {
                    var bAccess = await _rps.IsAuth(Roles, pathId, 2);

                    if (!bAccess)
                    {
                        return(new ContentResult {
                            Content = "用户没有权限", ContentType = "text/plain", StatusCode = 401
                        });
                    }
                }
            }
            #endregion

            //文件后缀
            var fileExtension = Path.GetExtension(req.file.FileName);
            //判断后缀是否是图片
            const string fileFilt = ".gif|.jpg|.jpeg|.png";
            if (fileExtension == null)
            {
                return(new BaseResponse {
                    Success = false, Message = "上传的文件没有后缀"
                });
            }
            if (fileFilt.IndexOf(fileExtension.ToLower(), StringComparison.Ordinal) <= -1)
            {
                return(new BaseResponse {
                    Success = false, Message = "请上传jpg、png、gif格式的图片"
                });
            }
            //判断文件大小
            long length = req.file.Length;
            if (length > 1024 * 1024 * 2) //2M
            {
                return(new BaseResponse {
                    Success = false, Message = "上传的文件不能大于2M"
                });
            }
            //类型图片保存的相对路径:Image+组织编号+TypeImage+TypeId+图片名称
            string webRootPath = _webHostEnvironment.WebRootPath;                          //wwwroot文件夹
            //string contentRootPath = _webHostEnvironment.ContentRootPath;//根目录
            string ext      = DateTime.Now.ToString("yyyyMMddhhmmss") + fileExtension;     //图片名称修改为日期加后缀名
            string userPath = Path.Combine(GroupId, "ProjectImage", projectId.ToString()); //图片保存位置
            userPath = Path.Combine(_config["StoredImagesPath"], 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 _pis.AddProjectImageAsync(req, path, Account, projectId);

                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 = "上传项目图片失败"
                });
            }
        }