parsav  Diff

Differences From Artifact [e8c2143a0c]:

To Artifact [af3d41e739]:


     1      1   -- vim: ft=terra
     2      2   local r = lib.srv.route
     3      3   local method = lib.http.method
     4      4   local pstring = lib.mem.ptr(int8)
     5      5   local rstring = lib.mem.ref(int8)
            6  +local binblob = lib.mem.ptr(uint8)
     6      7   local hpath = lib.mem.ptr(rstring)
     7      8   local http = {}
     8      9   
     9     10   terra meth_get(meth: method.t) return (meth == method.get) or (meth == method.head) end
    10     11   
    11     12   terra http.actor_profile(co: &lib.srv.convo, actor: &lib.store.actor, meth: method.t)
    12     13   	var rel: lib.store.relationship
................................................................................
   395    396   			return
   396    397   		else goto badop end
   397    398   	end
   398    399   
   399    400   	lib.render.notices(co)
   400    401   	do return end
   401    402   
   402         -	::badop :: do co:complain(405, 'invalid operation', 'the operation you have attempted on this post is not meaningful') return end
          403  +	::badop:: do co:complain(405, 'invalid operation', 'the operation you have attempted on this post is not meaningful') return end
          404  +end
          405  +
          406  +terra http.media_manager(co: &lib.srv.convo, path: hpath, meth: method.t)
          407  +	if meth == method.post then
          408  +		goto badop
          409  +	end
          410  +
          411  +	if path.ct == 1 or (path.ct >= 3 and path(1):cmp(lib.str.lit'a')) then
          412  +		if meth == method.post then goto badop end
          413  +		lib.render.media_gallery(co,path,co.who.id,nil)
          414  +	elseif path.ct == 2 then
          415  +		if path(1):cmp(lib.str.lit'upload') and co.who.rights.powers.artifact() then
          416  +			if meth == method.get then
          417  +				var view = data.view.media_upload {
          418  +					folders = ''
          419  +				}
          420  +				var pg = view:tostr() defer pg:free()
          421  +				co:stdpage([lib.srv.convo.page] {
          422  +					title = lib.str.plit'media :: upload';
          423  +					class = lib.str.plit'media upload';
          424  +					cache = false; body = pg;
          425  +				})
          426  +			elseif meth == method.post_file then
          427  +				var desc = pstring.null()
          428  +				var folder = pstring.null()
          429  +				var mime = pstring.null()
          430  +				var name = pstring.null()
          431  +				var body = binblob.null()
          432  +				for i=0, co.uploads.sz do var up = co.uploads.storage.ptr + i
          433  +					if up.body.ct > 0 then
          434  +						if up.field:cmp(lib.str.plit'desc') then
          435  +							desc = up.body
          436  +						elseif up.field:cmp(lib.str.plit'folder') then
          437  +							folder = up.body
          438  +						elseif up.field:cmp(lib.str.plit'file') then
          439  +							mime = up.ctype
          440  +							body = binblob {ptr = [&uint8](up.body.ptr), ct = up.body.ct}
          441  +							name = up.filename
          442  +						end
          443  +					end
          444  +				end
          445  +				if not body then goto badop end
          446  +				if body.ct > co.srv.cfg.maxupsz then
          447  +					co:complain(403, 'file too long', "the file you have attempted to upload exceeds the maximum length permitted by this server's upload policy. if it is an image or video, try compressing it at a lower quality setting or resolution")
          448  +					return
          449  +				end
          450  +				var id = co.srv:artifact_instantiate(body,mime)
          451  +				if id == 0 then
          452  +					co:complain(500,'upload failed','artifact rejected. either the server is running out of space or this file is banned from the server')
          453  +					return
          454  +				end
          455  +				co.srv:artifact_expropriate(co.who.id,id,desc,folder)
          456  +
          457  +				var idbuf: int8[lib.math.shorthand.maxlen]
          458  +				var idlen = lib.math.shorthand.gen(id,&idbuf[0])
          459  +
          460  +				var url = lib.str.acc{}:compose('/media/a/',pstring{&idbuf[0],idlen}):finalize()
          461  +				co:reroute(url.ptr)
          462  +				url:free()
          463  +			else goto badop end
          464  +		end
          465  +	else goto e404 end
          466  +	do return end
          467  +
          468  +	::badop:: do co:complain(405, 'invalid operation', 'the operation you have attempted on this post is not meaningful') return end
          469  +	::e404:: do co:complain(404, 'artifact not found', 'no such artifact has been uploaded by this user') return end
   403    470   end
   404    471   
   405    472   do local branches = quote end
   406    473   	local filename, flen = symbol(&int8), symbol(intptr)
   407    474   	local page = symbol(lib.http.page)
   408    475   	local send = label()
   409    476   	local storage = data.stmap
................................................................................
   483    550   		var path = lib.http.hier(uri) defer path:free()
   484    551   		if path.ct > 1 and path(0):cmp(lib.str.lit('user')) then
   485    552   			http.actor_profile_uid(co, path, meth)
   486    553   		elseif path.ct > 1 and path(0):cmp(lib.str.lit('post')) then
   487    554   			http.tweet_page(co, path, meth)
   488    555   		elseif path(0):cmp(lib.str.lit('tl')) then
   489    556   			http.timeline(co, path)
          557  +		elseif path(0):cmp(lib.str.lit('media')) then
          558  +			http.media_manager(co, path, meth)
   490    559   		elseif path(0):cmp(lib.str.lit('doc')) then
   491    560   			if not meth_get(meth) then goto wrongmeth end
   492    561   			http.documentation(co, path)
   493    562   		elseif path(0):cmp(lib.str.lit('conf')) then
   494    563   			if co.aid == 0 then goto unauth end
   495    564   			http.configure(co,path,meth)
   496    565   		else goto notfound end