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.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. 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 defaultExtension)
  20. {
  21. return SaveFile(db, config, file, contentType, contentLength, encrypt, defaultExtension, 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 defaultExtension, string iv)
  24. {
  25. return SaveFile(db, config, file, contentType, contentLength, encrypt, defaultExtension, 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 defaultExtension, string iv, string key)
  28. {
  29. return SaveFile(db, config, file, contentType, contentLength, encrypt, defaultExtension, 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 defaultExtension, 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. key = StringHelper.RandomString(config.UploadConfig.KeySize / 8);
  45. iv = StringHelper.RandomString(config.UploadConfig.BlockSize / 8);
  46. byte[] keyBytes = Encoding.UTF8.GetBytes(key);
  47. byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
  48. // Encrypt the file to disk
  49. AES.EncryptToFile(filePath, file, config.UploadConfig.ChunkSize, keyBytes, ivBytes, "CTR", "NoPadding");
  50. }
  51. else
  52. {
  53. // Just write the stream to the file
  54. using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
  55. {
  56. file.Seek(0, SeekOrigin.Begin);
  57. file.CopyTo(fileStream);
  58. }
  59. }
  60. // Generate a unique url
  61. string extension = (config.UploadConfig.IncludeExtension) ? FileHelper.GetDefaultExtension(contentType, defaultExtension) : string.Empty;
  62. string url = StringHelper.RandomString(config.UploadConfig.UrlLength) + extension;
  63. while (db.Uploads.Where(u => u.Url == url).FirstOrDefault() != null)
  64. {
  65. url = StringHelper.RandomString(config.UploadConfig.UrlLength) + extension;
  66. }
  67. // Now we need to update the database with the new upload information
  68. Models.Upload upload = db.Uploads.Create();
  69. upload.DateUploaded = DateTime.Now;
  70. upload.Url = url;
  71. upload.FileName = fileName;
  72. upload.ContentType = (!string.IsNullOrEmpty(contentType)) ? contentType : "application/octet-stream";
  73. upload.ContentLength = contentLength;
  74. upload.Key = key;
  75. upload.IV = iv;
  76. upload.KeySize = keySize;
  77. upload.BlockSize = blockSize;
  78. db.Uploads.Add(upload);
  79. db.SaveChanges();
  80. return upload;
  81. }
  82. }
  83. }