Files
BeWoPlaner/SharedLauncher/Logic/FileController.cs

195 lines
6.2 KiB
C#

using BS.SharedLauncher.DataContract;
using BS.SharedLauncher.Enums;
using BS.SharedLauncher.Extensions;
using BS.SharedLauncher.Information;
using BS.SharedLauncher.Update;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace BS.SharedLauncher.Logic
{
public static class FileController
{
private static string DotBeWoInfo => BeWoPathFileConfig.DotBeWoInfo;
public static List<UpdateFileDC> GetDownloadedFiles(string downloadFolder)
{
return GetPlanerFiles(downloadFolder, true, null);
}
public static UpdatePlanDC GetApplyPackage(string applyFolder)
{
TryGetPackageInfo(applyFolder, out var package);
if (package == null)
package = new UpdatePlanDC();
package.Files = GetAppliedFiles(applyFolder, true);
return package;
}
public static List<UpdateFileDC> GetAppliedFiles(string applyFolder, bool hash)
{
return GetPlanerFiles(applyFolder, hash, null);
}
public static UpdatePlanDC LoadPackageInfo(string filepath)
{
filepath = filepath.WithInfoExtension();
var xml = File.ReadAllText(filepath);
var obj = XmlConverter.Deserialize<UpdatePlanDC>(xml);
return obj;
}
public static bool TryGetPackageInfo(string filepath, out UpdatePlanDC planerPackageInfoDC)
{
planerPackageInfoDC = null;
try
{
planerPackageInfoDC = LoadPackageInfo(filepath);
}
catch (Exception)
{
return false;
}
return true;
}
#region file
public static void SaveFile(string path, byte[] data)
{
File.WriteAllBytes(path, data);
DownloadLogger.Post($"Save File: {path}");
}
public static void CopyFile(string source, string destination)
{
File.Copy(source, destination, true);
DownloadLogger.Post($"Copy File: {source} -> {destination}");
}
public static void DeleteFile(string source)
{
File.Delete(source);
DownloadLogger.Post($"Delete File: {source}");
}
#endregion
#region info
public static void SaveInfo<T>(string path, T t)
where T : class
{
File.WriteAllText(path.WithInfoExtension(), XmlConverter.Serialize<T>(t));
DownloadLogger.Post($"Info Save: {path}");
}
public static void DeleteInfo(string path)
{
File.Delete(path.WithInfoExtension());
DownloadLogger.Post($"Info delete: {path}");
}
public static string WithInfoExtension(this string path)
{
if (path.EndsWith(DotBeWoInfo))
return path;
return path + DotBeWoInfo;
}
#endregion
public static void DeleteEmptyDirectories(string path)
{
// Witzige Sache: Generiert mit chat.openai.com/chat
// Recursive function to delete empty directories
// Get all subdirectories in the specified path
string[] subdirectories = Directory.GetDirectories(path);
// Loop through each subdirectory
foreach (string subdirectory in subdirectories)
{
// Call the recursive function to delete empty directories in the subdirectory
DeleteEmptyDirectories(subdirectory);
// Check if the subdirectory is empty
if (IsDirectoryEmpty(subdirectory))
{
// If the subdirectory is empty, delete it
Directory.Delete(subdirectory);
DownloadLogger.Post($"Deleted empty directory: {subdirectory}");
}
}
}
public static List<UpdateFileDC> GetPlanerFiles(string path, bool hash, Func<UpdateFileDC, bool> skipFile)
{
var files = new List<UpdateFileDC>();
// Sollte der Path ohne / enden, wird der unterordner mit angegeben
// Deshalb manuell hinzufügen, falls nicht vorhanden
if (!path.EndsWith(Path.DirectorySeparatorChar.ToString()))
path += Path.DirectorySeparatorChar;
GetPlanerFilesRecursive(new DirectoryInfo(path), path, ref files, hash, skipFile);
return files;
}
public static void GetPlanerFilesRecursive(DirectoryInfo directoryInfo, string relativeTo, ref List<UpdateFileDC> files, bool load_hash, Func<UpdateFileDC, bool> skipFile)
{
foreach (var item in directoryInfo.GetDirectories())
{
GetPlanerFilesRecursive(item, relativeTo, ref files, load_hash, skipFile);
}
foreach (var item in directoryInfo.GetFiles())
{
var file = new UpdateFileDC(item, relativeTo);
if (file.IgnoreFile())
continue;
if (skipFile is object && skipFile(file))
continue;
if (load_hash)
file.LoadHash();
files.Add(file);
}
}
public static bool IsDirectoryWritable(string dirPath, bool throwIfFails = false)
{
try
{
Directory.CreateDirectory(dirPath);
using (FileStream fs = File.Create(
Path.Combine(
dirPath,
BeWoPathFileConfig.TestForPermissionFile
),
1,
FileOptions.DeleteOnClose)
)
{ }
return true;
}
catch
{
if (throwIfFails)
throw;
else
return false;
}
}
private static bool IsDirectoryEmpty(string path)
{
return !Directory.EnumerateFileSystemEntries(path).Any();
}
}
}