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

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