From 439da040d8725e8f815c20c815198cd3edc9abf4 Mon Sep 17 00:00:00 2001 From: ZigHolding Date: Thu, 5 Jun 2025 21:10:27 +0800 Subject: [PATCH] Add command: Download git rep --- data.json | 2 +- manifest.json | 2 +- src/commands.ts | 105 +++++++++++++++++++++++++++++++++++++++++++++++- src/setting.ts | 13 +++++- src/strings.ts | 16 ++++++++ 5 files changed, 134 insertions(+), 4 deletions(-) diff --git a/data.json b/data.json index c764ac4..1cf3864 100644 --- a/data.json +++ b/data.json @@ -1,4 +1,4 @@ { "strict_mode": false, - "vaultDir": "" + "vaultDir": "D:\\iLanix\\Obsidian" } \ No newline at end of file diff --git a/manifest.json b/manifest.json index ce42613..cca788a 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "note-sync", "name": "Note Sync", - "version": "0.5.2", + "version": "0.5.3", "minAppVersion": "1.8.3", "description": "Sync notes or plugins between vaults.", "author": "ZigHolding", diff --git a/src/commands.ts b/src/commands.ts index efe3393..c91a781 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -109,6 +109,108 @@ const cmd_export_plugin = (plugin:NoteSyncPlugin) => ({ } }); +const cmd_download_git_repo = (plugin:NoteSyncPlugin) => ({ + id: 'cmd_download_git_repo', + name: plugin.strings.cmd_download_git_repo, + callback: async () => { + let repos = plugin.settings.git_repo.split('\n') + let repo = await plugin.dialog_suggest(repos,repos); + if(!repo){return} + + let match = repo.match(/^https?:\/\/(.*)\.com\/([^/]*)\/([^/]*)\/tree\/([^/]*)\/?(.*)$/); + if(!match){return} + + let SOURCE = match[1]; // gitee + let repoOwner = match[2]; // 开发者 + let repoName = match[3]; // 项目名称 + let branch = match[4]; // 分支名称 + let path = match[5]; // 初始路径 + + async function list_files_of_path(repoOwner:string, repoName:string, path:string, branch = 'master') { + let url; + if(SOURCE=='github'){ + url = `https://api.github.com/repos/${repoOwner}/${repoName}/contents/${path}?ref=${branch}`; + }else{ + url = `https://gitee.com/api/v5/repos/${repoOwner}/${repoName}/contents/${path}?ref=${branch}` + } + let req = await (window as any).requestUrl(url) + req = JSON.parse(req.text) + if(!Array.isArray(req)){ + req = [req] + } + return req + } + + async function download_file(url:string,folder_path:string,file_name:string){ + let req = await (window as any).requestUrl(url) + // req = JSON.parse(req.text) + // let ctx = atob(req.content) + let ctx = req.text + let tfile_path = `${folder_path}/${file_name}` + // console.log(ctx) + if(folder_path.startsWith('.')){ + let flag = (plugin.app.vault as any).exists(folder_path); + if(!flag){ + await plugin.app.vault.createFolder(folder_path) + } + flag = (plugin.app.vault as any).exists(tfile_path); + if(flag){ + await (plugin.app.vault as any).adapter.remove(tfile_path) + await plugin.app.vault.create(tfile_path,ctx) + new Notice(`更新:${tfile_path}`,5000) + }else{ + await plugin.app.vault.create(tfile_path,ctx) + new Notice(`下载:${tfile_path}`,5000) + } + }else{ + let folder = plugin.app.vault.getFolderByPath(folder_path) + if(!folder){ + folder = await plugin.app.vault.createFolder(folder_path) + } + + let tfile = plugin.app.vault.getFileByPath(tfile_path) + if(!tfile){ + await plugin.app.vault.create(tfile_path,ctx); + new Notice(`下载:${tfile_path}`,5000) + }else{ + await plugin.app.vault.modify(tfile,ctx) + new Notice(`更新:${tfile.path}`,5000) + } + } + + } + + async function download_file_of_dir(repoOwner:string, repoName:string, path:string, branch:string){ + let items = await list_files_of_path(repoOwner, repoName, path, branch) + let nc = this.app.plugins.getPlugin('note-chain'); + let item = await nc.dialog_suggest( + items.map((x:any)=>(x.type=='file'?'📃':'📁')+x.path), + items,'',true + ); + if(!item){return} + if(typeof(item)=='string' && item=='all'){ + for(let item of items){ + if(item.type=='file'){ + let file_name = item.path.split('/').last(); + let folder_path = item.path.slice(0,item.path.length-file_name.length-1); + await download_file(item.download_url,folder_path,file_name) + } + } + }else if(item.type=='file'){ + let file_name = item.path.split('/').last(); + let folder_path = item.path.slice(0,item.path.length-file_name.length-1); + await download_file(item.download_url,folder_path,file_name) + }else if(item.type=='dir'){ + await download_file_of_dir(repoOwner, repoName, item.path, branch) + } + } + + download_file_of_dir(repoOwner, repoName, path, branch) + + } +}); + + const commandBuilders:Array = [ ]; @@ -116,7 +218,8 @@ const commandBuilders:Array = [ const commandBuildersDesktop:Array = [ cmd_export_current_note, cmd_set_vexporter, - cmd_export_plugin + cmd_export_plugin, + cmd_download_git_repo ]; export function addCommands(plugin:NoteSyncPlugin) { diff --git a/src/setting.ts b/src/setting.ts index df3dbee..0bef9c1 100644 --- a/src/setting.ts +++ b/src/setting.ts @@ -7,11 +7,13 @@ import NoteSyncPlugin from '../main'; export interface MySettings { strict_mode: boolean; vaultDir:string; + git_repo:string; } export const DEFAULT_SETTINGS: MySettings = { strict_mode:false, - vaultDir:'' + vaultDir: '', + git_repo: 'https://github.com/zigholding/ObsidianZ/tree/master\nhttps://gitee.com/zigholding/ObsidianZ/tree/master' } export class NoteSyncSettingTab extends PluginSettingTab { @@ -60,5 +62,14 @@ export class NoteSyncSettingTab extends PluginSettingTab { this.plugin.strings.setting_strict_mode_desc, 'strict_mode' ); + + new Setting(containerEl) + .setName(this.plugin.strings.setting_git_repo) + .addTextArea(text => text + .setValue(this.plugin.settings.git_repo) + .onChange(async (value) => { + this.plugin.settings.git_repo = value; + await this.plugin.saveSettings(); + })); } } diff --git a/src/strings.ts b/src/strings.ts index 8347984..9095aa9 100644 --- a/src/strings.ts +++ b/src/strings.ts @@ -34,6 +34,14 @@ export class Strings{ } } + get cmd_download_git_repo(){ + if(this.language=='zh'){ + return '下载 Git 仓库文件' + }else{ + return 'Download git repo'; + } + } + get prompt_path_of_folder(){ if(this.language=='zh'){ return '输入文件夹路径' @@ -81,6 +89,14 @@ export class Strings{ } } + get setting_git_repo(){ + if(this.language=='zh'){ + return 'Git 仓库库'; + }else{ + return 'Git repository'; + } + } + get item_copy_data_json(){ if(this.language=='zh'){ return '复制 data.json';