The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

TeknikMigration.cs 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using Microsoft.EntityFrameworkCore;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using Teknik.Areas.Paste;
  8. using Teknik.Areas.Paste.Models;
  9. using Teknik.Configuration;
  10. using Teknik.Data;
  11. using Teknik.Utilities;
  12. namespace Teknik.ServiceWorker
  13. {
  14. public static class TeknikMigration
  15. {
  16. public static bool RunMigration(TeknikEntities db, Config config)
  17. {
  18. bool success = false;
  19. MigratePastes(db, config);
  20. return success;
  21. }
  22. public static void MigratePastes(TeknikEntities db, Config config)
  23. {
  24. if (!Directory.Exists(config.PasteConfig.PasteDirectory))
  25. {
  26. Directory.CreateDirectory(config.PasteConfig.PasteDirectory);
  27. }
  28. var pastes = db.Pastes.Select(p => p.PasteId).ToList();
  29. foreach (var pasteId in pastes)
  30. {
  31. var paste = db.Pastes.Where(p => p.PasteId == pasteId).FirstOrDefault();
  32. if (!string.IsNullOrEmpty(paste.Content) && string.IsNullOrEmpty(paste.FileName) && string.IsNullOrEmpty(paste.HashedPassword))
  33. {
  34. // Generate a unique file name that does not currently exist
  35. string filePath = FileHelper.GenerateRandomFileName(config.PasteConfig.PasteDirectory, config.PasteConfig.FileExtension, 10);
  36. string fileName = Path.GetFileName(filePath);
  37. string key = PasteHelper.GenerateKey(config.PasteConfig.KeySize);
  38. string iv = PasteHelper.GenerateIV(config.PasteConfig.BlockSize);
  39. // Encrypt the contents to the file
  40. PasteHelper.EncryptContents(paste.Content, filePath, null, key, iv, config.PasteConfig.KeySize, config.PasteConfig.ChunkSize);
  41. // Generate a deletion key
  42. paste.DeleteKey = StringHelper.RandomString(config.PasteConfig.DeleteKeyLength);
  43. paste.Key = key;
  44. paste.KeySize = config.PasteConfig.KeySize;
  45. paste.IV = iv;
  46. paste.BlockSize = config.PasteConfig.BlockSize;
  47. paste.FileName = fileName;
  48. paste.Content = string.Empty;
  49. db.Entry(paste).State = EntityState.Modified;
  50. db.SaveChanges();
  51. }
  52. }
  53. }
  54. }
  55. }