Beispiel #1
0
        private static List <NewDownload> FindNewDownloads(YamlFileHelper yamlFileHelper, Dictionary <string, string> packageIds, IEnumerable <ManifestPackageVM> manifestPackages)
        {
            List <Tuple <ManifestPackageVM, string> > idFileTuples = manifestPackages.Join(packageIds, manifestPackage => manifestPackage.Id.ToLower(), i => i.Key, (mpvm, kvp) => Tuple.Create(mpvm, kvp.Value)).ToList();

            List <NewDownload> result = new List <NewDownload>();

            foreach (Tuple <ManifestPackageVM, string> idFileTuple in idFileTuples)
            {
                ManifestPackageVM manifestPackage = idFileTuple.Item1;
                string            idFilePath      = idFileTuple.Item2;

                string[] versionsToIgnoreDownload = Helpers.GetVersionsToIgnoreDownload(idFilePath);

                string idFileFolder  = Path.GetDirectoryName(idFilePath);
                string versionFolder = Path.Combine(idFileFolder, ConvertVersionToDirectoryName(manifestPackage.Version));                 // illegal chars in version shouldn't be a problem, because yaml files are stored in folders with version as name
                bool   exists        = versionsToIgnoreDownload.Any(v => v == manifestPackage.Version) || Directory.Exists(versionFolder);
                //if (manifestPackage.Version == "latest" && exists)
                if (false)                //TODO: the following code crashes when downloaded yaml-files are not version 1.0.0
                {
                    string downloadedYamlFilePath = Path.Combine(versionFolder, "latest.yaml");
                    ManifestPackage_1_0_0 downloadedManifestPackage = yamlFileHelper.ReadYamlFile(downloadedYamlFilePath).Manifest;
                    //TODO test all installers when winget supports multiple installers
                    if (manifestPackage.Installers[0].Sha256 != downloadedManifestPackage.Installers[0].InstallerSha256)
                    {
                        FileInfo fi            = new FileInfo(downloadedYamlFilePath);
                        string   versionSuffix = fi.LastWriteTime.ToString("_yyyy-MM-dd");
                        downloadedManifestPackage.PackageVersion += versionSuffix;
                        yamlFileHelper.WriteYamlFile(downloadedYamlFilePath, downloadedManifestPackage);
                        Directory.Move(versionFolder, versionFolder + versionSuffix);
                        exists = Directory.Exists(versionFolder);
                    }
                }
                if (manifestPackage.Version != "latest")                //TODO!!!!
                {
                    if (!exists)
                    {
                        NewDownload dl = new NewDownload()
                        {
                            MultiFileYaml = manifestPackage.MultiFileYaml,
                            VersionFolder = versionFolder,
                            IdFilePath    = idFilePath
                        };
                        result.Add(dl);
                    }
                }
            }
            return(result);
        }
Beispiel #2
0
 public NewDownloadVM(NewDownload newDownload)
 {
     _newDownload = newDownload;
 }
Beispiel #3
0
        private void ProcessOneNewDownload(NewDownload newDownload)
        {
            string versionFolder = newDownload.VersionFolder;
            // create a fresh instance, otherwise the changes we will make would be visible in the GUI-grid
            MultiFileYaml mfy = new YamlFileHelper().LoadMultiFileYaml(newDownload.MultiFileYaml.MainYamlFilePath);

            var installersWithMatchingLocale = mfy.Installers.Where(i => HasMatchingLocale(i, LocalesToDownload));

            foreach (ManifestInstaller_1_0_0 manifestInstaller in installersWithMatchingLocale)
            {
                string downloadUrl      = manifestInstaller.InstallerUrl;
                string downloadFileName = _installerDownloader.GetFileNameFromUrl(downloadUrl, out Uri responseUri);
                if (responseUri != null)
                {
                    // this download url returns html instead of the .exe
                    // https://sourceforge.net/projects/keepass/files/KeePass%202.x/2.46/KeePass-2.46-Setup.exe/download
                    // but the returned responseUri works:
                    // https://downloads.sourceforge.net/project/keepass/KeePass%202.x/2.46/KeePass-2.46-Setup.exe
                    downloadUrl = responseUri.ToString();
                }
                if (downloadFileName == null)
                {
                    AddLogLineBackground("Error: Unable to determine filename from download-url!");
                    continue;
                }
                try
                {
                    AddLogLineBackground("Creating directory: " + versionFolder);
                    Directory.CreateDirectory(versionFolder);
                    string downloadFolder = versionFolder;
                    if (newDownload.MultiFileYaml.Installers.Count > 1)
                    {
                        string archAndLocale = $"{manifestInstaller.Architecture ?? "null"}_{manifestInstaller.InstallerLocale ?? "null"}";
                        downloadFolder = Path.Combine(versionFolder, archAndLocale);
                        Directory.CreateDirectory(downloadFolder);
                    }

                    string downloadFilePath = Path.Combine(downloadFolder, downloadFileName);
                    AddLogLineBackground("Downloading: " + downloadUrl + " -> " + downloadFilePath);
                    _installerDownloader.DownloadFile(downloadUrl, downloadFilePath);
                    if (backgroundWorker1.CancellationPending)
                    {
                        return;
                    }

                    AddLogLineBackground("Calculating Sha256-Hash from file");
                    Helpers.CalculateFileHashResult calculateFileHashResult = Helpers.CalculateSha256HashFromFile(downloadFilePath);
                    if (calculateFileHashResult.ErrorMessage == null)
                    {
                        string calculatedHash = calculateFileHashResult.Hash;
                        string expectedHash   = manifestInstaller.InstallerSha256.ToLower();
                        if (calculatedHash != expectedHash)
                        {
                            AddLogLineBackground($"Error: Sha256-Hash mismatch (expected {expectedHash} - calculated {calculatedHash})");
                            ++_errorCount;
                        }
                    }
                    else
                    {
                        AddLogLineBackground($"Error: Calculating Sha256-Hash: {calculateFileHashResult.ErrorMessage}");
                        ++_errorCount;
                    }

                    manifestInstaller.InstallerUrl = downloadFileName + " |# " + manifestInstaller.InstallerUrl;                     //HACK!!!

                    if (backgroundWorker1.CancellationPending)
                    {
                        return;
                    }
                }
                catch (Exception ex)
                {
                    ++_errorCount;
                    AddLogLineBackground(ex.ToString());
                    if (Directory.GetFiles(versionFolder).Length == 0)
                    {
                        Directory.Delete(versionFolder);
                    }
                }
            }

            if (Directory.Exists(versionFolder))
            {
                //save modified manifest to download-folder
                _yamlFileHelper.SaveMultiFileYaml(mfy, versionFolder);
            }
        }