コード例 #1
0
def retrieve_individual_file(file_name):
    session_id = _checkfor_session_cookie()

    # Need to debug this in a single session, without uwsgi? 
    # Comment out the next line.
    #_is_authorized_or_abort(session_id,file_container_id)

    # If Accept header setting has JSON format as high priority, 
    # listings and status of commands will be returned. 

    # Otherwise Accept header needs to specify file type to stream it, 
    # or this request will fail.
    # This means DELETE requires a JSON Accept header setting to execute.

    file_in_disk_cache = str(config.local_file_root + '/' + file_name)

    # Inner function
    def get_file_from_source(file_in_disk_cache):
        # If not in disk cache, get it, then serve it.
        logger.debug("Retrieving it from source first, then disk cache...")

        response = requests.get(config.source_url, stream=True)

        dirname = os.path.dirname(file_in_disk_cache)
        if not os.path.exists(dirname):
            os.makedirs(dirname)

        with open(file_in_disk_cache, 'wb') as out_file:
            shutil.copyfileobj(response.raw, out_file)
        del response

    # Assuming an async operation or other process doesn't 
    # delete the file on us after this check...
    if not os.path.exists(file_in_disk_cache):
        get_file_from_source(file_in_disk_cache)

    # If file type matches their Accept header specification, assume they want to stream it.
    mimetype = request_matches_mime_type(file_in_disk_cache)
    if mimetype != False:
        if request.method == 'GET':
            logger.debug("Retrieving it from disk cache...")
            return send_file(file_in_disk_cache,mimetype)

    else: #default to json mime type.
        #if request_wants_accept_type('application/json'):

        # Assume that if user set Accept header to JSON and calls this, they want
        # the file retreived again into disk cache, even if it's already there.
        # Assume user knows what they're doing. 
        if request.method in ('GET','POST','PUT'):
            try:
                resp = jsonify({ 'results':file_in_disk_cache })
    
            except Exception, e:
                if 'No such' in e[0]:
                    # 404 is sent if URL is not found, and if container is not found. 
                    # This makes debugging a bit annoying.
                    abort(404)
                raise
    
            return resp


        elif request.method == 'DELETE':
    
            try:
                os.remove(file_in_disk_cache)
                return  jsonify({ 'results':'purged %s' % file_in_disk_cache })

            except OSError as e:
                if e.errno != errno.ENOENT:
                    raise

                '''
                Trying to delete a file that is not present is not 
                considered an error here.
                '''
                return  jsonify({
                   'results':'file not present: %s' % file_in_disk_cache
                    })
コード例 #2
0
def test_memory_cache(somenum):
    logger.info("Not from cache...")
    session_id = _checkfor_session_cookie()
    return jsonify({ 'got it!':somenum})