Skip to content

Commit

Permalink
Try recovering all .pixi files if DocumentInfo.json can't be parsed
Browse files Browse the repository at this point in the history
  • Loading branch information
CPKreu committed Dec 14, 2023
1 parent 0334c81 commit 8924a45
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions src/PixiEditor/Models/DataHolders/CrashReport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,25 +285,45 @@ public bool TryRecoverDocuments(out List<RecoveredPixi> list)

public List<RecoveredPixi> RecoverDocuments()
{
var originalPathsEntry = ZipFile.Entries.First(entry => entry.FullName == "DocumentInfo.json");
List<RecoveredPixi> recoveredDocuments = new();

// Load original paths
Dictionary<string, string> paths;
var paths = TryGetOriginalPaths();
if (paths == null)
{
using Stream stream = originalPathsEntry.Open();
using StreamReader reader = new(stream);
string json = reader.ReadToEnd();
paths = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
}
recoveredDocuments.AddRange(
ZipFile.Entries
.Where(x =>
x.FullName.StartsWith("Documents") &&
x.FullName.EndsWith(".pixi"))
.Select(entry => new RecoveredPixi(null, entry)));

// Load .pixi files
List<RecoveredPixi> recoveredDocuments = new();
foreach (var path in paths)
{
recoveredDocuments.Add(new RecoveredPixi(path.Value, ZipFile.GetEntry($"Documents/{path.Key}")));
return recoveredDocuments;
}

recoveredDocuments.AddRange(paths.Select(path => new RecoveredPixi(path.Value, ZipFile.GetEntry($"Documents/{path.Key}"))));

return recoveredDocuments;

Dictionary<string, string>? TryGetOriginalPaths()
{
var originalPathsEntry = ZipFile.Entries.FirstOrDefault(entry => entry.FullName == "DocumentInfo.json");

if (originalPathsEntry == null)
return null;

try
{
using var stream = originalPathsEntry.Open();
using var reader = new StreamReader(stream);
string json = reader.ReadToEnd();

return JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
}
catch
{
return null;
}
}
}


Expand Down

0 comments on commit 8924a45

Please sign in to comment.