コード例 #1
0
        public async Task <bool> DownloadAndExtract(string remotePath, FsPath targetDirectory, FsPath fileName, CancellationToken token)
        {
            if (!Str.Equals(".7z", fileName.Extension()))
            {
                throw new ArgumentException();
            }

            FsPath archiveFileName = targetDirectory.Join(fileName);

            if (archiveFileName.IsFile())
            {
                try
                {
                    archiveFileName.DeleteFile();
                }
                catch (Exception ex)
                {
                    lock (_syncOutput)
                        Console.WriteLine($"Failed to remove {archiveFileName}: {ex.Message}");
                    return(false);
                }
            }

            bool downloaded = await TryDownloadFile(remotePath, archiveFileName, token);

            if (!downloaded)
            {
                return(false);
            }

            if (!archiveFileName.IsFile())
            {
                lock (_syncOutput)
                    Console.WriteLine($"Failed to download {archiveFileName} from {remotePath}");
                return(false);
            }

            var sevenZip = new SevenZip(silent: true);

            sevenZip.Extract(archiveFileName, targetDirectory, Enumerable.Empty <FsPath>());

            try
            {
                archiveFileName.DeleteFile();
            }
            catch (Exception ex)
            {
                lock (_syncOutput)
                    Console.WriteLine($"Failed to remove {archiveFileName}: {ex.Message}");
            }

            return(true);
        }
コード例 #2
0
        public void SignImages(string setCodesStr, bool small, bool zoom, bool nonToken, bool token)
        {
            foreach (FsPath qualityDir in getQualities(small, zoom))
            {
                foreach ((_, _, FsPath tokenSuffix) in getIsToken(nonToken, token))
                {
                    FsPath outputFile  = getSignatureFile(qualityDir, tokenSuffix);
                    FsPath packagePath = TargetDir.Join(qualityDir).Concat(tokenSuffix);
                    new ImageDirectorySigner().SignFiles(packagePath, outputFile, setCodesStr);

                    FsPath signatureFile           = getSignatureFile(qualityDir, tokenSuffix);
                    FsPath compressedSignatureFile = signatureFile
                                                     .Parent()
                                                     .Join(signatureFile.Basename(extension: false))
                                                     .Concat(SevenZipExtension);

                    if (compressedSignatureFile.IsFile())
                    {
                        compressedSignatureFile.DeleteFile();
                    }

                    new SevenZip(false).Compress(signatureFile, compressedSignatureFile)
                    .Should().BeTrue();
                }
            }
        }
コード例 #3
0
ファイル: Installer.cs プロジェクト: NikolayXHD/Mtgdb
 private static void ensureFileDeleted(FsPath file)
 {
     if (file.IsFile())
     {
         file.DeleteFile();
     }
 }
コード例 #4
0
        public bool CreateShortcut(FsPath exePath, FsPath iconPath, FsPath shortcutPath)
        {
            bool   useBackup  = false;
            FsPath backupPath = FsPath.None;

            if (shortcutPath.IsFile())
            {
                backupPath = shortcutPath.WithName(_ => _ + ".bak");
                useBackup  = !backupPath.IsFile();
                if (useBackup)
                {
                    shortcutPath.MoveFileTo(backupPath);
                }
                else
                {
                    shortcutPath.DeleteFile();
                }
            }

            bool success = run($"\"{_script}\" \"{shortcutPath}\" \"{exePath}\" \"{iconPath}\"");

            if (!success && useBackup)
            {
                backupPath.MoveFileTo(shortcutPath);
            }

            return(success);
        }
コード例 #5
0
 public void Invalidate()
 {
     if (_completionLabelFile.IsFile())
     {
         _completionLabelFile.DeleteFile();
     }
 }
