Example #1
0
def _prep_image_object(image, can_download=None, **url_params):
    """
    Modifies an Image object to add calculated fields.
    This provides the common data dictionary for the file admin, image admin,
    image details, upload, and directory listing (with detail) APIs.

    If the download permission is None, it is calculated for the image's folder
    and the current user. The permissions engine returns this from cache when
    possible, but it is more efficient to pass in the permission if it is already
    known, or when handling many images in the same folder.

    If any url_params are provided (as kwargs), these are included in the
    generated image 'url' attribute.
    """
    if can_download is None:
        can_download = permissions_engine.is_folder_permitted(
            image.folder, FolderPermission.ACCESS_DOWNLOAD, get_session_user())
    image.url = external_url_for('image', src=image.src, **url_params)
    image.download = can_download
    image.filename = filepath_filename(image.src)
    # Unsupported files shouldn't be in the database but it can happen if
    # support is removed for a file type that was once enabled
    image.supported = (get_file_extension(image.filename)
                       in image_engine.get_image_formats(supported_only=True))
    return image
Example #2
0
def _prep_folioexport_object(folio, folioexport):
    """
    Modifies a FolioExport object to add calculated fields.
    """
    # Add attribute for the download URL
    if folioexport.filename:
        folioexport.url = external_url_for('folios.portfolio_download',
                                           human_id=folio.human_id,
                                           filename=folioexport.filename)
    else:
        folioexport.url = ''
    return folioexport
Example #3
0
def _prep_folio_object(folio):
    """
    Modifies a Folio object to add calculated fields.
    """
    # Add attribute for public viewing URL
    folio.url = external_url_for('folios.portfolio_view',
                                 human_id=folio.human_id)
    # Add extra fields to the images
    if data_engine.attr_is_loaded(folio, 'images'):
        folio.images = [_prep_folioimage_object(fi) for fi in folio.images]
    # Add extra fields to the downloads
    if data_engine.attr_is_loaded(folio, 'downloads'):
        folio.downloads = [
            _prep_folioexport_object(folio, fe) for fe in folio.downloads
        ]
    return folio
Example #4
0
def _image_dict(db_image, can_download=None):
    """
    Returns the common data dictionary for the imagedetails and upload APIs.
    Provide an Image data model object and optionally the pre-calculated
    boolean download permission. If the download permission is None,
    it is calculated for the image's folder and the current user.
    """
    if can_download is None:
        can_download = permissions_engine.is_folder_permitted(
            db_image.folder,
            FolderPermission.ACCESS_DOWNLOAD,
            get_session_user()
        )
    return {
        'src': db_image.src,
        'url': external_url_for('image', src=db_image.src),
        'id': db_image.id,
        'folder_id': db_image.folder_id,
        'title': db_image.title,
        'description': db_image.description,
        'width': db_image.width,
        'height': db_image.height,
        'download': can_download
    }
Example #5
0
def imagelist():
    # Check parameters
    try:
        from_path = request.args.get('path', '')
        want_info = parse_boolean(request.args.get('attributes', ''))
        limit = parse_int(request.args.get('limit', '1000'))
        validate_string(from_path, 1, 1024)
    except ValueError as e:
        raise ParameterError(e)

    # Get extra parameters for image URL construction
    image_params = request.args.to_dict()
    if 'path' in image_params:
        del image_params['path']
    if 'attributes' in image_params:
        del image_params['attributes']
    if 'limit' in image_params:
        del image_params['limit']

    # Get directory listing
    directory_info = get_directory_listing(from_path, False, limit)
    if not directory_info.exists():
        raise DoesNotExistError('Invalid path')

    ret_list = []
    db_session = data_engine.db_get_session()
    db_commit = False
    try:
        # Auto-populate the folders database
        db_folder = auto_sync_folder(
            from_path, data_engine, task_engine, _db_session=db_session
        )
        db_session.commit()

        # Require view permission or file admin
        permissions_engine.ensure_folder_permitted(
            db_folder,
            FolderPermission.ACCESS_VIEW,
            get_session_user()
        )

        # Create the response
        file_list = directory_info.contents()
        img_types = image_engine.get_image_formats()
        base_folder = add_sep(directory_info.name())
        for f in file_list:
            # Filter out non-images
            if get_file_extension(f['filename']) in img_types:
                entry_path = base_folder + f['filename']
                entry = {
                    'filename': f['filename'],
                    'url': external_url_for('image', src=entry_path, **image_params)
                }
                if want_info:
                    db_entry = auto_sync_existing_file(
                        entry_path,
                        data_engine,
                        task_engine,
                        burst_pdf=False,  # Don't burst a PDF just by finding it here
                        _db_session=db_session
                    )
                    entry['id'] = db_entry.id if db_entry else 0
                    entry['folder_id'] = db_entry.folder_id if db_entry else 0
                    entry['title'] = db_entry.title if db_entry else ''
                    entry['description'] = db_entry.description if db_entry else ''
                    entry['width'] = db_entry.width if db_entry else 0
                    entry['height'] = db_entry.height if db_entry else 0
                ret_list.append(entry)

        db_commit = True
    finally:
        try:
            if db_commit:
                db_session.commit()
            else:
                db_session.rollback()
        finally:
            db_session.close()

    return make_api_success_response(ret_list)
