Ejemplo n.º 1
0
        /// <summary>
        /// Compares the xml file list with the files currently in the library
        /// Downloads any missing files
        /// For file types of Layout will fire a LayoutChanged event, giving the filename of the layout changed
        /// </summary>
        public void CompareAndCollect()
        {
            XmlNodeList fileNodes = _xml.SelectNodes("/files/file");

            //Inspect each file we have here
            foreach (XmlNode file in fileNodes)
            {
                XmlAttributeCollection attributes = file.Attributes;
                RequiredFile fileList = new RequiredFile();

                if (attributes["type"].Value == "layout")
                {
                    // Layout
                    string path = attributes["path"].Value;

                    // Does this file exist?
                    if (File.Exists(Properties.Settings.Default.LibraryPath + @"\" + path + ".xlf"))
                    {
                        // Calculate a MD5 for the current file
                        String md5 = _cacheManager.GetMD5(path + ".xlf");

                        System.Diagnostics.Debug.WriteLine(String.Format("Comparing current MD5 [{0}] with given MD5 [{1}]", md5, attributes["md5"].Value));

                        // Now we have the md5, compare it to the md5 in the xml
                        if (attributes["md5"].Value != md5)
                        {
                            // They are different
                            _cacheManager.Remove(path + ".xlf");

                            //TODO: This might be bad! Delete the old layout as it is wrong
                            try
                            {
                                File.Delete(Properties.Settings.Default.LibraryPath + @"\" + path + ".xlf");
                            }
                            catch (Exception ex)
                            {
                                Trace.WriteLine(new LogMessage("CompareAndCollect", "Unable to delete incorrect file because: " + ex.Message));
                            }

                            // Get the file and save it
                            fileList.chunkOffset = 0;
                            fileList.chunkSize = 0;
                            fileList.complete = false;
                            fileList.downloading = false;
                            fileList.path = path;
                            fileList.type = "layout";
                            fileList.md5 = attributes["md5"].Value;
                            fileList.retrys = 0;

                            _files.Add(fileList);
                        }
                        else
                        {
                            // The MD5 of the current file and the MD5 in RequiredFiles are the same.
                            // Therefore make sure this MD5 is in the CacheManager
                            _cacheManager.Add(path + ".xlf", md5);

                            _requiredFiles.MarkComplete(int.Parse(path), md5);
                        }
                    }
                    else
                    {
                        // No - get the file and save it (no chunks)
                        fileList.chunkOffset = 0;
                        fileList.chunkSize = 0;
                        fileList.complete = false;
                        fileList.downloading = false;
                        fileList.path = path;
                        fileList.type = "layout";
                        fileList.md5 = attributes["md5"].Value;
                        fileList.retrys = 0;

                        _files.Add(fileList);
                    }
                }
                else if (attributes["type"].Value == "media")
                {
                    // Media
                    string path = attributes["path"].Value;

                    // Does this media exist?
                    if (File.Exists(Properties.Settings.Default.LibraryPath + @"\" + path))
                    {
                        String md5 = _cacheManager.GetMD5(path);

                        System.Diagnostics.Debug.WriteLine(String.Format("Comparing current MD5 [{0}] with given MD5 [{1}]", md5, attributes["md5"].Value));

                        // MD5 the file to make sure it is the same.
                        if (md5 != attributes["md5"].Value)
                        {
                            // File changed
                            _cacheManager.Remove(path);

                            // Delete the old media as it is wrong
                            try
                            {
                                File.Delete(Properties.Settings.Default.LibraryPath + @"\" + path);
                            }
                            catch (Exception ex)
                            {
                                Trace.WriteLine(new LogMessage("CompareAndCollect", "Unable to delete incorrect file because: " + ex.Message));
                            }

                            // Add to queue
                            fileList.chunkOffset = 0;
                            fileList.chunkSize = 512000;
                            fileList.complete = false;
                            fileList.downloading = false;
                            fileList.path = path;
                            fileList.type = "media";
                            fileList.size = int.Parse(attributes["size"].Value);
                            fileList.md5 = attributes["md5"].Value;
                            fileList.retrys = 0;

                            _files.Add(fileList);
                        }
                        else
                        {
                            // The MD5 of the current file and the MD5 in RequiredFiles are the same.
                            // Therefore make sure this MD5 is in the CacheManager
                            _cacheManager.Add(path, md5);

                            string[] filePart = path.Split('.');
                            _requiredFiles.MarkComplete(int.Parse(filePart[0]), md5);
                        }
                    }
                    else
                    {
                        // No - Get it (async call - with chunks... through another class?)
                        fileList.chunkOffset = 0;
                        fileList.chunkSize = 512000;
                        fileList.complete = false;
                        fileList.downloading = false;
                        fileList.path = path;
                        fileList.type = "media";
                        fileList.size = int.Parse(attributes["size"].Value);
                        fileList.md5 = attributes["md5"].Value;
                        fileList.retrys = 0;

                        _files.Add(fileList);
                    }
                }
                else if (attributes["type"].Value == "blacklist")
                {
                    // Expect <file type="blacklist"><file id="" /></file>
                    XmlNodeList items = file.ChildNodes;

                    BlackList blackList = new BlackList();

                    try { blackList.Truncate(); }
                    catch { }

                    if (items.Count > 0)
                    {
                        blackList.Add(items);

                        blackList.Dispose();
                        blackList = null;
                    }

                    items = null;
                }
                else
                {
                    //Ignore node
                }
            }

            Debug.WriteLine(String.Format("There are {0} files to get", _files.Count.ToString()));

            // Output a list of the files we need to get
            string debugMessage = "";

            foreach (RequiredFile fileToGet in _files)
                debugMessage += string.Format("File: {0}, Type: {1}, MD5: {2}. ", fileToGet.path, fileToGet.type, fileToGet.md5);

            Debug.WriteLine(debugMessage);

            // Report the files files back to XMDS
            _requiredFiles.ReportInventory();

            // Write Required Files
            _requiredFiles.WriteRequiredFiles();

            // Is there anything to get?
            if (_files.Count == 0)
            {
                CollectionComplete();
                return;
            }

            // Start with the first file
            _currentFile = 0;

            // Preload the first filelist
            _currentFileList = _files[_currentFile];

            // Get the first file
            GetFile();
        }