コード例 #6
0
ファイル: Installer.cs プロジェクト: NikolayXHD/Mtgdb
        public void CreateApplicationShortcut(FsPath shortcutLocation)
        {
            string appVersionInstalled = GetAppVersionInstalled();
            // Mtgdb.Gui.v1.3.5.10.zip
            var prefix     = "Mtgdb.Gui.";
            var postfix    = ".zip";
            var versionDir = appVersionInstalled.Substring(prefix.Length, appVersionInstalled.Length - prefix.Length - postfix.Length);

            FsPath currentBin = AppDir.BinVersion.Parent().Join(versionDir);
            // may be different from currently running executable because of just installed upgrade
            FsPath execPath = currentBin.Join(ExecutableFileName);
            FsPath iconPath = currentBin.Join("mtg64.ico");

            FsPath shortcutPath = shortcutLocation.Join(ShortcutFileName);

            if (createApplicationShortcut(shortcutPath, execPath, iconPath))
            {
                return;
            }

            // workaround a problem with WshShell unable to create the link within desktop directory
            // due to a mismatch between physical and localized directory names
            var    tempLocation = new FsPath(Path.GetTempPath());
            FsPath tempPath     = tempLocation.Join(ShortcutFileName);

            if (createApplicationShortcut(tempPath, execPath, iconPath))
            {
                try
                {
                    if (shortcutPath.IsFile())
                    {
                        shortcutPath.DeleteFile();
                    }

                    tempPath.MoveFileTo(shortcutPath);
                    Console.WriteLine("Moved application shortcut from {0} to {1}",
                                      tempLocation, shortcutLocation);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Failed to move application shortcut from {0} to {1}: {2}",
                                      tempLocation, shortcutLocation, ex);

                    try
                    {
                        tempPath.DeleteFile();
                    }
                    catch (Exception cleanupEx)
                    {
                        Console.WriteLine("Failed to remove application shortcut from {0}: {1}",
                                          tempLocation, cleanupEx);
                    }
                }
            }
        }
コード例 #7
0
ファイル: Shortcut.cs プロジェクト: NikolayXHD/Mtgdb
        public static void CreateApplicationShortcut(FsPath exePath, FsPath iconPath, FsPath shortcutPath)
        {
            if (shortcutPath.IsFile())
            {
                shortcutPath.DeleteFile();
            }

            var          wsh = new WshShell();
            IWshShortcut shortcut;

            try
            {
                shortcut = wsh.CreateShortcut(shortcutPath.Value) as IWshShortcut;
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(
                    "Failed to create shortcut object {0} at {1}: {2}",
                    exePath, shortcutPath, ex);
                return;
            }

            FsPath bin = exePath.Parent();

            if (shortcut == null)
            {
                Console.Error.WriteLine("Failed to create shortcut {0} at {1}: {2}.{3} returned null",
                                        exePath, shortcutPath, nameof(WshShell), nameof(wsh.CreateShortcut));
                return;
            }

            shortcut.Arguments   = "";
            shortcut.TargetPath  = exePath.Value;
            shortcut.WindowStyle = 1;

            shortcut.Description      = "Application to search MTG cards and build decks";
            shortcut.WorkingDirectory = bin.Value;

            if (iconPath.HasValue())
            {
                shortcut.IconLocation = iconPath.Value;
            }

            try
            {
                shortcut.Save();
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("Failed to create shortcut {0} at {1}: {2}", exePath, shortcutPath, ex);
            }
        }