Example #6
0
def imagelist():
    # Check parameters
    try:
        from_path = request.args.get('path', '')
        want_info = parse_boolean(request.args.get('attributes', ''))
        start = parse_int(request.args.get('start', '0'))
        limit = parse_int(request.args.get('limit', '1000'))
        validate_string(from_path, 1, 1024)
        validate_number(start, 0, 999999999)
        validate_number(limit, 1, 1000)
    except ValueError as e:
        raise ParameterError(e)

    # Get extra parameters for image URL construction, remove API parameters
    image_params = request.args.to_dict()
    image_params.pop('path', None)
    image_params.pop('attributes', None)
    image_params.pop('start', None)
    image_params.pop('limit', None)

    # Get directory listing
    directory_info = get_directory_listing(from_path, False, 2, start, limit)
    if not directory_info.exists():
        raise DoesNotExistError('Invalid path')

    ret_list = []
    db_session = data_engine.db_get_session()
    db_commit = False
    try:
        # Auto-populate the folders database
        db_folder = auto_sync_folder(
            from_path, data_engine, task_engine, _db_session=db_session
        )
        db_session.commit()

        # Require view permission or file admin
        permissions_engine.ensure_folder_permitted(
            db_folder,
            FolderPermission.ACCESS_VIEW,
            get_session_user()
        )
        # Get download permission in case we need to return it later
        can_download = permissions_engine.is_folder_permitted(
            db_folder,
            FolderPermission.ACCESS_DOWNLOAD,
            get_session_user()
        )

        # Create the response
        file_list = directory_info.contents()
        supported_img_types = image_engine.get_image_formats(supported_only=True)
        base_folder = add_sep(directory_info.name())
        for f in file_list:
            # v2.6.4 Return unsupported files too. If you want to reverse this change,
            # the filtering needs to be elsewhere for 'start' and 'limit' to work properly
            supported_file = get_file_extension(f['filename']) in supported_img_types
            file_path = base_folder + f['filename']

            if want_info:
                # Need to return the database fields too
                if supported_file:
                    db_entry = auto_sync_existing_file(
                        file_path,
                        data_engine,
                        task_engine,
                        burst_pdf=False,  # Don't burst a PDF just by finding it here
                        _db_session=db_session
                    )
                    db_entry = _prep_image_object(db_entry, can_download, **image_params)
                else:
                    db_entry = _prep_blank_image_object()
                    db_entry.filename = f['filename']
                    db_entry.supported = False
                # Return images in full (standard) image dict format
                entry = object_to_dict(db_entry, _omit_fields)
            else:
                # Return images in short dict format
                entry = {
                    'filename': f['filename'],
                    'supported': supported_file,
                    'url': (external_url_for('image', src=file_path, **image_params)
                            if supported_file else '')
                }

            ret_list.append(entry)

        db_commit = True
    finally:
        try:
            if db_commit:
                db_session.commit()
            else:
                db_session.rollback()
        finally:
            db_session.close()

    return make_api_success_response(ret_list)