Beispiel #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));
        }
Beispiel #2
0
        public async Task <IActionResult> Edit(int id, AlbumEditBindingModel model)
        {
            if (id != model.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    Album oldAlbum = _context.Albums.AsNoTracking().Where(a => a.Id == model.Id).FirstOrDefault();

                    string folderName       = FileHelpers.GetValidAlbumFolderName(model.Title.Trim());
                    string albumFolder      = GetAlbumPath(folderName);
                    string albumThumbFolder = Path.Combine(albumFolder, "thumb");

                    //if the album name has been changed, then update folder name
                    if (oldAlbum.Title.Trim() != model.Title.Trim())
                    {
                        string sourcePath = GetAlbumPath(oldAlbum.AlbumFolderName);

                        if (sourcePath != albumFolder)
                        {
                            Directory.Move(sourcePath, albumFolder);
                        }
                    }

                    //replace  image if changed
                    string fileName = string.Empty;
                    if (model.UploadImage != null)
                    {
                        fileName = await PrepareImage.CoverPhotoAsync(model.UploadImage, albumFolder, albumThumbFolder, "AlbumCover");
                    }

                    Album newAlbum = new Album()
                    {
                        Id              = model.Id,
                        Title           = model.Title.Trim(),
                        CoverPhoto      = string.IsNullOrEmpty(fileName) ? oldAlbum.CoverPhoto : fileName,
                        AlbumFolderName = folderName,
                        Description     = model.Description
                    };

                    _context.Update(newAlbum);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!AlbumExists(model.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(model));
        }