Created
December 1, 2015 22:19
-
-
Save reinholdsson/d142bab5c2bbc22c5d56 to your computer and use it in GitHub Desktop.
lua lapis upload file service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 [email protected] --form press=OK 192.168.47.17:8080/ns | |
xFile:write(style_image.content) | |
xFile:close() | |
end | |
return filePath | |
end) | |
return app |
Thanks, good to know :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey, I guess we are not supposed to write image content files in the above mentioned way. For those who are facing issues,
Try,
Cheers !! XD