Пример #1
0
        public IActionResult EditImage(int?Id)
        {
            if (Id == null)
            {
                return(NotFound());
            }

            var image = _context.StoreImages.Where(i => i.Id == Id).FirstOrDefault();

            if (image == null)
            {
                return(NotFound());
            }


            var    album     = _context.Albums.Where(a => a.Id == image.AlbumId).FirstOrDefault();
            string AlbumPath = Path.Combine(_configuration.GetValue <string>("CustomSettings:ImagesPath"), album.AlbumFolderName);
            string FilePath  = Path.Combine(AlbumPath, image.FileName);

            var model = new EditImageBindingModel()
            {
                Id       = image.Id,
                Title    = image.Title,
                Album    = image.Album,
                AlbumId  = image.Album.Id,
                FilePath = FilePath,
                AltText  = image.Title
            };


            //load albums
            GetAlbums();

            return(View(model));
        }
Пример #2
0
        public async Task <IActionResult> EditImage(int Id, EditImageBindingModel model)
        {
            if (Id != model.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var image = _context.StoreImages.AsNoTracking().Where(i => i.Id == Id).FirstOrDefault();
                    if (image == null)
                    {
                        return(NotFound());
                    }

                    //move file to the new album's folder
                    if (image.AlbumId != model.AlbumId)

                    {
                        var    destAlbum          = _context.Albums.Where(a => a.Id == model.AlbumId).FirstOrDefault();
                        string destAlbumPath      = Path.Combine(_configuration.GetValue <string>("CustomSettings:UploadPath"), destAlbum.AlbumFolderName);
                        string destFilePath       = Path.Combine(destAlbumPath, image.FileName);
                        string destThubmsFilePath = Path.Combine(destAlbumPath, "thumb", image.FileName);

                        var    sourceAlbum         = _context.Albums.Where(a => a.Id == image.AlbumId).FirstOrDefault();
                        string sourceAlbumPath     = Path.Combine(_configuration.GetValue <string>("CustomSettings:UploadPath"), sourceAlbum.AlbumFolderName);
                        string sourceFilePath      = Path.Combine(sourceAlbumPath, image.FileName);
                        string sourceThumbFilePath = Path.Combine(sourceAlbumPath, "thumb", image.FileName);

                        System.IO.File.Move(sourceFilePath, destFilePath);

                        System.IO.File.Move(sourceThumbFilePath, destThubmsFilePath);
                    }
                    ;

                    image.Title   = model.Title;
                    image.AlbumId = model.AlbumId;
                    _context.Update(image);

                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!AlbumExists(model.Id))//todo
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }

                return(RedirectToAction("Images", new { id = model.AlbumId }));
            }
            return(View(model));
        }
Пример #3
0
        public async Task <ActionResult> EditImage(EditImageBindingModel model)
        {
            var imageTarget = this.Data.Images.GetById(model.Id);

            if (imageTarget == null)
            {
                return(new HttpStatusCodeResult(400, "No record for this image in the database"));
            }

            var userId = User.Identity.GetUserId();

            if (User.IsInRole("Admin") || userId != imageTarget.User.Id)
            {
                return(this.Content("You don't have the right to edit this picture."));
            }

            imageTarget.Title       = model.Title;
            imageTarget.Description = model.Description;

            await this.Data.SaveChangesAsync();

            return(this.Content("The image was successfully edited."));
        }
        public async Task<ActionResult> EditImage(EditImageBindingModel model)
        {
            var imageTarget = this.Data.Images.GetById(model.Id);
            if (imageTarget == null)
            {
                return new HttpStatusCodeResult(400, "No record for this image in the database");
            }

            var userId = User.Identity.GetUserId();

            if (User.IsInRole("Admin") || userId != imageTarget.User.Id)
            {
                return this.Content("You don't have the right to edit this picture.");
            }

            imageTarget.Title = model.Title;
            imageTarget.Description = model.Description;

            await this.Data.SaveChangesAsync();

            return this.Content("The image was successfully edited.");
        }