public async Task <IActionResult> Add(AddSeries addSeries) { if (ModelState.IsValid) { var series = new Series { Name = addSeries.Name, Author = addSeries.Author, Artist = addSeries.Artist, Synopsis = addSeries.Synopsis, Type = addSeries.Type, Status = addSeries.Status, UrlSlug = Utils.GenerateSlug(addSeries.Name), MangadexInfo = new MangadexSeries { MangadexId = addSeries.MangadexId ?? 0, DisplayLink = addSeries.DisplayMangadexLink } }; await _naniWebContext.Series.AddAsync(series); await _naniWebContext.SaveChangesAsync(); var path = $"{_webHostEnvironment.WebRootPath}{Path.DirectorySeparatorChar}images{Path.DirectorySeparatorChar}covers{Path.DirectorySeparatorChar}{series.Id}.png"; Directory.CreateDirectory(path.Replace($"{Path.DirectorySeparatorChar}{series.Id}.png", string.Empty)); using (var file = System.IO.File.Create(path)) { await addSeries.Cover.CopyToAsync(file); } var fileInfo = new FileInfo(path); var destination = fileInfo.Directory; Utils.ResizeImage(fileInfo, destination, $"{series.Id}_small", 209, 300); Utils.ResizeImage(fileInfo, destination, $"{series.Id}_smaller", 209 / 2, 300 / 2); return(RedirectToAction("List")); } TempData["Error"] = true; return(RedirectToAction("Add")); }
public async Task <IActionResult> Add(AddAnnouncement addAnnouncement) { if (ModelState.IsValid) { var announcement = new Announcement { Title = addAnnouncement.Title, Content = addAnnouncement.Content, PostDate = DateTime.UtcNow, UrlSlug = Utils.GenerateSlug(addAnnouncement.Title) }; await _naniWebContext.Announcements.AddAsync(announcement); await _naniWebContext.SaveChangesAsync(); return(RedirectToAction("List")); } TempData["Error"] = true; return(RedirectToAction("Add")); }
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")); }