Ejemplo n.º 1
0
        public async Task <BaseResponse> AddDeviceImageAsync(string account, DeviceImageAddDto req, string deviceSn, string path)
        {
            //验证图片名称是否重复
            var ret = await _dir.Find(a => a.DeviceSn == deviceSn && a.ImageName == req.ImageName).FirstOrDefaultAsync();

            if (ret != null)
            {
                return(new BaseResponse {
                    Success = false, Message = "该设备下已存在相同名称的图片"
                });
            }
            try
            {
                var dto = _mapper.Map <DeviceImageModel>(req);
                dto.DeviceSn = deviceSn;
                dto.url      = path;
                dto.Create   = account;
                await _dir.AddAsync(dto);

                _log.LogInformation($"{account}添加设备图片{dto.Id},名称为{dto.ImageName}成功");
                return(new HandleResponse <int> {
                    Success = true, Message = "添加设备图片成功", Key = dto.Id
                });
            }
            catch (Exception ex)
            {
                _log.LogError($"{account}添加设备图片失败,失败原因:{ex.Message}->{ex.StackTrace}->{ex.InnerException}");
                return(new BaseResponse {
                    Success = false, Message = "添加设备图片失败"
                });
            }
        }
Ejemplo n.º 2
0
        public async Task <ActionResult <BaseResponse> > AddDeviceImage(string GroupId, string DeviceSn, [FromForm] DeviceImageAddDto req)
        {
            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 = ".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 * 5) //5M
            {
                return(new BaseResponse {
                    Success = false, Message = "上传的文件不能大于5M"
                });
            }
            //类型图片保存的相对路径:Image+组织编号+DeviceImage+DeviceSn+图片名称
            string webRootPath = _webHostEnvironment.WebRootPath;                      //wwwroot文件夹
            //string contentRootPath = _webHostEnvironment.ContentRootPath;//根目录
            string ext      = DateTime.Now.ToString("yyyyMMddhhmmss") + fileExtension; //图片修改为时间加后缀名
            string userPath = Path.Combine(groupId, "DeviceImage", DeviceSn);          //设备图片保存位置

            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 _dis.AddDeviceImageAsync(account, req, DeviceSn, 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);
                }
                return(new BaseResponse {
                    Success = false, Message = "上传类型图片失败"
                });
            }
        }