Exemple #1
0
        async Task <IActionResult> SaveImage(string imagePath, Size size)
        {
            if (!Request.Form.Files.Any())
            {
                _logger.LogWarning("Tried to upload image with no body/image attached");
                return(BadRequest("No Image supplied"));
            }

            // Ensure it's a valid extension
            var extension = Path.GetExtension(Request.Form.Files[0].FileName).ToLower();

            if (!(new[] { ".jpg", ".png", ".jpeg" }.Any(s => extension == s)))
            {
                return(BadRequest("File must be .jpg or .png"));
            }

            // Get Path to the speaker directory
            var path = Path.Combine("img", imagePath, Request.Form.Files[0].FileName).Replace("\\", "/").ToLower();

            using (var newStream = ResizeImage(Request.Form.Files[0].OpenReadStream(), size))
            {
                // Write It
                newStream.Position = 0;
                var imageUrl = await _imageService.StoreImage(path, newStream.ToArray());

                return(Created(imageUrl, new { succeeded = true }));
            }
        }
Exemple #2
0
            public async Task <ApiResponse <PostDto> > Handle(Command command, CancellationToken cancellationToken)
            {
                var post = context.Posts.SingleOrDefault(x => x.Id == command.Post.Id);

                if (post == null)
                {
                    return(ApiResponse <PostDto> .NotFound($"Unable to find article with id '{command.Post.Id}'"));
                }

                if (await context.Posts.AnyAsync(x => x.Slug == command.Post.Slug && x.Id != command.Post.Id, cancellationToken))
                {
                    return(ApiResponse <PostDto> .BadRequest("A post with that slug already exists"));
                }

                mapper.Map(command.Post, post);

                if (!string.IsNullOrWhiteSpace(command.Post.HeaderImage))
                {
                    post.HeaderImage = await imageStorageService.StoreImage(command.Post.HeaderImage, command.Post.Title, cancellationToken);
                }

                await context.SaveChangesAsync(cancellationToken);

                return(ApiResponse <PostDto> .Ok(mapper.Map <PostDto>(post)));
            }
Exemple #3
0
            public async Task <ApiResponse <ImageUploadedResponseDto> > Handle(Command command, CancellationToken cancellationToken)
            {
                var imageUrl = await imageStorageService.StoreImage(command.Content, command.FileName, cancellationToken);

                await context.File.AddAsync(new File { FileName = imageUrl }, cancellationToken);

                await context.SaveChangesAsync(cancellationToken);

                return(ApiResponse <ImageUploadedResponseDto> .Ok(new ImageUploadedResponseDto(imageUrl)));
            }
        public async Task <ProfileImage> UploadProfileImage(string profileId, string fileName, byte[] image)
        {
            var blobUrl = await _imageStorageService.StoreImage(fileName, image);

            var entity = new ProfileImageEntity
            {
                ImageUrl    = blobUrl,
                LastUpdated = DateTime.UtcNow,
                ProfileId   = profileId
            };

            var updatedEntity = await _profileImagesRepository.UpsertImage(entity);

            var contract = ProfileImageMapper.ToContract(updatedEntity);

            return(contract);
        }
    public async Task<MediaObjectInfo> NewMediaObjectAsync(string blogid, string username, string password, MediaObject mediaObject)
    {
      await EnsureUser(username, password);

      var bits = Convert.FromBase64String(mediaObject.bits);
      var op = _imageService.StoreImage(mediaObject.name, bits);

      op.Wait();
      if (!op.IsCompletedSuccessfully) throw op.Exception;
      var url = op.Result;

      // Create the response
      MediaObjectInfo objectInfo = new MediaObjectInfo();
      objectInfo.url = url;

      return objectInfo;
    }
        public async Task <IActionResult> UploadImages(List <IFormFile> formFiles)
        {
            var tasks = new List <Task <string> >();

            foreach (var file in formFiles)
            {
                using (var stream = new MemoryStream())
                {
                    await file.CopyToAsync(stream);

                    tasks.Add(imageServiceStorage.StoreImage(file.FileName, stream.GetBuffer()));
                }
            }

            await Task.WhenAll(tasks);

            return(Redirect("Index"));
        }
Exemple #7
0
            public async Task <ApiResponse <PostDto> > Handle(Command command, CancellationToken cancellationToken)
            {
                var post = mapper.Map <CreatePostDto, Post>(command.Post);

                if (await context.Posts.AnyAsync(x => x.Slug == command.Post.Slug, cancellationToken))
                {
                    return(ApiResponse <PostDto> .BadRequest("A post with that slug already exists"));
                }

                if (!string.IsNullOrWhiteSpace(command.Post.HeaderImage))
                {
                    post.HeaderImage = await imageStorageService.StoreImage(command.Post.HeaderImage, command.Post.Title, cancellationToken);
                }

                await context.Posts.AddAsync(post, cancellationToken);

                await context.SaveChangesAsync(cancellationToken);

                return(ApiResponse <PostDto> .Ok(mapper.Map <PostDto>(post)));
            }
        public async Task <ActionResult> Create(EventModel eventModel)
        {
            var eventToBeCreated = (new EventDTO().InjectFrom(eventModel) as EventDTO);

            using (var stream = new MemoryStream())
            {
                await eventModel.EventImage.CopyToAsync(stream);

                eventToBeCreated.ImageUri = await imageStorageService.StoreImage(eventModel.EventImage.FileName, stream.GetBuffer());
            }

            int eventId = eventService.CreateEvent(eventToBeCreated);

            if (eventId > 0)
            {
                return(RedirectToAction("Index"));
            }
            else
            {
                return(RedirectToAction("Error", "Home"));
            }
        }