Skip to content

Commit

Permalink
Add error handling to img loading from paths--don't crash on paste
Browse files Browse the repository at this point in the history
  • Loading branch information
rgrams committed Dec 8, 2021
1 parent b81cce2 commit 61233f2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
13 changes: 9 additions & 4 deletions Editor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -147,18 +147,23 @@ local function addImage(self, imgData, name, x, y, scale, dontDirty)
end

local function getImageFromAbsolutePath(path)
local imgData
local file, error = io.open(path, "rb")
if error then
print(error)
return
end
local fileData, error = love.filesystem.newFileData(file:read("*a"), "new.jpg")
local filename = fileman.get_filename_from_path(path) or "new.jpg"
local fileData, error = love.filesystem.newFileData(file:read("*a"), filename)
file:close()
if not error then
return love.graphics.newImage(fileData)
local isSuccess, result = pcall(love.graphics.newImage, fileData)
if not isSuccess then
print("Error generating image from file:\n "..result)
else
return result
end
else
print(error)
print("Error reading file:\n "..error)
end
end

Expand Down
2 changes: 1 addition & 1 deletion file_manager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function M.get_file_extension(path)
end

function M.get_filename_from_path(path)
return string.match(path, ".*[\\/]([^\\/%.]*)%" .. M.fileExt .. "$")
return string.match(path, ".*[\\/]([^\\/%.]+)%.(.*)$")
end

function M.ensure_file_extension(path)
Expand Down

0 comments on commit 61233f2

Please sign in to comment.