コード例 #8
0
        private async Task downloadSignatures(QualityGroupConfig qualityGroup, CancellationToken token)
        {
            if (qualityGroup.FileListMegaId == null && qualityGroup.YandexName == null)
            {
                return;
            }

            (FsPath signaturesDir, FsPath signaturesFile) = getSignaturesFile(qualityGroup);
            FsPath signaturesFileBak = signaturesFile.Concat(".bak");

            if (signaturesFile.IsFile())
            {
                if (signaturesFileBak.IsFile())
                {
                    signaturesFileBak.DeleteFile();
                }

                signaturesFile.MoveFileTo(signaturesFileBak);
            }

            signaturesDir.CreateDirectory();

            if (qualityGroup.FileListMegaId != null)
            {
                string megaUrl = _config.MegaPrefix + qualityGroup.FileListMegaId;
                await _megatools.Download(megaUrl, signaturesDir,
                                          name : $"Signatures for {qualityGroup.Quality} images", silent : true, token : token);
            }
            else if (qualityGroup.YandexName != null)
            {
                FsPath fileListArchive = signaturesDir.Join("filelist.7z");
                var    client          = new YandexDiskClient();
                Console.Write("{0} filelist.7z: get YandexDisk download url ... ", qualityGroup.Name);
                var url = await client.GetFilelistDownloadUrl(_config, qualityGroup, token);

                Console.Write("downloading ... ");

                bool success;
                try
                {
                    await client.DownloadFile(url, fileListArchive, token);

                    Console.WriteLine($"done");
                    success = true;
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"failed: {ex.Message}");
                    _log.Warn(ex, $"Failed download {fileListArchive} from {url}");
                    success = false;
                }

                if (success)
                {
                    if (fileListArchive.IsFile())
                    {
                        new SevenZip(silent: true).Extract(fileListArchive, signaturesDir);
                    }
                }
            }
            else
            {
                throw new ArgumentException($"No downloader can get filelist for quality {qualityGroup.Quality}");
            }


            if (!signaturesFile.IsFile())
            {
                if (signaturesFileBak.IsFile())
                {
                    Console.WriteLine("Failed to unzip signatures");

                    Console.WriteLine("Move {0} {1}", signaturesFileBak, signaturesFile);
                    signaturesFileBak.MoveFileTo(signaturesFile);
                }
            }
            else
            {
                signaturesFileBak.DeleteFile();
            }
        }
コード例 #9
0
ファイル: ImageDownloader.cs プロジェクト: NikolayXHD/Mtgdb
        private static bool isAlreadyDownloaded(ImageDownloadProgress progress)
        {
            FsPath targetSubdirectory = progress.TargetSubdirectory;

            targetSubdirectory.CreateDirectory();

            if (progress.FilesOnline == null)
            {
                return(false);
            }

            bool alreadyDownloaded = true;

            var existingFiles = new HashSet <FsPath>(
                targetSubdirectory.EnumerateFiles("*", SearchOption.AllDirectories));

            var existingSignatures = new Dictionary <FsPath, FileSignature>();

            foreach (var fileOnline in progress.FilesOnline.Values)
            {
                FsPath filePath = targetSubdirectory.Join(fileOnline.Path);
                if (!existingFiles.Contains(filePath))
                {
                    alreadyDownloaded = false;
                    continue;
                }

                FileSignature tempQualifier     = Signer.CreateSignature(filePath, useAbsolutePath: true);
                var           existingSignature =
                    progress.FilesCorrupted.TryGet(fileOnline.Path) ??
                    progress.FilesDownloaded.TryGet(fileOnline.Path) ??
                    new FileSignature
                {
                    Path    = tempQualifier.Path.RelativeTo(targetSubdirectory).Intern(true),
                    Md5Hash = tempQualifier.Md5Hash
                };

                if (existingSignature.Md5Hash != fileOnline.Md5Hash)
                {
                    alreadyDownloaded = false;
                    Console.WriteLine("Deleting modified or corrupted file {0}", filePath);

                    lock (ImageLoader.SyncIo)
                    {
                        try
                        {
                            filePath.DeleteFile();
                        }
                        catch (IOException ex)
                        {
                            Console.WriteLine($"Failed to remove {filePath}. {ex.Message}");
                        }
                    }
                }
                else
                {
                    existingSignatures.Add(existingSignature.Path, existingSignature);
                }
            }

            foreach (FsPath file in existingFiles)
            {
                var relativePath = file.RelativeTo(targetSubdirectory);
                if (!progress.FilesOnline.ContainsKey(relativePath) && relativePath != Signer.SignaturesFile)
                {
                    Console.WriteLine("Deleting {0}", file);
                    file.DeleteFile();
                }
            }

            if (alreadyDownloaded)
            {
                ImageDownloadProgressReader.WriteExistingSignatures(progress, existingSignatures.Values);
            }

            return(alreadyDownloaded);
        }