]> granicus.if.org Git - handbrake/commitdiff
WinGui: Cleanup the QueueRecoveryHelper and make it a little smarter about how it...
authorsr55 <sr55.hb@outlook.com>
Sat, 14 Apr 2018 19:45:02 +0000 (20:45 +0100)
committersr55 <sr55.hb@outlook.com>
Sat, 14 Apr 2018 19:45:02 +0000 (20:45 +0100)
win/CS/HandBrakeWPF/Helpers/QueueRecoveryHelper.cs

index 83494ee36811e073a8d4987b19810a8a94e659a7..4b9aa4f81ebae0092927526a78a12ed2d4d061ba 100644 (file)
@@ -12,14 +12,12 @@ namespace HandBrakeWPF.Helpers
     using System;\r
     using System.Collections.Generic;\r
     using System.Diagnostics;\r
-    using System.Globalization;\r
     using System.IO;\r
     using System.Linq;\r
     using System.Text.RegularExpressions;\r
     using System.Windows;\r
     using System.Xml.Serialization;\r
 \r
-    using HandBrake.ApplicationServices.Model;\r
     using HandBrake.ApplicationServices.Utilities;\r
 \r
     using HandBrakeWPF.Services.Interfaces;\r
@@ -46,27 +44,29 @@ namespace HandBrakeWPF.Helpers
             try\r
             {\r
                 string tempPath = DirectoryUtilities.GetUserStoragePath(VersionHelper.IsNightly());\r
-                List<string> queueFiles = new List<string>();\r
                 DirectoryInfo info = new DirectoryInfo(tempPath);\r
-                IEnumerable<FileInfo> logFiles = info.GetFiles("*.xml").Where(f => f.Name.StartsWith("hb_queue_recovery"));\r
+                IEnumerable<FileInfo> foundFiles = info.GetFiles("*.xml").Where(f => f.Name.StartsWith("hb_queue_recovery"));\r
+                var queueFiles = GetFilesExcludingActiveProcesses(foundFiles);\r
 \r
-                if (!logFiles.Any())\r
+                if (!queueFiles.Any())\r
                 {\r
                     return queueFiles;\r
-                }\r
+                }        \r
 \r
                 List<string> removeFiles = new List<string>();\r
+                List<string> acceptedFiles = new List<string>();\r
+\r
                 XmlSerializer ser = new XmlSerializer(typeof(List<QueueTask>));\r
-                foreach (FileInfo file in logFiles)\r
+                foreach (string file in queueFiles)\r
                 {\r
                     try\r
                     {\r
-                        using (FileStream strm = new FileStream(file.FullName, FileMode.Open, FileAccess.Read))\r
+                        using (FileStream strm = new FileStream(file, FileMode.Open, FileAccess.Read))\r
                         {\r
                             List<QueueTask> list = ser.Deserialize(strm) as List<QueueTask>;\r
                             if (list != null && list.Count == 0)\r
                             {\r
-                                removeFiles.Add(file.FullName);\r
+                                removeFiles.Add(file);\r
                             }\r
 \r
                             if (list != null && list.Count != 0)\r
@@ -74,7 +74,11 @@ namespace HandBrakeWPF.Helpers
                                 List<QueueTask> tasks = list.Where(l => l.Status != QueueItemStatus.Completed).ToList();\r
                                 if (tasks.Count != 0)\r
                                 {\r
-                                    queueFiles.Add(file.Name);\r
+                                    acceptedFiles.Add(Path.GetFileName(file));\r
+                                }\r
+                                else\r
+                                {\r
+                                    removeFiles.Add(file);\r
                                 }\r
                             }\r
                         }\r
@@ -85,23 +89,9 @@ namespace HandBrakeWPF.Helpers
                     }\r
                 }\r
 \r
-                // Cleanup old/unused queue files for now.\r
-                foreach (string file in removeFiles)\r
-                {\r
-                    Match m = Regex.Match(file, @"([0-9]+).xml");\r
-                    if (m.Success)\r
-                    {\r
-                        int processId = int.Parse(m.Groups[1].ToString());\r
-                        if (GeneralUtilities.IsPidACurrentHandBrakeInstance(processId))\r
-                        {\r
-                            continue;\r
-                        }\r
-                    }\r
-\r
-                    File.Delete(file);\r
-                }\r
+                CleanupFiles(removeFiles);\r
 \r
