The next generation of the Teknik Services. Written in ASP.NET. Fork for blog tags.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Uploader.cs 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. 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. AesCounterManaged.EncryptToFile(filePath, file, config.UploadConfig.ChunkSize, keyBytes, ivBytes);
  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) ? fileExt : 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. // Generate a deletion key
  68. string delKey = StringHelper.RandomString(config.UploadConfig.DeleteKeyLength);
  69. // Now we need to update the database with the new upload information
  70. Models.Upload upload = db.Uploads.Create();
  71. upload.DateUploaded = DateTime.Now;
  72. upload.Url = url;
  73. upload.FileName = fileName;
  74. upload.ContentType = (!string.IsNullOrEmpty(contentType)) ? contentType : "application/octet-stream";
  75. upload.ContentLength = contentLength;
  76. upload.Key = key;
  77. upload.IV = iv;
  78. upload.KeySize = keySize;
  79. upload.BlockSize = blockSize;
  80. upload.DeleteKey = delKey;
  81. db.Uploads.Add(upload);
  82. db.SaveChanges();
  83. return upload;
  84. }
  85. }
  86. }