Beispiel #1
0
def get_chunk(client, data_id):
    """
    Get a chunk.
    Return the chunk on success
    Return None on error
    """
    assert WEBDAV_DIR
    assert SCRATCH_DIR

    sanitized_data_id = urllib.quote(data_id.replace('/', '-2f'))

    path = os.path.join(WEBDAV_DIR, sanitized_data_id)
    local_path = make_scratch_file(SCRATCH_DIR)
    data = None

    try:
        easywebdav.download(path, local_path)
        with open(local_path, 'r') as f:
            data = f.read()

        return data
    except Exception as e:
        if DEBUG:
            log.exception(e)

        log.error("Failed to load {}".format(data_id))
        return None
Beispiel #2
0
def put_chunk(drive, name, chunk_buf):
    """
    Put a chunk into onedrive.
    Compress it first.
    Return True on success
    Return False on error
    """
    global ONEDRIVE_FOLDER_ID, DOWNLOAD_SCRATCH_SPACE
    assert DOWNLOAD_SCRATCH_SPACE

    if ONEDRIVE_COMPRESS:
        compressed_chunk = compress_chunk(chunk_buf)
    else:
        compressed_chunk = chunk_buf

    name = base64.b64encode(name).replace('/',
                                          '_').replace('+',
                                                       '-').replace('=', '~')

    fid = None
    if ONEDRIVE_FOLDER_ID is not None:
        fid = ONEDRIVE_FOLDER_ID
    else:
        assert ONEDRIVE_FOLDER_NAME
        ONEDRIVE_FOLDER_ID = get_blockstack_folder_id(drive,
                                                      ONEDRIVE_FOLDER_NAME)
        if ONEDRIVE_FOLDER_ID is None:
            return False

        fid = ONEDRIVE_FOLDER_ID

    scratch_path = make_scratch_file(DOWNLOAD_SCRATCH_SPACE)
    with open(scratch_path, 'w') as f:
        f.write(chunk_buf)

    rc = None

    try:
        drive.item(drive='me', id=fid).children[name].upload(scratch_path)
        rc = True
    except Exception, e:
        if DEBUG:
            log.exception(e)

        log.error("Failed to save {} to OneDrive".format(name))
        rc = False
def put_chunk( drive, name, chunk_buf ):
    """
    Put a chunk into onedrive.
    Compress it first.
    Return True on success
    Return False on error
    """
    global ONEDRIVE_FOLDER_ID, DOWNLOAD_SCRATCH_SPACE
    assert DOWNLOAD_SCRATCH_SPACE

    if ONEDRIVE_COMPRESS:
        compressed_chunk = compress_chunk(chunk_buf)
    else:
        compressed_chunk = chunk_buf

    name = base64.b64encode(name).replace('/', '_').replace('+', '-').replace('=', '~')
    
    fid = None
    if ONEDRIVE_FOLDER_ID is not None:
        fid = ONEDRIVE_FOLDER_ID
    else:
        assert ONEDRIVE_FOLDER_NAME
        ONEDRIVE_FOLDER_ID = get_blockstack_folder_id(drive, ONEDRIVE_FOLDER_NAME)
        if ONEDRIVE_FOLDER_ID is None:
            return False

        fid = ONEDRIVE_FOLDER_ID
   
    scratch_path = make_scratch_file(DOWNLOAD_SCRATCH_SPACE)
    with open(scratch_path, 'w') as f:
        f.write(chunk_buf)

    rc = None

    try:
        drive.item(drive='me', id=fid).children[name].upload(scratch_path)
        rc = True
    except Exception, e:
        if DEBUG:
            log.exception(e)

        log.error("Failed to save {} to OneDrive".format(name))
        rc = False
Beispiel #4
0
def get_chunk_via_onedrive(drive, data_id):
    """
    Get data via Onedrive's API
    Return the data on success
    Return None on failure
    """
    global ONEDRIVE_FOLDER_ID, ONEDRIVE_FOLDER_NAME

    assert DOWNLOAD_SCRATCH_SPACE

    if ONEDRIVE_FOLDER_ID is None:
        assert ONEDRIVE_FOLDER_NAME
        fid = get_blockstack_folder_id(drive, ONEDRIVE_FOLDER_NAME)
        if fid is None:
            return None

        ONEDRIVE_FOLDER_ID = fid

    scratch_path = make_scratch_file(DOWNLOAD_SCRATCH_SPACE)

    try:
        drive.item(
            drive='me',
            id=ONEDRIVE_FOLDER_ID).children[data_id].download(scratch_path)
    except Exception as e:
        if DEBUG:
            log.exception(e)

        log.error("Failed to download {}".format(data_id))
        return None

    data = None
    with open(scratch_path, 'r') as f:
        data = f.read()

    try:
        os.unlink(scratch_path)
    except:
        pass

    return data
def get_chunk_via_onedrive(drive, data_id):
    """
    Get data via Onedrive's API
    Return the data on success
    Return None on failure
    """
    global ONEDRIVE_FOLDER_ID, ONEDRIVE_FOLDER_NAME

    assert DOWNLOAD_SCRATCH_SPACE

    if ONEDRIVE_FOLDER_ID is None:
        assert ONEDRIVE_FOLDER_NAME
        fid = get_blockstack_folder_id(drive, ONEDRIVE_FOLDER_NAME)
        if fid is None:
            return None 

        ONEDRIVE_FOLDER_ID = fid
    
    scratch_path = make_scratch_file(DOWNLOAD_SCRATCH_SPACE)

    try:
        drive.item(drive='me', id=ONEDRIVE_FOLDER_ID).children[data_id].download(scratch_path)
    except Exception as e:
        if DEBUG:
            log.exception(e)
        
        log.error("Failed to download {}".format(data_id))
        return None

    data = None
    with open(scratch_path, 'r') as f:
        data = f.read()

    try:
        os.unlink(scratch_path)
    except:
        pass

    return data