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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using Teknik.Configuration;
  6. using Teknik.Models;
  7. namespace Teknik.Areas.Upload
  8. {
  9. public static class Uploader
  10. {
  11. public static Models.Upload SaveFile(HttpPostedFileBase file)
  12. {
  13. return SaveFile(file, null, null);
  14. }
  15. public static Models.Upload SaveFile(HttpPostedFileBase file, string iv)
  16. {
  17. return SaveFile(file, iv, null);
  18. }
  19. public static Models.Upload SaveFile(HttpPostedFileBase file, string iv, string key)
  20. {
  21. Config config = Config.Load();
  22. TeknikEntities db = new TeknikEntities();
  23. // Generate a unique file name that does not currently exist
  24. string fileName = Utility.GenerateUniqueFileName(config.UploadConfig.UploadDirectory, config.UploadConfig.FileExtension, 10);
  25. // once we have the filename, lets save the file
  26. file.SaveAs(fileName);
  27. // Generate a unique url
  28. string extension = (config.UploadConfig.IncludeExtension) ? Utility.GetDefaultExtension(file.ContentType) : string.Empty;
  29. string url = Utility.RandomString(config.UploadConfig.UrlLength) + extension;
  30. while (db.Uploads.Where(u => u.Url == url).FirstOrDefault() != null)
  31. {
  32. url = Utility.RandomString(config.UploadConfig.UrlLength) + extension;
  33. }
  34. // Now we need to update the database with the new upload information
  35. Models.Upload upload = db.Uploads.Create();
  36. upload.DateUploaded = DateTime.Now;
  37. upload.Url = url;
  38. upload.FileName = fileName;
  39. upload.ContentLength = file.ContentLength;
  40. upload.ContentType = file.ContentType;
  41. upload.Key = key;
  42. upload.IV = iv;
  43. db.Uploads.Add(upload);
  44. return upload;
  45. }
  46. }
  47. }