fulltext/controllers.py
def start_extraction(id_type: str, identifier: str, token: str,
                     force: bool = False,
                     authorizer: Optional[Authorizer] = None) -> Response:
    """Handle a request to force text extraction."""
    if id_type not in SupportedBuckets:
        raise NotFound('Unsupported identifier')

    canonical = legacy.CanonicalPDF.current_session()
    storage = store.Storage.current_session()
    previews = preview.PreviewService.current_session()

    # Before creating an extraction task, check that the intended document
    # even exists. This gives the client a clear failure now, rather than
    # waiting until the async task fails. At the same time, we'll also grab
    # the owner (if there is one) so that we can authorize the request.
    owner: Optional[str] = None
    if id_type == SupportedBuckets.ARXIV:
        if not canonical.exists(identifier):
            logger.debug('No PDF for this resource exists')
            raise NotFound('No such document')

        # Make sure that the client is authorized to work with this resource.
        # The route may have passed in an authorizer function that works with
        # the auth API to authorize the request.
        if authorizer is not None and not authorizer(identifier, owner):
            logger.debug('Client is not authorized to work with this resource')
            # Pretend that the resource does not even exist.
            raise NotFound('No such document')
    elif id_type == SupportedBuckets.SUBMISSION:
        try:
            owner = previews.get_owner(identifier, token)
            logger.debug('Got owner %s', owner)
        except preview.exceptions.NotFound as e:
            logger.debug('Preview returned 404 Not Found for %s', identifier)
            raise NotFound('No such document') from e

        # Make sure that the client is authorized to work with this resource.
        # For submissions, an authorizer function must be provided, so we
        # should deny access if one is mistakenly not provided.
        if authorizer is None or not authorizer(identifier, owner):
            logger.debug('Client is not authorized to work with this resource')
            # Pretend that the resource does not even exist.
            raise NotFound('No such document')
    else:
        raise NotFound('No such document')

    if not force:
        # If an extraction product or task already exists for this paper,
        # redirect. Don't do the same work twice for a given version of the
        # extractor.
        logger.debug('Check for an existing product or task')
        product: Optional[Extraction] = None
        try:
            product = storage.retrieve(identifier, bucket=id_type,
                                       meta_only=True)
        except IOError as e:
            raise InternalServerError('Could not connect to backend') from e
        except store.DoesNotExist:
            pass

        if product is not None:
            # Redirect to either the task status endpoint or the finished
            # extraction product.
            logger.debug('Got an extraction product: %s', product)
            return _redirect(product, authorizer)

    logger.debug('No existing task nor extraction for %s', identifier)

    try:
        logger.debug('Create a new extraction task with %s, %s',
                     identifier, id_type)
        extract.create_task(identifier, id_type, owner, token)
    except extract.TaskCreationFailed as e:
        raise InternalServerError('Could not start extraction') from e
    target = url_for('fulltext.task_status', identifier=identifier,
                     id_type=id_type)
    return ACCEPTED, status.ACCEPTED, {'Location': target}
Handle a request to force text extraction.
Handle a request to force text extraction.
Handle a request to force text extraction .
def start_extraction(id_type: str, identifier: str, token: str,
                     force: bool = False,
                     authorizer: Optional[Authorizer] = None) -> Response:
    if id_type not in SupportedBuckets:
        raise NotFound('Unsupported identifier')
    canonical = legacy.CanonicalPDF.current_session()
    storage = store.Storage.current_session()
    previews = preview.PreviewService.current_session()
    owner: Optional[str] = None
    if id_type == SupportedBuckets.ARXIV:
        if not canonical.exists(identifier):
            logger.debug('No PDF for this resource exists')
            raise NotFound('No such document')
        if authorizer is not None and not authorizer(identifier, owner):
            logger.debug('Client is not authorized to work with this resource')
            raise NotFound('No such document')
    elif id_type == SupportedBuckets.SUBMISSION:
        try:
            owner = previews.get_owner(identifier, token)
            logger.debug('Got owner %s', owner)
        except preview.exceptions.NotFound as e:
            logger.debug('Preview returned 404 Not Found for %s', identifier)
            raise NotFound('No such document') from e
        if authorizer is None or not authorizer(identifier, owner):
            logger.debug('Client is not authorized to work with this resource')
            raise NotFound('No such document')
    else:
        raise NotFound('No such document')
    if not force:
        logger.debug('Check for an existing product or task')
        product: Optional[Extraction] = None
        try:
            product = storage.retrieve(identifier, bucket=id_type,
                                       meta_only=True)
        except IOError as e:
            raise InternalServerError('Could not connect to backend') from e
        except store.DoesNotExist:
            pass
        if product is not None:
            logger.debug('Got an extraction product: %s', product)
            return _redirect(product, authorizer)
    logger.debug('No existing task nor extraction for %s', identifier)
    try:
        logger.debug('Create a new extraction task with %s, %s',
                     identifier, id_type)
        extract.create_task(identifier, id_type, owner, token)
    except extract.TaskCreationFailed as e:
        raise InternalServerError('Could not start extraction') from e
    target = url_for('fulltext.task_status', identifier=identifier,
                     id_type=id_type)
    return ACCEPTED, status.ACCEPTED, {'Location': target}
