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

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