示例#1
0
        public async Task <MRImage> Upload(IFormFile file)
        {
            if (!_isAuthorized)
            {
                throw new MRException <MRImage>((int)ExceptionCode.ACCESS_DENIED, "Access denied");
            }

            if (file == null)
            {
                throw new MRException <MRImage>((int)ExceptionCode.BAD_REQUEST, "Bad request");
            }

            if (!ALLOWED_TYPES.Contains(file.ContentType))
            {
                throw new MRException <MRImage>((int)ExceptionCode.BAD_REQUEST, "Not allowed");
            }

            var content = new byte[file.Length];

            using (var reader = file.OpenReadStream())
            {
                await reader.ReadAsync(content, 0, content.Length);
            }

            var response = new MRImage
            {
                CreatedBy   = _currentUser.Id,
                ContentType = file.ContentType,
            };

            using (var image = Image.FromStream(file.OpenReadStream()))
            {
                response.Height = image.Height;
                response.Width  = image.Width;
            }

            var bucketResponse = await _bucketImageD.UploadWithName(file.FileName, content, file.ContentType, new Dictionary <string, string>
            {
                { nameof(MRImage.Height).ToUpperInvariant(), response.Height.ToString() },
                { nameof(MRImage.Width).ToUpperInvariant(), response.Width.ToString() },
                { nameof(MRImage.CreatedBy).ToUpperInvariant(), response.CreatedBy.ToString() },
            });

            response.Key = bucketResponse.Key;
            response.Url = bucketResponse.Url;

            return(response);
        }
        public async Task <MRImage> UpdateAvatar(MRImage image)
        {
            var rImage = await CopyToRBucket(image.Key);

            await _bucketD.Delete(image.Key);

            var currentUser = await _repositoryUser.Get(_userId);

            currentUser.Image = new MRApiCommon.Infrastructure.IdentityExtensions.Components.MRUserImage
            {
                Key = rImage.Key,
                Url = rImage.Url
            };

            await _repositoryUser.Replace(currentUser);

            return(rImage);
        }
        protected async Task <string> UpdateEntityImage(MRImage oldImage, MRImage newImage)
        {
            string result = null;

            if (oldImage != null && string.IsNullOrWhiteSpace(newImage?.Key))
            {
                result = oldImage.Key;
            }
            else if (oldImage == null && !string.IsNullOrWhiteSpace(newImage?.Key))
            {
                await CopyToRBucket(newImage.Key);
            }
            else if (oldImage != null && !string.IsNullOrWhiteSpace(newImage?.Key) && !oldImage.Key.Equals(newImage.Key))
            {
                result = oldImage.Key;
                await CopyToRBucket(newImage.Key);
            }

            return(result);
        }
 public async Task <IActionResult> UpdateAvatar([FromBody] MRImage image)
 {
     return(Json(await _managerUserControl.UpdateAvatar(image)));
 }