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 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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.Helpers;
  9. namespace Teknik.Areas.Upload
  10. {
  11. public static class Uploader
  12. {
  13. public static Models.Upload SaveFile(TeknikEntities db, Config config, byte[] file, string contentType, int contentLength)
  14. {
  15. return SaveFile(db, config, file, contentType, contentLength, string.Empty, null, null, 256, 128);
  16. }
  17. public static Models.Upload SaveFile(TeknikEntities db, Config config, byte[] file, string contentType, int contentLength, string defaultExtension)
  18. {
  19. return SaveFile(db, config, file, contentType, contentLength, defaultExtension, null, null, 256, 128);
  20. }
  21. public static Models.Upload SaveFile(TeknikEntities db, Config config, byte[] file, string contentType, int contentLength, string defaultExtension, string iv)
  22. {
  23. return SaveFile(db, config, file, contentType, contentLength, defaultExtension, iv, null, 256, 128);
  24. }
  25. public static Models.Upload SaveFile(TeknikEntities db, Config config, byte[] file, string contentType, int contentLength, string defaultExtension, string iv, string key)
  26. {
  27. return SaveFile(db, config, file, contentType, contentLength, defaultExtension, iv, key, 256, 128);
  28. }
  29. public static Models.Upload SaveFile(TeknikEntities db, Config config, byte[] file, string contentType, int contentLength, string defaultExtension, string iv, string key, int keySize, int blockSize)
  30. {
  31. if (!Directory.Exists(config.UploadConfig.UploadDirectory))
  32. {
  33. Directory.CreateDirectory(config.UploadConfig.UploadDirectory);
  34. }
  35. // Generate a unique file name that does not currently exist
  36. string filePath = Utility.GenerateUniqueFileName(config.UploadConfig.UploadDirectory, config.UploadConfig.FileExtension, 10);
  37. string fileName = Path.GetFileName(filePath);
  38. // once we have the filename, lets save the file
  39. File.WriteAllBytes(filePath, file);
  40. // Generate a unique url
  41. string extension = (config.UploadConfig.IncludeExtension) ? Utility.GetDefaultExtension(contentType, defaultExtension) : string.Empty;
  42. string url = Utility.RandomString(config.UploadConfig.UrlLength) + extension;
  43. while (db.Uploads.Where(u => u.Url == url).FirstOrDefault() != null)
  44. {
  45. url = Utility.RandomString(config.UploadConfig.UrlLength) + extension;
  46. }
  47. // Now we need to update the database with the new upload information
  48. Models.Upload upload = db.Uploads.Create();
  49. upload.DateUploaded = DateTime.Now;
  50. upload.Url = url;
  51. upload.FileName = fileName;
  52. upload.ContentType = (!string.IsNullOrEmpty(contentType)) ? contentType : "application/octet-stream";
  53. upload.ContentLength = contentLength;
  54. upload.Key = key;
  55. upload.IV = iv;
  56. upload.KeySize = keySize;
  57. upload.BlockSize = blockSize;
  58. db.Uploads.Add(upload);
  59. db.SaveChanges();
  60. return upload;
  61. }
  62. }
  63. }