-- vim: ft=terra
local pstr = lib.str.t
local P = lib.str.plit
local terra cs(s: rawstring)
return pstr { ptr = s, ct = lib.str.sz(s) }
end
local show_all,show_new,show_unfiled,show_files,show_vid,show_img=1,2,3,4,5,6
local terra
render_media_gallery(co: &lib.srv.convo, path: lib.mem.ptr(lib.mem.ref(int8)), uid: uint64, acc: &lib.str.acc)
-- note that when calling this function, path must be adjusted so that path(0)
-- eq "media"
var owner = false
if co.aid ~= 0 and co.who.id == uid then owner = true end
var ou = co.srv:actor_fetch_uid(uid)
if not ou then goto e404 end
var mode: uint8 = show_new
var folder: pstr
if path.ct == 2 then
if path(1):cmp(lib.str.lit'unfiled') then
mode=show_unfiled
elseif path(1):cmp(lib.str.lit'all') then
mode=show_all
else goto e404 end
elseif path.ct == 3 and path(1):cmp(lib.str.lit'kind') then
end
if mode == show_new then
folder = lib.str.plit''
elseif mode == show_all then
folder = pstr.null()
elseif mode == show_unfiled then
folder = lib.str.plit'' -- TODO
-- else get folder from query str
end
var view = data.view.media_gallery {
menu = pstr{'',0};
folders = pstr{'',0};
directory = pstr{'',0};
images = pstr{'',0};
pfx = pstr{'',0};
}
if not owner then
var pa: lib.str.acc pa:init(32)
pa:lpush('/')
if ou(0).origin ~= 0 then pa:lpush('@') end
pa:push(ou(0).xid,0)
view.pfx = pa:finalize()
end
if owner then
view.menu = P'<a class="pos" href="/media/upload">upload</a><hr>'
end
var md = co.srv:artifact_enum_uid(uid, folder)
var gallery: lib.str.acc gallery:init(256)
var files: lib.str.acc files:init(256)
for i=0,md.ct do
if lib.str.ncmp(md(i)(0).mime, 'image/', 6) == 0 then
gallery:lpush('<a class="thumb" href="'):ppush(view.pfx):lpush('/media/a/')
:push(md(i)(0).url,0):lpush('"><img src="/file/'):push(md(i)(0).url,0)
:lpush('"><div class="caption">'):push(md(i)(0).desc,0)
:lpush('</div></a>')
else
files:lpush('<a class="file" href="'):ppush(view.pfx):lpush('/media/a/')
:push(md(i)(0).url,0):lpush('"><span class="label">'):push(md(i)(0).desc,0)
:lpush('</span> <span class="mime">'):push(md(i)(0).mime,0)
:lpush('</span></a>')
end
md(i):free()
end
view.images = gallery:finalize()
view.directory = files:finalize()
if acc ~= nil then
view:append(acc)
else
lib.dbg('emitting page')
var pg = view:tostr() defer pg:free()
lib.dbg('compiled page')
co:stdpage([lib.srv.convo.page] {
title = P'media';
class = P'media manager';
cache = false;
body = pg;
})
lib.dbg('sent page')
end
view.images:free()
view.directory:free()
if not owner then view.pfx:free() end
if md:ref() then md:free() end
do return end
::e404:: co:complain(404,'media not found','no such media exists on this server')
end
return render_media_gallery