parsav  Diff

Differences From Artifact [7a98efa9ff]:

To Artifact [da27a31f83]:


11
12
13
14
15
16
17









18










































































19
20
21
22
23
24
25
26
27
28
29


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46


47












48
49
50
51

52
53
54
55
56
57
58
59
60
61

62
63
64
65
66
67

68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96


97


98
99
100
101
102
103
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







>
>
>
>
>
>
>
>
>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
|
|
|
|
|
|
|

>
>
|
|
|
|
<
<
<
|

|
|
|
|
|
|
|
<
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
|
<
<
|
>
|

|
|
|

|
|
|
|
>
|
|
|
|
|
|
>
|
|
|
|
|
|
|

|
|

|
|
|
|
|
|
|
|
|
|
|
|
|
|

|
|
<
>
>
|
>
>
|





11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118



119
120
121
122
123
124
125
126
127

128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143


144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191

192
193
194
195
196
197
198
199
200
201
202
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
	do defer ou:free()
		var pfx = pstr.null()
		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)
			pfx = pa:finalize()
		end

		if path.ct >= 3 and path(1):cmp(lib.str.lit'a') then
			var id, idok = lib.math.shorthand.parse(path(2).ptr, path(2).ct)
			if not idok then goto e404 end
			var art = co.srv:artifact_fetch(uid, id)
			if not art then goto e404 end
			if path.ct == 3 then
			-- sniff out the artifact type and display the appropriate viewer
				var artid = cs(art(0).url)
				var btns: lib.str.acc
				if owner then
					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>')
				else
					btns:compose('<a class="pos button" href="',pfx,'/media/a/',artid,'/collect">collect</a>')
				end
				var btntxt = btns:finalize() defer btntxt:free()
				var desc = lib.smackdown.html(pstr{art(0).desc,0}, true) defer desc:free()
				var viewerprops = {
					pfx = pfx, desc = desc;
					id = artid; btns = btntxt;
				}
				if lib.str.ncmp(art(0).mime, 'image/', 6) == 0 then
					var view = data.view.media_image(viewerprops)
					var pg = view:tostr()
					co:stdpage([lib.srv.convo.page] {
						title = lib.str.plit'media :: image';
						class = lib.str.plit'media viewer img';
						cache = false, body = pg;
					})
					pg:free()
				elseif lib.str.cmp(art(0).mime, 'text/markdown') == 0 then
					var view = data.view.media_text(viewerprops)
					var text, mime = co.srv:artifact_load(id) mime:free()
					view.text = lib.smackdown.html(pstr{[rawstring](text.ptr),text.ct}, false)
					text:free()
					var pg = view:tostr()
					view.text:free()
					co:stdpage([lib.srv.convo.page] {
						title = lib.str.plit'media :: text';
						class = lib.str.plit'media viewer text';
						cache = false, body = pg;
					})
					pg:free()
				elseif
					lib.str.ncmp(art(0).mime, 'text/', 5) == 0          or
					lib.str.cmp(art(0).mime, 'application/x-perl') == 0 or
					lib.str.cmp(art(0).mime, 'application/sql') == 0
					-- and so on (we need a mimelib at some point) --
				then
					var view = data.view.media_text(viewerprops)
					var text, mime = co.srv:artifact_load(id) mime:free()
					var san = lib.html.sanitize(pstr{[rawstring](text.ptr),text.ct}, false)
					text:free()
					view.text = lib.str.acc{}:compose('<pre>',san,'</pre>'):finalize()
					san:free()
					var pg = view:tostr()
					view.text:free()
					co:stdpage([lib.srv.convo.page] {
						title = lib.str.plit'media :: text';
						class = lib.str.plit'media viewer text';
						cache = false, body = pg;
					})
					pg:free()
				else co:complain(500,'bad file type','this file type is not supported') end
			elseif path.ct == 4 then
				var act = path(3)
				var curl = lib.str.acc{}:compose(pfx, '/media/a/', path(2)):finalize()
				defer curl:free()
				if act:cmp(lib.str.lit'avi') and lib.str.ncmp(art(0).mime, 'image/', 6) == 0 then
					co:confirm('set avatar', 'are you sure you want this image to be your new avatar?',curl)
				elseif act:cmp(lib.str.lit'del') then
					co:confirm('delete', 'are you sure you want to permanently delete this artifact?',curl)
				else goto e404 end
			end
		else
			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

			var folders = co.srv:artifact_folder_enum(uid)

			if mode == show_new then
				folder = lib.str.plit''
			elseif mode == show_all or mode == show_unfiled then
				folder = pstr.null()



			end

			var view = data.view.media_gallery {
				menu = pstr{'',0};
				folders = pstr{'',0};
				directory = pstr{'',0};
				images = pstr{'',0};
				pfx = pfx;
			}


			if folders.ct > 0 then
				var fa: lib.str.acc fa:init(128)
				var fldr = co:pgetv('folder')
				for i=0,folders.ct do
					var ule = lib.html.urlenc(folders(i), true) defer ule:free()
					var san = lib.html.sanitize(folders(i), true) defer san:free()
					fa:lpush('<a href="'):ppush(pfx):lpush('/media?folder='):ppush(ule)
						:lpush('">'):ppush(san):lpush('</a>')
					lib.dbg('checking folder ',{fldr.ptr,fldr.ct},' against ',{folders(i).ptr,folders(i).ct})
					if fldr:ref() and folders(i):cmp(fldr)
						then folder = folders(i) lib.dbg('folder match ',{fldr.ptr,fldr.ct})
						else folders(i):free()
					end
				end
				fa:lpush('<hr>')


				view.folders = fa:finalize()
				folders:free()
			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
				var desc = lib.smackdown.html(pstr{md(i)(0).desc,0}, true) defer desc:free()
				if lib.str.ncmp(md(i)(0).mime, 'image/', 6) == 0 then
					gallery:lpush('<a class="thumb" href="'):ppush(pfx):lpush('/media/a/')
						:push(md(i)(0).url,0):lpush('"><img src="/file/'):push(md(i)(0).url,0)
						:lpush('"><div class="caption">'):ppush(desc)
						:lpush('</div></a>')
				else
					var mime = lib.html.sanitize(pstr{md(i)(0).mime,0}, true) defer mime:free() --just in case
					files:lpush('<a class="file" href="'):ppush(pfx):lpush('/media/a/')
						:push(md(i)(0).url,0):lpush('"><span class="label">'):ppush(desc)
						:lpush('</span> <span class="mime">'):ppush(mime)
						: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 view.folders.ct > 0 then view.folders:free() end
			if folder.ct > 0 then folder:free() end
			if md:ref() then md:free() end
		end
		if not owner then pfx:free() end
	return end

	::e404:: co:complain(404,'media not found','no such media exists on this server')
end

return render_media_gallery