Files
lua-scripts-w-api/contrib/darktable-api/base64.lua
stephen schuresko bd40b4f850 Add darktable REST API implementation
- Introduced a new Lua-based REST API for darktable, allowing external applications to interact with the darktable library.
- Implemented core functionalities including image listing, retrieval, and preview serving.
- Added support for OAuth 2.0 authentication with token generation.
- Created a simple HTTP router to handle API requests.
- Included JSON encoding/decoding utilities for API responses.
- Developed a script to launch the API server alongside darktable, ensuring seamless integration.
- Added necessary configuration and setup for the API server, including CORS support and error handling.
2026-06-10 15:02:19 -04:00

53 lines
1.8 KiB
Lua

-- Base64 and Base64url encode/decode (pure Lua, no dependencies)
local M = {}
local CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
local LOOKUP = {}
for i = 1, #CHARS do LOOKUP[CHARS:sub(i,i)] = i - 1 end
function M.encode(s)
local out = {}
for i = 1, #s, 3 do
local a, b, c = s:byte(i, i + 2)
b, c = b or 0, c or 0
local n = a * 65536 + b * 256 + c
out[#out+1] = CHARS:sub(math.floor(n / 262144) % 64 + 1, math.floor(n / 262144) % 64 + 1)
out[#out+1] = CHARS:sub(math.floor(n / 4096) % 64 + 1, math.floor(n / 4096) % 64 + 1)
out[#out+1] = CHARS:sub(math.floor(n / 64) % 64 + 1, math.floor(n / 64) % 64 + 1)
out[#out+1] = CHARS:sub( n % 64 + 1, n % 64 + 1)
end
local rem = #s % 3
if rem == 1 then out[#out-1], out[#out] = "=", "=" end
if rem == 2 then out[#out] = "=" end
return table.concat(out)
end
function M.decode(s)
s = s:gsub("[^A-Za-z0-9+/=]", "")
local out = {}
for i = 1, #s, 4 do
local a = LOOKUP[s:sub(i, i )] or 0
local b = LOOKUP[s:sub(i+1, i+1)] or 0
local c = LOOKUP[s:sub(i+2, i+2)] or 0
local d = LOOKUP[s:sub(i+3, i+3)] or 0
local n = a * 262144 + b * 4096 + c * 64 + d
out[#out+1] = string.char(math.floor(n / 65536))
if s:sub(i+2, i+2) ~= "=" then out[#out+1] = string.char(math.floor(n / 256) % 256) end
if s:sub(i+3, i+3) ~= "=" then out[#out+1] = string.char(n % 256) end
end
return table.concat(out)
end
-- URL-safe Base64 (RFC 4648 §5): replace +/ with -_ and strip padding
function M.urlencode(s)
return M.encode(s):gsub("+", "-"):gsub("/", "_"):gsub("=", "")
end
function M.urldecode(s)
s = s:gsub("-", "+"):gsub("_", "/")
local pad = (4 - #s % 4) % 4
return M.decode(s .. string.rep("=", pad))
end
return M