public static bool CheckExpiration(Models.Paste paste) { if (paste.ExpireDate != null && DateTime.Now >= paste.ExpireDate) { return(true); } if (paste.MaxViews > 0 && paste.Views > paste.MaxViews) { return(true); } return(false); }
public static Models.Paste CreatePaste(Config config, TeknikEntities db, string content, string title = "", string syntax = "text", ExpirationUnit expireUnit = ExpirationUnit.Never, int expireLength = 1, string password = "") { Models.Paste paste = new Models.Paste(); paste.DatePosted = DateTime.Now; paste.MaxViews = 0; paste.Views = 0; // Generate random url string url = StringHelper.RandomString(config.PasteConfig.UrlLength); while (db.Pastes.Where(p => p.Url == url).FirstOrDefault() != null) { url = StringHelper.RandomString(config.PasteConfig.UrlLength); } paste.Url = url; // Figure out the expire date (null if 'never' or 'visit') switch (expireUnit) { case ExpirationUnit.Never: break; case ExpirationUnit.Views: paste.MaxViews = expireLength; break; case ExpirationUnit.Minutes: paste.ExpireDate = paste.DatePosted.AddMinutes(expireLength); break; case ExpirationUnit.Hours: paste.ExpireDate = paste.DatePosted.AddHours(expireLength); break; case ExpirationUnit.Days: paste.ExpireDate = paste.DatePosted.AddDays(expireLength); break; case ExpirationUnit.Months: paste.ExpireDate = paste.DatePosted.AddMonths(expireLength); break; case ExpirationUnit.Years: paste.ExpireDate = paste.DatePosted.AddYears(expireLength); break; default: break; } if (!Directory.Exists(config.PasteConfig.PasteDirectory)) { Directory.CreateDirectory(config.PasteConfig.PasteDirectory); } // Generate a unique file name that does not currently exist string filePath = FileHelper.GenerateRandomFileName(config.PasteConfig.PasteDirectory, config.PasteConfig.FileExtension, 10); string fileName = Path.GetFileName(filePath); string key = GenerateKey(config.PasteConfig.KeySize); string iv = GenerateIV(config.PasteConfig.BlockSize); if (!string.IsNullOrEmpty(password)) { paste.HashedPassword = HashPassword(key, password); } // Encrypt the contents to the file EncryptContents(content, filePath, password, key, iv, config.PasteConfig.KeySize, config.PasteConfig.ChunkSize); // Generate a deletion key string delKey = StringHelper.RandomString(config.PasteConfig.DeleteKeyLength); paste.Key = key; paste.KeySize = config.PasteConfig.KeySize; paste.IV = iv; paste.BlockSize = config.PasteConfig.BlockSize; paste.FileName = fileName; //paste.Content = content; paste.Title = title; paste.Syntax = syntax; paste.DeleteKey = delKey; return(paste); }