Beispiel #1
0
    def put(self, path):
        """Update a file under a given path."""
        if 'file' not in request.files:
            abort(400, 'No file attached.')
        file = request.files['file']

        internal_path = validate_path(path)

        *parents, name = internal_path.parts
        parent = Path('/')
        entry = None
        for part in parents:
            entry = mongo.db.index.find_one({
                'name': part,
                'parent': str(parent)
            })
            if entry is None:
                mongo.db.index.insert_one({
                    'name': part,
                    'parent': str(parent),
                    'is_directory': True
                })
            parent /= part

        entry = mongo.db.index.find_one({'name': name, 'parent': str(parent)})
        if entry is not None:
            abort(400, 'A file with this name already exists.')

        ok_servers = []
        for server in mongo.db.servers.find():
            response = requests.post(
                f'http://{server["_id"]}/file/{internal_path}',
                files={'file': file.stream})
            if not response.ok:
                continue

            server['free_space'] = int(response.text)
            mongo.db.servers.replace_one({'_id': server['_id']}, server)
            ok_servers.append(server['_id'])

        mongo.db.index.replace_one({
            'name': name,
        }, {
            'name': name,
            'parent': str(parent),
            'is_directory': False,
            'size': get_file_size(file),
            'last_modified': int(time.time()),
            'servers': ok_servers,
        },
                                   upsert=True)

        return jsonify(get_min_free_space())
Beispiel #2
0
 def check_file_size(self, file_path):
     file_size = utils.get_file_size(file_path)
     if file_size > MAX_POST_BODY_SIZE:
         filesize = '{0:.2f}MB'.format(file_size / MB)
         maxsize = '{0:.2f}MB'.format(MAX_POST_BODY_SIZE_ASYNC / MB)
         raise excp.InvocationPayloadError(file_size=filesize,
                                           max_size=maxsize)
     elif self.is_asynchronous() and file_size > MAX_POST_BODY_SIZE_ASYNC:
         filesize = '{0:.2f}KB'.format(file_size / KB)
         maxsize = '{0:.2f}KB'.format(MAX_POST_BODY_SIZE_ASYNC / KB)
         raise excp.InvocationPayloadError(file_size=filesize,
                                           max_size=maxsize)
Beispiel #3
0
 def check_file_size(self, file_path):
     asynch = self.get_property("asynchronous")
     file_size = utils.get_file_size(file_path)
     error_msg = None
     if file_size > MAX_POST_BODY_SIZE:
         error_msg = "Invalid request: Payload size {0:.2f} MB greater than 6 MB".format((file_size/(1024*1024)))
     elif asynch and file_size > MAX_POST_BODY_SIZE_ASYNC:
         error_msg = "Invalid request: Payload size {0:.2f} KB greater than 128 KB".format((file_size/(1024)))
     if error_msg:
         error_msg += "\nCheck AWS Lambda invocation limits in : https://docs.aws.amazon.com/lambda/latest/dg/limits.html"
         logger.error(error_msg)
         utils.finish_failed_execution()         
Beispiel #4
0
def compare_files_with_destination() -> List[Dict[str, Any]]:
    collisions: List[Dict[str, Any]] = []
    for file in GlobalStateHolder.files_to_upload:
        source_file = f'{GlobalStateHolder.source_dir}/{file}'
        destination_file = f'{GlobalStateHolder.destination_dir}/{file}'

        try:
            size = GlobalStateHolder.disk_wrapper.get_size(destination_file)
        except PathNotFoundError:
            continue

        source_size = get_file_size(source_file)
        collisions.append(
            dict(
                source_file=source_file,
                destination_file=destination_file,
                source_size=source_size,
                destination_size=size,
            ))
        if source_size == size:
            # Do not upload files with the same size
            GlobalStateHolder.exists_same.add(source_file)

    if collisions:
        print('\nConflict files:')
        print(
            f'{"Source": ^70} | {"Size": ^16} || {"Destination": ^70} | {"Size": ^16}'
        )
        print(f'{"-" * 70} | {"-" * 16} || {"-" * 70} | {"-" * 16}')

        for file in collisions:
            print(f'{file["source_file"]: <70} | '
                  f'{file["source_size"]: <16} || '
                  f'{file["destination_file"]: <70} | '
                  f'{file["destination_size"]: <16}')

    return collisions