The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
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.

UploadHelper.cs 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 Teknik.Utilities.Cryptography;
  11. using Teknik.Data;
  12. namespace Teknik.Areas.Upload
  13. {
  14. public static class UploadHelper
  15. {
  16. public static Models.Upload SaveFile(TeknikEntities db, Config config, Stream file, string contentType, long contentLength, bool encrypt, ExpirationUnit expirationUnit, int expirationLength)
  17. {
  18. return SaveFile(db, config, file, contentType, contentLength, encrypt, expirationUnit, expirationLength, string.Empty, null, null, 256, 128);
  19. }
  20. public static Models.Upload SaveFile(TeknikEntities db, Config config, Stream file, string contentType, long contentLength, bool encrypt, ExpirationUnit expirationUnit, int expirationLength, string fileExt)
  21. {
  22. return SaveFile(db, config, file, contentType, contentLength, encrypt, expirationUnit, expirationLength, fileExt, null, null, 256, 128);
  23. }
  24. 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)
  25. {
  26. return SaveFile(db, config, file, contentType, contentLength, encrypt, expirationUnit, expirationLength, fileExt, iv, null, 256, 128);
  27. }
  28. 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)
  29. {
  30. return SaveFile(db, config, file, contentType, contentLength, encrypt, expirationUnit, expirationLength, fileExt, iv, key, 256, 128);
  31. }
  32. 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)
  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. if (string.IsNullOrEmpty(key))
  46. key = StringHelper.RandomString(config.UploadConfig.KeySize / 8);
  47. if (string.IsNullOrEmpty(iv))
  48. iv = StringHelper.RandomString(config.UploadConfig.BlockSize / 8);
  49. byte[] keyBytes = Encoding.UTF8.GetBytes(key);
  50. byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
  51. // Encrypt the file to disk
  52. AesCounterManaged.EncryptToFile(filePath, file, config.UploadConfig.ChunkSize, keyBytes, ivBytes);
  53. }
  54. else
  55. {
  56. // Just write the stream to the file
  57. using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
  58. {
  59. file.Seek(0, SeekOrigin.Begin);
  60. file.CopyTo(fileStream);
  61. }
  62. }
  63. // Generate a unique url
  64. string extension = (config.UploadConfig.IncludeExtension) ? fileExt : string.Empty;
  65. string url = StringHelper.RandomString(config.UploadConfig.UrlLength) + extension;
  66. while (db.Uploads.Where(u => u.Url == url).FirstOrDefault() != null)
  67. {
  68. url = StringHelper.RandomString(config.UploadConfig.UrlLength) + extension;
  69. }
  70. // Generate a deletion key
  71. string delKey = StringHelper.RandomString(config.UploadConfig.DeleteKeyLength);
  72. // Now we need to update the database with the new upload information
  73. Models.Upload upload = new Models.Upload();
  74. upload.DateUploaded = DateTime.Now;
  75. upload.Url = url;
  76. upload.FileName = fileName;
  77. upload.ContentType = (!string.IsNullOrEmpty(contentType)) ? contentType : "application/octet-stream";
  78. upload.ContentLength = contentLength;
  79. upload.Key = key;
  80. upload.IV = iv;
  81. upload.KeySize = keySize;
  82. upload.BlockSize = blockSize;
  83. upload.DeleteKey = delKey;
  84. if (expirationUnit == ExpirationUnit.Views)
  85. {
  86. upload.MaxDownloads = expirationLength;
  87. }
  88. else
  89. {
  90. switch (expirationUnit)
  91. {
  92. case ExpirationUnit.Minutes:
  93. upload.ExpireDate = DateTime.Now.AddMinutes(expirationLength);
  94. break;
  95. case ExpirationUnit.Hours:
  96. upload.ExpireDate = DateTime.Now.AddHours(expirationLength);
  97. break;
  98. case ExpirationUnit.Days:
  99. upload.ExpireDate = DateTime.Now.AddDays(expirationLength);
  100. break;
  101. case ExpirationUnit.Months:
  102. upload.ExpireDate = DateTime.Now.AddMonths(expirationLength);
  103. break;
  104. case ExpirationUnit.Years:
  105. upload.ExpireDate = DateTime.Now.AddYears(expirationLength);
  106. break;
  107. }
  108. }
  109. db.Uploads.Add(upload);
  110. db.SaveChanges();
  111. return upload;
  112. }
  113. public static bool CheckExpiration(Models.Upload upload)
  114. {
  115. if (upload.ExpireDate != null && DateTime.Now >= upload.ExpireDate)
  116. return true;
  117. if (upload.MaxDownloads > 0 && upload.Downloads >= upload.MaxDownloads)
  118. return true;
  119. return false;
  120. }
  121. }
  122. }