コード例 #1
0
        public async Task <IActionResult> Add(int placeId, bool isMain)
        {
            if (
                !IsCurrentUserAdmin() &&
                !dbContext.Places.Any(
                    p => p.Id == placeId && p.OwnerId == currentUserProvider.UserId
                    )
                )
            {
                return(Json(new ResultViewModel()
                {
                    Status = (int)ResultStatus.Error,
                    Message = "Access denied"
                }));
            }


            for (int i = 0; i < Request.Form.Files.Count; i++)
            {
                var file = Request.Form.Files[i];
                if (
                    file.ContentType == "image/jpeg" ||
                    file.ContentType == "image/png"
                    )
                {
                    using (var fileStream = file.OpenReadStream()) {
                        //Generate thumbnails
                        var thumbnails = thumbnailConfig.Sizes
                                         .Select(
                            size => new
                        {
                            stream = ImageResizer.ResizeToCenter(fileStream, size),
                            size   = size
                        }
                            );

                        //Resize image itself
                        var resizedImage = ImageResizer.Resize(
                            fileStream,
                            imagesConfiguration.PhotoMaxWidth,
                            imagesConfiguration.PhotoMaxWidth
                            );

                        // Generate image filename
                        var fileName = MD5.Create().CalculateStringHash(
                            DateTime.Now.Ticks.ToString()
                            )
                                       .ToBase64String() + ".jpg";

                        // Replace main photo if it was already assigned
                        if (isMain)
                        {
                            var currentMainPhoto = dbContext.Photos
                                                   .Where(p => p.Type == PhotoType.Main && p.PlaceId == placeId)
                                                   .FirstOrDefault();
                            if (currentMainPhoto != null)
                            {
                                // Delete thumbnails and the photo itself
                                var filePath = Path.Combine(
                                    imagesConfiguration.PhotosPath,
                                    currentMainPhoto.Filename
                                    );
                                System.IO.File.Delete(filePath);
                                deleteAllThumbnails(currentMainPhoto.Filename);

                                dbContext.Photos.Remove(currentMainPhoto);
                            }
                        }
                        // Save thumbnails
                        try
                        {
                            foreach (var thumbnail in thumbnails)
                            {
                                var path = thumbnailConfig.Path(thumbnail.size, fileName);
                                // Create directory if not existing
                                (new FileInfo(path)).Directory.Create();
                                // Save each thumbnail
                                var thumbnailFile = System.IO.File.Create(path);
                                using (var thumbnailWriter = new System.IO.StreamWriter(thumbnailFile)) {
                                    await thumbnail.stream.CopyToAsync(thumbnailWriter.BaseStream);
                                }
                            }

                            //Save image
                            var photoFile = System.IO.File.Create(fileName);
                            using (var photoWriter = new System.IO.StreamWriter(photoFile)) {
                                await resizedImage.CopyToAsync(photoWriter.BaseStream);
                            }
                        }
                        catch (Exception)
                        {
                            return(Json(new ResultViewModel()
                            {
                                Status = (int)ResultStatus.Error, Message = "Niewłaściwy plik obrazu. Spróbuj jeszcze raz."
                            }));
                        }

                        dbContext.Photos.Add(new Photo()
                        {
                            Filename = fileName,
                            PlaceId  = placeId,
                            Type     = isMain ? PhotoType.Main : PhotoType.Additional
                        });

                        dbContext.SaveChanges();
                    }
                }
            }
            return(Json(new ResultViewModel()
            {
                Status = (int)ResultStatus.Success
            }));
        }