Skip to content

Commit

Permalink
media upload (#68)
Browse files Browse the repository at this point in the history
* media upload

* wip

* working upload

---------

Co-authored-by: BuckarooBanzay <BuckarooBanzay@users.noreply.github.com>
  • Loading branch information
BuckarooBanzay and BuckarooBanzay committed Mar 29, 2024
1 parent 325009c commit 711830a
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 1 deletion.
48 changes: 48 additions & 0 deletions api/media.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---------
-- media api calls

local http, url = ...

local function response_handler(res)
if res.code == 200 then
return res.json()
else
return Promise.rejected("unexpected http error")
end
end

--- creates a new mod
-- @param token the token in string format
-- @param mod the mod to create
-- @return a promise with the result
function blockexchange.api.create_media_mod(token, mod)
return Promise.http(http, url .. "/api/media/mod/" .. mod.name, {
method = "POST",
data = mod,
headers = { "Authorization: " .. token }
}):next(response_handler)
end

--- creates a new nodedef
-- @param token the token in string format
-- @param mod the nodedef to create
-- @return a promise with the result
function blockexchange.api.create_media_nodedef(token, nodedef)
return Promise.http(http, url .. "/api/media/nodedef/" .. nodedef.name, {
method = "POST",
data = nodedef,
headers = { "Authorization: " .. token }
}):next(response_handler)
end

--- creates a new mediafile
-- @param token the token in string format
-- @param mod the mediafile to create
-- @return a promise with the result
function blockexchange.api.create_media_mediafile(token, mediafile)
return Promise.http(http, url .. "/api/media/mediafile/" .. mediafile.name, {
method = "POST",
data = mediafile,
headers = { "Authorization: " .. token }
}):next(response_handler)
end
2 changes: 1 addition & 1 deletion api/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function blockexchange.api.update_schema(token, schema)
"Authorization: " .. token
},
timeout = 10,
method = "PUT",
method = "PUT", -- NOTE: might not work (https://github.com/minetest/minetest/issues/14394)
data = json
}, function(res)
if res.succeeded and res.code == 200 then
Expand Down
97 changes: 97 additions & 0 deletions commands/media.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@

local function upload_nodedefs(token, nodedefs)
local promise = Promise.new()

local function process_next()
local nodedef = table.remove(nodedefs)
if not nodedef then
promise:resolve()
return
end
blockexchange.api.create_media_nodedef(token, nodedef)
:next(process_next)
:catch(function(e) promise:reject(e) end)
end
process_next()

return promise
end

local function upload_mediafiles(token, mediafiles)
local promise = Promise.new()

local function process_next()
local mediafile = table.remove(mediafiles)
if not mediafile then
promise:resolve({})
return
end
blockexchange.api.create_media_mediafile(token, mediafile)
:next(process_next)
:catch(function(e) promise:reject(e) end)
end
process_next()

return promise
end

function blockexchange.upload_mod_media(token, mod_name, license, source)
local modpath = minetest.get_modpath(mod_name)

return blockexchange.api.create_media_mod(token, {
name = mod_name,
source = source or "",
media_license = license
}):next(function()
local stats = { nodedefs = 0, mediafiles = 0, size = 0 }

local nodedefs = {}
for nodename, def in pairs(minetest.registered_nodes) do
local mn = string.gmatch(nodename, "([^:]+)")()
if mn == mod_name then
stats.nodedefs = stats.nodedefs + 1
table.insert(nodedefs, {
name = nodename,
mod_name = mod_name,
definition = minetest.write_json({
name = nodename,
drawtype = def.drawtype,
paramtype = def.paramtype,
paramtype2 = def.paramtype2,
light_source = def.light_source,
mesh = def.mesh,
tiles = def.tiles,
node_box = def.node_box,
connects_to = def.connects_to,
groups = def.groups
})
})
end
end
local nodedef_promise = upload_nodedefs(token, nodedefs)

local mediafiles = {}
for _, foldername in ipairs({"textures", "models"}) do
local texturepath = modpath .. "/" .. foldername
local dir_list = minetest.get_dir_list(texturepath)
for _, filename in pairs(dir_list) do
local infile = io.open(texturepath .. "/" .. filename, "rb")
local data = infile:read("*a")
infile:close()

stats.mediafiles = stats.mediafiles + 1
stats.size = stats.size + #data
table.insert(mediafiles, {
name = filename,
mod_name = mod_name,
data = minetest.encode_base64(data)
})
end
end
local mediafile_promise = upload_mediafiles(token, mediafiles)

return Promise.all(nodedef_promise, mediafile_promise):next(function()
return stats
end)
end)
end
32 changes: 32 additions & 0 deletions commands/media_chat.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

minetest.register_chatcommand("bx_upload_media", {
params = "<modname> <license>",
privs = { blockexchange = true },
description = "Uploads the media of the given mod and license",
func = blockexchange.api_check_wrapper(function(name, param)
local _, _, modname, license = string.find(param, "^([^%s]+)%s+(.*)$")

if not modname or not license then
return true, "please specify a modname and a license"
end

local modpath = minetest.get_modpath(modname)
if not modpath then
return true, "mod '" .. modname .. "' not found"
end

local token = blockexchange.get_token(name)
if not token then
return true, "please login first to upload media"
end

blockexchange.upload_mod_media(token, modname, license):next(function(stats)
minetest.chat_send_player(name, "[blockexchange] uploaded " .. stats.nodedefs .. " node-definitions and " ..
stats.mediafiles .. " mediafiles with " .. stats.size .. " bytes")
end):catch(function(e)
minetest.chat_send_player(name, "[blockexchange] and error occured: " .. dump(e))
end)

return true, "uploading media from mod '" .. modname .. "' with license '" .. license .. "'"
end)
})
3 changes: 3 additions & 0 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ if blockexchange.is_online then
loadfile(MP.."/api/schemapart.lua")(http, blockexchange.url)
loadfile(MP.."/api/schemamods.lua")(http, blockexchange.url)
loadfile(MP.."/api/token.lua")(http, blockexchange.url)
loadfile(MP.."/api/media.lua")(http, blockexchange.url)
end

-- internal stuff
Expand Down Expand Up @@ -87,6 +88,8 @@ if blockexchange.is_online then
dofile(MP.."/commands/load_update_chat.lua")
dofile(MP.."/commands/load_chat.lua")
dofile(MP.."/commands/autosave.lua")
dofile(MP.."/commands/media.lua")
dofile(MP.."/commands/media_chat.lua")
end
-- commands that are available offline
dofile(MP.."/commands/pos.lua")
Expand Down

0 comments on commit 711830a

Please sign in to comment.