コード例 #1
0
        public async Task <IActionResult> Create(Guid projectId, IFormFile image, CreateViewModel createViewModel)
        {
            if (!ModelState.IsValid)
            {
                if (createViewModel.TagId == null)
                {
                    TempData["Warning"] = "You must choose a tag for the photo.";
                }

                return(RedirectToAction(nameof(ImageController.Create), new { projectId }));
            }

            string imageId = null;

            try
            {
                if (image == null)
                {
                    throw new Exception("Choose an image to send.");
                }
                using (var stream = image.OpenReadStream())
                {
                    imageId = await _imageStorageService.SaveImageAsync(stream, image.FileName);
                }
            }
            catch (Exception ex)
            {
                TempData["Error"] = ex.Message;
                return(RedirectToAction(nameof(ImageController.Create), new { projectId }));
            }

            var imageUrl = _imageStorageService.UriFor(imageId);
            ImageUrlCreateEntry imageUrlCreateEntry = new ImageUrlCreateEntry(imageUrl, new List <Guid>()
            {
                Guid.Parse(createViewModel.TagId)
            });
            IList <ImageUrlCreateEntry> imageUrlCreateEntries = new List <ImageUrlCreateEntry>()
            {
                imageUrlCreateEntry
            };
            ImageUrlCreateBatch url = new ImageUrlCreateBatch(imageUrlCreateEntries);

            trainingApi.CreateImagesFromUrls(projectId, url);

            TempData["Success"] = $"The image has been successfully uploaded.";

            return(RedirectToAction(nameof(ProjectController.Details), "Project", new { projectId }));
        }
コード例 #2
0
        public async Task <ActionResult <SavedImageDataResponse> > UploadImage(IFormFile image)
        {
            if (image == null)
            {
                return(BadRequest());
            }
            _logger.LogInformation($"Uploading new image with name - {image.Name}");

            var imageStream     = image.OpenReadStream();
            var saveImageResult = await _imageStorageService.SaveImageAsync(image.FileName, imageStream);

            if (!saveImageResult.IsSuccess)
            {
                var statusCodeResult = GetImageSaveErrorResult(saveImageResult.ErrorType);
                _logger.LogWarning(statusCodeResult.StatusCode.HasValue
                    ? $"Uploading new image with name - {image.Name} failed; Status code - {statusCodeResult.StatusCode.Value}, reason - {statusCodeResult.Value}"
                    : $"Uploading new image with name - {image.Name} failed; Reason - {statusCodeResult.Value}");

                return(statusCodeResult);
            }

            var imageUrl     = GetImageUrl(saveImageResult.Value.ExternalId);
            var dataContract = new SavedImageDataResponse
            {
                Url        = imageUrl,
                ExternalId = saveImageResult.Value.ExternalId
            };

            _logger.LogInformation($"Uploading new image with name - {image.Name} success");

            return(dataContract);
        }
コード例 #3
0
        public async Task <ActionResult <SavedImageDataContract> > UploadImage(IFormFile image)
        {
            if (image == null)
            {
                return(BadRequest());
            }
            var imageStream        = image.OpenReadStream();
            var getImageSizeResult = _imageService.GetImageSize(imageStream);

            if (!getImageSizeResult.IsSuccessful)
            {
                return(GetImageSaveErrorResult(getImageSizeResult.Error));
            }
            var saveImageResult = await _imageStorageService.SaveImageAsync(image.FileName, imageStream);

            if (!saveImageResult.IsSuccessful)
            {
                return(GetImageSaveErrorResult(saveImageResult.Error));
            }
            var imageUrl     = GetImageUrl(saveImageResult.ExternalId, getImageSizeResult.Width, getImageSizeResult.Height);
            var dataContract = new SavedImageDataContract
            {
                Url        = imageUrl,
                ExternalId = saveImageResult.ExternalId
            };

            return(dataContract);
        }
コード例 #4
0
        public async Task <IActionResult> SendPhoto(IFormFile photo)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction(nameof(PlantsController.SendPhoto)));
            }

            try
            {
                using (var stream = photo.OpenReadStream())
                {
                    var photoId = await _imageStorageService.SaveImageAsync(stream, photo.FileName);

                    return(RedirectToAction(nameof(PlantsController.Recognize), new { photoId }));
                }
            }
            catch (Exception ex)
            {
                TempData["Error"] = ex.Message;
            }

            return(RedirectToAction(nameof(HomeController.Index), "Home"));
        }