84 lines
2.0 KiB
C#
84 lines
2.0 KiB
C#
using Gremlin_BlazorServer.Data.EntityClasses;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
|
|
namespace Gremlin_BlazorServer.Services;
|
|
|
|
internal static class FileService
|
|
{
|
|
private static readonly Encoding defaultEncodingIfNoBom = Encoding.UTF8;
|
|
|
|
public static string ReadResource(string name)
|
|
{
|
|
Assembly assembly = Assembly.GetExecutingAssembly();
|
|
// Format: "{Namespace}.{Folder}.{filename}.{Extension}"
|
|
string resourcePath = name.StartsWith(nameof(Gremlin_BlazorServer))
|
|
? name
|
|
: assembly.GetManifestResourceNames().Single(str => str.EndsWith(name));
|
|
using Stream? stream = assembly.GetManifestResourceStream(resourcePath);
|
|
if (stream == null)
|
|
{
|
|
return "";
|
|
}
|
|
|
|
using StreamReader reader = new(stream);
|
|
return reader.ReadToEnd();
|
|
}
|
|
|
|
public static Encoding GetEncoding(string fileName)
|
|
{
|
|
Stream fileStream = File.OpenRead(fileName);
|
|
using StreamReader reader = new(fileStream, defaultEncodingIfNoBom, true);
|
|
_ = reader.Peek();
|
|
Encoding encoding = reader.CurrentEncoding;
|
|
return encoding;
|
|
}
|
|
|
|
public static void WriteQuoteToTsv(string quoteContent, Quote quote)
|
|
{
|
|
string datei = $"{quote.Path}{Path.DirectorySeparatorChar}{quote.QuotationNumber}.tsv";
|
|
FileInfo fileInfo = new(datei);
|
|
if (fileInfo.Directory == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!fileInfo.Directory.Exists)
|
|
{
|
|
try
|
|
{
|
|
_ = Directory.CreateDirectory(fileInfo.DirectoryName ?? "");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex);
|
|
}
|
|
}
|
|
|
|
using StreamWriter writer = new(datei, false, Encoding.UTF8);
|
|
{
|
|
writer.WriteLine(quoteContent);
|
|
}
|
|
}
|
|
|
|
public static async Task WriteTexFile(Quote quote)
|
|
{
|
|
string datei = $"{quote.Path}{Path.DirectorySeparatorChar}{quote.QuotationNumber}.tex";
|
|
FileInfo fileInfo = new(datei);
|
|
if (fileInfo.Directory == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!fileInfo.Directory.Exists)
|
|
{
|
|
_ = Directory.CreateDirectory(fileInfo.DirectoryName ?? "");
|
|
}
|
|
|
|
using StreamWriter writer = new(datei, false, Encoding.UTF8);
|
|
{
|
|
await writer.WriteLineAsync(quote.Tex);
|
|
}
|
|
}
|
|
}
|