Ejemplo n.º 1
0
def img(bkey):
    blob_info = blobstore.get(bkey)
    if blob_info:
        response = make_response(blob_info.open().read())
        response.headers['Content-Type'] = blob_info.content_type
        return response
    return make_response()
Ejemplo n.º 2
0
def upload():
    """
    Endpoint after GCS upload
    :return: JSON --> filelink, filename, thumb
    """
    if request.method == 'POST':
        request_file = request.files['file']

        # Creates the options for the file
        header = request_file.headers['Content-Type']
        parsed_header = parse_options_header(header)

        # IF everything is OK, save the file
        if request_file:
            try:
                blob_key = parsed_header[1]['blob-key']
                blob_info = blobstore.get(blob_key)
                blob_key_object = blob_info._BlobInfo__key
                img_url = '/admin/file_serve/'+blob_key
                img_gallery = img_url

                # Check if is image and save a reference
                if blob_info.content_type in IMAGES_MIME:
                    img = Image(image_data=blob_info.open().read())

                    if img.height > 1600 or img.width > 1600:
                        img_gallery = get_serving_url(blob_key_object,
                                                      size=1600)

                        # Landscape
                        if img.height < img.width:
                            img_height = (img.height*1600)/img.width
                            img_width = 1600

                        # Portrait
                        else:
                            img_width = (img.width*1600)/img.height
                            img_height = 1600
                    else:
                        img_height = img.height
                        img_width = img.width

                    img_ref = ImageReference(filename=blob_info.filename,
                                             blob=blob_key_object,
                                             url=img_url,
                                             thumb=get_serving_url(blob_key_object, crop=True, size=200),
                                             gallery=img_gallery,
                                             height=img_height,
                                             width=img_width)
                    img_ref.put()

                return jsonify({"filelink": "/admin/file_serve/" + blob_key,
                                "filegallery": img_gallery,
                                "filename": "" + blob_info.filename,
                                "thumb": get_serving_url(blob_key, crop=True, size=200)})
            except Exception as e:
                logging.exception(e)
                return jsonify({"error": e})
Ejemplo n.º 3
0
def file_serve(blob_key):
    """
    Serves request file
    :param blob_key: The file blob key
    :return: File
    """
    blob_info = blobstore.get(blob_key)
    response = make_response(blob_info.open().read())
    response.headers['Content-Type'] = blob_info.content_type
    response.headers['Content-Disposition'] = 'attachment; filename = "' + blob_info.filename + '"'
    return response
Ejemplo n.º 4
0
def file_serve(blob_key):
    """
    Serves request file
    :param blob_key: The file blob key
    :return: File
    """
    blob_info = blobstore.get(blob_key)
    response = make_response(blob_info.open().read())
    response.headers['Content-Type'] = blob_info.content_type
    response.headers[
        'Content-Disposition'] = 'attachment; filename = "' + blob_info.filename + '"'
    return response
Ejemplo n.º 5
0
 def create_from_blob_key(cls, blob_key):
     # return the img instance
     if blob_key:
         blob_info = blobstore.get(blob_key)
         if blob_info:
             image = images.Image(blobstore.BlobReader(blob_key).read())
             return cls(
                 blob_key=blob_key,
                 url=images.get_serving_url(blob_key),
                 width=image.width,
                 height=image.height,
                 format=cls.get_format_string(image.format),
             )
     raise DoesNotExistError(u"Could not get image by Blob Key: %s" % blob_key)
Ejemplo n.º 6
0
 def get(self, blob_key):
     blob_key = str(urllib.unquote(blob_key))
     if not blobstore.get(blob_key):
         self.error(404)
     else:
         self.send_blob(blobstore.BlobInfo.get(blob_key), save_as=True)
Ejemplo n.º 7
0
def upload():
    """
    Endpoint after GCS upload
    :return: JSON --> filelink, filename, thumb
    """
    if request.method == 'POST':
        request_file = request.files['file']

        # Creates the options for the file
        header = request_file.headers['Content-Type']
        parsed_header = parse_options_header(header)

        # IF everything is OK, save the file
        if request_file:
            try:
                blob_key = parsed_header[1]['blob-key']
                blob_info = blobstore.get(blob_key)
                blob_key_object = blob_info._BlobInfo__key
                img_url = '/admin/file_serve/' + blob_key
                img_gallery = img_url

                # Check if is image and save a reference
                if blob_info.content_type in IMAGES_MIME:
                    img = Image(image_data=blob_info.open().read())

                    if img.height > 1600 or img.width > 1600:
                        img_gallery = get_serving_url(blob_key_object,
                                                      size=1600)

                        # Landscape
                        if img.height < img.width:
                            img_height = (img.height * 1600) / img.width
                            img_width = 1600

                        # Portrait
                        else:
                            img_width = (img.width * 1600) / img.height
                            img_height = 1600
                    else:
                        img_height = img.height
                        img_width = img.width

                    img_ref = ImageReference(filename=blob_info.filename,
                                             blob=blob_key_object,
                                             url=img_url,
                                             thumb=get_serving_url(
                                                 blob_key_object,
                                                 crop=True,
                                                 size=200),
                                             gallery=img_gallery,
                                             height=img_height,
                                             width=img_width)
                    img_ref.put()

                return jsonify({
                    "filelink": "/admin/file_serve/" + blob_key,
                    "filegallery": img_gallery,
                    "filename": "" + blob_info.filename
                })
            except Exception as e:
                logging.exception(e)
                return jsonify({"error": e})