-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* media upload * wip * working upload --------- Co-authored-by: BuckarooBanzay <BuckarooBanzay@users.noreply.github.com>
- Loading branch information
1 parent
325009c
commit 711830a
Showing
5 changed files
with
181 additions
and
1 deletion.
There are no files selected for viewing
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
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 |
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
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
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 |
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
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) | ||
}) |
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