Ejemplo n.º 1
0
        public async Task <IActionResult> Create(AlbumCreateBindingModel album)//*
        {
            if (ModelState.IsValid)
            {
                //generate folder name, remove any chatacters that are not allowed
                string albumFolderName = FileHelpers.GetValidAlbumFolderName(album.Title.Trim());

                //create album folder
                string albumPath = GetAlbumPath(albumFolderName);
                if (!Directory.Exists(albumPath))
                {
                    Directory.CreateDirectory(albumPath);
                }

                //create thumbs folder
                string albumPathThumb = Path.Combine(albumPath, "thumb");
                if (!Directory.Exists(albumPathThumb))
                {
                    Directory.CreateDirectory(albumPathThumb);
                }

                string fileName;
                if (album.UploadImage != null)
                {
                    fileName = await PrepareImage.CoverPhotoAsync(album.UploadImage, albumPath, albumPathThumb, "AlbumCover");

                    if (fileName == null)
                    {
                        ModelState.AddModelError("InvoiceFile", "Invalid File Type!");
                        return(View());
                    }
                }
                else
                {
                    fileName = "AlbumCover.jpg";
                    System.IO.File.Copy(Path.Combine(_configuration.GetValue <string>("CustomSettings:ImagesRootPath", ".\\Images"), fileName),
                                        Path.Combine(albumPath, fileName));
                }


                Album newAlbum = new Album()
                {
                    AlbumFolderName = albumFolderName,
                    Title           = album.Title,
                    CoverPhoto      = fileName,
                    Description     = album.Description
                };

                _context.Add(newAlbum);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(album));
        }
Ejemplo n.º 2
0
        public HttpResponse Create(AlbumCreateBindingModel model)
        {
            if (!this.IsUserLoggedIn())
            {
                return(this.Redirect("/Users/Login"));
            }

            if (string.IsNullOrWhiteSpace(model.Name) ||
                string.IsNullOrWhiteSpace(model.Cover) ||
                model.Name.Length < 4 ||
                model.Name.Length > 20)
            {
                return(this.Redirect("/Albums/Create"));
            }

            this.albumService.CreateAlbum(model.Name, model.Cover);

            return(this.Redirect("/Albums/All"));
        }