in source/backend/src/transfer_sftp_backend.py [0:0]
def download():
try:
logger.info(f'TaskID: {fargate_task_id}(PID:{pid}) - download(): Request received')
sftp_hostname = app.config.get('sftp_hostname')
usr, passwd = authenticate_request(request)
request_data = request.get_json()
filepath = request_data['path']
logger.info(f'TaskID: {fargate_task_id}(PID:{pid}) - download(): File Path: {filepath}')
filename = str(filepath).split('/')[-1] # Returns the filename
sftp_connection = get_sftp_connection(usr, passwd, sftp_hostname)
download_scratch_space_path = '/var/sftp-download-scratch-space'
if (os.path.exists(download_scratch_space_path) == False):
logger.debug(f'TaskID: {fargate_task_id}(PID:{pid}) - download(): download scratch path {download_scratch_space_path} does not exist. Creating it ')
os.mkdir(download_scratch_space_path)
logger.debug(f'TaskID: {fargate_task_id}(PID:{pid}) - download(): created the {download_scratch_space_path} path')
else:
logger.debug(f'TaskID: {fargate_task_id}(PID:{pid}) - download(): download scratch path {download_scratch_space_path} exists ')
listfolders = os.listdir(download_scratch_space_path)
logger.debug(f'TaskID: {fargate_task_id}(PID:{pid}) - download(): List of existing folders (should be empty) : {listfolders} ')
# create unique temp path for the file
temppath = download_scratch_space_path + '/' + str(uuid.uuid4().hex) + '_' + str(
datetime.datetime.utcnow().timestamp()).replace('.', '_')
logger.debug(f'TaskID: {fargate_task_id}(PID:{pid}) - download(): temp path to be created : {temppath}')
if (os.path.exists(temppath)):
logger.debug(f'TaskID: {fargate_task_id}(PID:{pid}) - download(): download temp path already exists')
os.rmdir(temppath)
logger.debug(f'TaskID: {fargate_task_id}(PID:{pid}) - download(): deleted the existing download temp path')
os.mkdir(temppath)
logger.debug(f'TaskID: {fargate_task_id}(PID:{pid}) - download(): created the download temp path again')
else:
logger.debug(f'TaskID: {fargate_task_id}(PID:{pid}) - download(): download temp path does not exists. Creating new one')
os.mkdir(temppath)
remote_path = filepath
local_path = temppath + "/" + filename
# Getting file from SFTP endpoint
sftp_connection.get(remote_path, local_path, callback=None)
logger.debug(f'TaskID: {fargate_task_id}(PID:{pid}) - download(): sending file : remote path: {remote_path} local_path: {local_path}')
# Remove the temp path and the file from the local storage after it is sent to the client
@after_this_request
def remove_temp_path(response):
try:
os.remove(local_path)
os.rmdir(temppath)
logger.debug(f'TaskID: {fargate_task_id}(PID:{pid}) - download(): removed temp path after_this_request')
except Exception as e:
logger.error(f'TaskID: {fargate_task_id}(PID:{pid}) - download(): Download operation after_this_request error occurred : {e}')
return bad_request(e, 500)
return response
logger.info(f'TaskID: {fargate_task_id}(PID:{pid}) - download(): Responding back with 200 status code')
return send_file(local_path, as_attachment=True, attachment_filename=filename)
except Exception as e:
logger.error(f'TaskID: {fargate_task_id}(PID:{pid}) - download(): call to /api/download returned exception {e}')
return bad_request("Bad or Invalid Request", 500)