0

I am using a custom python framework, (not django or webapp2), following this instructions: Create an upload URL, I have been available to upload the file, basically I submit my form to an URL provided by a script with this:

from google.appengine.ext import blobstore
upload_url = blobstore.create_upload_url('/upload')

By checking the blobstore viewer I notice that the file has been uploaded, this brings my first question.

How to filter, before uploading? for example, how to upload only if an image type is a jpg or png and discard anything else.

My second question is, how to properly handle the the uploaded file, what I found so far is to create a custom get_uploads method, this is what I am using but wondering if is the right way:

import cgi
from google.appengine.ext import blobstore

class APIResource(object):

    def get_uploads(self, request, field_name=None):
        if hasattr(request, '__uploads') == False:
            request.environ['wsgi.input'].seek(0)
            fields = cgi.FieldStorage(
                    request.environ['wsgi.input'],
                    environ=request.environ)

        request.__uploads = {}

        for key in fields.keys():
            field = fields[key]
            if isinstance(field, cgi.FieldStorage):
                if 'blob-key' in field.type_options:
                    request.__uploads.setdefault(
                        key, []).append(
                        blobstore.parse_blob_info(field))

    if field_name:
        try:
            return list(request.__uploads[field_name])
        except KeyError:
            return []
    else:
        results = []
        for uploads in request.__uploads.itervalues():
            results.extend(uploads)
        return results

    def dispatch(self, request, response):

        blob_info = self.get_uploads(request, 'picture')[0]

        print blob_info, blob_info.key(), request.__uploads

I would like to avoid uploading first, later check if type is valid and delete if not, so that I can not fill my storage with garbage in case I can't handle/get the upload key.

Any ideas ?

nbari
  • 25,603
  • 10
  • 76
  • 131
  • Limit your questions to one per question because otherwise you won't get any good answers and it's not how SO works.. – Lipis Mar 12 '14 at 10:35
  • http://stackoverflow.com/questions/1561847/html-how-to-limit-file-upload-to-be-only-images – Lipis Mar 12 '14 at 10:36

0 Answers0