Skip to content

Commit

Permalink
Fixed upload function.
Browse files Browse the repository at this point in the history
  • Loading branch information
taehoon-kang committed Mar 27, 2012
1 parent 2c3bbd7 commit 7cbb84b
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 430 deletions.
10 changes: 4 additions & 6 deletions action/upload.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from google.appengine.api import urlfetch
from lib.controller import Action
from lib.multipart import MultipartParser
from lib.poster.encode import multipart_encode, MultipartParam
import settings

Expand All @@ -15,15 +14,14 @@ def post(self):
if item.find('=') > -1:
pair = item.split('=')
meta[pair[0].strip()] = pair[1].replace('"', '').strip()

part = MultipartParser(stream=self.request.body_file, boundary=meta['boundary'], charset='utf-8' if not meta.has_key('charset') else meta['charset']).get('fileupload')
if not part.content_type.lower().startswith('image/'):
file_vars = self.request.body_file.vars['fileupload']
if not file_vars.type.lower().startswith('image'):
raise ValueError('This is not a image file.')
datagen, headers = multipart_encode([('key', settings.IMAGE_SHACK_API_KEY), MultipartParam('fileupload', fileobj=part.file, filename=part.filename, filetype=part.content_type, filesize=part.size)])
datagen, headers = multipart_encode([('key', settings.IMAGE_SHACK_API_KEY), MultipartParam(file_vars.name, fileobj=file_vars.file, filename=file_vars.filename, filetype=file_vars.type)])
result = urlfetch.fetch(
url='http://www.imageshack.us/upload_api.php',
payload=''.join(datagen),
method=urlfetch.POST,
headers=headers)
part.file.close()
file_vars.file.close()
self.response.out.write(result.content)
6 changes: 3 additions & 3 deletions app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ libraries:
version: "latest"

builtins:
- remote_api: on
- appstats: on
- deferred: on
- admin_redirect: on

admin_console:
pages:
- name: Category Admin
url: /admin/category

inbound_services:
- xmpp_message

handlers:
- url: /static
static_dir: static
Expand Down
6 changes: 2 additions & 4 deletions lib/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ def initialize(self, request, response):
if not action_module :
action_module = 'index'

self._current_request_args = {}
path_len = len(path)
if path_len > 1:
action_instance = ''.join([x.title() for x in path[1].split('-')])
self._current_request_args = [urllib.unquote_plus(item).decode('utf-8') for item in path[2:]]
else:
action_instance = 'Index'
self._current_request_args = []
del path

logging.debug('Current action module : %s, class : %s' % (action_module, action_instance))
Expand Down Expand Up @@ -107,9 +107,7 @@ def _execute(self, method_name, *args):
if message:
self.response.write(status)
return

self.__action.lang = self.request.lang


output = self.request.get('output')

if output == Action.Result.JSON or (output != Action.Result.HTML and result == Action.Result.DEFAULT and self.__action.is_ajax) or result is Action.Result.JSON:
Expand Down
Loading

0 comments on commit 7cbb84b

Please sign in to comment.