54 lines
1.8 KiB
C#
54 lines
1.8 KiB
C#
|
using DatabaseCore;
|
|||
|
using ModuleTools.Interfaces;
|
|||
|
using ModuleTools.Models;
|
|||
|
using SecurityBusinessLogic.BindingModels;
|
|||
|
using SecurityBusinessLogic.Interfaces;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.IO;
|
|||
|
using System.IO.Compression;
|
|||
|
using System.Linq;
|
|||
|
using System.Reflection;
|
|||
|
using System.Runtime.Serialization.Json;
|
|||
|
|
|||
|
namespace SecurityDatabaseImplementation.Implementations
|
|||
|
{
|
|||
|
public class BackupService : IBackupService
|
|||
|
{
|
|||
|
public OperationResultModel CreateBackUp(BackupBindingModel model)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
var asm = typeof(DatabaseManager).Assembly;
|
|||
|
MethodInfo method = GetType().GetTypeInfo().GetDeclaredMethod("SaveToFile");
|
|||
|
foreach (var t in asm.GetExportedTypes())
|
|||
|
{
|
|||
|
if (t.IsClass && t.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IEntitySecurityExtenstion<>)))
|
|||
|
{
|
|||
|
var elem = asm.CreateInstance(t.FullName);
|
|||
|
MethodInfo generic = method.MakeGenericMethod(elem.GetType());
|
|||
|
generic.Invoke(this, new object[] { model.FolderName, model.FullData, t });
|
|||
|
}
|
|||
|
}
|
|||
|
if (model.CreateArchive)
|
|||
|
{
|
|||
|
ZipFile.CreateFromDirectory(model.FolderName, $"{model.FolderName}.zip");
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
return OperationResultModel.Error(ex);
|
|||
|
}
|
|||
|
return OperationResultModel.Success(null);
|
|||
|
}
|
|||
|
|
|||
|
private void SaveToFile<T>(string folderName, bool allowFullData, Type t) where T : class, IEntitySecurityExtenstion<T>, new()
|
|||
|
{
|
|||
|
using var context = DatabaseManager.GetContext;
|
|||
|
var records = context.Set<T>().Select(x => x.SecurityCheck(x, allowFullData));
|
|||
|
DataContractJsonSerializer jsonFormatter = new(typeof(List<T>));
|
|||
|
using FileStream fs = new(string.Format("{0}/{1}.json", folderName, t.Name), FileMode.OpenOrCreate);
|
|||
|
jsonFormatter.WriteObject(fs, records);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|