Example #1
0
        public async Task <IActionResult> Index(SiteViewModel model)
        {
            if (ModelState.IsValid)
            {
                model.Sites = await _context.GetAllAsync();

                model.ToDownload = model.ToDownload.Trim('/');
                var siteName = model.Sites.Where(x => x.Id == model.SiteId).First().Name;

                var filesContext = new FilesContext(siteName, _environment.ContentRootPath);
                if (!await filesContext.AddDownloadingAsync(model.ToDownload))
                {
                    return(await GetFileForDownload(siteName, model.ToDownload));
                }

                try
                {
                    var siteType = _context.IdToType(model.SiteId);

                    var site = (ISite)Activator.CreateInstance(siteType);

                    var url      = site.GetDownloadUrl(model.Sites.Where(x => x.Id == model.SiteId).First().Url, model.ToDownload);
                    var response = await client.GetStringAsync(url);

                    var genericParser = Type.GetType("Ahegao.Models.HentaiParser`1");
                    var parser        = genericParser.MakeGenericType(new Type[] { siteType });
                    var p             = (IParser)Activator.CreateInstance(parser, new string[]
                    {
                        response,
                        model.ToDownload,
                        siteName,
                        _environment.ContentRootPath
                    });

                    await p.DownloadImages();

                    await p.GeneratePdf();

                    return(await GetFileForDownload(siteName, model.ToDownload));
                }
                catch (HttpRequestException e)
                {
                    Console.WriteLine("Exception Caught!");
                    Console.WriteLine("Message :{0} ", e.Message);
                }
            }

            return(View(model));
        }