示例#1
0
        public async Task <IActionResult> Upload(int vehicleId, IFormFile file)
        {
            var vehicle = unitOfWork.Vehicles.Get(vehicleId);

            if (vehicle == null)
            {
                return(BadRequest(new ApiResponse(400, "Vehicle does not exists")));
            }

            if (file == null)
            {
                return(BadRequest(new ApiResponse(400, "File cannot be null")));
            }

            if (file.Length == 0)
            {
                return(BadRequest(new ApiResponse(400, "Empty File")));
            }

            if (file.Length > photoSettings.MaxBytes)
            {
                return(BadRequest(new ApiResponse(400, "Maximum File Size exceeded")));
            }

            if (!photoSettings.IsSupported(file.FileName))
            {
                return(BadRequest(new ApiResponse(400, "Only Images allowed")));
            }


            var uploadsFolderPath = Path.Combine(host.WebRootPath, "images");

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

            var fileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
            var filePath = Path.Combine(uploadsFolderPath, fileName);

            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                await file.CopyToAsync(stream);
            };
            Stream fileStream = file.OpenReadStream();
            Image  newImage   = photoSettings.GetReducedImage(150, 150, fileStream);

            var thumbnailFolderPath = Path.Combine(host.WebRootPath, "thumbnails");

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

            var thumbnailFileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
            var thumbnailFilePath = Path.Combine(thumbnailFolderPath, thumbnailFileName);

            newImage.Save(thumbnailFilePath);

            var newPhoto = new Photo {
                ImageUrl = fileName
            };
            var newThumbnail = new Thumbnail {
                ThumbnailUrl = thumbnailFileName
            };

            vehicle.Images.Add(newPhoto);
            vehicle.Thumbnails.Add(newThumbnail);
            await unitOfWork.CompleteAsync();

            return(Ok(mapper.Map <Thumbnail, ThumbnailResource>(newThumbnail)));
        }
示例#2
0
        public async Task <IActionResult> EditUserProfile(IFormFile file)
        {
            var userEmail = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email).Value;
            var user      = unitOfWork.Accounts.FindUserByEmail(userEmail);

            //Validation Section
            if (user == null)
            {
                return(BadRequest(new ApiResponse(400, "User does not exists")));
            }

            if (file == null)
            {
                return(BadRequest(new ApiResponse(400, "File cannot be null")));
            }

            if (file.Length == 0)
            {
                return(BadRequest(new ApiResponse(400, "Empty File")));
            }

            if (file.Length > photoSettings.MaxBytes)
            {
                return(BadRequest(new ApiResponse(400, "Maximum File Size exceeded")));
            }

            if (!photoSettings.IsSupported(file.FileName))
            {
                return(BadRequest(new ApiResponse(400, "Only Images allowed")));
            }

            var uploadsFolderPath = Path.Combine(host.WebRootPath, "profile");

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

            var fileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
            var filePath = Path.Combine(uploadsFolderPath, fileName);

            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                await file.CopyToAsync(stream);
            };
            Stream fileStream = file.OpenReadStream();
            Image  newImage   = photoSettings.GetReducedImage(100, 100, fileStream);

            var thumbnailFolderPath = Path.Combine(host.WebRootPath, "profile-thumbnails");

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

            var thumbnailFileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
            var thumbnailFilePath = Path.Combine(thumbnailFolderPath, thumbnailFileName);

            newImage.Save(thumbnailFilePath);
            user.ImageUrl     = fileName;
            user.ThumbnailUrl = thumbnailFileName;

            await unitOfWork.CompleteAsync();

            return(Ok(mapper.Map <User, UserResource>(user)));
        }