private void DoWork(object state) { return; Log.ForContext <TimedHostedService>().Information("Downloading pics from Reddit.."); using (var scope = scopeFactory.CreateScope()) { var _context = scope.ServiceProvider.GetRequiredService <ApplicationDbContext>(); var _env = scope.ServiceProvider.GetRequiredService <Microsoft.AspNetCore.Hosting.IHostingEnvironment>(); //these pics are not favorited, so we store URLs only //all of them are approved from start var pics = pictures.GetPicsUrls(); //https://i.imgur.com/QA5wmpx.jpg pics.ForEach(p => { //little conversin for imgur if (p.ImageUrl.Contains("imgur")) { var image = p.ImageUrl.Substring("https://imgur.com/".Length); p.ImageUrl = "https://i.imgur.com/" + image + ".jpg"; } }); //https://v.redd.it/tc8a5z4xp4d31 pics.RemoveAll(p => p.ImageUrl.Contains("v.redd.it", StringComparison.Ordinal)); //delet all videos pics.ForEach(pic => { string webRootPath = _env.WebRootPath; var imagePath = Path.Combine(webRootPath, "images\\tempDoge.jpg"); using (var client = new WebClient()) { client.DownloadFile(pic.ThumbnailUrl, imagePath); } Bitmap bp = new Bitmap(imagePath); byte[] Thumbnail = bp.ToThumbnail(); DogeImage im = new DogeImage { URL = pic.ImageUrl, Pictogram = Thumbnail }; DogePost post = new DogePost { AddDate = DateTime.Now, DogeImage = im, IsApproved = true, UpVotes = 0 }; im.Post = post; Log.ForContext <TimedHostedService>().Information(pic.ImageUrl); _context.Images.Add(im); _context.Posts.Add(post); bp.Dispose(); File.Delete(Path.Combine(webRootPath, "images\\tempDoge.jpg")); }); _context.SaveChanges(); } Log.ForContext <TimedHostedService>().Information("saved images to database"); }
public async Task <IActionResult> UploadNewDogePOST(UploadDoge _doge) { var file = HttpContext.Request.Form.Files; if (!_doge.DogeURL.IsNullOrEmpty() && !file.First().FileName.IsNullOrEmpty()) { Alert("Either upload file or paste URL!", NotificationType.error); return(RedirectToAction("UploadNewDoge")); } if (_doge.DogeURL.IsNullOrEmpty() && file.First().FileName.IsNullOrEmpty()) { Alert("Either upload file or paste URL!", NotificationType.error); return(RedirectToAction("UploadNewDoge")); } string webRootPath = _env.WebRootPath; var imagePath = Path.Combine(webRootPath, "images\\tempDoge.jpg"); using (var client = new WebClient()) { client.DownloadFile(_doge.DogeURL, imagePath); } byte[] Thumbnail = null; byte[] _Image = null; if (!file.First().FileName.IsNullOrEmpty()) { Thumbnail = new Bitmap(file.First().FileName).ToThumbnail(); _Image = new Bitmap(file.First().FileName).ToByteArray(ImageFormat.Jpeg); } else { Thumbnail = new Bitmap(imagePath).ToThumbnail(); _Image = new Bitmap(imagePath).ToByteArray(ImageFormat.Jpeg); } //as user uploads image, I need to create a post for it //to do pre-moderation DogeImage im = new DogeImage { Image = _Image, Pictogram = Thumbnail }; DogePost post = new DogePost { AddDate = DateTime.Now, DogeImage = im, IsApproved = false, UpVotes = 0 }; string alertText = "File uploaded, wait for moderator to approve it"; if (User.IsInRole(UserRoles.DogeAdmin)) { post.IsApproved = true; alertText = "File uploaded"; } var claimsId = (ClaimsIdentity)User.Identity; var cl = claimsId.FindFirst(ClaimTypes.NameIdentifier); var userId = cl.Value; var dbUser = _db.DogeUsers.FirstOrDefault(u => u.Id == userId); UserPost _up = new UserPost { DogePost = post, DogeUser = dbUser }; post.Users.Add(_up); im.Post = post; await _db.Images.AddAsync(im); await _db.Posts.AddAsync(post); await _db.SaveChangesAsync(); Alert(alertText, NotificationType.success); return(RedirectToAction("Index")); }