def get_and_update_cached_file(file_url):
    """Returns cached file for the given URL only if the file exists."""
    cached_file = CachedFile.get(file_url)
    if cached_file:
        # check if the file still exists and remove the object from the DB otherwise
        if os.path.isfile(app.config['CACHE_DIR'] + cached_file.filename):
            return cached_file
        CachedFile.delete(cached_file)  # else
    return None
示例#2
0
def get_and_update_cached_file(file_url):
    """Returns cached file for the given URL only if the file exists."""
    cached_file = CachedFile.get(file_url)
    if cached_file:
        # check if the file still exists and remove the object from the DB otherwise
        if os.path.isfile(app.config['CACHE_DIR'] + cached_file.filename):
            return cached_file
        CachedFile.delete(cached_file)  # else
    return None
def delete_file(cached_file):
    try:
        os.remove(app.config['CACHE_DIR'] + cached_file.filename)
        CachedFile.delete(cached_file)
    except OSError as e:  # E.g., if the file does not exist.
        if e.errno==errno.ENOENT:
            # We wanted to delete it anyway so go ahead
            CachedFile.delete(cached_file)
        else: raise  # E.g., permission denied
示例#4
0
def delete_file(cached_file):
    try:
        os.remove(app.config['CACHE_DIR'] + cached_file.filename)
        CachedFile.delete(cached_file)
    except OSError as e:  # E.g., if the file does not exist.
        if e.errno == errno.ENOENT:
            # We wanted to delete it anyway so go ahead
            CachedFile.delete(cached_file)
        else:
            raise  # E.g., permission denied
示例#5
0
def clear_cache():
    """
    Clears the cache of files.
    ---
    tags:
      - file
    responses:
      200:
        description: Cached files.
        schema:
            properties:
                files:
                    type: array
                    items:
                      $ref: '#/definitions/get_cached_file_get_File'
      500:
        description: The file could not be deleted from the cache.
        schema:
            $ref: '#/definitions/allocate_instance_post_Error'
    """
    deleted_files = []
    c_dir = app.config['CACHE_CONTAINER_DIR']
    for cached_file in CachedFile.get_all():
        try:
            delete_file(cached_file)  # TODO capture errors?
        except OSError as e:
            return internal_error(
                ('Error during the file removal from the cache. %s. ' +
                 'The exception raised with the following file: %s') %
                (e.strerror, cached_file.filename))
        deleted_files.append(cached_file.serialize(c_dir))
    return jsonify(files=deleted_files)
def clear_cache():
    """
    Clears the cache of files.
    ---
    tags:
      - file
    responses:
      200:
        description: Cached files.
        schema:
            properties:
                files:
                    type: array
                    items:
                      $ref: '#/definitions/get_cached_file_get_File'
      500:
        description: The file could not be deleted from the cache.
        schema:
            $ref: '#/definitions/allocate_instance_post_Error'
    """
    deleted_files = []
    c_dir = app.config['CACHE_CONTAINER_DIR']
    for cached_file in CachedFile.get_all():
        try:
            delete_file(cached_file)  # TODO capture errors?
        except OSError as e:
            return internal_error(('Error during the file removal from the cache. %s. ' +
                                'The exception raised with the following file: %s') %
                                (e.strerror, cached_file.filename))
        deleted_files.append(cached_file.serialize(c_dir))
    return jsonify(files=deleted_files)
示例#7
0
def cache_file():
    """
    Caches a Packet Tracer file.
    ---
    tags:
      - file
    parameters:
      - name: file_url
        in: body
        description: URL of the file to be cached.
        required: true
        type: string
    responses:
      201:
        description: Packet Tracer file cached.
        schema:
            $ref: '#/definitions/get_cached_file_get_File'
      400:
        description: The URL could not be accessed. It might not exist.
        schema:
            $ref: '#/definitions/allocate_instance_post_Error'
      500:
        description: The body of the request was incorrect. Please provide a valid file URL.
        schema:
            $ref: '#/definitions/allocate_instance_post_Error'
    """
    file_url = request.data
    if not file_url:
        return internal_error('Empty body.')

    cached_file = get_and_update_cached_file(file_url)
    if cached_file:
        return jsonify(cached_file.serialize(
            app.config['CACHE_CONTAINER_DIR']))
    # if not exist download and store
    filename = get_random_name()
    try:
        with open(app.config['CACHE_DIR'] + filename, 'w') as f:
            f.write(urllib2.urlopen(file_url).read())
    except IOError:
        return bad_request(
            error="The URL passed could not be reached. Is '%s' correct?" %
            file_url)
    except ValueError:
        return internal_error('Invalid URL passed in the body.')

    new_cached = CachedFile.create(file_url, filename)
    return jsonify(new_cached.serialize(app.config['CACHE_CONTAINER_DIR']))
def cache_file():
    """
    Caches a Packet Tracer file.
    ---
    tags:
      - file
    parameters:
      - name: file_url
        in: body
        description: URL of the file to be cached.
        required: true
        type: string
    responses:
      201:
        description: Packet Tracer file cached.
        schema:
            $ref: '#/definitions/get_cached_file_get_File'
      400:
        description: The URL could not be accessed. It might not exist.
        schema:
            $ref: '#/definitions/allocate_instance_post_Error'
      500:
        description: The body of the request was incorrect. Please provide a valid file URL.
        schema:
            $ref: '#/definitions/allocate_instance_post_Error'
    """
    file_url = request.data
    if not file_url:
        return internal_error('Empty body.')

    cached_file = get_and_update_cached_file(file_url)
    if cached_file:
        return  jsonify(cached_file.serialize(app.config['CACHE_CONTAINER_DIR']))
    # if not exist download and store
    filename = get_random_name()
    try:
        with open(app.config['CACHE_DIR'] + filename, 'w') as f:
            f.write(urllib2.urlopen(file_url).read())
    except IOError:
        return bad_request(error="The URL passed could not be reached. Is '%s' correct?" % file_url)
    except ValueError:
        return internal_error('Invalid URL passed in the body.')

    new_cached = CachedFile.create(file_url, filename)
    return jsonify(new_cached.serialize(app.config['CACHE_CONTAINER_DIR']))
def list_cached_files():
    """
    Returns the files cached and the original URLs that they cache.
    ---
    tags:
      - file
    responses:
      200:
        description: Cached files.
        schema:
            properties:
                files:
                    type: array
                    items:
                      $ref: '#/definitions/get_cached_file_get_File'
    """
    # list available files
    c_dir = app.config['CACHE_CONTAINER_DIR']
    return jsonify(files=[cached_file.serialize(c_dir) for cached_file in CachedFile.get_all()])
示例#10
0
def list_cached_files():
    """
    Returns the files cached and the original URLs that they cache.
    ---
    tags:
      - file
    responses:
      200:
        description: Cached files.
        schema:
            properties:
                files:
                    type: array
                    items:
                      $ref: '#/definitions/get_cached_file_get_File'
    """
    # list available files
    c_dir = app.config['CACHE_CONTAINER_DIR']
    return jsonify(files=[
        cached_file.serialize(c_dir) for cached_file in CachedFile.get_all()
    ])