Skip to content

Commit

Permalink
working upload
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed Mar 29, 2024
1 parent 2cdaa7d commit 4e26a8f
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 11 deletions.
14 changes: 11 additions & 3 deletions api/media.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@

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
Expand All @@ -12,7 +20,7 @@ function blockexchange.api.create_media_mod(token, mod)
method = "POST",
data = mod,
headers = { "Authorization: " .. token }
}):next(function(res) return res.json() end)
}):next(response_handler)
end

--- creates a new nodedef
Expand All @@ -24,7 +32,7 @@ function blockexchange.api.create_media_nodedef(token, nodedef)
method = "POST",
data = nodedef,
headers = { "Authorization: " .. token }
}):next(function(res) return res.json() end)
}):next(response_handler)
end

--- creates a new mediafile
Expand All @@ -36,5 +44,5 @@ function blockexchange.api.create_media_mediafile(token, mediafile)
method = "POST",
data = mediafile,
headers = { "Authorization: " .. token }
}):next(function(res) return res.json() end)
}):next(response_handler)
end
85 changes: 77 additions & 8 deletions commands/media.lua
Original file line number Diff line number Diff line change
@@ -1,28 +1,97 @@

function blockexchange.upload_mod_media(token, modname, license, source)
local modpath = minetest.get_modpath(modname)
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 = modname,
name = mod_name,
source = source or "",
media_license = license
}):next(function()
local stats = { nodedefs = 0, mediafiles = 0, size = 0 }

for nodename in pairs(minetest.registered_nodes) do
local nodedefs = {}
for nodename, def in pairs(minetest.registered_nodes) do
local mn = string.gmatch(nodename, "([^:]+)")()
if mn == modname then
-- TODO: create nodedef
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
-- TODO: texturepath .. "/" .. filename
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 { nodedefs = 0, mediafiles = 0, size = 0 }
return Promise.all(nodedef_promise, mediafile_promise):next(function()
return stats
end)
end)
end

0 comments on commit 4e26a8f

Please sign in to comment.