示例#1
0
        /// <summary>
        /// Adds a file to the manifest, if new, else, overwrites its checksum
        /// </summary>
        /// <param name="file">The MonitoredFile to add</param>
        public void AddFile(MonitoredFile file)
        {
            if (files == null)
            {
                files = new List <MonitoredFile>();
            } // end if files is null

            MonitoredFile current = files.FirstOrDefault(f => f.filename == file.filename);

            if (current != null)
            {
                current.checksum = file.checksum;
                return;
            } // end if current not null

            //  If reaches here, is a new file
            files.Add(file);
        } // end function AddFile
        }         // 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