Differences From
Artifact [7a98efa9ff]:
11 11 render_media_gallery(co: &lib.srv.convo, path: lib.mem.ptr(lib.mem.ref(int8)), uid: uint64, acc: &lib.str.acc)
12 12 -- note that when calling this function, path must be adjusted so that path(0)
13 13 -- eq "media"
14 14 var owner = false
15 15 if co.aid ~= 0 and co.who.id == uid then owner = true end
16 16 var ou = co.srv:actor_fetch_uid(uid)
17 17 if not ou then goto e404 end
18 -
19 - var mode: uint8 = show_new
20 - var folder: pstr
21 - if path.ct == 2 then
22 - if path(1):cmp(lib.str.lit'unfiled') then
23 - mode=show_unfiled
24 - elseif path(1):cmp(lib.str.lit'all') then
25 - mode=show_all
26 - else goto e404 end
27 - elseif path.ct == 3 and path(1):cmp(lib.str.lit'kind') then
28 - end
18 + do defer ou:free()
19 + var pfx = pstr.null()
20 + if not owner then
21 + var pa: lib.str.acc pa:init(32)
22 + pa:lpush('/')
23 + if ou(0).origin ~= 0 then pa:lpush('@') end
24 + pa:push(ou(0).xid,0)
25 + pfx = pa:finalize()
26 + end
29 27
30 - if mode == show_new then
31 - folder = lib.str.plit''
32 - elseif mode == show_all then
33 - folder = pstr.null()
34 - elseif mode == show_unfiled then
35 - folder = lib.str.plit'' -- TODO
36 - -- else get folder from query str
37 - end
38 -
39 - var view = data.view.media_gallery {
40 - menu = pstr{'',0};
41 - folders = pstr{'',0};
42 - directory = pstr{'',0};
43 - images = pstr{'',0};
44 - pfx = pstr{'',0};
45 - }
46 - if not owner then
47 - var pa: lib.str.acc pa:init(32)
48 - pa:lpush('/')
49 - if ou(0).origin ~= 0 then pa:lpush('@') end
50 - pa:push(ou(0).xid,0)
51 - view.pfx = pa:finalize()
52 - end
53 -
54 - if owner then
55 - view.menu = P'<a class="pos" href="/media/upload">upload</a><hr>'
56 - end
57 -
58 - var md = co.srv:artifact_enum_uid(uid, folder)
59 - var gallery: lib.str.acc gallery:init(256)
60 - var files: lib.str.acc files:init(256)
61 - for i=0,md.ct do
62 - if lib.str.ncmp(md(i)(0).mime, 'image/', 6) == 0 then
63 - gallery:lpush('<a class="thumb" href="'):ppush(view.pfx):lpush('/media/a/')
64 - :push(md(i)(0).url,0):lpush('"><img src="/file/'):push(md(i)(0).url,0)
65 - :lpush('"><div class="caption">'):push(md(i)(0).desc,0)
66 - :lpush('</div></a>')
28 + if path.ct >= 3 and path(1):cmp(lib.str.lit'a') then
29 + var id, idok = lib.math.shorthand.parse(path(2).ptr, path(2).ct)
30 + if not idok then goto e404 end
31 + var art = co.srv:artifact_fetch(uid, id)
32 + if not art then goto e404 end
33 + if path.ct == 3 then
34 + -- sniff out the artifact type and display the appropriate viewer
35 + var artid = cs(art(0).url)
36 + var btns: lib.str.acc
37 + if owner then
38 + btns:compose('<a class="neg button" href="',pfx,'/media/a/',artid,'/del">delete</a><a class="button" href="',pfx,'/media/a/',artid,'/edit">alter</a>')
39 + else
40 + btns:compose('<a class="pos button" href="',pfx,'/media/a/',artid,'/collect">collect</a>')
41 + end
42 + var btntxt = btns:finalize() defer btntxt:free()
43 + var desc = lib.smackdown.html(pstr{art(0).desc,0}, true) defer desc:free()
44 + var viewerprops = {
45 + pfx = pfx, desc = desc;
46 + id = artid; btns = btntxt;
47 + }
48 + if lib.str.ncmp(art(0).mime, 'image/', 6) == 0 then
49 + var view = data.view.media_image(viewerprops)
50 + var pg = view:tostr()
51 + co:stdpage([lib.srv.convo.page] {
52 + title = lib.str.plit'media :: image';
53 + class = lib.str.plit'media viewer img';
54 + cache = false, body = pg;
55 + })
56 + pg:free()
57 + elseif lib.str.cmp(art(0).mime, 'text/markdown') == 0 then
58 + var view = data.view.media_text(viewerprops)
59 + var text, mime = co.srv:artifact_load(id) mime:free()
60 + view.text = lib.smackdown.html(pstr{[rawstring](text.ptr),text.ct}, false)
61 + text:free()
62 + var pg = view:tostr()
63 + view.text:free()
64 + co:stdpage([lib.srv.convo.page] {
65 + title = lib.str.plit'media :: text';
66 + class = lib.str.plit'media viewer text';
67 + cache = false, body = pg;
68 + })
69 + pg:free()
70 + elseif
71 + lib.str.ncmp(art(0).mime, 'text/', 5) == 0 or
72 + lib.str.cmp(art(0).mime, 'application/x-perl') == 0 or
73 + lib.str.cmp(art(0).mime, 'application/sql') == 0
74 + -- and so on (we need a mimelib at some point) --
75 + then
76 + var view = data.view.media_text(viewerprops)
77 + var text, mime = co.srv:artifact_load(id) mime:free()
78 + var san = lib.html.sanitize(pstr{[rawstring](text.ptr),text.ct}, false)
79 + text:free()
80 + view.text = lib.str.acc{}:compose('<pre>',san,'</pre>'):finalize()
81 + san:free()
82 + var pg = view:tostr()
83 + view.text:free()
84 + co:stdpage([lib.srv.convo.page] {
85 + title = lib.str.plit'media :: text';
86 + class = lib.str.plit'media viewer text';
87 + cache = false, body = pg;
88 + })
89 + pg:free()
90 + else co:complain(500,'bad file type','this file type is not supported') end
91 + elseif path.ct == 4 then
92 + var act = path(3)
93 + var curl = lib.str.acc{}:compose(pfx, '/media/a/', path(2)):finalize()
94 + defer curl:free()
95 + if act:cmp(lib.str.lit'avi') and lib.str.ncmp(art(0).mime, 'image/', 6) == 0 then
96 + co:confirm('set avatar', 'are you sure you want this image to be your new avatar?',curl)
97 + elseif act:cmp(lib.str.lit'del') then
98 + co:confirm('delete', 'are you sure you want to permanently delete this artifact?',curl)
99 + else goto e404 end
100 + end
67 101 else
68 - files:lpush('<a class="file" href="'):ppush(view.pfx):lpush('/media/a/')
69 - :push(md(i)(0).url,0):lpush('"><span class="label">'):push(md(i)(0).desc,0)
70 - :lpush('</span> <span class="mime">'):push(md(i)(0).mime,0)
71 - :lpush('</span></a>')
72 - end
73 - md(i):free()
74 - end
102 + var mode: uint8 = show_new
103 + var folder: pstr
104 + if path.ct == 2 then
105 + if path(1):cmp(lib.str.lit'unfiled') then
106 + mode=show_unfiled
107 + elseif path(1):cmp(lib.str.lit'all') then
108 + mode=show_all
109 + else goto e404 end
110 + elseif path.ct == 3 and path(1):cmp(lib.str.lit'kind') then
111 + end
112 +
113 + var folders = co.srv:artifact_folder_enum(uid)
114 +
115 + if mode == show_new then
116 + folder = lib.str.plit''
117 + elseif mode == show_all or mode == show_unfiled then
118 + folder = pstr.null()
119 + end
120 +
121 + var view = data.view.media_gallery {
122 + menu = pstr{'',0};
123 + folders = pstr{'',0};
124 + directory = pstr{'',0};
125 + images = pstr{'',0};
126 + pfx = pfx;
127 + }
128 +
129 + if folders.ct > 0 then
130 + var fa: lib.str.acc fa:init(128)
131 + var fldr = co:pgetv('folder')
132 + for i=0,folders.ct do
133 + var ule = lib.html.urlenc(folders(i), true) defer ule:free()
134 + var san = lib.html.sanitize(folders(i), true) defer san:free()
135 + fa:lpush('<a href="'):ppush(pfx):lpush('/media?folder='):ppush(ule)
136 + :lpush('">'):ppush(san):lpush('</a>')
137 + lib.dbg('checking folder ',{fldr.ptr,fldr.ct},' against ',{folders(i).ptr,folders(i).ct})
138 + if fldr:ref() and folders(i):cmp(fldr)
139 + then folder = folders(i) lib.dbg('folder match ',{fldr.ptr,fldr.ct})
140 + else folders(i):free()
141 + end
142 + end
143 + fa:lpush('<hr>')
144 + view.folders = fa:finalize()
145 + folders:free()
146 + end
147 +
148 + if owner then
149 + view.menu = P'<a class="pos" href="/media/upload">upload</a><hr>'
150 + end
151 +
152 + var md = co.srv:artifact_enum_uid(uid, folder)
153 + var gallery: lib.str.acc gallery:init(256)
154 + var files: lib.str.acc files:init(256)
155 + for i=0,md.ct do
156 + var desc = lib.smackdown.html(pstr{md(i)(0).desc,0}, true) defer desc:free()
157 + if lib.str.ncmp(md(i)(0).mime, 'image/', 6) == 0 then
158 + gallery:lpush('<a class="thumb" href="'):ppush(pfx):lpush('/media/a/')
159 + :push(md(i)(0).url,0):lpush('"><img src="/file/'):push(md(i)(0).url,0)
160 + :lpush('"><div class="caption">'):ppush(desc)
161 + :lpush('</div></a>')
162 + else
163 + var mime = lib.html.sanitize(pstr{md(i)(0).mime,0}, true) defer mime:free() --just in case
164 + files:lpush('<a class="file" href="'):ppush(pfx):lpush('/media/a/')
165 + :push(md(i)(0).url,0):lpush('"><span class="label">'):ppush(desc)
166 + :lpush('</span> <span class="mime">'):ppush(mime)
167 + :lpush('</span></a>')
168 + end
169 + md(i):free()
170 + end
75 171
76 - view.images = gallery:finalize()
77 - view.directory = files:finalize()
172 + view.images = gallery:finalize()
173 + view.directory = files:finalize()
78 174
79 - if acc ~= nil then
80 - view:append(acc)
81 - else
82 - lib.dbg('emitting page')
83 - var pg = view:tostr() defer pg:free()
84 - lib.dbg('compiled page')
85 - co:stdpage([lib.srv.convo.page] {
86 - title = P'media';
87 - class = P'media manager';
88 - cache = false;
89 - body = pg;
90 - })
91 - lib.dbg('sent page')
92 - end
175 + if acc ~= nil then
176 + view:append(acc)
177 + else
178 + lib.dbg('emitting page')
179 + var pg = view:tostr() defer pg:free()
180 + lib.dbg('compiled page')
181 + co:stdpage([lib.srv.convo.page] {
182 + title = P'media';
183 + class = P'media manager';
184 + cache = false;
185 + body = pg;
186 + })
187 + lib.dbg('sent page')
188 + end
93 189
94 - view.images:free()
95 - view.directory:free()
96 - if not owner then view.pfx:free() end
97 - if md:ref() then md:free() end
98 - do return end
190 + view.images:free()
191 + view.directory:free()
192 + if view.folders.ct > 0 then view.folders:free() end
193 + if folder.ct > 0 then folder:free() end
194 + if md:ref() then md:free() end
195 + end
196 + if not owner then pfx:free() end
197 + return end
99 198
100 199 ::e404:: co:complain(404,'media not found','no such media exists on this server')
101 200 end
102 201
103 202 return render_media_gallery