# -*- coding: utf-8 -*- # gae_untargz.py 2012.3.13 import logging import os import StringIO import tarfile from google.appengine.api import users from google.appengine.api import memcache from google.appengine.ext import webapp from google.appengine.ext.webapp import template from google.appengine.ext.webapp.util import run_wsgi_app class untargz(webapp.RequestHandler): def get(self): user = users.get_current_user() prefix = user.user_id() logging.info(prefix) targz_file = memcache.get(prefix + 'targz_file') filename = memcache.get(prefix + 'filename') action = self.request.get('action') member = self.request.get('member') members = [] if targz_file: fileobj = StringIO.StringIO(targz_file) tar = tarfile.open(fileobj = fileobj, mode = 'r:gz') if action=='view': f = tar.extractfile(member) self.response.headers["Content-Type"] = "text/plain" self.response.out.write(f.read()) return for tarinfo in tar: members.append(tarinfo.name) template_values = {'filename': filename, 'members': members, 'action': action} path = os.path.join(os.path.dirname(__file__), "templates", "untargz.html") self.response.out.write(template.render(path, template_values)) def post(self): user = users.get_current_user() prefix = user.user_id() targz_file = self.request.get('targz_file') if targz_file: filename = self.request.body_file.vars['targz_file'].filename memcache.set(prefix + 'targz_file', targz_file, 180) memcache.set(prefix + 'filename', filename, 180) self.redirect('/untargz?action=list') application = webapp.WSGIApplication([ ('/untargz.*', untargz), ], debug=True) def main(): run_wsgi_app(application) if __name__ == '__main__': main()
---
0 件のコメント:
コメントを投稿