Browse Source

Try recovering all .pixi files if DocumentInfo.json can't be parsed

CPKreuz 1 year ago
parent
commit
8924a45cfa
1 changed files with 33 additions and 13 deletions
  1. 33 13
      src/PixiEditor/Models/DataHolders/CrashReport.cs

+ 33 - 13
src/PixiEditor/Models/DataHolders/CrashReport.cs

@@ -285,25 +285,45 @@ internal class CrashReport : IDisposable
     
     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;
+            }
+        }
     }