def upload()

in source/backend/src/transfer_sftp_backend.py [0:0]


def upload():
    try:
        logger.info(f'TaskID: {fargate_task_id}(PID:{pid}) - upload(): Upload file request received')

        sftp_hostname = app.config.get('sftp_hostname')
        usr, passwd = authenticate_request(request)

        req_form = request.form
        filename = req_form.getlist('file_name')[0]

        filename = replace_forward_slash(filename)

        logger.info(f'TaskID: {fargate_task_id}(PID:{pid}) - upload(): File to be uploaded : {filename}')

        file = request.files.get("filetoupload")
        response_body = {}

        upload_scratch_space_path = '/var/sftp-upload-scratch-space'

        if (os.path.exists(upload_scratch_space_path) == False):
            logger.debug(f'TaskID: {fargate_task_id}(PID:{pid}) - upload(): upload scratch path {upload_scratch_space_path} does not exist. Creating it ')
            os.mkdir(upload_scratch_space_path)
            logger.debug(f'TaskID: {fargate_task_id}(PID:{pid}) - upload(): created the {upload_scratch_space_path} path')
        else:
            logger.debug(f'TaskID: {fargate_task_id}(PID:{pid}) - upload():upload scratch path {upload_scratch_space_path} exists ')
            listfolders = os.listdir(upload_scratch_space_path)
            logger.debug(f'TaskID: {fargate_task_id}(PID:{pid}) - upload():List of existing folders (should be empty) : {listfolders} ')

        # create unique temp path for the file
        temppath = upload_scratch_space_path + '/' + str(uuid.uuid4().hex) + '_' + str(
            datetime.datetime.utcnow().timestamp()).replace('.', '_')
        logger.debug(f'TaskID: {fargate_task_id}(PID:{pid}) - upload(): upload temp path to be created : {temppath}')
        if (os.path.exists(temppath)):
            logger.debug(f'TaskID: {fargate_task_id}(PID:{pid}) - upload(): upload temp path already exists')
            os.rmdir(temppath)
            logger.debug(f'TaskID: {fargate_task_id}(PID:{pid}) - upload(): deleted the existing upload temp path')
            os.mkdir(temppath)
            logger.debug(f'TaskID: {fargate_task_id}(PID:{pid}) - upload(): created the upload temp path again')
        else:
            logger.debug(f'TaskID: {fargate_task_id}(PID:{pid}) - upload(): upload temp path does not exists. Creating new one')
            os.mkdir(temppath)

        #Saving the file to fargate task scratch space
        file.save(os.path.join(temppath, filename))

        form_data = request.form.to_dict(flat=False)
        file_path = form_data['file_path'][0]
        filetouploadname = file_path + "/" + filename

        #Making SFTP Connection
        sftp_connection = get_sftp_connection(usr, passwd, sftp_hostname)
        #Uploading the file to SFTP endpoint, from fargate task scratch space
        logger.debug(f'TaskID: {fargate_task_id}(PID:{pid}) - upload(): Uploading the file to SFTP endpoint, from fargate task scratch space')
        upload_file_response = sftp_connection.put(os.path.join(temppath, filename), filetouploadname)

        # Remove the temp path and the file from the local storage after it is uploaded to the SFTP endpoint
        logger.debug(f'TaskID: {fargate_task_id}(PID:{pid}) - upload(): file uploaded to the SFTP server')
        os.remove(os.path.join(temppath, filename))
        logger.debug(f'TaskID: {fargate_task_id}(PID:{pid}) - upload(): upload temp path file deleted')
        os.rmdir(temppath)
        logger.debug(f'TaskID: {fargate_task_id}(PID:{pid}) - upload(): upload temp path deleted')

        response_body['file_permission'] = str(upload_file_response)
        response_body['message'] = "File uploaded successfully."

        response = jsonify(response_body)
        response.status_code = 200
        logger.info(f'TaskID: {fargate_task_id}(PID:{pid}) - upload(): Responding back with 200 status code')
        return response

    except Exception as e:
        logger.error(f'TaskID: {fargate_task_id}(PID:{pid}) - upload(): call to /api/upload returned exception {e}')
        return bad_request(e.description, 500)