Exemplo n.º 1
0
        public async Task CreateAsync(CreatePhotoInputModel model, string userId, string imagePath)
        {
            Directory.CreateDirectory($"{imagePath}/photos/");
            foreach (var image in model.Images)
            {
                var extension = Path.GetExtension(image.FileName).TrimStart('.');
                if (!this.allowedExtensions.Any(x => extension.EndsWith(x)))
                {
                    throw new Exception($"Invalid image extension {extension}");
                }

                var dbImage = new Photo
                {
                    UserId    = userId,
                    Extension = extension,
                };

                var physicalPath = $"{imagePath}/photos/{dbImage.Id}.{extension}";
                using Stream fileStream = new FileStream(physicalPath, FileMode.Create);
                await image.CopyToAsync(fileStream);

                await this.photosRepository.AddAsync(dbImage);

                await this.photosRepository.SaveChangesAsync();
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Upload(CreatePhotoInputModel model)
        {
            if (!this.ModelState.IsValid)
            {
                var licenses = this.licenseService.GetAll();
                model.Licenses = licenses;
                return(this.View(model));
            }

            var user = await this.userManager.GetUserAsync(this.User);

            try
            {
                await this.photoService.CreatePhotoAsync(user.Id, $"{this.hostingEnvironment.WebRootPath}/images", model);
            }
            catch (Exception ex)
            {
                this.ModelState.AddModelError(string.Empty, ex.Message);
                var licenses = this.licenseService.GetAll();
                model.Licenses = licenses;
                return(this.View(model));
            }

            return(this.Redirect("/Photo/UploadSuccessful"));
        }
Exemplo n.º 3
0
        public async Task CreatePhotoAsync(string userId, string imagePath, CreatePhotoInputModel model)
        {
            var photo = new Photo
            {
                Title            = model.Title,
                Description      = model.Description,
                LicenseId        = model.LicenseId,
                IsCommentAllowed = model.IsCommentAllowed,
                IsPrivate        = model.IsPrivate,
                OwnerId          = userId,
            };

            var extension = Path.GetExtension(model.File.FileName).TrimStart('.');

            if (!this.allowedExtensions.Any(x => extension.ToLower().EndsWith(x)))
            {
                throw new Exception($"Invalid image extension {extension}");
            }

            var metadata = this.photoMetadataService.GetMetadata(model.File);

            photo = this.WriteMetadataToPhoto(photo, metadata);

            var uploadResult = await this.photoStorageService.UploadImageAsync(model.File);

            var publicId = uploadResult.PublicId;

            photo.Href          = this.photoStorageService.GetImageUrl(publicId);
            photo.ThumbnailHref = this.photoStorageService.GetThumbnailUrl(publicId);
            photo.PublicId      = publicId;

            await this.photoRespository.AddAsync(photo);

            await this.photoRespository.SaveChangesAsync();
        }
Exemplo n.º 4
0
        public void Create_ShouldCreateSuccessfully()
        {
            var user  = UserCreator.Create("test");
            var file  = new FormFile(new MemoryStream(Encoding.UTF8.GetBytes("This is a dummy file")), 0, 0, "Data", "dummy.png");
            var model = new CreatePhotoInputModel()
            {
                Title            = "Test",
                Description      = "Test",
                IsCommentAllowed = false,
                IsPrivate        = false,
                LicenseId        = "Test",
                File             = file,
            };
            var list = new List <Photo>();

            var photosRepo      = DeletableEntityRepositoryMock.Get <Photo>(list);
            var photoAlbumsRepo = EfRepositoryMock.Get <PhotoAlbum>(new List <PhotoAlbum>());
            var storageService  = PhotoStorageServiceMock.Get();
            var metadataService = PhotoMetadataServiceMock.Get();

            var service = new PhotoService(photosRepo.Object, storageService.Object, metadataService.Object, photoAlbumsRepo.Object);

            service.CreatePhotoAsync(user.Id, "path", model).Wait();

            Assert.Single(list);
        }
Exemplo n.º 5
0
        public void Create_ShouldNotCreateWhenExtensionIsNotCorrect()
        {
            var user  = UserCreator.Create("test");
            var file  = new FormFile(new MemoryStream(Encoding.UTF8.GetBytes("This is a dummy file")), 0, 0, "Data", "dummy.txt");
            var model = new CreatePhotoInputModel()
            {
                Title            = "Test",
                Description      = "Test",
                IsCommentAllowed = false,
                IsPrivate        = false,
                LicenseId        = "Test",
                File             = file,
            };
            var list = new List <Photo>();

            var photosRepo      = DeletableEntityRepositoryMock.Get <Photo>(list);
            var photoAlbumsRepo = EfRepositoryMock.Get <PhotoAlbum>(new List <PhotoAlbum>());
            var storageService  = PhotoStorageServiceMock.Get();
            var metadataService = PhotoMetadataServiceMock.Get();

            var       service = new PhotoService(photosRepo.Object, storageService.Object, metadataService.Object, photoAlbumsRepo.Object);
            Exception ex      = Assert.Throws <AggregateException>(() => service.CreatePhotoAsync(user.Id, "path", model).Wait());

            Assert.Contains("Invalid image extension", ex.Message);
            Assert.Empty(list);
        }
Exemplo n.º 6
0
        public IActionResult Upload()
        {
            var viewModel = new CreatePhotoInputModel();
            var licenses  = this.licenseService.GetAll();

            viewModel.Licenses = licenses;

            return(this.View(viewModel));
        }
Exemplo n.º 7
0
        public async Task <IActionResult> Create(CreatePhotoInputModel input)
        {
            var user = this.userManager.GetUserId(this.User);

            try
            {
                await this.photosService.CreateAsync(input, user, $"{this.webHostEnvironment.WebRootPath}/images");
            }
            catch (Exception ex)
            {
                this.ModelState.AddModelError(string.Empty, ex.Message);
                return(this.View(input));
            }
            return(this.Redirect($"/Photos/All?id={user}"));
        }