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