Repository: sxyazi/yazi
File: yazi-plugin/preset/plugins/extract.lua (Lua)

Code before:
	if output and output.status.code == 2 and archive.is_encrypted(output.stderr) then
		fs.remove("dir_all", tmp)
		return true -- Need to retry
	end

	self:tidy(from, to, tmp)
	if not output then
		fail("7zip failed to output when extracting '%s', error: %s", from, err)
	elseif output.status.code ~= 0 then
		fail("7zip exited with error code %s when extracting '%s':\n%s", output.status.code, from, output.stderr)
	end
end

function M:tidy(from, to, tmp)
	local outs = fs.read_dir(tmp, { limit = 2 })
	if not outs then
		fail("Failed to read the temporary directory '%s' when extracting '%s'", tmp, from)
	elseif #outs == 0 then
		fs.remove("dir", tmp)
		fail("No files extracted from '%s'", from)
	end

	local only = #outs == 1 and outs[1]
	if only and not only.cha.is_dir and require("archive").is_tar(outs[1].url) then
		self:entry { args = { tostring(outs[1].url), tostring(to) } }
		fs.remove("file", outs[1].url)
		fs.remove("dir", tmp)
		return
	end

	local target = to:join(only and only.name or self.trim_ext(from.name))
	target = fs.unique(only and not only.cha.is_dir and "file" or "dir", target)
	if not target then
		fail("Failed to determine a target for '%s'", from)
	end

	if only and not fs.rename(outs[1].url, target) then
		fail('Failed to move "%s" to "%s"', outs[1].url, target)
	elseif not only and not fs.rename(tmp, target) then
		fail('Failed to move "%s" to "%s"', tmp, target)
	end
	fs.remove("dir", tmp)
end

function M.tmp_name(url) return ".tmp_" .. ya.hash(string.format("extract//%s//%.10f", url, ya.time())) end

function M.trim_ext(name)
	-- stylua: ignore
	local exts = { ["7z"] = true, apk = true, bz2 = true, bzip2 = true, cbr = true, cbz = true, exe = true, gz = true, gzip = true, iso = true, jar = true, rar = true, tar = true, tgz = true, xz = true, zip = true, zst = true }

	while true do

Review comment (refactor):
Minor consistency: `only` is now the extracted entry (`outs[1]`), but the tar check still references `outs[1].url` instead of `only.url`. Using `only` throughout would make the intent clearer and avoids mixing the two forms.

Code after:
	if output and output.status.code == 2 and archive.is_encrypted(output.stderr) then
		fs.remove("dir_all", tmp)
		return true -- Need to retry
	end

	self:tidy(from, to, tmp)
	if not output then
		fail("7zip failed to output when extracting '%s', error: %s", from, err)
	elseif output.status.code ~= 0 then
		fail("7zip exited with error code %s when extracting '%s':\n%s", output.status.code, from, output.stderr)
	end
end

function M:tidy(from, to, tmp)
	local outs = fs.read_dir(tmp, { limit = 2 })
	if not outs then
		fail("Failed to read the temporary directory '%s' when extracting '%s'", tmp, from)
	elseif #outs == 0 then
		fs.remove("dir", tmp)
		fail("No files extracted from '%s'", from)
	end

	local only = #outs == 1 and outs[1]
	if only and not only.cha.is_dir and require("archive").is_tar(only.url) then
		self:entry { args = { tostring(only.url), tostring(to) } }
		fs.remove("file", only.url)
		fs.remove("dir", tmp)
		return
	end

	local target = to:join(only and only.name or self.trim_ext(from.name))
	target = fs.unique(only and not only.cha.is_dir and "file" or "dir", target)
	if not target then
		fail("Failed to determine a target for '%s'", from)
	end

	if only and not fs.rename(only.url, target) then
		fail('Failed to move "%s" to "%s"', only.url, target)
	elseif not only and not fs.rename(tmp, target) then
		fail('Failed to move "%s" to "%s"', tmp, target)
	end
	fs.remove("dir", tmp)
end

function M.tmp_name(url) return ".tmp_" .. ya.hash(string.format("extract//%s//%.10f", url, ya.time())) end

function M.trim_ext(name)
	-- stylua: ignore
	local exts = { ["7z"] = true, apk = true, bz2 = true, bzip2 = true, cbr = true, cbz = true, exe = true, gz = true, gzip = true, iso = true, jar = true, rar = true, tar = true, tgz = true, xz = true, zip = true, zst = true }

	while true do