} // end function backup monitored file /// <summary> /// Downloads a monitored file /// </summary> /// <param name="http"></param> /// <param name="version"></param> /// <param name="fileName"></param> private void DownloadMonitoredFile( ref RealSimpleNet.Helpers.Http http, string version, models.Manifest manifest, string fileName) { // Prepare folder structure Console.WriteLine("Download Monitored file: " + fileName); // The separator string[] sep = new string[] { "\\" }; // Split by directorty separator string[] parts = fileName.Split(sep, StringSplitOptions.RemoveEmptyEntries); // If there's more than one part, then the file is a subdirectory if (parts.Length > 1) { int i = 0; string treeDir = ""; // Loop through the directories for (i = 0; i < parts.Length - 1; i++) { // Setup the tree (parent and current directories) treeDir += "\\" + parts[i]; // If not exists, created if (!Directory.Exists(treeDir)) { Directory.CreateDirectory(treeDir); } // end if not exists } // end for } // end if parts > 1 // Setup source and destination string source = string.Format("{0}/{1}/{2}", manifest.ftpcredentials.Url, version, fileName); string destination = fileName + ".tmp"; // Actually download the file http.Download( source, destination, manifest.ftpcredentials.User, manifest.ftpcredentials.Pwd ); // end download } // end function DownloadManifestFile
} // end function ValidateManifest /// <summary> /// Upgrades the application /// First read the manifest /// Then look for the latest version /// If the latest version is bigger than the current version, updates the application /// Download new manifest /// Compare the files in both manifests /// If there's an upgrade, backup current file and download the new one /// </summary> public void Upgrade() { // Read the manifest this.ReadManifest(); // Get the current manifest string currentVersion = this.currentManifest.version; Log("Current version is {0}", currentVersion); // This will download the files RealSimpleNet.Helpers.Http http = new RealSimpleNet.Helpers.Http(); // Donwload the latest flag http.Download( currentManifest.ftpcredentials.Url + "/latest", "latest.tmp", currentManifest.ftpcredentials.User, currentManifest.ftpcredentials.Pwd ); // end http download // Read the downloaded flag string version = File.ReadAllText("latest.tmp"); Log("Last version is {0}", version); // Here we compare versions if (version.CompareTo(currentManifest.version) < 0) { // If same version, exit Log("Old version. Exiting"); return; } if (version.CompareTo(currentManifest.version) == 0) { // If same version, exit Log("Same version. Check if local files exists"); // Check files in manifest exists bool existFiles = true; currentManifest.files.ForEach(f => existFiles = existFiles && File.Exists(f.filename)); if (existFiles) { Log("Local files exists. Exiting"); StartMain(currentManifest); return; } else { Log("Local files do not exists. Upgrading."); } // end if then else existFiles } // end if same version Log("New version, updating"); // Another version // Download new manifest http.Download( currentManifest.ftpcredentials.Url + "/" + version + "/manifest.json", "manifest.json.tmp", currentManifest.ftpcredentials.User, currentManifest.ftpcredentials.Pwd ); // end http download Log("New manifest downloaded"); // Load new manifest string tmpJson = File.ReadAllText("manifest.json.tmp"); models.Manifest tmpManifest = serializer.Deserialize <models.Manifest>(tmpJson); tmpManifest.ftpcredentials.Decrypt(); Log("New manifest loaded in memory. Start comparing files."); List <models.MonitoredFile> updatedFiles = new List <models.MonitoredFile>(); // Compare files from new manifest with the oldone foreach (models.MonitoredFile file in tmpManifest.files) { Log("Comparing " + file.filename + "..."); models.MonitoredFile localFile = null; localFile = currentManifest.files.FirstOrDefault(f => f.filename == file.filename); if (localFile == null || !File.Exists(file.filename)) { Log("New file {0}. Downloading...", file.filename); // Donwload new file this.DownloadMonitoredFile( ref http, version, currentManifest, file.filename ); // end DownloadMonitoredFile // Add to the list updatedFiles.Add(file); Console.WriteLine("add updated file " + file.filename); Log("{0} downloaded.", file.filename); } else { // Compare the checksum if (!file.checksum.Equals(localFile.checksum)) { Log("New version file {0}. Updating...", file.filename); // Donwload new file this.DownloadMonitoredFile( ref http, version, currentManifest, file.filename ); // end DownloadMonitoredFile // Add to the list updatedFiles.Add(file); Console.WriteLine("add updated file " + file.filename); Log("{0} downloaded.", file.filename + ".tmp"); } // end if diff checsum } // end if then else local file is null } // end foreach file // Loop the updated files // Backup the files // Rename the files updatedFiles.ForEach(f => { Console.WriteLine("updateFile: " + f.filename); // Here exists and has different chechsum // Backup the file, move it this.BackupMonitoredFile(currentVersion, f.filename); Log("{0} backed up.", f.filename); // Delete if exists if (File.Exists(f.filename)) { File.Delete(f.filename); } // end if file exists // Rename the downloaded file is exists if (!File.Exists(f.filename + ".tmp")) { throw new Exception(string.Format("{0} file was not downloaded correctly", f.filename)); } // end if not exists downloaded file // Rename the files Console.WriteLine("Moving file " + f.filename + ".tmp"); File.Move(f.filename + ".tmp", f.filename); }); // end for each file // if the file exists in local, but not in remote, do nothing // Validates the latest manifest Log("Validating the manifest"); ValidateManifest(tmpManifest); StartMain(currentManifest); } // void update