Beispiel #1
0
    def post(self):
        # 'file' is file upload field in the form
        upload_files = self.get_uploads('file')
        for blob_info in upload_files:
            if blob_info.content_type not in content_types.split(','):
                # Ignore non-images.
                logging.warning("Invalid mimetype %s, skipping"
                                % blob_info.content_type)
                blob_info.delete()
                continue

            # images.Image doesn't have width and height when built from a
            # blob_key.  The documentation doesn't state that it's safe to
            # build an images.Image with partial data, so manually sniff image
            # dimensions.
            # http://thejapanesepage.com/phpbb/download/file.php?id=247 needs
            # at least 150kB of image data.
            data = blobstore.fetch_data(blob_info, 0, 200000) 
            try:
                width, height = peekimagedata.peek_dimensions(data)
                mimetype = peekimagedata.peek_mimetype(data)
            except ValueError:
                logging.warning("Failed to peek, skipping")
                blob_info.delete()
                continue

            if blob_info.content_type != mimetype:
                logging.warning("Sniffed mimetype didn't match, skipping")
                blob_info.delete()
                continue
            
            store_blob(blob_info.key(), width, height)

        self.redirect('/')
Beispiel #2
0
def write_image_blob(data, name):
    _, ext = os.path.splitext(name)
    ext = ext.lower()
    if ext not in ['.png', '.jpg', '.jpeg']:
        logging.warning("invalid extension on '%s', skipping" % name)
        return

    mimetype = {
      '.png': 'image/png',
      '.jpg': 'image/jpeg',
      '.jpeg': 'image/jpeg',
    }[ext]

    try:
      sniffed_mimetype = peekimagedata.peek_mimetype(data)
      width, height = peekimagedata.peek_dimensions(data)
    except ValueError as e:
      logging.warning("Failed to get dimensions/mimetype, skipping '%s': %s" %
                      (name, e))
      return

    if sniffed_mimetype != mimetype:
        logging.warning("Invalid mimetype (%s, %s), skipping '%s'" %
                        (sniffed_mimetype, mimetype, name))
        return

    # Note: Setting _blobinfo_uploaded_filename is extra-unsupported.
    file_name = files.blobstore.create(mime_type=mimetype,
                                       _blobinfo_uploaded_filename=name)
    with files.open(file_name, 'a') as f:
      # XXX: filter out dupes
      f.write(data)
    files.finalize(file_name)

    blob_key = files.blobstore.get_blob_key(file_name)
    store_blob(blob_key, width, height)
 def assertSize(self, expected_size, image):
     size = peekimagedata.peek_dimensions(open(image).read())
     self.assertEqual(expected_size, size)