private void SetFilePath(string filepath) { var uri = new Uri(filepath); if (uri.Scheme == "qmk") { string url; url = filepath.Replace(filepath.Contains("qmk://") ? "qmk://" : "qmk:", ""); if (!Directory.Exists(Path.Combine(Application.LocalUserAppDataPath, "downloads"))) { Directory.CreateDirectory(Path.Combine(Application.LocalUserAppDataPath, "downloads")); } try { _printer.Print($"Downloading the file: {url}", MessageType.Info); using (var wb = new WebClient()) { wb.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.33 Safari/537.36"); filepath = Path.Combine(KnownFolders.Downloads.Path, filepath.Substring(filepath.LastIndexOf("/") + 1).Replace(".", "_" + Guid.NewGuid().ToString().Substring(0, 8) + ".")); wb.DownloadFile(url, filepath); } } catch (Exception e) { _printer.PrintResponse("Something went wrong when trying to get the default keymap file.", MessageType.Error); return; } _printer.PrintResponse($"File saved to: {filepath}", MessageType.Info); } if (filepath.EndsWith(".qmk", true, null)) { _printer.Print("Found .qmk file", MessageType.Info); var qmkFilepath = $"{Path.GetTempPath()}qmk_toolbox{filepath.Substring(filepath.LastIndexOf("\\"))}\\"; _printer.PrintResponse($"Extracting to {qmkFilepath}\n", MessageType.Info); if (Directory.Exists(qmkFilepath)) { Directory.Delete(qmkFilepath, true); } ZipFile.ExtractToDirectory(filepath, qmkFilepath); var files = Directory.GetFiles(qmkFilepath); var readme = ""; var info = new Info(); foreach (var file in files) { _printer.PrintResponse($" - {file.Substring(file.LastIndexOf("\\") + 1)}\n", MessageType.Info); if (file.Substring(file.LastIndexOf("\\") + 1).Equals("firmware.hex", StringComparison.OrdinalIgnoreCase) || file.Substring(file.LastIndexOf("\\") + 1).Equals("firmware.bin", StringComparison.OrdinalIgnoreCase)) { SetFilePath(file); } if (file.Substring(file.LastIndexOf("\\") + 1).Equals("readme.md", StringComparison.OrdinalIgnoreCase)) { readme = File.ReadAllText(file); } if (file.Substring(file.LastIndexOf("\\") + 1).Equals("info.json", StringComparison.OrdinalIgnoreCase)) { info = JsonConvert.DeserializeObject <Info>(File.ReadAllText(file)); } } if (!string.IsNullOrEmpty(info.Keyboard)) { _printer.Print($"Keymap for keyboard \"{info.Keyboard}\" - {info.VendorId}:{info.ProductId}", MessageType.Info); } if (string.IsNullOrEmpty(readme)) { return; } _printer.Print("Notes for this keymap:", MessageType.Info); _printer.PrintResponse(readme, MessageType.Info); } else { if (string.IsNullOrEmpty(filepath)) { return; } filepathBox.Text = filepath; if (!filepathBox.Items.Contains(filepath)) { filepathBox.Items.Add(filepath); } } }
private void SetFilePath(string filepath) { if (!filepath.StartsWith("\\\\wsl$")) { var uri = new Uri(filepath); if (uri.Scheme == "qmk") { string url; url = filepath.Replace(filepath.Contains("qmk://") ? "qmk://" : "qmk:", ""); filepath = Path.Combine(KnownFolders.Downloads.Path, filepath.Substring(filepath.LastIndexOf("/") + 1).Replace(".", "_" + Guid.NewGuid().ToString().Substring(0, 8) + ".")); try { _printer.Print($"Downloading the file: {url}", MessageType.Info); DownloadFirmwareFile(url, filepath); } catch (Exception e1) { try { // Try .bin extension if hex 404'd url = Path.ChangeExtension(url, "bin"); filepath = Path.ChangeExtension(filepath, "bin"); _printer.Print($"No .hex file found, trying {url}", MessageType.Info); DownloadFirmwareFile(url, filepath); } catch (Exception e2) { _printer.PrintResponse("Something went wrong when trying to get the default keymap file.", MessageType.Error); return; } } _printer.PrintResponse($"File saved to: {filepath}", MessageType.Info); } } if (filepath.EndsWith(".qmk", true, null)) { _printer.Print("Found .qmk file", MessageType.Info); var qmkFilepath = $"{Path.GetTempPath()}qmk_toolbox{filepath.Substring(filepath.LastIndexOf("\\"))}\\"; _printer.PrintResponse($"Extracting to {qmkFilepath}\n", MessageType.Info); if (Directory.Exists(qmkFilepath)) { Directory.Delete(qmkFilepath, true); } ZipFile.ExtractToDirectory(filepath, qmkFilepath); var files = Directory.GetFiles(qmkFilepath); var readme = ""; var info = new Info(); foreach (var file in files) { _printer.PrintResponse($" - {file.Substring(file.LastIndexOf("\\") + 1)}\n", MessageType.Info); if (file.Substring(file.LastIndexOf("\\") + 1).Equals("firmware.hex", StringComparison.OrdinalIgnoreCase) || file.Substring(file.LastIndexOf("\\") + 1).Equals("firmware.bin", StringComparison.OrdinalIgnoreCase)) { SetFilePath(file); } if (file.Substring(file.LastIndexOf("\\") + 1).Equals("readme.md", StringComparison.OrdinalIgnoreCase)) { readme = File.ReadAllText(file); } if (file.Substring(file.LastIndexOf("\\") + 1).Equals("info.json", StringComparison.OrdinalIgnoreCase)) { info = JsonConvert.DeserializeObject <Info>(File.ReadAllText(file)); } } if (!string.IsNullOrEmpty(info.Keyboard)) { _printer.Print($"Keymap for keyboard \"{info.Keyboard}\" - {info.VendorId}:{info.ProductId}", MessageType.Info); } if (string.IsNullOrEmpty(readme)) { return; } _printer.Print("Notes for this keymap:", MessageType.Info); _printer.PrintResponse(readme, MessageType.Info); } else { if (string.IsNullOrEmpty(filepath)) { return; } filepathBox.Text = filepath; if (!filepathBox.Items.Contains(filepath)) { filepathBox.Items.Add(filepath); } } }
private void setFilePath(string filepath) { Uri uri = new Uri(filepath); if (uri.Scheme == "qmk") { string url; if (filepath.Contains("qmk://")) { url = filepath.Replace("qmk://", ""); } else { url = filepath.Replace("qmk:", ""); } printer.print("Downloading the file: " + url, MessageType.Info); WebClient wb = new WebClient(); if (!Directory.Exists(Path.Combine(Application.LocalUserAppDataPath, "downloads"))) { Directory.CreateDirectory(Path.Combine(Application.LocalUserAppDataPath, "downloads")); } wb.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.33 Safari/537.36"); filepath = Path.Combine(KnownFolders.Downloads.Path, filepath.Substring(filepath.LastIndexOf("/") + 1).Replace(".", "_" + Guid.NewGuid().ToString().Substring(0, 8) + ".")); wb.DownloadFile(url, filepath); printer.printResponse("Filed saved to: " + filepath, MessageType.Info); } if (filepath.EndsWith(".qmk", true, null)) { printer.print("Found .qmk file", MessageType.Info); string qmk_filepath = Path.GetTempPath() + "qmk_toolbox" + filepath.Substring(filepath.LastIndexOf("\\")) + "\\"; printer.printResponse("Extracting to " + qmk_filepath + "\n", MessageType.Info); if (Directory.Exists(qmk_filepath)) { Directory.Delete(qmk_filepath, true); } ZipFile.ExtractToDirectory(filepath, qmk_filepath); string[] files = Directory.GetFiles(qmk_filepath); string readme = ""; Info info = new Info(); foreach (string file in files) { printer.printResponse(" - " + file.Substring(file.LastIndexOf("\\") + 1) + "\n", MessageType.Info); if (file.Substring(file.LastIndexOf("\\") + 1).Equals("firmware.hex", StringComparison.OrdinalIgnoreCase) || file.Substring(file.LastIndexOf("\\") + 1).Equals("firmware.bin", StringComparison.OrdinalIgnoreCase)) { setFilePath(file); } if (file.Substring(file.LastIndexOf("\\") + 1).Equals("readme.md", StringComparison.OrdinalIgnoreCase)) { readme = System.IO.File.ReadAllText(file); } if (file.Substring(file.LastIndexOf("\\") + 1).Equals("info.json", StringComparison.OrdinalIgnoreCase)) { info = JsonConvert.DeserializeObject <Info>(System.IO.File.ReadAllText(file)); } } if (!string.IsNullOrEmpty(info.keyboard)) { printer.print("Keymap for keyboard \"" + info.keyboard + "\" - " + info.vendor_id + ":" + info.product_id, MessageType.Info); } if (!readme.Equals("")) { printer.print("Notes for this keymap:", MessageType.Info); printer.printResponse(readme, MessageType.Info); } } else { if (filepath != "") { filepathBox.Text = filepath; if (!filepathBox.Items.Contains(filepath)) { filepathBox.Items.Add(filepath); } } } }