public async Task <IActionResult> UploadPointOfInterest([FromBody] PointOfInterestExtended pointOfInterest,
                                                                [FromQuery] string language)
        {
            if (!pointOfInterest.Source.Equals(Sources.OSM, StringComparison.InvariantCultureIgnoreCase))
            {
                return(BadRequest("OSM is the only supported source for this action..."));
            }
            if ((pointOfInterest.Description?.Length ?? 0) > 255)
            {
                return(BadRequest("Description must not be more than 255 characters..."));
            }
            if ((pointOfInterest.Title?.Length ?? 0) > 255)
            {
                return(BadRequest("Title must not be more than 255 characters..."));
            }
            var tokenAndSecret = _cache.Get(User.Identity.Name);
            var osmGateway     = _clientsFactory.CreateOAuthClient(_options.OsmConfiguration.ConsumerKey, _options.OsmConfiguration.ConsumerSecret, tokenAndSecret.Token, tokenAndSecret.TokenSecret);
            var user           = await osmGateway.GetUserDetails();

            var imageUrls = pointOfInterest.ImagesUrls ?? new string[0];

            for (var urlIndex = 0; urlIndex < imageUrls.Length; urlIndex++)
            {
                var fileName = string.IsNullOrWhiteSpace(pointOfInterest.Title)
                    ? pointOfInterest.Icon.Replace("icon-", "")
                    : pointOfInterest.Title;
                var file = _base64ImageConverter.ConvertToFile(imageUrls[urlIndex], fileName);
                if (file == null)
                {
                    continue;
                }
                using (var md5 = MD5.Create())
                {
                    var imageUrl = await _imageUrlStoreExecutor.GetImageUrlIfExists(md5, file.Content);

                    if (imageUrl != null)
                    {
                        imageUrls[urlIndex] = imageUrl;
                        continue;
                    }
                    using (var memoryStream = new MemoryStream(file.Content))
                    {
                        var imageName = await _wikimediaCommonGateway.UploadImage(pointOfInterest.Title,
                                                                                  pointOfInterest.Description, user.DisplayName, file.FileName, memoryStream,
                                                                                  pointOfInterest.Location.ToCoordinate());

                        imageUrls[urlIndex] = await _wikimediaCommonGateway.GetImageUrl(imageName);

                        await _imageUrlStoreExecutor.StoreImage(md5, file.Content, imageUrls[urlIndex]);
                    }
                }
            }

            if (string.IsNullOrWhiteSpace(pointOfInterest.Id))
            {
                return(Ok(await _pointsOfInterestProvider.AddPointOfInterest(pointOfInterest, tokenAndSecret, language)));
            }
            return(Ok(await _pointsOfInterestProvider.UpdatePointOfInterest(pointOfInterest, tokenAndSecret, language)));
        }
Example #2
0
        public async Task <IActionResult> UploadPointOfInterest([FromBody] PointOfInterestExtended pointOfInterest,
                                                                [FromQuery] string language)
        {
            if (!pointOfInterest.Source.Equals(Sources.OSM, StringComparison.InvariantCultureIgnoreCase))
            {
                return(BadRequest("OSM is the only supported source for this action..."));
            }
            var tokenAndSecret = _cache.Get(User.Identity.Name);
            var osmGateway     = _httpGatewayFactory.CreateOsmGateway(tokenAndSecret);
            var user           = await osmGateway.GetUser();

            var imageUrls = pointOfInterest.ImagesUrls ?? new string[0];

            for (var urlIndex = 0; urlIndex < imageUrls.Length; urlIndex++)
            {
                var url      = imageUrls[urlIndex];
                var fileName = string.IsNullOrWhiteSpace(pointOfInterest.Title)
                    ? pointOfInterest.Icon.Replace("icon-", "")
                    : pointOfInterest.Title;
                var file = _base64ImageConverter.ConvertToFile(url, fileName);
                if (file == null)
                {
                    continue;
                }
                using (var memoryStream = new MemoryStream(file.Content))
                {
                    var imageName = await _wikimediaCommonGateway.UploadImage(pointOfInterest.Title,
                                                                              pointOfInterest.Description, user.DisplayName, file.FileName, memoryStream,
                                                                              new Coordinate().FromLatLng(pointOfInterest.Location));

                    url = await _wikimediaCommonGateway.GetImageUrl(imageName);

                    imageUrls[urlIndex] = url;
                }
            }

            if (string.IsNullOrWhiteSpace(pointOfInterest.Id))
            {
                return(Ok(await _pointsOfInterestProvider.AddPointOfInterest(pointOfInterest, tokenAndSecret, language)));
            }
            return(Ok(await _pointsOfInterestProvider.UpdatePointOfInterest(pointOfInterest, tokenAndSecret, language)));
        }