Skip to content

Commit

Permalink
fix(plugins/memosplus/): 当MemosApi出错时,可能同步时误删GitHub对应文件
Browse files Browse the repository at this point in the history
  • Loading branch information
yiyungent committed Mar 15, 2023
1 parent 21d030a commit b54fd31
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 23 deletions.
70 changes: 51 additions & 19 deletions plugins/MemosPlus/MemosPlus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,23 @@ public async Task ExecuteAsync()
var settings = PluginSettingsModelFactory.Create<SettingsModel>(nameof(MemosPlus));

#region 备份到 GitHub
// TODO: 备份到 GitHub
// 备份到 GitHub
if (settings.Backup.EnableBackupToGitHub)
{
MemosUtil memosUtil = new MemosUtil(settings.Memos.BaseUrl);
int offset = 0;
var list = memosUtil.List(memosSession: settings.Memos.MemosSession, offset: 0, limit: 20);
bool isErrorMemosApi = false;
var list = new List<MemoItemModel>();
try
{
list = memosUtil.List(memosSession: settings.Memos.MemosSession, offset: 0, limit: 20);
}
catch (System.Exception ex)
{
// 只要 memosUtil.List 出现一次 异常, 就标记放弃删除 GitHub 文件, 防止因为没有记录 而误删除 GitHub 文件
isErrorMemosApi = true;
System.Console.WriteLine(ex.ToString());
}
GitHubUtil gitHubUtil = new GitHubUtil();
settings.GitHub.RepoTargetDirPath = settings.GitHub.RepoTargetDirPath.Trim().TrimEnd('/');

Expand Down Expand Up @@ -106,35 +117,56 @@ public async Task ExecuteAsync()
}
}
offset = offset + list.Count;
list = memosUtil.List(memosSession: settings.Memos.MemosSession, offset: offset, limit: 20);
try
{
list = memosUtil.List(memosSession: settings.Memos.MemosSession, offset: offset, limit: 20);
}
catch (System.Exception ex)
{
isErrorMemosApi = true;
System.Console.WriteLine(ex.ToString());
}
}
// 清理不存在的文件: 对于存放 memos 的文件夹, 清理 memos 中已删除的对应文件
List<string> githubExistFilePaths = gitHubUtil.Files(
repoOwner: settings.GitHub.RepoOwner,
repoName: settings.GitHub.RepoName,
repoBranch: settings.GitHub.RepoBranch,
repoTargetDirPath: $"{settings.GitHub.RepoTargetDirPath}/",
accessToken: settings.GitHub.AccessToken);
// 对于 GitHub 存在 但 memos 并没有此对应文件, 就需要删除
List<string> deletedFilePaths = githubExistFilePaths.Where(m => !memoFilePaths.Contains(m)).ToList();
foreach (var deletedFilePath in deletedFilePaths)
{

#region 清理不存在的文件
if (!isErrorMemosApi) {
// 清理不存在的文件: 对于存放 memos 的文件夹, 清理 memos 中已删除的对应文件
try
{
gitHubUtil.Delete(
List<string> githubExistFilePaths = gitHubUtil.Files(
repoOwner: settings.GitHub.RepoOwner,
repoName: settings.GitHub.RepoName,
repoBranch: settings.GitHub.RepoBranch,
repoTargetFilePath: deletedFilePath,
accessToken: settings.GitHub.AccessToken
);
repoTargetDirPath: $"{settings.GitHub.RepoTargetDirPath}/",
accessToken: settings.GitHub.AccessToken);
// 对于 GitHub 存在 但 memos 并没有此对应文件, 就需要删除
List<string> deletedFilePaths = githubExistFilePaths.Where(m => !memoFilePaths.Contains(m)).ToList();
foreach (var deletedFilePath in deletedFilePaths)
{
try
{
gitHubUtil.Delete(
repoOwner: settings.GitHub.RepoOwner,
repoName: settings.GitHub.RepoName,
repoBranch: settings.GitHub.RepoBranch,
repoTargetFilePath: deletedFilePath,
accessToken: settings.GitHub.AccessToken
);
}
catch (System.Exception ex)
{
System.Console.WriteLine($"删除失败: {deletedFilePath}");
System.Console.WriteLine(ex.ToString());
}
}
}
catch (System.Exception ex)
{
System.Console.WriteLine($"删除失败: {deletedFilePath}");
System.Console.WriteLine(ex.ToString());
}
}
#endregion

}
#endregion

Expand Down
4 changes: 2 additions & 2 deletions plugins/MemosPlus/MemosPlus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PackageId>PluginCore.MemosPlus</PackageId>
<Version>0.1.5</Version>
<FileVersion>0.1.5.0</FileVersion>
<Version>0.1.6</Version>
<FileVersion>0.1.6.0</FileVersion>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<Company>yiyun</Company>
<Authors>yiyun</Authors>
Expand Down
2 changes: 1 addition & 1 deletion plugins/MemosPlus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@


<!-- Matomo Image Tracker-->
<img referrerpolicy="no-referrer-when-downgrade" src="https://matomo.moeci.com/matomo.php?idsite=2&amp;rec=1&amp;action_name=Plugins.MemosPlus-v0.1.5.README" style="border:0" alt="" />
<img referrerpolicy="no-referrer-when-downgrade" src="https://matomo.moeci.com/matomo.php?idsite=2&amp;rec=1&amp;action_name=Plugins.MemosPlus-v0.1.6.README" style="border:0" alt="" />
<!--
### 其它
Expand Down
2 changes: 1 addition & 1 deletion plugins/MemosPlus/info.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"DisplayName": "memos+",
"Description": "提升 memos 体验",
"Author": "yiyun",
"Version": "0.1.5",
"Version": "0.1.6",
"SupportedVersions": [ "0.0.1" ]
}

0 comments on commit b54fd31

Please sign in to comment.