Skip to content

Commit dee05d6

Browse files
committed
Fix erro on save highlight first time
1 parent e503dde commit dee05d6

3 files changed

Lines changed: 23 additions & 91 deletions

File tree

highlightsync.koplugin/main.lua

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,24 @@ local SyncService = require("frontend/apps/cloudstorage/syncservice")
1111
local Merge = require("merge")
1212
local rapidjson = require("rapidjson")
1313

14+
local function dir_exists(path)
15+
local ok, _, code = os.rename(path, path)
16+
if not ok then
17+
-- Código 13 = permission denied, mas o diretório existe
18+
return code == 13
19+
end
20+
return true
21+
end
22+
23+
local function ensure_dir_exists(path)
24+
if not dir_exists(path) then
25+
local result = os.execute('mkdir -p "' .. path .. '"')
26+
if not result then
27+
error("Failed to create directory: " .. path)
28+
end
29+
end
30+
end
31+
1432
local Highlightsync = WidgetContainer:extend{
1533
name = "Highlightsync",
1634
is_doc_only = false,
@@ -61,10 +79,10 @@ end
6179

6280
function Highlightsync.onSync(local_path, cached_path, income_path)
6381

64-
local local_highlights = read_json_file(local_path) or {}
82+
local local_highlights = DataAnnotations --read_json_file(local_path) or {}
6583
local cached_highlights = read_json_file(cached_path) or {}
6684
local income_highlights = read_json_file(income_path) or {}
67-
local annotations = Merge.merge_highlights(local_highlights,income_highlights,cached_highlights)
85+
local annotations = Merge.Merge_highlights(local_highlights,income_highlights,cached_highlights)
6886

6987
write_json_file(SidecarDir .. "/" .. FileName .. ".json", annotations) -- Save annotations local
7088
DataAnnotations = annotations
@@ -94,9 +112,10 @@ function Highlightsync:onSyncBookHighlights()
94112
})
95113

96114
UIManager:nextTick(function()
97-
DataAnnotations = self.ui.doc_settings.data.annotations
115+
DataAnnotations = self.ui.annotation.annotations -- self.ui.doc_settings.data.annotations
98116
SidecarDir = self.ui.doc_settings.doc_sidecar_dir
99117
FileName = SidecarDir:match("([^/]+)/*$")
118+
ensure_dir_exists(SidecarDir)
100119
write_json_file(SidecarDir .. "/" .. FileName .. ".json", self.ui.annotation.annotations) -- Save annotations local
101120
SyncService.sync(self.settings.sync_server, SidecarDir .. "/" .. FileName .. ".json", self.onSync)
102121
self.ui.annotation.annotations = DataAnnotations

highlightsync.koplugin/merge.lua

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
local salt = assert(require("salt"))
2-
31
local function parse_datetime_cached()
42
local cache = {}
53
return function(str)
@@ -89,6 +87,6 @@ end
8987

9088
local M = {}
9189

92-
M.merge_highlights = merge_highlights
90+
M.Merge_highlights = merge_highlights
9391

9492
return M

highlightsync.koplugin/salt.lua

Lines changed: 0 additions & 85 deletions
This file was deleted.

0 commit comments

Comments
 (0)