public override UploadResult Upload(Stream stream, string fileName) { if (!CheckAuthorization()) { return(null); } Dictionary <string, string> args = new Dictionary <string, string>(); args.Add("encrypt", (!Encryption).ToString()); args.Add("expirationUnit", ExpirationUnit.ToString()); args.Add("expirationLength", ExpirationLength.ToString()); args.Add("saveKey", (!Encryption).ToString()); args.Add("keySize", "256"); args.Add("blockSize", "128"); args.Add("genDeletionKey", GenerateDeletionKey.ToString()); UploadResult result = SendRequestFile(APIUrl, stream, fileName, "file", args, teknik.GetAuthHeaders()); if (result.IsSuccess) { TeknikUploadResponseWrapper response = JsonConvert.DeserializeObject <TeknikUploadResponseWrapper>(result.Response); if (response.Result != null && response.Error == null) { result.URL = response.Result.Url; } } return(result); }
public override UploadResult UploadText(string text, string fileName) { Dictionary <string, string> args = new Dictionary <string, string>(); args.Add("code", text); args.Add("expirationUnit", ExpirationUnit.ToString()); args.Add("expirationLength", ExpirationLength.ToString()); string response = SendRequestMultiPart(APIUrl, args, teknik.GetAuthHeaders()); TeknikPasteResponseWrapper apiResponse = JsonConvert.DeserializeObject <TeknikPasteResponseWrapper>(response); UploadResult ur = new UploadResult(); if (apiResponse.Result != null && apiResponse.Error == null) { ur.URL = apiResponse.Result.Url; } return(ur); }
public static Models.Upload SaveFile(TeknikEntities db, Config config, Stream file, string contentType, long contentLength, bool encrypt, ExpirationUnit expirationUnit, int expirationLength, string fileExt, string iv, string key, int keySize, int blockSize) { if (!Directory.Exists(config.UploadConfig.UploadDirectory)) { Directory.CreateDirectory(config.UploadConfig.UploadDirectory); } // Generate a unique file name that does not currently exist string filePath = FileHelper.GenerateRandomFileName(config.UploadConfig.UploadDirectory, config.UploadConfig.FileExtension, 10); string fileName = Path.GetFileName(filePath); // once we have the filename, lets save the file if (encrypt) { // Generate a key and iv if (string.IsNullOrEmpty(key)) { key = StringHelper.RandomString(config.UploadConfig.KeySize / 8); } if (string.IsNullOrEmpty(iv)) { iv = StringHelper.RandomString(config.UploadConfig.BlockSize / 8); } byte[] keyBytes = Encoding.UTF8.GetBytes(key); byte[] ivBytes = Encoding.UTF8.GetBytes(iv); // Encrypt the file to disk AesCounterManaged.EncryptToFile(filePath, file, config.UploadConfig.ChunkSize, keyBytes, ivBytes); } else { // Just write the stream to the file using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write)) { file.Seek(0, SeekOrigin.Begin); file.CopyTo(fileStream); } } // Generate a unique url string extension = (config.UploadConfig.IncludeExtension) ? fileExt : string.Empty; string url = StringHelper.RandomString(config.UploadConfig.UrlLength) + extension; while (db.Uploads.Where(u => u.Url == url).FirstOrDefault() != null) { url = StringHelper.RandomString(config.UploadConfig.UrlLength) + extension; } // Generate a deletion key string delKey = StringHelper.RandomString(config.UploadConfig.DeleteKeyLength); // Now we need to update the database with the new upload information Models.Upload upload = new Models.Upload(); upload.DateUploaded = DateTime.Now; upload.Url = url; upload.FileName = fileName; upload.ContentType = (!string.IsNullOrEmpty(contentType)) ? contentType : "application/octet-stream"; upload.ContentLength = contentLength; upload.Key = key; upload.IV = iv; upload.KeySize = keySize; upload.BlockSize = blockSize; upload.DeleteKey = delKey; if (expirationUnit == ExpirationUnit.Views) { upload.MaxDownloads = expirationLength; } else { switch (expirationUnit) { case ExpirationUnit.Minutes: upload.ExpireDate = DateTime.Now.AddMinutes(expirationLength); break; case ExpirationUnit.Hours: upload.ExpireDate = DateTime.Now.AddHours(expirationLength); break; case ExpirationUnit.Days: upload.ExpireDate = DateTime.Now.AddDays(expirationLength); break; case ExpirationUnit.Months: upload.ExpireDate = DateTime.Now.AddMonths(expirationLength); break; case ExpirationUnit.Years: upload.ExpireDate = DateTime.Now.AddYears(expirationLength); break; } } db.Uploads.Add(upload); db.SaveChanges(); return(upload); }
public static Models.Upload SaveFile(TeknikEntities db, Config config, Stream file, string contentType, long contentLength, bool encrypt, ExpirationUnit expirationUnit, int expirationLength, string fileExt, string iv, string key) { return(SaveFile(db, config, file, contentType, contentLength, encrypt, expirationUnit, expirationLength, fileExt, iv, key, 256, 128)); }
public UploadSettings() { Encrypt = false; ExpirationLength = 1; ExpirationUnit = ExpirationUnit.Never; }
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); }