Filehandler
#!/usr/bin/python
# -*- coding: utf-8 -*-
from google.appengine.ext.webapp import blobstore_handlers
from db import Filedb
from . import BaseHTML
from ..import util as siteutil
 
#暫例,實際上需要跟其他畫面配合
class UploadFormHandler(webapp.RequestHandler):
    def get(self):
        upload_url = blobstore.create_upload_url('/upload_photo')
        # The method must be "POST" and enctype must be set to "multipart/form-data".
        htmlbody = '''<html><body>select a file to upload<form action="%s" method="POST" enctype="multipart/form-data">
            Upload File: <input type="file" name="file"><br> <input type="submit"
            name="submit" value="Submit"> </form></body></html>'''
        self.response.out.write(htmlbody % upload_url)
 
class FileUploadHandler(blobstore_handlers.BlobstoreUploadHandler):
    '''
    連結網址:/upload/(username)/
    '''
    def post(self, username):
        try:
            upload = self.get_uploads('file')[0]
            blobkey = upload.key()
            user = siteutil.findUsedUser(username)
            file = Filedb(parent = user,key_name = blobkey[:5] + blobkey[-5:],uploader = user, file = blobkey)
        except:
            #上傳失敗
            pass
            #還沒想到怎麼處理
 
class FileOutputer(blobstore_handlers.BlobstoreDownloadHandler):
    '''
    連結網址:/file/(fileid)/
    '''
    def get(self, fileid, username = none):
        filedata = Filedb.gql('where fileid = :1', fileid)[0]
        if not filedata or blobstore.get(filedata.file):
            self.error(404)
        else:
            self.send_blob(filedata.file)