コード例 #1
0
        public IActionResult CreateImage([FromBody] Model.ImageForCreation imageForCreation)
        {
            if (imageForCreation == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                // return 422 - Unprocessable Entity when validation fails
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            // Automapper maps only the Title in our configuration
            var imageEntity = Mapper.Map <Image>(imageForCreation);

            // Create an image from the passed-in bytes (Base64), and
            // set the filename on the image

            // get this environment's web root path (the path
            // from which static content, like an image, is served)
            var webRootPath = _hostingEnvironment.ContentRootPath;

            // create the filename
            string fileName = Guid.NewGuid().ToString() + ".jpg";

            // the full file path
            var filePath = Path.Combine($"{webRootPath}/images/{fileName}");

            // write bytes and auto-close stream
            System.IO.File.WriteAllBytes(filePath, imageForCreation.Bytes);

            // fill out the filename
            imageEntity.FileName = fileName;

            // ownerId should be set - can't save image in starter solution, will
            // be fixed during the course
            //imageEntity.OwnerId = ...;

            var ownerId = User.Claims.FirstOrDefault(c => c.Type == "sub").Value;

            imageEntity.OwnerId = ownerId;

            // add and save.
            _galleryRepository.AddImage(imageEntity);

            if (!_galleryRepository.Save())
            {
                throw new Exception($"Adding an image failed on save.");
            }

            var imageToReturn = Mapper.Map <Image>(imageEntity);

            return(CreatedAtRoute("GetImage",
                                  new { id = imageToReturn.Id },
                                  imageToReturn));
        }
コード例 #2
0
        public IActionResult CreateImage([FromBody] Model.ImageForCreation imageForCreation)
        {
            // Automapper maps only the Title in our configuration
            var imageEntity = _mapper.Map <Entities.Image>(imageForCreation);

            // Create an image from the passed-in bytes (Base64), and
            // set the filename on the image

            // get this environment's web root path (the path
            // from which static content, like an image, is served)
            var webRootPath = _hostingEnvironment.WebRootPath;

            // create the filename
            string fileName = Guid.NewGuid().ToString() + ".jpg";

            // the full file path
            var filePath = Path.Combine($"{webRootPath}/Images/{fileName}");

            // write bytes and auto-close stream
            System.IO.File.WriteAllBytes(filePath, imageForCreation.Bytes);

            // fill out the filename
            imageEntity.FileName = fileName;

            // set the ownerId on the imageEntity
            var ownerId = User.Claims.FirstOrDefault(c => c.Type == "sub").Value;

            imageEntity.OwnerId = ownerId;

            // add and save.
            _galleryRepository.AddImage(imageEntity);

            _galleryRepository.Save();

            var imageToReturn = _mapper.Map <Model.Image>(imageEntity);

            return(CreatedAtRoute("GetImage",
                                  new { id = imageToReturn.Id },
                                  imageToReturn));
        }