def start_extraction ( id_type : str , identifier : str , token : str , force : bool = False , authorizer : Optional [ Authorizer ] = None ) -> Response : if id_type not in SupportedBuckets : raise NotFound ( 'Unsupported identifier' ) canonical = legacy . CanonicalPDF . current_session ( ) storage = store . Storage . current_session ( ) previews = preview . PreviewService . current_session ( ) owner : Optional [ str ] = None if id_type == SupportedBuckets . ARXIV : if not canonical . exists ( identifier ) : logger . debug ( 'No PDF for this resource exists' ) raise NotFound ( 'No such document' ) if authorizer is not None and not authorizer ( identifier , owner ) : logger . debug ( 'Client is not authorized to work with this resource' ) raise NotFound ( 'No such document' ) elif id_type == SupportedBuckets . SUBMISSION : try : owner = previews . get_owner ( identifier , token ) logger . debug ( 'Got owner %s' , owner ) except preview . exceptions . NotFound as e : logger . debug ( 'Preview returned 404 Not Found for %s' , identifier ) raise NotFound ( 'No such document' ) from e if authorizer is None or not authorizer ( identifier , owner ) : logger . debug ( 'Client is not authorized to work with this resource' ) raise NotFound ( 'No such document' ) else : raise NotFound ( 'No such document' ) if not force : logger . debug ( 'Check for an existing product or task' ) product : Optional [ Extraction ] = None try : product = storage . retrieve ( identifier , bucket = id_type , meta_only = True ) except IOError as e : raise InternalServerError ( 'Could not connect to backend' ) from e except store . DoesNotExist : pass if product is not None : logger . debug ( 'Got an extraction product: %s' , product ) return _redirect ( product , authorizer ) logger . debug ( 'No existing task nor extraction for %s' , identifier ) try : logger . debug ( 'Create a new extraction task with %s, %s' , identifier , id_type ) extract . create_task ( identifier , id_type , owner , token ) except extract . TaskCreationFailed as e : raise InternalServerError ( 'Could not start extraction' ) from e target = url_for ( 'fulltext.task_status' , identifier = identifier , id_type = id_type ) return ACCEPTED , status . ACCEPTED , { 'Location' : target }
Handle a request to force text extraction.
Handle a request to force text extraction .
"""Handle a request to force text extraction.""" # Before creating an extraction task, check that the intended document # even exists. This gives the client a clear failure now, rather than # waiting until the async task fails. At the same time, we'll also grab # the owner (if there is one) so that we can authorize the request. # Make sure that the client is authorized to work with this resource. # The route may have passed in an authorizer function that works with # the auth API to authorize the request. # Pretend that the resource does not even exist. # Make sure that the client is authorized to work with this resource. # For submissions, an authorizer function must be provided, so we # should deny access if one is mistakenly not provided. # Pretend that the resource does not even exist. # If an extraction product or task already exists for this paper, # redirect. Don't do the same work twice for a given version of the # extractor. # Redirect to either the task status endpoint or the finished # extraction product.
id_type
str
identifier
str
token
str
force
bool
authorizer
Optional[Authorizer]