The next generation of the Teknik Services. Written in ASP.NET. Fork for blog tags.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Uploader.cs 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.IO;
  6. using Teknik.Configuration;
  7. using Teknik.Models;
  8. using Teknik.Utilities;
  9. using System.Text;
  10. using Org.BouncyCastle.Utilities.Encoders;
  11. using Teknik.Utilities.Cryptography;
  12. namespace Teknik.Areas.Upload
  13. {
  14. public static class Uploader
  15. {
  16. public static Models.Upload SaveFile(TeknikEntities db, Config config, System.IO.Stream file, string contentType, int contentLength, bool encrypt)
  17. {
  18. return SaveFile(db, config, file, contentType, contentLength, encrypt, string.Empty, null, null, 256, 128);
  19. }
  20. public static Models.Upload SaveFile(TeknikEntities db, Config config, System.IO.Stream file, string contentType, int contentLength, bool encrypt, string defaultExtension)
  21. {
  22. return SaveFile(db, config, file, contentType, contentLength, encrypt, defaultExtension, null, null, 256, 128);
  23. }
  24. public static Models.Upload SaveFile(TeknikEntities db, Config config, System.IO.Stream file, string contentType, int contentLength, bool encrypt, string defaultExtension, string iv)
  25. {
  26. return SaveFile(db, config, file, contentType, contentLength, encrypt, defaultExtension, iv, null, 256, 128);
  27. }
  28. public static Models.Upload SaveFile(TeknikEntities db, Config config, System.IO.Stream file, string contentType, int contentLength, bool encrypt, string defaultExtension, string iv, string key)
  29. {
  30. return SaveFile(db, config, file, contentType, contentLength, encrypt, defaultExtension, iv, key, 256, 128);
  31. }
  32. public static Models.Upload SaveFile(TeknikEntities db, Config config, System.IO.Stream file, string contentType, int contentLength, bool encrypt, string defaultExtension, string iv, string key, int keySize, int blockSize)
  33. {
  34. if (!Directory.Exists(config.UploadConfig.UploadDirectory))
  35. {
  36. Directory.CreateDirectory(config.UploadConfig.UploadDirectory);
  37. }
  38. // Generate a unique file name that does not currently exist
  39. string filePath = FileHelper.GenerateRandomFileName(config.UploadConfig.UploadDirectory, config.UploadConfig.FileExtension, 10);
  40. string fileName = Path.GetFileName(filePath);
  41. // once we have the filename, lets save the file
  42. if (encrypt)
  43. {
  44. // Generate a key and iv
  45. key = StringHelper.RandomString(config.UploadConfig.KeySize / 8);
  46. iv = StringHelper.RandomString(config.UploadConfig.BlockSize / 8);
  47. byte[] keyBytes = Encoding.UTF8.GetBytes(key);
  48. byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
  49. // Encrypt the file to disk
  50. AES.EncryptToFile(filePath, file, config.UploadConfig.ChunkSize, keyBytes, ivBytes);
  51. }
  52. else
  53. {
  54. // Just write the stream to the file
  55. using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
  56. {
  57. file.Seek(0, SeekOrigin.Begin);
  58. file.CopyTo(fileStream);
  59. }
  60. }
  61. // Generate a unique url
  62. string extension = (config.UploadConfig.IncludeExtension) ? FileHelper.GetDefaultExtension(contentType, defaultExtension) : string.Empty;
  63. string url = StringHelper.RandomString(config.UploadConfig.UrlLength) + extension;
  64. while (db.Uploads.Where(u => u.Url == url).FirstOrDefault() != null)
  65. {
  66. url = StringHelper.RandomString(config.UploadConfig.UrlLength) + extension;
  67. }
  68. // Generate a deletion key
  69. string delKey = StringHelper.RandomString(config.UploadConfig.DeleteKeyLength);
  70. // Now we need to update the database with the new upload information
  71. Models.Upload upload = db.Uploads.Create();
  72. upload.DateUploaded = DateTime.Now;
  73. upload.Url = url;
  74. upload.FileName = fileName;
  75. upload.ContentType = (!string.IsNullOrEmpty(contentType)) ? contentType : "application/octet-stream";
  76. upload.ContentLength = contentLength;
  77. upload.Key = key;
  78. upload.IV = iv;
  79. upload.KeySize = keySize;
  80. upload.BlockSize = blockSize;
  81. upload.DeleteKey = delKey;
  82. db.Uploads.Add(upload);
  83. db.SaveChanges();
  84. return upload;
  85. }
  86. }
  87. }