1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2013 MediaGoblin contributors. See AUTHORS.
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 from werkzeug
.exceptions
import MethodNotAllowed
, BadRequest
, NotImplemented
21 from werkzeug
.wrappers
import BaseResponse
23 from mediagoblin
import mg_globals
24 from mediagoblin
.meddleware
.csrf
import csrf_exempt
25 from mediagoblin
.submit
.lib
import check_file_field
26 from .tools
import CmdTable
, PwgNamedArray
, response_xml
, check_form
27 from .forms
import AddSimpleForm
, AddForm
30 _log
= logging
.getLogger(__name__
)
33 @CmdTable("pwg.session.login", True)
34 def pwg_login(request
):
35 username
= request
.form
.get("username")
36 password
= request
.form
.get("password")
37 _log
.info("Login for %r/%r...", username
, password
)
41 @CmdTable("pwg.session.logout")
42 def pwg_logout(request
):
47 @CmdTable("pwg.getVersion")
48 def pwg_getversion(request
):
49 return "2.5.0 (MediaGoblin)"
52 @CmdTable("pwg.session.getStatus")
53 def pwg_session_getStatus(request
):
54 return {'username': "fake_user"}
57 @CmdTable("pwg.categories.getList")
58 def pwg_categories_getList(request
):
59 catlist
= ({'id': -29711,
60 'uppercats': "-29711",
61 'name': "All my images"},)
63 'categories': PwgNamedArray(
79 @CmdTable("pwg.images.exist")
80 def pwg_images_exist(request
):
84 @CmdTable("pwg.images.addSimple", True)
85 def pwg_images_addSimple(request
):
86 form
= AddSimpleForm(request
.form
)
87 if not form
.validate():
88 _log
.error("addSimple: form failed")
92 dump
.append("%s=%r" % (f
.name
, f
.data
))
93 _log
.info("addimple: %r %s %r", request
.form
, " ".join(dump
), request
.files
)
95 if not check_file_field(request
, 'image'):
98 return {'image_id': 123456, 'url': ''}
101 md5sum_matcher
= re
.compile(r
"^[0-9a-fA-F]{32}$")
103 def fetch_md5(request
, parm_name
, optional_parm
=False):
104 val
= request
.form
.get(parm_name
)
105 if (val
is None) and (not optional_parm
):
106 _log
.error("Parameter %s missing", parm_name
)
107 raise BadRequest("Parameter %s missing" % parm_name
)
108 if not md5sum_matcher
.match(val
):
109 _log
.error("Parameter %s=%r has no valid md5 value", parm_name
, val
)
110 raise BadRequest("Parameter %s is not md5" % parm_name
)
114 @CmdTable("pwg.images.addChunk", True)
115 def pwg_images_addChunk(request
):
116 o_sum
= fetch_md5(request
, 'original_sum')
117 typ
= request
.form
.get('type')
118 pos
= request
.form
.get('position')
119 data
= request
.form
.get('data')
123 if not typ
in ("file", "thumb"):
124 _log
.error("type %r not allowed for now", typ
)
127 _log
.info("addChunk for %r, type %r, position %d, len: %d",
128 o_sum
, typ
, pos
, len(data
))
130 _log
.info("addChunk: Ignoring thumb, because we create our own")
136 @CmdTable("pwg.images.add", True)
137 def pwg_images_add(request
):
138 _log
.info("add: %r", request
.form
)
139 form
= AddForm(request
.form
)
142 return {'image_id': 123456, 'url': ''}
147 if request
.method
not in ("GET", "POST"):
148 _log
.error("Method %r not supported", request
.method
)
149 raise MethodNotAllowed()
151 func
= CmdTable
.find_func(request
)
153 _log
.warn("wsphp: Unhandled %s %r %r", request
.method
,
154 request
.args
, request
.form
)
155 raise NotImplemented()
157 result
= func(request
)
159 if isinstance(result
, BaseResponse
):
162 response
= response_xml(result
)