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
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
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
}\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
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