public async Task <IActionResult> Add(ChapterAdd chapterAdd) { if (ModelState.IsValid) { var series = await _naniWebContext.Series.SingleAsync(srs => srs.Id == chapterAdd.SeriesId); var chapter = new Chapter { Volume = chapterAdd.Volume ?? 0, ChapterNumber = chapterAdd.ChapterNumber, Name = chapterAdd.Name ?? string.Empty, SeriesId = series.Id, Pages = new List <Page>(), ReleaseDate = DateTime.UtcNow }; 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"; Directory.CreateDirectory(destination); using (var stream = System.IO.File.Create(pagesZip)) { await chapterAdd.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(); if (pageList.Count == 0) { temp.Delete(true); TempData["Error"] = true; return(RedirectToAction("Add")); } 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"); } await _naniWebContext.Chapters.AddAsync(chapter); await _naniWebContext.SaveChangesAsync(); var siteName = _settingsKeeper.GetSetting("SiteName").Value; var chapterUrl = $"{_settingsKeeper.GetSetting("SiteUrl").Value}{Url.Action("Project", "Home", new {urlSlug = series.UrlSlug, chapterNumber = chapter.ChapterNumber})}"; var chapterDownloadUrl = $"{_settingsKeeper.GetSetting("SiteUrl").Value}{Url.Action("Download", "Home", new {urlSlug = series.UrlSlug, chapterNumber = chapter.ChapterNumber})}"; var iconUrl = $"{_settingsKeeper.GetSetting("SiteUrl").Value}/assets/icon.png"; var tasks = new Task[4]; tasks[0] = Task.Run(async() => { var mangadexSeries = await _naniWebContext.MangadexSeries.SingleOrDefaultAsync(srs => srs.SeriesId == series.Id); MangadexChapter mangadexChapter = null; if (chapterAdd.UploadToMangadex && mangadexSeries.MangadexId > 0) { using (var stream = System.IO.File.OpenRead(pagesZip)) { mangadexChapter = await _mangadexUploader.UploadChapter(chapter, mangadexSeries, stream); } } if (mangadexChapter == null) { mangadexChapter = new MangadexChapter { Chapter = chapter, ChapterId = chapter.Id, MangadexId = 0 } } ; await _naniWebContext.MangadexChapters.AddAsync(mangadexChapter); await _naniWebContext.SaveChangesAsync(); }); tasks[1] = Task.Run(async() => { await _firebaseCloudMessaging.SendNotification($"A new release is available at {siteName}!", $"New chapter of {series.Name} is available at {siteName}!", chapterUrl, iconUrl, $"series_{series.Id}"); }); tasks[2] = Task.Run(async() => { if (chapterAdd.AnnounceOnDiscord) { await _discordBot.SendMessage($"@everyone **{series.Name}** - Chapter {chapter.ChapterNumber} is out!{Environment.NewLine}Read it here: {chapterUrl}{Environment.NewLine}Download it here: {chapterDownloadUrl}"); } }); tasks[3] = Task.Run(async() => { if (chapterAdd.AnnounceOnReddit) { await _redditPoster.PostLink("/r/manga", $"[DISC] {series.Name} - Chapter {chapter.ChapterNumber}", chapterUrl, chapterAdd.RedditNsfw); } }); await Task.WhenAll(tasks); temp.Delete(true); return(RedirectToAction("List", "SeriesManager", new { id = series.Id })); } TempData["Error"] = true; return(RedirectToAction("Add")); }