Пример #1
0
        public async Task <IActionResult> Add([FromBody] UploadImageDTO imageDTO)
        {
            if (ModelState.IsValid)
            {
                string userId = _userRepo.GetUserIdFromEmail(imageDTO.UploaderEmail);

                string fName = imageDTO.FileName;
                string relativeDirectoryPath = "Images/" + userId;
                string fullDirectoryPath     = Path.Combine(_env.ContentRootPath, relativeDirectoryPath);
                Directory.CreateDirectory(fullDirectoryPath);
                byte[] byteArray = Convert.FromBase64String(imageDTO.DataBase64);

                using (var stream = new FileStream(fullDirectoryPath + "/" + imageDTO.FileName, FileMode.Create, FileAccess.Write))
                {
                    if (stream.CanWrite)
                    {
                        await stream.WriteAsync(byteArray, 0, byteArray.Length).ConfigureAwait(false);

                        _imageRepo.Add(relativeDirectoryPath + "/" + imageDTO.FileName, userId);;
                        return(Ok(relativeDirectoryPath + "/" + imageDTO.FileName));
                    }
                }
            }
            return(BadRequest());
        }
        public ResponseModel <UploadImageDTO> UploadCommentImage(UploadImage ImageInfo)
        {
            var result         = new ResponseModel <UploadImageDTO>();
            var uploadImageDTO = new UploadImageDTO();

            try
            {
                result.error_code = Result.SUCCESS;
                if (string.IsNullOrWhiteSpace(ImageInfo.Base64) || string.IsNullOrWhiteSpace(ImageInfo.SuffixType))
                {
                    result.error_code = Result.ERROR;
                    result.message    = "参数无效";
                    return(result);
                }
                //string nnn = "%2f9j%2f4AAQSkZJRgABAQEAYABgAAD%2f2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz%2f2wBDAQICAgMDAwYDAwYMCAcIDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz%2fwAARCABeAF8DASIAAhEBAxEB%2f8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL%2f8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0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PPk1Lx1YwXdmXudR1YNpmuapc%2bGruPS7Wa2nnnW4jWwj8qYLcNcRsksetTi3FRpTaS5oyXTo6c5rrd7Jaau1lq0jzaXhFlM8RBOUlCUb%2fEr3U4xl9myW%2brdle7slc8S%2bKnwv1X4Q61ZWerXHhq8TV7dr7TbvRPEVhrNrqFpvKJco9rNJtjk2kp5gQsAeODjmHru%2f2u9c%2ftv40aH5ektLbw6PGF8U2v%2fCO3ek%2bJWEFvG62OoaJo%2bnQXsNoY%2fs25%2fMKGIoFg2sh4VjkV9fkeNq4rCRrVt7taeTa%2b%2fv59FsfjvGmSYfKs3qYPC35EotX31in92unl1e4FvlrA8Z%2bANL8eW6RalF5qJyBn8a2v%2bWdNZcgV6k4RmuWSuj5zD1alGaqUpNNdUcsnwd0WJUURMUj%2b6pCED%2fx2o4vgh4finkkS3aIzDa%2bzaNw%2fKuuPBprtgCsvqtH%2bVHd%2famMX%2fL1%2fec0fg1of2byDE7xZzsbaR%2f6DVaL4BeF4WJjsERiOSAp757iuwJyaVPvH%2fdpfVKP8qD%2b1calpVl95zOh%2fBPw1oO8w6dHuk%2b8WOM%2fliqlz%2bzp4Uu7oz%2fYNkhOfkbHP5V2h7fQUqNg0PB0GrOC%2b4hZvjoyc1Wld%2bbOUHwY0WO4SXbP5sQAVtwyuOnaov%2bFIeH95ZrZpCxy2%2fad36V2DnLUKcDnn09qf1Wj%2fAComOa4y%2fwDFf3mF4X8AaZ4NlZrCE2%2b8YKA8flW4vJpknD0sb4rWEIwXLFWRhWq1Ksueo7vuz%2f%2fZ";
                //过滤特殊字符即可
                string dummyData = ImageInfo.Base64.Trim().Replace("%", "").Replace(",", "").Replace(" ", "+");
                if (dummyData.Length % 4 > 0)
                {
                    dummyData = dummyData.PadRight(dummyData.Length + 4 - dummyData.Length % 4, '=');
                }
                //上传图片至服务器
                lock (imgLock)
                {
                    //string Base64 = ImageInfo.Base64.Replace(' ', '+');
                    string       path       = string.Format("/UploadImge/" + DateTime.Now.ToString("yyyyMMdd") + "/");
                    string       Base64     = System.Web.HttpUtility.UrlDecode(dummyData, System.Text.Encoding.UTF8); //UrlDecode解码
                    byte[]       imgByte    = Convert.FromBase64String(ImageInfo.Base64);
                    MemoryStream ms         = new MemoryStream(imgByte);
                    Image        image      = System.Drawing.Image.FromStream(ms);
                    string       fileName   = string.Format("{0}." + ImageInfo.SuffixType, DateTime.Now.ToString("yyyyMMddhhmmssffff") + Assistant.GetRandomNumber(6));
                    string       filePath   = string.Format("{0}{1}", path, fileName);
                    string       serverPath = HttpContext.Current.Server.MapPath("~" + path);
                    if (!Directory.Exists(serverPath))
                    {
                        Directory.CreateDirectory(serverPath);
                    }
                    //保存图片
                    image.Save(serverPath + fileName);
                    string    ImageUrl  = WebConfigHelper.GetAppSettingsInfo("ImageUrl");
                    BaseImage baseimage = _baseImageService.Insert(new BaseImage()
                    {
                        Title      = "H5点餐系统评价图片",
                        CreateTime = System.DateTime.Now,
                        Path       = filePath,
                        Source     = ImageUrl
                    });
                    uploadImageDTO.baseImageId = baseimage.BaseImageId;
                    uploadImageDTO.path        = ImageUrl + filePath;
                    result.data = uploadImageDTO;
                }
            }
            catch (Exception ex)
            {
                result.message = ex.Message;
            }
            return(result);
        }
