Exemple #1
0
def burst_pdf(**kwargs):
    """
    A task that creates a sub-folder next to a PDF file and extracts all
    pages from the PDF as PNG files into the sub-folder.
    """
    from flask_app import app
    from filesystem_manager import get_abs_path, get_burst_path, get_file_data
    from filesystem_manager import delete_dir, make_dirs, path_exists
    from filesystem_sync import delete_folder
    from imagemagick import imagemagick_burst_pdf
    from models import Folder
    from util import get_file_extension

    (src,) = _extract_parameters(["src"], **kwargs)
    burst_folder_rel = get_burst_path(src)

    # Ensure src is a PDF
    if get_file_extension(src) not in app.config["PDF_FILE_TYPES"]:
        app.log.warn("Cannot burst non-PDF file: " + src)
        return

    # See if the burst folder already exists (in the database and on disk)
    db_folder = app.data_engine.get_folder(folder_path=burst_folder_rel)
    if db_folder is not None and db_folder.status == Folder.STATUS_ACTIVE:
        # Wipe the folder, old images, data, and uncache the old images
        delete_folder(db_folder, None, app.data_engine, None, app.log)
        deleted_ids = app.data_engine.list_image_ids(db_folder)
        for image_id in deleted_ids:
            app.image_engine._uncache_image_id(image_id)

    # See if the burst folder already exists (just on disk)
    if path_exists(burst_folder_rel, require_directory=True):
        # Wipe the folder and old images
        delete_dir(burst_folder_rel, recursive=True)

    # Create the burst folder and burst
    pdf_data = get_file_data(src)
    if pdf_data is not None:
        make_dirs(burst_folder_rel)
        burst_folder_abs = get_abs_path(burst_folder_rel)
        if not imagemagick_burst_pdf(pdf_data, burst_folder_abs, app.config["PDF_BURST_DPI"]):
            app.log.warn("Failed to burst PDF: " + src)
    else:
        app.log.warn("Cannot burst PDF, file not found: " + src)
Exemple #2
0
def delete_folder(**kwargs):
    """
    Deletes a disk folder and its contents (including sub-folders),
    marks all affected images and folders as deleted in the database,
    and triggers the uncache_folder_images task. Returns the deleted
    folder object.

    See filesystem_sync.delete_folder() for possible exceptions.
    """
    from flask_app import app
    from filesystem_sync import delete_folder
    from errors import DoesNotExistError
    from models import Task

    (folder_id,) = _extract_parameters(["folder_id"], **kwargs)
    this_task = _get_task(**kwargs)

    # Get folder data
    db_folder = app.data_engine.get_folder(folder_id)
    if not db_folder:
        raise DoesNotExistError(str(folder_id))

    # Delete
    try:
        db_folder = delete_folder(db_folder, this_task.user, app.data_engine, app.permissions_engine, app.log)
    except ValueError as e:
        if type(e) is ValueError:
            raise ParameterError(str(e))
        else:
            raise  # Sub-classes of ValueError

    # Remove cached images for old path (as another background task)
    app.task_engine.add_task(
        this_task.user,
        "Uncache deleted images",
        "uncache_folder_images",
        {"folder_id": folder_id, "recursive": True},
        Task.PRIORITY_NORMAL,
        "debug",
        "warn",
        10,
    )

    # #2476 db_folder now has the 'children' attribute fully populated, a sub-tree
    #       that might be huge. Return instead the basic object to store in the db.
    return app.data_engine.get_folder(folder_id)