107 lines
No EOL
3.3 KiB
C#
107 lines
No EOL
3.3 KiB
C#
using System.IO.Compression;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace wp2ghost
|
|
{
|
|
// =============================================================================
|
|
// GhostExporter — Ghost ZIP erstellen
|
|
// =============================================================================
|
|
static class GhostExporter
|
|
{
|
|
private static string BaseDir = "";
|
|
private static string MarkdownDir = "";
|
|
|
|
public static int PostCount => PostFetcher.PostCount;
|
|
|
|
public static void Init(string baseDir)
|
|
{
|
|
BaseDir = baseDir;
|
|
MarkdownDir = Path.Combine(baseDir, "markdown");
|
|
}
|
|
|
|
public static async Task CreateZipAsync()
|
|
{
|
|
Console.WriteLine();
|
|
Console.WriteLine("Erstelle Ghost ZIP-Datei...");
|
|
|
|
var posts = PostFetcher.Posts;
|
|
if (posts.Count == 0)
|
|
{
|
|
Console.WriteLine("Keine Beiträge geladen. Bitte zuerst Option 1 ausführen.");
|
|
return;
|
|
}
|
|
|
|
string outputPath = Path.Combine(BaseDir, Program.OutputZipPath);
|
|
|
|
try
|
|
{
|
|
//PostFetcher.SaveAllMarkdownFiles();
|
|
|
|
string jsonString = JsonSerializer.Serialize(new GhostRoot
|
|
{
|
|
Db = new List<GhostDb> { new GhostDb { Data = new GhostData { Posts = posts.ToList() } } }
|
|
}, new JsonSerializerOptions
|
|
{
|
|
WriteIndented = true,
|
|
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
|
|
});
|
|
|
|
string outputDir = Path.GetDirectoryName(outputPath) ?? BaseDir;
|
|
if (!Directory.Exists(outputDir))
|
|
Directory.CreateDirectory(outputDir);
|
|
|
|
using (var zipStream = new FileStream(outputPath, FileMode.Create))
|
|
using (var archive = new ZipArchive(zipStream, ZipArchiveMode.Create, true))
|
|
{
|
|
var jsonEntry = archive.CreateEntry("import.json");
|
|
using (var writer = new StreamWriter(jsonEntry.Open()))
|
|
{
|
|
await writer.WriteAsync(jsonString);
|
|
}
|
|
|
|
if (Directory.Exists(MarkdownDir))
|
|
{
|
|
foreach (string mdFile in Directory.GetFiles(MarkdownDir, "*.md", SearchOption.AllDirectories))
|
|
{
|
|
string relativeMdPath = Path.GetFileName(mdFile);
|
|
var mdEntry = archive.CreateEntry($"content/{relativeMdPath}");
|
|
using (var entryStream = mdEntry.Open())
|
|
using (var fileStream = File.OpenRead(mdFile))
|
|
{
|
|
await fileStream.CopyToAsync(entryStream);
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach (var file in ImageManager.CachedFiles)
|
|
{
|
|
string fullLocalPath = Path.Combine(Path.Combine(BaseDir, "images"), file.RelativePath);
|
|
if (File.Exists(fullLocalPath))
|
|
{
|
|
var fileEntry = archive.CreateEntry($"content/images/{file.RelativePath}");
|
|
using (var entryStream = fileEntry.Open())
|
|
using (var fileStream = File.OpenRead(fullLocalPath))
|
|
{
|
|
await fileStream.CopyToAsync(entryStream);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
Console.WriteLine();
|
|
Console.WriteLine("=========================================");
|
|
Console.WriteLine($" Ghost ZIP erstellt!");
|
|
Console.WriteLine($" Beiträge: {posts.Count}");
|
|
Console.WriteLine($" Bilder: {ImageManager.CachedFiles.Count} Dateien");
|
|
Console.WriteLine($" Ausgabe: {outputPath}");
|
|
Console.WriteLine("=========================================");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"\nFehler beim Erstellen der ZIP-Datei: {ex.Message}");
|
|
Console.WriteLine($"Stack Trace: {ex.StackTrace}");
|
|
}
|
|
}
|
|
}
|
|
} |