Пример #3
0
        public async Task <DomainStatusCodes> ChangeImageAsync(int personId, UploadImageDTO personImage)
        {
            if (!CheckIfPersonExists(personId, out var existingPerson))
            {
                return(DomainStatusCodes.RecordNotFound);
            }

            var imageName = $"{personId}-person-image.png";
            var fileUrl   = await _imageService.SaveImageAsync(personImage.Image, imageName);

            existingPerson.Image = imageName;

            _personRepository.Update(existingPerson);

            Commit();

            return(DomainStatusCodes.Success);
        }
Пример #4
0
        public async Task <IActionResult> Post([FromForm] UploadImageDTO uploadImageDTO)
        {
            var operationResult = await imageService.UploadImageAsync(uploadImageDTO, User.SubjectId());

            switch (operationResult.Status)
            {
            case OperationStatus.SUCCESS:
                return(Ok(operationResult));

            case OperationStatus.FAIL:
                return(NotFound(operationResult));

            case OperationStatus.ERROR:
                return(StatusCode(StatusCodes.Status500InternalServerError, operationResult));

            default:
                return(Ok(operationResult));
            }
        }
Пример #5
0
        /// <summary>
        /// Upload file to db and filesystem for given user.
        /// </summary>
        /// <param name="uploadImageDTO">Image to be uploaded with wishlist id</param>
        /// <param name="userId">Id of user</param>
        /// <returns>OperationResult success if image saved on filesystem and in db.
        /// <para>Fail if could not find wish for given id. </para><para> Error if some exception occured durning execution.</para></returns>
        public async Task <OperationResult <ImageDTO> > UploadImageAsync(UploadImageDTO uploadImageDTO, string userId)
        {
            Guard.IsNull(uploadImageDTO, nameof(uploadImageDTO));
            Guard.IsNullEmptyOrWhiteSpace(userId, nameof(userId));

            var wish = await dbContext.Wishes
                       .Include(w => w.WishList)
                       .ThenInclude(wl => wl.User)
                       .Include(w => w.Image)
                       .FirstOrDefaultAsync(w => w.Id == uploadImageDTO.WishId && w.WishList.User.Auth0Id == userId);

            if (wish == null)
            {
                return new OperationResult <ImageDTO>()
                       {
                           Status  = OperationStatus.FAIL,
                           Message = MessageHelper.CreateEntityNotFoundMessage(nameof(Wish), uploadImageDTO.WishId),
                       }
            }
            ;

            try
            {
                var fileName = await filesService.StoreImageAsync(uploadImageDTO.ImageFile, userId, wish.WishList.DirectoryName);

                var fileToDelete = string.Empty;

                if (wish.Image == null)
                {
                    wish.Image = new Image()
                    {
                        FileName = fileName
                    };
                }
                else
                {
                    fileToDelete        = wish.Image.FileName;
                    wish.Image.FileName = fileName;
                }

                await dbContext.SaveChangesAsync();

                //Delete old file
                if (!string.IsNullOrWhiteSpace(fileToDelete))
                {
                    filesService.DeleteImage(userId, wish.WishList.DirectoryName, fileToDelete);
                }
            }
            catch (Exception ex)
            {
                var operationFailResult = new OperationResult <ImageDTO>()
                {
                    Status  = OperationStatus.ERROR,
                    Message = MessageHelper.CreateOperationErrorMessage(nameof(Image), OperationType.create)
                };

                if (ex is FileServiceException)
                {
                    return(operationFailResult);
                }
                else if (ex is DbUpdateException || ex is DbUpdateConcurrencyException)
                {
                    //File was saved on filesystem so do cleanup and delete that file

                    filesService.DeleteImage(userId, wish.WishList.DirectoryName, wish.Image.FileName);

                    return(operationFailResult);
                }

                throw;
            }

            return(new OperationResult <ImageDTO>()
            {
                Status = OperationStatus.SUCCESS,
                Message = MessageHelper.CreateOperationSuccessMessage(nameof(Image), OperationType.create),
                Data = new ImageDTO()
                {
                    Id = wish.Image.Id, WishId = wish.Id
                }
            });
        }
Пример #6
0
        public async Task <IActionResult> UploadImageAsync(int personId, [FromForm] UploadImageDTO personImage)
        {
            var statusCode = await _personService.ChangeImageAsync(personId, personImage);

            return(Response(statusCode));
        }