-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
66 lines (55 loc) · 1.53 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
local M = {}
function M:peek()
local start, cache = os.clock(), ya.file_cache(self)
if not cache or self:preload() ~= 1 then
return
end
ya.sleep(math.max(0, PREVIEW.image_delay / 1000 + start - os.clock()))
ya.image_show(cache, self.area)
end
function M:seek() end
function M:document_to_pdf()
local tmp_dir = "/tmp/"
local pdf_file = tmp_dir .. self.file.name:gsub("%..*$", ".pdf")
read_perm = io.open(pdf_file, "r")
if read_perm then
read_perm:close()
else
local convert = Command("libreoffice"):args({
"--headless",
"--convert-to",
"pdf:draw_pdf_Export:{\"PageRange\":{\"type\":\"string\",\"value\":\"1\"}}",
"--outdir",
tmp_dir,
tostring(self.file.url)
})
:stdout(Command.NULL)
:stderr(Command.NULL)
:output()
end
return pdf_file
end
function M:preload()
local cache = ya.file_cache(self)
if not cache or fs.cha(cache) then
local cha, err = fs.cha(cache)
return 1
end
local pdf_file = self:document_to_pdf()
local output = Command("pdftoppm")
:args({ "-singlefile", "-jpeg", "-jpegopt", "quality=75", "-f", tostring(self.skip + 1), tostring(pdf_file) })
:stdout(Command.PIPED)
:stderr(Command.PIPED)
:output()
if not output then
return 0
elseif not output.status.success then
local pages = tonumber(output.stderr:match("the last page %((%d+)%)")) or 0
if self.skip > 0 and pages > 0 then
ya.manager_emit("peek", { math.max(0, pages - 1), only_if = self.file.url, upper_bound = true })
end
return 0
end
return fs.write(cache, output.stdout) and 1 or 2
end
return M