Skip to content

Commit

Permalink
feat: Put file extensions on filePaths that dont have them
Browse files Browse the repository at this point in the history
  • Loading branch information
Lythium4848 committed Aug 18, 2023
1 parent afec8f3 commit d18e3ea
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lua/pixelui/core/cl_images.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ local useProxy = false

file.CreateDir(PIXEL.DownloadPath)

local contentTypes = {
["image/png"] = ".png",
["image/jpeg"] = ".jpg",
["image/gif"] = ".gif",
["image/webp"] = ".webp",
["image/svg+xml"] = ".svg",
["image/x-icon"] = ".ico"
}

local function endsWithExtension(str)
local dotIndex = string.find(str, "%.[^%.]+$")
return (dotIndex and dotIndex == #str - string.len(string.match(str, "%.[^%.]+$")) + 1) or false
end

print(endsWithExtension("test"))

local function processQueue()
if queue[1] then
local url, filePath, matSettings, callback = unpack(queue[1])
Expand All @@ -31,13 +47,19 @@ local function processQueue()
if len > 2097152 then
materials[filePath] = Material("nil")
else
local contentType = headers["Content-Type"]
if not endsWithExtension(filePath) then
filePath = filePath .. (contentTypes[contentType] or ".png")
end

file.Write(filePath, body)
materials[filePath] = Material("../data/" .. filePath, matSettings or "noclamp smooth mips")
end

callback(materials[filePath])
end,
function(error)
print("Failed to download", url, error)
if useProxy then
materials[filePath] = Material("nil")
callback(materials[filePath])
Expand All @@ -52,6 +74,7 @@ end

function PIXEL.GetImage(url, callback, matSettings)
local protocol = url:match("^([%a]+://)")
print(protocol, url)
local urlWithoutProtocol = string.gsub(url, protocol, "")

local fileNameStart = url:find("[^/]+$")
Expand Down

0 comments on commit d18e3ea

Please sign in to comment.