-                return queueFiles;\r
+                return acceptedFiles;\r
             }\r
             catch (Exception exc)\r
             {\r
@@ -161,62 +151,58 @@ namespace HandBrakeWPF.Helpers
                 bool isRecovered = false;\r
                 foreach (string file in queueFiles)\r
                 {\r
-                    // Skip over the file if it belongs to another HandBrake instance.\r
-                    Match m = Regex.Match(file, @"([0-9]+).xml");\r
-                    if (m.Success)\r
-                    {\r
-                        int processId = int.Parse(m.Groups[1].ToString());\r
-                        if (processId != GeneralUtilities.ProcessId && GeneralUtilities.IsPidACurrentHandBrakeInstance(processId))\r
-                        {\r
-                            continue;\r
-                        }\r
-                    }\r
-\r
                     // Recover the Queue\r
                     encodeQueue.RestoreQueue(Path.Combine(appDataPath, file));\r
                     isRecovered = true;\r
 \r
                     // Cleanup\r
-                    if (!file.Contains(GeneralUtilities.ProcessId.ToString(CultureInfo.InvariantCulture)))\r
-                    {\r
-                        try\r
-                        {\r
-                            // Once we load it in, remove it as we no longer need it.\r
-                            File.Delete(Path.Combine(appDataPath, file));\r
-                        }\r
-                        catch (Exception)\r
-                        {\r
-                            // Keep quite, nothing much we can do if there are problems.\r
-                            // We will continue processing files.\r
-                        }\r
-                    }\r
+                    CleanupFiles(new List<string> { Path.Combine(appDataPath, file) });                   \r
                 }\r
 \r
                 return isRecovered;\r
             }\r
-            else\r
+\r
+            CleanupFiles(queueFiles);\r
+            return false;\r
+        }\r
+\r
+        private static List<string> GetFilesExcludingActiveProcesses(IEnumerable<FileInfo> foundFiles)\r
+        {\r
+            List<string> queueFiles = new List<string>();\r
+\r
+            // Remove any files where we have an active instnace.\r
+            foreach (FileInfo file in foundFiles)\r
             {\r
-                foreach (string file in queueFiles)\r
+                string fileProcessId = file.Name.Replace("hb_queue_recovery", string.Empty).Replace(".xml", string.Empty);\r
+                int processId = 0;\r
+                if (!string.IsNullOrEmpty(fileProcessId) && int.TryParse(fileProcessId, out processId))\r
                 {\r
-                    if (File.Exists(Path.Combine(appDataPath, file)))\r
+                    if (!GeneralUtilities.IsPidACurrentHandBrakeInstance(processId))\r
                     {\r
-                        // Check that the file doesn't belong to another running instance.\r
-                        Match m = Regex.Match(file, @"([0-9]+).xml");\r
-                        if (m.Success)\r
-                        {\r
-                            int processId = int.Parse(m.Groups[1].ToString());\r
-                            if (GeneralUtilities.IsPidACurrentHandBrakeInstance(processId))\r
-                            {\r
-                                continue;\r
-                            }\r
-                        }\r
+                        queueFiles.Add(file.FullName);\r
+                    }\r
+                }\r
+            }\r
+\r
+            return queueFiles;\r
+        }\r
 \r
-                        // Delete it if it doesn't\r
-                        File.Delete(Path.Combine(appDataPath, file));\r
+        private static void CleanupFiles(List<string> removeFiles)\r
+        {\r
+            // Cleanup old/unused queue files for now.\r
+            foreach (string file in removeFiles)\r
+            {\r
+                Match m = Regex.Match(file, @"([0-9]+).xml");\r
+                if (m.Success)\r
+                {\r
+                    int processId = int.Parse(m.Groups[1].ToString());\r
+                    if (GeneralUtilities.IsPidACurrentHandBrakeInstance(processId))\r
+                    {\r
+                        continue;\r
                     }\r
                 }\r
 \r
-                return false;\r
+                File.Delete(file);\r
             }\r
         }\r
     }\r