diff --git a/pillar/application/modules/file_storage.py b/pillar/application/modules/file_storage.py index cc8021e6..64bcea93 100644 --- a/pillar/application/modules/file_storage.py +++ b/pillar/application/modules/file_storage.py @@ -628,6 +628,24 @@ def create_file_doc_for_upload(project_id, uploaded_file): return file_fields['_id'], internal_filename, status +def compute_aggregate_length(file_doc, original=None): + """Computes the total length (in bytes) of the file and all variations. + + Stores the result in file_doc['length_aggregate_in_bytes'] + """ + + # Compute total size of all variations. + variations = file_doc.get('variations', ()) + var_length = sum(var.get('length', 0) for var in variations) + + file_doc['length_aggregate_in_bytes'] = file_doc.get('length', 0) + var_length + + +def compute_aggregate_length_items(file_docs): + for file_doc in file_docs: + compute_aggregate_length(file_doc) + + def setup_app(app, url_prefix): app.on_pre_GET_files += on_pre_get_files @@ -636,4 +654,8 @@ def setup_app(app, url_prefix): app.on_delete_item_files += before_deleting_file + app.on_update_files += compute_aggregate_length + app.on_replace_files += compute_aggregate_length + app.on_insert_files += compute_aggregate_length_items + app.register_blueprint(file_storage, url_prefix=url_prefix) diff --git a/pillar/settings.py b/pillar/settings.py index 1a8d6837..8281b009 100644 --- a/pillar/settings.py +++ b/pillar/settings.py @@ -402,6 +402,10 @@ files_schema = { 'type': 'integer', 'required': True, }, + 'length_aggregate_in_bytes': { # Size of file + all variations + 'type': 'integer', + 'required': False, # it's computed on the fly anyway, so clients don't need to provide it. + }, 'md5': { 'type': 'string', 'required': True,