Esempio n. 1
0
        public async Task <IActionResult> DeletePhoto(Guid id, Guid albumId)
        {
            ApplicationUser user = await _userManager.GetUserAsync(HttpContext.User);

            User myUser = _repository.GetUser(user.Id);

            //delete from server
            Photo photoToDelete = await _repository.GetPhotoAsync(id);

            List <string> paths = new List <string>();

            string[] parts = photoToDelete.URL.Split('/');

            string filename = Path.Combine(_environment.WebRootPath, "uploads") + $@"\{parts[2]}";;

            paths.Add(filename);
            filename = Path.Combine(_environment.WebRootPath, "uploads") + $@"\thumbs" + $@"\{parts[2]}";
            paths.Add(filename);

            try
            {
                foreach (String path in paths)
                {
                    if (System.IO.File.Exists(path))
                    {
                        System.IO.File.Delete(path);
                    }
                }
            }
            catch (IOException ex)
            {
                return(View("AlbumError"));
            }

            //save change to DB
            await _repository.RemovePhotoFromAlbumAsync(albumId, id, user.Id);

            return(RedirectToAction("ShowAlbumPhotos", new RouteValueDictionary(
                                        new { controller = "Album", action = "ShowAlbumPhotos", id = albumId })));
        }