-- Modified from folke/snacks.nvim (Apache-2.0); see lua/bugabinga/image/NOTICE. local uv = vim.uv local MAX_SOURCE_SIZE = 100 * 1024 * 1024 local MAX_OUTPUT_SIZE = 100 * 1024 * 1024 local MAX_PIXELS = 100000000 local function stat(path) local value = uv.fs_stat(path) if not value or value.type ~= 'file' then return nil, 'source is not a regular file' end if value.size > MAX_SOURCE_SIZE then return nil, 'source exceeds size limit' end return value end local function fingerprint(path, value) return table.concat({ path, value.size, value.mtime.sec, value.mtime.nsec or 0 }, '\0') end local function read_png(path) local file, err = io.open(path, 'rb') if not file then return nil, err end local header = file:read(24) file:close() if not header or #header < 24 or header:sub(1, 8) ~= '\137PNG\r\n\26\n' then return nil, 'invalid PNG signature or IHDR' end if header:sub(9, 12) ~= '\0\0\0\13' or header:sub(13, 16) ~= 'IHDR' then return nil, 'invalid PNG IHDR' end local function uint32(offset) local a, b, c, d = header:byte(offset, offset + 3) return ((a * 256 + b) * 256 + c) * 256 + d end local width, height = uint32(17), uint32(21) if width == 0 or height == 0 or width * height > MAX_PIXELS then return nil, 'invalid PNG dimensions' end return width, height end local function dimensions(path) vim.validate('path', path, 'string') local value, err = stat(path) if not value then return nil, err end return read_png(path) end local function cache_paths(key) local directory = vim.fs.joinpath(vim.fn.stdpath('cache'), 'bugabinga-image') return directory, vim.fs.joinpath(directory, key .. '.png') end local function prepare(path, callback) vim.validate('path', path, 'string') vim.validate('callback', callback, 'function') local canceled = false local job local partial local function finish(err, result) if not canceled then vim.schedule(function() if not canceled then callback(err, result) end end) end end local function cancel() canceled = true if job then job:kill(15) end if partial then uv.fs_unlink(partial) end end local source, source_err = stat(path) if not source then finish(source_err) return cancel end local identity = 'ffmpeg-rgba-v2\0' .. fingerprint(path, source) local key = vim.fn.sha256(identity) local directory, cached = cache_paths(key) local function result_for(file) local width, height = read_png(file) local output = uv.fs_stat(file) if not width or not output or output.size > MAX_OUTPUT_SIZE then return nil end return { path = file, width = width, height = height, key = key } end local cached_result = result_for(cached) if cached_result then finish(nil, cached_result) return cancel end local extension = path:lower():match('%.([^.]+)$') if extension == 'png' then local width, height = read_png(path) if not width then finish(height) return cancel end finish(nil, { path = path, width = width, height = height, key = key }) return cancel end if extension ~= 'jpg' and extension ~= 'jpeg' and extension ~= 'webp' and extension ~= 'gif' then finish('unsupported image format') return cancel end local mkdir_status = vim.fn.mkdir(directory, 'p') if mkdir_status ~= 1 and not uv.fs_stat(directory) then finish('unable to create image cache directory') return cancel end partial = vim.fs.joinpath( directory, key .. '.' .. tostring(uv.os_getpid()) .. '.' .. string.format('%.0f', uv.hrtime()) .. '.partial.png' ) local args = { 'ffmpeg', '-nostdin', '-v', 'error', '-threads', '1', '-max_pixels', tostring(MAX_PIXELS), '-i', path, '-frames:v', '1' } vim.list_extend(args, { '-threads', '1', '-f', 'image2', '-vcodec', 'png', '-pix_fmt', 'rgba', '-fs', tostring(MAX_OUTPUT_SIZE), '-y', partial }) local ok, system_job = pcall(vim.system, args, { text = true, timeout = 30000 }, function(response) if canceled then if partial then uv.fs_unlink(partial) end return end job = nil if response.code ~= 0 then uv.fs_unlink(partial) finish(response.stderr and response.stderr ~= '' and response.stderr or 'ffmpeg conversion failed') return end local current = stat(path) if not current or 'ffmpeg-rgba-v2\0' .. fingerprint(path, current) ~= identity then uv.fs_unlink(partial) finish('source changed during conversion') return end local output = uv.fs_stat(partial) local width, height = read_png(partial) if not output or output.size > MAX_OUTPUT_SIZE or not width then uv.fs_unlink(partial) finish('ffmpeg produced invalid PNG') return end local renamed, rename_err = uv.fs_rename(partial, cached) if not renamed then uv.fs_unlink(partial) finish(rename_err or 'unable to commit image cache entry') return end partial = nil finish(nil, { path = cached, width = width, height = height, key = key }) end) if not ok then uv.fs_unlink(partial) finish(system_job) else job = system_job end return cancel end return { dimensions = dimensions, prepare = prepare }