Skip to content

Instantly share code, notes, and snippets.

@reinholdsson
Created December 1, 2015 22:19
Show Gist options
  • Save reinholdsson/d142bab5c2bbc22c5d56 to your computer and use it in GitHub Desktop.
Save reinholdsson/d142bab5c2bbc22c5d56 to your computer and use it in GitHub Desktop.
lua lapis upload file service
local lapis = require("lapis")
local http = require("socket.http")
local app = lapis.Application()
is_file = function(input)
return type(input) == "table" and input.filename ~= "" and input.content ~= "", "Missing file"
end
app:match("/", function()
return "Image Service"
end)
app:match("/version", function()
return "Welcome to Lapis " .. require("lapis.version")
end)
app:post("/ns", function(self)
local style_image = self.params.style_image
-- Create temporary file (/tmp/****)
local filePath = os.tmpname()
local xFile = io.open(filePath, "w")
if is_file(style_image) == false then
-- Image provided as url
-- curl --data-urlencode "style_image=http://www.vetprofessionals.com/catprofessional/images/home-cat.jpg" 192.168.47.17:8080/ns
local save = ltn12.sink.file(xFile)
http.request{url = style_image, sink = save }
else
-- Image provided as image object
-- curl --form style_image=@katt.jpg --form press=OK 192.168.47.17:8080/ns
xFile:write(style_image.content)
xFile:close()
end
return filePath
end)
return app
@aylmerbritto
Copy link

Hey, I guess we are not supposed to write image content files in the above mentioned way. For those who are facing issues,
Try,

  local xFile = io.open(filePath, "w")
  io.output(xFile)
  io.write(style_image.content)
  io.close(xFile)

Cheers !! XD

@reinholdsson
Copy link
Author

Thanks, good to know :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment