Esempio n. 1
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})
Esempio n. 2
0
def image_manager(page):
    """
    GET --> The main image manager page
    POST --> Delete requested file(s)
    :param page: The requested page
    """
    if request.method == 'POST':
        img_ref_key = request.get_json()

        # Delete the img from ndb
        for img_ref in img_ref_key['objects']:
            img_inst = ndb.Key(ImageReference, int(img_ref))
            img = img_inst.get()
            blob_key = img.blob

            # Delete img and blob
            img_inst.delete()
            BlobInfo.get(blob_key).delete()
            logging.info("Delete image: {}".format(img_ref))

        return "true"

    offset = (page - 1) * IMAGES_PER_PAGE
    images = ImageReference.query().order(-ImageReference.date)
    pagination = Pagination(page, IMAGES_PER_PAGE, images.count())
    query = images.fetch(IMAGES_PER_PAGE, offset=offset)

    return render_template('image-manager/admin-manager-images.html',
                           keys=query,
                           pagination=pagination)
Esempio n. 3
0
def image_manager(page):
    """
    GET --> The main image manager page
    POST --> Delete requested file(s)
    :param page: The requested page
    """
    if request.method == 'POST':
        img_ref_key = request.get_json()

        # Delete the img from ndb
        for img_ref in img_ref_key['objects']:
            img_inst = ndb.Key(ImageReference, int(img_ref))
            img = img_inst.get()
            blob_key = img.blob

            # Delete img and blob
            img_inst.delete()
            BlobInfo.get(blob_key).delete()
            logging.info("Delete image: {}".format(img_ref))

        return "true"

    offset = (page-1)*IMAGES_PER_PAGE
    images = ImageReference.query().order(-ImageReference.date)
    pagination = Pagination(page, IMAGES_PER_PAGE, images.count())
    query = images.fetch(IMAGES_PER_PAGE, offset=offset)

    return render_template('image-manager/admin-manager-images.html',
                           keys=query,
                           pagination=pagination)
Esempio n. 4
0
def images_redactor():
    """
    Image manager of redactor
    :return: JSON with image objects list
    """
    images = ImageReference.query().order(-ImageReference.date)
    urls = []

    for img in images:
        urls.append({'thumb': img.thumb,
                     'image': img.gallery,
                     'title': img.filename})

    response = make_response(dumps(urls))
    response.mimetype = 'application/json'
    return response
Esempio n. 5
0
def images_redactor():
    """
    Image manager of redactor
    :return: JSON with image objects list
    """
    images = ImageReference.query().order(-ImageReference.date)
    urls = []

    for img in images:
        urls.append({
            'thumb': img.thumb,
            'image': img.gallery,
            'title': img.filename
        })

    response = make_response(dumps(urls))
    response.mimetype = 'application/json'
    return response
Esempio n. 6
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})