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); }
public void RenameWizardsWebpageImages(string htmlFile, string targetSubdir) { FsPath htmlPath = HtmlDir.Join(htmlFile); FsPath targetDir = DevPaths.GathererOriginalDir.Join(targetSubdir); string htmlFileName = htmlPath.Basename(extension: false); FsPath directoryName = htmlPath.Parent(); if (!directoryName.HasValue()) { throw new ArgumentException(htmlPath.Value, nameof(htmlPath)); } FsPath filesDirectory = directoryName.Join(htmlFileName + "_files"); string content = htmlPath.ReadAllText(); var matches = _imgTagPattern.Matches(content); targetDir.CreateDirectory(); foreach (Match match in matches) { string originalFileName = match.Groups["file"].Value; string ext = Path.GetExtension(originalFileName); FsPath filePath = filesDirectory.Join(originalFileName); string name = HttpUtility.HtmlDecode(match.Groups["name"].Value) .Replace(" // ", ""); FsPath defaultTargetPath = targetDir.Join(name + ext); bool defaultTargetExists = defaultTargetPath.IsFile(); if (defaultTargetExists || getTargetPath(1).IsFile()) { if (defaultTargetExists) { defaultTargetPath.MoveFileTo(getTargetPath(1)); } for (int i = 2; i < 12; i++) { FsPath targetPath = getTargetPath(i); if (!targetPath.IsFile()) { filePath.CopyFileTo(targetPath, overwrite: false); break; } } } else { filePath.CopyFileTo(defaultTargetPath, overwrite: false); } FsPath getTargetPath(int num) => targetDir.Join(name + num + ext); } }
private static void move(FsPath appOnline, FsPath appDownloaded, bool overwrite) { if (overwrite) { ensureFileDeleted(appDownloaded); } appOnline.MoveFileTo(appDownloaded); }
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); } } } }
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(); } }