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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
|
-- Debugger
local IS_DEBUG = false
local function dmsg(msg)
if IS_DEBUG then
ya.dbg(msg)
end
end
-- Sync Helpers
local function tabs_to_table(tabs)
local t = {}
for i = 1, #tabs do
table.insert(t, tabs[i])
end
return t
end
-- Syncs
local get_configs = ya.sync(function(state)
return state.config
end)
local get_files = ya.sync(function(state, args)
local tabs, paths = {}, {}
if args.tabselect then
if args.tabselect == "all" then
tabs = tabs_to_table(cx.tabs)
elseif args.tabselect == "active" or args.tabselect == "current" then
table.insert(tabs, cx.active)
end
for _, tab in pairs(tabs) do
for _, url in pairs(tab.selected) do
table.insert(paths, tostring(url.path))
end
end
end
if not args.no_hover then
table.insert(paths, tostring(cx.active.current.hovered.url.path))
end
return paths
end)
-- Notify Users
local function alert(msg, opts)
opts = opts or {}
ya.notify({
title = "lin-decompress",
content = msg,
timeout = opts.timeout or 10,
level = opts.level or "info",
})
end
-- Create directory
local function create_dir(dir_path)
local is_successful, error = fs.create("dir_all", Url(dir_path))
if not is_successful then
alert("Unable to create directory -> " .. dir_path, { level = "error" })
ya.err(tostring(error))
end
return is_successful
end
-- Ask User
local function ask(msg, default, opts)
opts = opts or {}
local width = opts.width or 40
local y = opts.y or 10
local should_fail = opts.should_fail and true or false
local should_hide = opts.obscure and true or false
local value, event = ya.input({
pos = { "top-center", x = -width + 10, y = y, w = width },
title = msg,
value = default,
obscure = should_hide,
realtime = false,
debounce = 0.3,
})
dmsg("Asked: '" .. msg .. "'")
if should_fail or not value then
error("Error occurred when asking user: '" .. msg .. "'")
end
return value
end
-- Ask encryption credential
local function ask_cred()
return ask("Password?: (Stop asking[!!!])", "", { obscure = true })
end
-- Ask User output directory
local function ask_output(path)
local url = Url(path)
local parent_dir = url.parent
local extract_dir = ask("Directory to extract ALL archives?", tostring(parent_dir))
return extract_dir
end
-- Variables
local cmd_options = {}
local global_tar_cmd = {}
local tar_cmd = {
["tar"] = {
tool_name = "tar",
cmd = { "--overwrite" },
in_cmd = "-xf",
out_cmd = "-C",
sub_cmd = "-I",
},
}
local archive_cmds = {}
-- Strip content from mime-type
local function strip_type(str)
local f_index, _ = string.find(str, ":")
local file_type = string.sub(str, f_index + 1 or 1)
file_type = string.gsub(file_type, "\n", "")
file_type = string.gsub(file_type, "%s", "")
return file_type
end
-- Identify filetype
local function get_type(path)
dmsg("Getting type for Path: " .. path)
local output, err = Command("file"):arg("--mime-type"):arg(path):output()
if err then
alert("Unable to get mimetype for -> " .. path, { level = "error" })
ya.err(tostring(err))
return nil
end
return strip_type(output.stdout)
end
-- Get tar archive commands
local function get_tar_cmds_by_mime(mimetype)
for archive_name, cmd in pairs(tar_cmd) do
if string.find(mimetype, archive_name) then
return { is_tar_type = true, cmds = cmd }
end
end
return {}
end
local function get_tar_cmds_by_ext(ext)
for _, cmd in pairs(tar_cmd) do
if cmd.exts[ext] then
return { is_tar_type = true, cmds = cmd }
end
end
return {}
end
-- Get other archive type commands
local function get_other_cmds_by_mime(mimetype)
for archive_name, cmd in pairs(archive_cmds) do
if string.find(mimetype, archive_name) then
return { cmds = cmd }
end
end
return { cmds = archive_cmds["default"] }
end
local function get_other_cmds_by_ext(ext)
for _, cmd in pairs(archive_cmds) do
if cmd.exts[ext] then
return { cmds = cmd }
end
end
return { cmds = archive_cmds["default"] }
end
-- Get commands for archive by extension
local function get_cmds_by_ext(ext)
local cmds = get_tar_cmds_by_ext(ext)
if cmds.is_tar_type then
return cmds
end
return get_other_cmds_by_ext(ext)
end
-- Get commands for archive by mimetype
local function get_cmds_by_mime(mimetype)
local cmds = get_tar_cmds_by_mime(mimetype)
if cmds.is_tar_type then
return cmds
end
return get_other_cmds_by_mime(mimetype)
end
-- Get commands for archive
local function get_cmds(mimetype, path)
if string.find(mimetype, "octet-stream") then
local ext = Url(path).ext
return get_cmds_by_ext(ext)
else
return get_cmds_by_mime(mimetype)
end
end
-- Is tool installed?
local function tool_exists(tool_name)
local s = Command("which"):arg(tool_name):status()
return s and s.code == 0
end
-- Selects an archive extract tool
local function get_tool(cmds)
if tool_exists(cmds.tool_name) then
return cmds
end
cmds.is_tar_type = false
return archive_cmds["default"]
end
-- Concatenate two items to strings
local function str_concat(item1, item2)
local result = ""
for _, v in ipairs({ item1, item2 }) do
if type(v) == "table" then
result = string.format("%s %s", result, table.concat(v, " "))
else
if result == "" then
result = v
else
result = result .. " " .. v
end
end
end
return string.gsub(result, "(%s%s)%s*", " ")
end
-- Add collection of arguments to a command
local function get_command_args(command, args)
if args then
for i, arg in ipairs(args) do
command = command:arg(arg)
end
end
return command
end
-- Remove extracted folder with the same name as parent. e.g. (foo/foo/)
local function remove_dup_dir(archive_dir)
local archive_url = Url(archive_dir)
local files, err = fs.read_dir(archive_url, { limit = 1 })
if err then
alert(err, { level = "error" })
return
end
local file = files and files[1] or nil
dmsg("Dup Suspect: " .. file.name)
dmsg("Dup: " .. file.name .. " and " .. archive_url.name)
if not file or not file.cha.is_dir or file.name ~= archive_url.name then
dmsg("Directory does not contain duplicate at -> " .. archive_dir)
return
end
local _, err2 = Command("sh"):arg("-c"):arg("mv " .. file.url.path .. "/* " .. archive_dir):output()
if err2 then
dmsg(err2)
dmsg("Failed to move duplicate directory")
return
end
fs.remove("dir", file.url)
end
-- Retrieve archive directory save location
local function get_archive_dir(inputs, path)
local url = Url(path)
local strip_tar_path = string.gsub(url.stem, "%.tar$", "", 1)
local output_dir = Url(inputs.output)
local archive_url = output_dir:join(strip_tar_path)
return tostring(archive_url)
end
-- Retrieve extracted file url
local function get_extracted_file_url(inputs, path)
local path_url = Url(path)
local output_url = Url(inputs.output)
local full_file_name = path_url.name or "file_name"
local file_name_wo_archive_ext = string.gsub(full_file_name, ".%w+$", "", 1)
return output_url:join(file_name_wo_archive_ext)
end
-- Basic extract command for tar
local function tar_command(path, tar_tool, archive_dir)
local command = Command(tar_tool.tool_name)
dmsg(tar_tool)
-- Tar extract commands
command = command:arg(tar_tool.in_cmd):arg(path)
-- Tar flag commands
command = get_command_args(command, tar_tool.cmd)
-- Tar argument commands
return command:arg(tar_tool.out_cmd):arg(archive_dir)
end
-- Extracts a .tar archive
local function tar_extract(path, tool_cmd, archive_dir)
local command = tar_command(path, tool_cmd, archive_dir)
local _, error = command:output()
if error then
alert("An error occurred extracting -> " .. path, { level = "error" })
ya.err(tostring(error))
fs.remove("dir_clean", Url(archive_dir))
end
end
-- Extracts archives with tar.*
local function double_extract(path, tool_cmd, archive_dir)
-- Tar flag commands
local command = tar_command(path, tar_cmd["tar"], archive_dir)
dmsg(tool_cmd)
-- Sub compressor commands
local compressor_cmds = tool_cmd.tool_name
if tool_cmd.cmd then
compressor_cmds = str_concat(compressor_cmds, tool_cmd.cmd) .. " "
end
-- Sub compressor global commands
if not tool_cmd.no_global_tar then
compressor_cmds = str_concat(compressor_cmds, global_tar_cmd.cmd)
end
local _, error = command:arg(tar_cmd["tar"].sub_cmd):arg(compressor_cmds):output()
if error then
alert("An error occurred extracting -> " .. path, { level = "error" })
ya.err(tostring(error))
fs.remove("dir_clean", Url(archive_dir))
end
end
-- Extracts single tar related files (.lz,.zstd,etc)
local function tar_like_extract(path, tool_cmd, file_url)
local command = Command(tool_cmd.tool_name)
dmsg(tool_cmd)
-- Compressor specific commands
command = get_command_args(command, tool_cmd.cmd)
-- Global compressor commands
if not tool_cmd.no_global_tar then
command = get_command_args(command, global_tar_cmd.cmd)
end
local output = command:arg(path):output()
dmsg("File URL -> " .. tostring(file_url))
local is_successful, error = fs.write(file_url, output.stdout)
if not is_successful then
alert("Unable to extract file -> " .. tostring(file_url), { level = "error" })
ya.err(tostring(error))
fs.remove("dir_clean", file_url.parent)
end
end
-- Extracts other types of archives (zip,rar,7z,etc.)
local function other_extract(path, tool_cmd, archive_dir, inputs)
dmsg(tool_cmd)
local command = Command(tool_cmd.tool_name)
command = get_command_args(command, tool_cmd.cmd)
if tool_cmd.pw_cmd and inputs.cred ~= "!!!" and not cmd_options.no_password then
inputs.cred = ask_cred()
if inputs.cred and inputs.cred ~= "!!!" then
local format_pw = string.format("%s%s", tool_cmd.pw_cmd, inputs.cred)
command = command:arg(format_pw)
end
end
local is_successful = create_dir(archive_dir)
if not is_successful then
return
end
local format_out = string.format("%s%s", tool_cmd.out_cmd, archive_dir)
local _, error = command:arg(format_out):arg(path):stdout(Command.PIPED):output()
if error then
fs.remove("dir_clean", Url(archive_dir))
alert("An error occurred extracting -> " .. path, { level = "error" })
ya.err(tostring(error))
end
end
-- Operate on a single file
local function decompress_file(path, mimetype, inputs)
local cmds = get_cmds(mimetype, path)
local tool_cmd = get_tool(cmds.cmds)
dmsg("Chosen tool -> " .. tool_cmd.tool_name)
local archive_dir = get_archive_dir(inputs, path)
dmsg("Archive Dir -> " .. archive_dir)
local skip_dup_removal = false
if cmds.is_tar_type then
if string.find(path, "%.tar%.", 1) then
create_dir(archive_dir)
dmsg("Extract type -> double")
double_extract(path, tool_cmd, archive_dir)
elseif string.find(path, "%.tar$", 1) then
create_dir(archive_dir)
dmsg("Extract type -> tar")
tar_extract(path, tool_cmd, archive_dir)
else
create_dir(inputs.output)
skip_dup_removal = true
dmsg("Extract type -> tar-like")
local file_url = get_extracted_file_url(inputs, path)
tar_like_extract(path, tool_cmd, file_url)
end
else
create_dir(archive_dir)
dmsg("Extract type -> other")
other_extract(path, tool_cmd, archive_dir, inputs)
end
if not skip_dup_removal then
remove_dup_dir(archive_dir)
end
end
-- Extract all archive files
local function decompress_files(paths)
local inputs = {}
alert("Attempting to extract " .. #paths .. " files", { timeout = 5 })
for _, path in ipairs(paths) do
local mimetype = get_type(path)
dmsg(path .. ": " .. mimetype)
if mimetype and not string.find(mimetype, "directory") then
if not inputs.output then
inputs.output = ask_output(path)
dmsg("Ask Output directory -> " .. inputs.output)
end
decompress_file(path, mimetype, inputs)
end
end
alert("Extraction process completed!")
end
-- Move table 1 into table 2
local function table_move(t1, t2)
for k, v in pairs(t1) do
t2[k] = v
end
end
-- Setup Configurations
local function setup_vars()
local configs = get_configs()
global_tar_cmd = configs.global_tar_compressor
table_move(configs.tar_compressors, tar_cmd)
archive_cmds = configs.other_compressors
end
-- Entry point
local M = {}
function M:setup(state)
self.config = state
end
function M:entry(job)
setup_vars()
cmd_options = job.args
local paths = get_files(cmd_options)
if #paths > 0 then
decompress_files(paths)
end
end
return M
|