Ejemplo n.º 1
0
        public async Task <IActionResult> PostImage([FromBody] ImagePostRequest request)
        {
            Contract.Requires(request != null);
            //if (request == null)
            //{
            //	throw new ArgumentNullException(nameof(request));
            //}

            //Uri requestUri = new Uri(request.URL);
            HttpWebRequest  aRequest  = (HttpWebRequest)WebRequest.Create(request.URL);
            HttpWebResponse aResponse = (await aRequest.GetResponseAsync().ConfigureAwait(false)) as HttpWebResponse;

            // Get a reference to a blob named "sample-file" in a container named "sample-container"
            string     blobName  = $"{request.Id}.{request.EncodingFormat}";
            BlobClient blockBlob = this._container.GetBlobClient(blobName);

            using (var stream = aResponse.GetResponseStream())
            {
                await blockBlob.UploadAsync(stream).ConfigureAwait(false);
            }

            //Save metadata
            var savedImage = new SavedImage()
            {
                UserId      = request.UserId,
                Description = request.Description,
                StorageUrl  = blockBlob.Uri
            };

            foreach (var tag in request.Tags)
            {
                savedImage.Tags.Add(new SavedImageTag()
                {
                    Tag = tag
                });
            }

            _context.Add(savedImage);
            _context.SaveChanges();

            return(Ok());
        }
Ejemplo n.º 2
0
        private void SaveImageMetadata(ImagePostRequest request, CloudBlockBlob blockBlob)
        {
            var savedImage = new SavedImage();

            savedImage.UserId      = request.UserId;
            savedImage.Description = request.Description;
            savedImage.StorageUrl  = blockBlob.Uri.ToString();
            savedImage.Tags        = new List <SavedImageTag>();

            foreach (var tag in request.Tags)
            {
                savedImage.Tags.Add(new SavedImageTag()
                {
                    Tag = tag
                });
            }

            _context.Add(savedImage);
            _context.SaveChanges();
        }
        public async Task <IActionResult> PostImage([FromBody] ImagePostRequest request)
        {
            try
            {
                CloudBlockBlob  blockBlob = _container.GetBlockBlobReference($"{request.Id}.{request.EncodingFormat}");
                HttpWebRequest  aRequest  = (HttpWebRequest)WebRequest.Create(request.URL);
                HttpWebResponse aResponse = (await aRequest.GetResponseAsync()) as HttpWebResponse;
                var             stream    = aResponse.GetResponseStream();
                await blockBlob.UploadFromStreamAsync(stream);

                stream.Dispose();

                //Save metadata
                var savedImage = new SavedImage();
                savedImage.UserId      = request.UserId;
                savedImage.Description = request.Description;
                savedImage.StorageUrl  = blockBlob.Uri.ToString();
                savedImage.Tags        = new List <SavedImageTag>();

                foreach (var tag in request.Tags)
                {
                    savedImage.Tags.Add(new SavedImageTag()
                    {
                        Tag = tag
                    });
                }

                _context.Add(savedImage);
                _context.SaveChanges();


                return(Ok());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }