// Generate a random filename for a thumbnail and make sure that the file does not exist. private static string GenerateRandomFilename() { string rndFileName; // if CDN flag is active, check if file exists on CDN, otherwise check if file exists on local storage if (Settings.UseContentDeliveryNetwork) { // make sure blob with same name doesn't exist already do { rndFileName = Guid.NewGuid().ToString(); } while (CloudStorageUtility.BlobExists(rndFileName, "thumbs")); rndFileName = Guid.NewGuid().ToString(); } else { do { rndFileName = Guid.NewGuid().ToString(); } while (FileSystemUtility.FileExists(rndFileName, DestinationPathThumbs)); } return(rndFileName); }
// store uploaded avatar public static async Task <bool> GenerateAvatar(Image inputImage, string userName, string mimetype) { try { // store avatar locally var originalImage = new KalikoImage(inputImage); originalImage.Scale(new PadScaling(MaxWidth, MaxHeight)).SaveJpg(DestinationPathAvatars + '\\' + userName + ".jpg", 90); if (!Settings.UseContentDeliveryNetwork) { return(true); } // call upload to storage since CDN is enabled in config string tempAvatarLocation = DestinationPathAvatars + '\\' + userName + ".jpg"; // the avatar file was not found at expected path, abort if (!FileSystemUtility.FileExists(tempAvatarLocation, DestinationPathAvatars)) { return(false); } // upload to CDN await CloudStorageUtility.UploadBlobToStorageAsync(tempAvatarLocation, "avatars"); // delete local file after uploading to CDN File.Delete(tempAvatarLocation); return(true); } catch (Exception ex) { return(false); } }
// generate a thumbnail while removing transparency and preserving aspect ratio public static async Task <string> GenerateThumbFromUrl(string sourceUrl) { var randomFileName = GenerateRandomFilename(); var request = WebRequest.Create(sourceUrl); request.Timeout = 300; using (var response = request.GetResponse()) { var originalImage = new KalikoImage(response.GetResponseStream()) { BackgroundColor = Color.Black }; originalImage.Scale(new PadScaling(MaxWidth, MaxHeight)).SaveJpg(DestinationPathThumbs + '\\' + randomFileName + ".jpg", 90); } // call upload to storage method if CDN config is enabled if (Settings.UseContentDeliveryNetwork) { string tempThumbLocation = DestinationPathThumbs + '\\' + randomFileName + ".jpg"; if (FileSystemUtility.FileExists(tempThumbLocation, DestinationPathThumbs)) { await CloudStorageUtility.UploadBlobToStorageAsync(tempThumbLocation, "thumbs"); // delete local file after uploading to CDN File.Delete(tempThumbLocation); } } return(randomFileName + ".jpg"); }
// generate a thumbnail while removing transparency and preserving aspect ratio public static async Task <string> GenerateThumbFromImageUrl(string imageUrl, int timeoutInMilliseconds = 3000) { var randomFileName = GenerateRandomFilename(); var tempPath = Path.Combine(DestinationPathThumbs, $"{randomFileName}.jpg"); var request = WebRequest.Create(imageUrl); request.Timeout = timeoutInMilliseconds; //Putts: extended this from 300 mills using (var response = request.GetResponse()) { var originalImage = new KalikoImage(response.GetResponseStream()) { BackgroundColor = Color.Black }; originalImage.Scale(new PadScaling(MaxWidth, MaxHeight)).SaveJpg(tempPath, 90); } // call upload to storage method if CDN config is enabled if (Settings.UseContentDeliveryNetwork) { if (FileSystemUtility.FileExists(tempPath, DestinationPathThumbs)) { await CloudStorageUtility.UploadBlobToStorageAsync(tempPath, "thumbs"); // delete local file after uploading to CDN File.Delete(tempPath); } } return(Path.GetFileName(tempPath)); }
// delete a user account and all history: comments, posts and votes public static bool DeleteUser(string userName) { using (var db = new voatEntities()) { using (var tmpUserManager = new UserManager <VoatUser>(new UserStore <VoatUser>(new ApplicationDbContext()))) { var tmpuser = tmpUserManager.FindByName(userName); if (tmpuser != null) { // remove voting history for submisions db.SubmissionVoteTrackers.RemoveRange(db.SubmissionVoteTrackers.Where(x => x.UserName == userName)); // remove voting history for comments db.CommentVoteTrackers.RemoveRange(db.CommentVoteTrackers.Where(x => x.UserName == userName)); // remove all comments var comments = db.Comments.Where(c => c.UserName == userName).ToList(); foreach (Comment c in comments) { c.IsDeleted = true; c.Content = "deleted by user"; } db.SaveChanges(); // remove all submissions var submissions = db.Submissions.Where(c => c.UserName == userName).ToList(); foreach (var s in submissions) { if (s.Type == 1) { s.IsDeleted = true; s.Content = "deleted by user"; s.Title = "deleted by user"; } else { s.IsDeleted = true; s.LinkDescription = "deleted by user"; s.Content = "http://voat.co"; } } // resign from all moderating positions db.SubverseModerators.RemoveRange(db.SubverseModerators.Where(m => m.UserName.Equals(userName, StringComparison.OrdinalIgnoreCase))); // delete comment reply notifications db.CommentReplyNotifications.RemoveRange(db.CommentReplyNotifications.Where(crp => crp.Recipient.Equals(userName, StringComparison.OrdinalIgnoreCase))); // delete post reply notifications db.SubmissionReplyNotifications.RemoveRange(db.SubmissionReplyNotifications.Where(prp => prp.Recipient.Equals(userName, StringComparison.OrdinalIgnoreCase))); // delete private messages db.PrivateMessages.RemoveRange(db.PrivateMessages.Where(pm => pm.Recipient.Equals(userName, StringComparison.OrdinalIgnoreCase))); // delete user preferences var userPrefs = db.UserPreferences.Find(userName); if (userPrefs != null) { // delete short bio userPrefs.Bio = null; // delete avatar if (userPrefs.Avatar != null) { var avatarFilename = userPrefs.Avatar; if (Settings.UseContentDeliveryNetwork) { // try to delete from CDN CloudStorageUtility.DeleteBlob(avatarFilename, "avatars"); } else { // try to remove from local FS string tempAvatarLocation = Settings.DestinationPathAvatars + '\\' + userName + ".jpg"; // the avatar file was not found at expected path, abort if (!FileSystemUtility.FileExists(tempAvatarLocation, Settings.DestinationPathAvatars)) { return(false); } // exec delete File.Delete(tempAvatarLocation); } } } // TODO: // keep this updated as new features are added (delete sets etc) // username will stay permanently reserved to prevent someone else from registering it and impersonating db.SaveChanges(); return(true); } // user account could not be found return(false); } } }