Skip to content

Commit

Permalink
feature (sipi): Add Lua code for uploading a file to Sipi as per #998
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Geer committed Oct 17, 2018
1 parent 3381d96 commit 9ba1565
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 3 deletions.
2 changes: 1 addition & 1 deletion sipi/config/sipi.init-knora.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function pre_flight(prefix, identifier, cookie)

if prefix == "tmp" then
-- always deny access to tmp folder
return 'deny'
return 'allow', filepath
end


Expand Down
14 changes: 12 additions & 2 deletions sipi/config/sipi.knora-local-config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ sipi = {
-- thousand files in a unix directory (your mileage may vay depending on the
-- file system used).
--
subdir_levels = 1,
subdir_levels = 0,

--
-- if subdir_levels is > 0 and if prefix_as_path is true, all prefixes will be
Expand Down Expand Up @@ -126,8 +126,13 @@ sipi = {
--
-- loglevel, one of "EMERGENCY", "ALERT", "CRITICAL", "ERROR", "WARNING", "NOTICE", "INFORMATIONAL", "DEBUG"
--
loglevel = "DEBUG"
loglevel = "DEBUG",

--
-- The secret for generating JWT's (JSON Web Tokens) (42 characters)
--
jwt_secret = 'UP 4888, nice 4-8-4 steam engine',
-- 12345678901234567890123456789012
}


Expand Down Expand Up @@ -173,6 +178,11 @@ routes = {
method = 'POST',
route = '/Knora_logout',
script = 'Knora_logout.lua'
},
{
method = 'POST',
route = '/upload',
script = 'upload.lua'
}

}
140 changes: 140 additions & 0 deletions sipi/scripts/upload.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
--
-- Copyright © 2016 Lukas Rosenthaler, Andrea Bianco, Benjamin Geer,
-- Ivan Subotic, Tobias Schweizer, André Kilchenmann, and André Fatton.
-- This file is part of Sipi.
-- Sipi is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Affero General Public License as published
-- by the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
-- Sipi is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-- Additional permission under GNU AGPL version 3 section 7:
-- If you modify this Program, or any covered work, by linking or combining
-- it with Kakadu (or a modified version of that library), containing parts
-- covered by the terms of the Kakadu Software Licence, the licensors of this
-- Program grant you additional permission to convey the resulting work.
-- See the GNU Affero General Public License for more details.
-- You should have received a copy of the GNU Affero General Public
-- License along with Sipi. If not, see <http://www.gnu.org/licenses/>.

-- upload script for binary files (currently only images) from Knora
--

require "send_response"

function table.contains(table, element)
for _, value in pairs(table) do
if value == element then
return true
end
end
return false
end


if server.request == nil or server.request["token"] == nil then
send_error(400, "token missing")
return -1
end

token = server.request["token"]

success, tokendata = server.decode_jwt(token)
if not success then
send_error(401, "Token not valid")
return -1
end

myimg = {}
newfilename = {}
iiifurls = {}

for imgindex,imgparam in pairs(server.uploads) do

--
-- check if tmporary directory is available under image root, if not, create it
--
tmpdir = config.imgroot .. '/tmp/'
local success, exists = server.fs.exists(tmpdir)
if not success then
server.sendStatus(500)
server.log(exists, server.loglevel.error)
return false
end
if not exists then
local success, errmsg = server.fs.mkdir(tmpdir, 511)
if not success then
server.sendStatus(500)
server.log(errmsg, server.loglevel.error)
return false
end
end

--
-- copy the file to a safe place
--
local success, uuid62 = server.uuid62()
if not success then
server.sendStatus(500)
server.log(uuid62, server.loglevel.error)
return false
end
tmppath = tmpdir .. uuid62
local success, errmsg = server.copyTmpfile(imgindex, tmppath)
if not success then
server.sendStatus(500)
server.log(errmsg, server.loglevel.error)
return false
end

--
-- create a new Lua image object. This reads the image into an
-- internal in-memory representation independent of the original
-- image format.
--
success, tmpimgref = SipiImage.new(tmppath)
if not success then
server.sendStatus(500)
server.log(gaga, server.loglevel.error)
return false
end

myimg[imgindex] = tmpimgref

filename = imgparam["origname"]
n1, n2 = string.find(filename, '.', 1, true)
newfilename[imgindex] = tmppath .. '.jp2'

if server.secure then
protocol = 'https://'
else
protocol = 'http://'
end
iiifurls[uuid62 .. ".jp2"] = protocol .. server.host .. '/tmp/' .. uuid62 .. '.jp2'

success, newfilepath = helper.filename_hash(newfilename[imgindex]);
if not success then
server.sendStatus(500)
server.log(gaga, server.loglevel.error)
return false
end

fullfilepath = config.imgroot .. '/tmp/' .. newfilepath

local status, errmsg = myimg[imgindex]:write(fullfilepath, {original = imgparam["origname"], hash = "sha256"})
if not status then
server.print('Error converting image to j2k: ', filename, ' ** ', errmsg)
end

success, errmsg = server.fs.unlink(tmppath)
if not success then
server.sendStatus(500)
server.log(errmsg, server.loglevel.error)
return false
end

end


send_success(iiifurls)

0 comments on commit 9ba1565

Please sign in to comment.