예제 #1
0
        private string ProcessFoto(PhotosPostDTO model)
        {
            string uniqueFileName = null;

            if (model.Photo != null)
            {
                uniqueFileName = Guid.NewGuid().ToString() + "_" + model.Photo.FileName;


                var uploadsFolder = webHostEnvironment.WebRootPath + "/uploads/gallery/" + model.CuriosityId;

                if (!Directory.Exists(uploadsFolder))
                {
                    Directory.CreateDirectory(uploadsFolder);
                }


                //string uploadsFolder = Path.Combine(webHostEnvironment.WebRootPath, "images");
                string filePath = Path.Combine(uploadsFolder, uniqueFileName);

                using (FileStream fileStream = new FileStream(filePath, FileMode.Create))
                {
                    model.Photo.CopyTo(fileStream);
                }
            }

            return(uniqueFileName);
        }
예제 #2
0
        public async Task <ActionResult <Photo> > PostPhoto([FromForm] PhotosPostDTO model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            //string uniqueFileName = ProcessFoto(model);
            string uniqueFileName = await _AzureFileService.ProcessPhotoGalleryForCuriosityAsync(model);

            Photo photo = new Photo()
            {
                CuriosityId = model.CuriosityId,
                PhotoPath   = uniqueFileName
            };

            _context.Photos.Add(photo);
            await _context.SaveChangesAsync();

            var photoDTO = await ProcessPhotoDTO(photo);

            return(CreatedAtAction("GetPhoto", new { id = photo.Id }, photoDTO));


            //return Ok(newMmtFoto);
            //return CreatedAtAction("GetPhoto", new { CuiosityId = photo.CuriosityId }, photo);



            //_context.Photos.Add(photo);
            //await _context.SaveChangesAsync();

            //return CreatedAtAction("GetPhoto", new { id = photo.Id }, photo);
        }
예제 #3
0
        //photo gallery
        public async Task <string> ProcessPhotoGalleryForCuriosityAsync(PhotosPostDTO model)
        {
            string uniqueFileName = null;

            if (model.Photo != null)
            {
                uniqueFileName = Guid.NewGuid().ToString() + "_" + model.Photo.FileName;

                // Retrieve storage account from connection string.
                CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(StorageConnectionString);
                // Create the blob client.
                CloudBlobClient blobClient = cloudStorageAccount.CreateCloudBlobClient();
                // Retrieve a reference to a container.
                CloudBlobContainer container = blobClient.GetContainerReference("gallery");
                // This also does not make a service call; it only creates a local object.
                CloudBlockBlob blockBlob = container.GetBlockBlobReference(uniqueFileName);
                await using (var data = model.Photo.OpenReadStream())
                {
                    await blockBlob.UploadFromStreamAsync(data);
                }
            }

            return(uniqueFileName);
        }