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.

Uploader.cs 4.4KB

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