Skip to content

Commit

Permalink
fix: hash urls/ids for temp paths
Browse files Browse the repository at this point in the history
  • Loading branch information
3rd committed May 13, 2024
1 parent 66af29f commit e3d2cc1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lua/image/image.lua
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ local from_url = function(url, options, callback, state)
return
end

local tmp_path = state.tmp_dir .. "/" .. utils.base64.encode(url) .. ".png"
local tmp_path = state.tmp_dir .. "/" .. utils.hash.simple(url) .. ".png"
local stdout = vim.loop.new_pipe()

vim.loop.spawn("curl", {
Expand Down
8 changes: 7 additions & 1 deletion lua/image/utils/document.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ local create_document_integration = function(config)
local cursor_row = vim.api.nvim_win_get_cursor(window.id)[1] - 1 -- 0-indexed row

for _, match in ipairs(matches) do
local id = string.format("%d:%d:%d:%s", window.id, window.buffer, match.range.start_row, match.url)
local id = string.format(
"%d:%d:%d:%s",
window.id,
window.buffer,
match.range.start_row,
utils.hash.simple(match.url)
)

if ctx.options.only_render_image_at_cursor and match.range.start_row ~= cursor_row then goto continue end

Expand Down
14 changes: 14 additions & 0 deletions lua/image/utils/hash.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
local bit = require("bit")

local filename = function(str)
local hash = 5381
for i = 1, #str do
local char = string.byte(str, i)
hash = bit.lshift(hash, 5) + hash + char
end
return hash
end

return {
simple = filename,
}
2 changes: 2 additions & 0 deletions lua/image/utils/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local base64 = require("image/utils/base64")
local hash = require("image/utils/hash")
local logger = require("image/utils/logger")
local magic = require("image/utils/magic")
local math = require("image/utils/math")
Expand All @@ -20,4 +21,5 @@ return {
offsets = offsets,
tmux = tmux,
magic = magic,
hash = hash,
}

0 comments on commit e3d2cc1

Please sign in to comment.