public async Task <IActionResult> Edit(int id) { var chapter = await _naniWebContext.Chapters.SingleAsync(chp => chp.Id == id); var mangadex = await _naniWebContext.MangadexChapters.SingleOrDefaultAsync(chp => chp.Chapter == chapter); var model = new ChapterEdit { ChapterId = chapter.Id, Volume = chapter.Volume, ChapterNumber = chapter.ChapterNumber, Name = chapter.Name, MangadexId = mangadex.MangadexId }; return(View("EditChapter", model)); }
public async Task <IActionResult> Edit(ChapterEdit chapterEdit) { if (ModelState.IsValid) { var chapter = await _naniWebContext.Chapters.Include(chp => chp.MangadexInfo).SingleAsync(chp => chp.Id == chapterEdit.ChapterId); var mangadexSeries = await _naniWebContext.MangadexSeries.SingleAsync(mgdx => mgdx.SeriesId == chapter.SeriesId); chapter.Volume = chapterEdit.Volume ?? 0; chapter.ChapterNumber = chapterEdit.ChapterNumber; chapter.Name = chapterEdit.Name ?? string.Empty; chapter.MangadexInfo.MangadexId = chapterEdit.MangadexId ?? 0; if (chapterEdit.Pages != null) { var temp = Utils.CurrentDirectory.CreateSubdirectory($"Temp{Path.DirectorySeparatorChar}{Guid.NewGuid()}"); var tempPages = temp.CreateSubdirectory("Pages"); var destination = $"{_webHostEnvironment.WebRootPath}{Path.DirectorySeparatorChar}images{Path.DirectorySeparatorChar}pages{Path.DirectorySeparatorChar}"; var pagesZip = $"{temp.FullName}{Path.DirectorySeparatorChar}pages.zip"; var pages = _naniWebContext.Pages.Where(pg => pg.Chapter == chapter); var downloadsDir = Utils.CurrentDirectory.CreateSubdirectory("Downloads"); var downloadFile = $"{downloadsDir.FullName}{Path.DirectorySeparatorChar}{chapter.Id}.zip"; foreach (var page in pages) { System.IO.File.Delete($"{destination}{page.Id}.png"); } _naniWebContext.Pages.RemoveRange(pages); using (var stream = System.IO.File.Create(pagesZip)) { await chapterEdit.Pages.CopyToAsync(stream); } ZipFile.ExtractToDirectory(pagesZip, tempPages.FullName); var pageList = tempPages.EnumerateFiles().Where(fl => fl.Extension == ".png").OrderBy(fl => fl.Name.Length).ThenBy(fl => fl.Name).ToList(); chapter.Pages = new List <Page>(); for (var i = 0; i < pageList.Count; i++) { var page = new Page { PageNumber = i }; do { page.Id = Guid.NewGuid(); } while (System.IO.File.Exists($"{destination}{page.Id}.png")); chapter.Pages.Add(page); pageList[i].CopyTo($"{destination}{page.Id}.png"); System.IO.File.Delete(downloadFile); } if (chapterEdit.UploadToMangadex && chapter.MangadexInfo.MangadexId > 0) { using (var stream = System.IO.File.OpenRead(pagesZip)) { await _mangadexUploader.UpdateChapter(chapter, mangadexSeries, chapter.MangadexInfo, stream); } } temp.Delete(true); } else if (chapterEdit.UploadToMangadex) { await _mangadexUploader.UpdateChapterInfo(chapter, mangadexSeries, chapter.MangadexInfo); } _naniWebContext.Chapters.Update(chapter); await _naniWebContext.SaveChangesAsync(); return(RedirectToAction("List", new { id = chapter.SeriesId })); } TempData["Error"] = true; return(RedirectToAction("Add")); }