Skip to content

Commit 78e3f15

Browse files
committed
feat: respect config.storage.store_files initial intent
1 parent 72d4fff commit 78e3f15

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed

src/aleph/handlers/content/store.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,6 @@ async def fetch_related_content(
107107
# This check is essential to ensure that files are not added to the system
108108
# or the current node when the configuration disables storing of files.
109109
config = get_config()
110-
if not config.storage.store_files.value:
111-
return # Ignore if files are not to be stored.
112-
113110
content = message.parsed_content
114111
assert isinstance(content, StoreContent)
115112

@@ -122,9 +119,14 @@ async def fetch_related_content(
122119
do_standard_lookup = True
123120

124121
# Sentinel value, the code below always sets a value but mypy does not see it.
122+
# otherwise if config.storage.store_files is False, this will be the database value
125123
size: int = -1
126124

127-
if engine == ItemType.ipfs and ipfs_enabled:
125+
if (
126+
config.storage.store_files.value
127+
and engine == ItemType.ipfs
128+
and ipfs_enabled
129+
):
128130
if item_type_from_hash(item_hash) != ItemType.ipfs:
129131
LOGGER.warning("Invalid IPFS hash: '%s'", item_hash)
130132
raise InvalidMessageFormat(
@@ -175,7 +177,7 @@ async def fetch_related_content(
175177
)
176178
do_standard_lookup = True
177179

178-
if do_standard_lookup:
180+
if config.storage.store_files.value and do_standard_lookup:
179181
try:
180182
file_content = await self.storage_service.get_hash_content(
181183
item_hash,

tests/storage/test_store_message.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,56 @@ async def test_handle_new_storage_directory(
154154
assert stored_file.type == FileType.DIRECTORY
155155

156156
assert not storage_engine.called
157+
158+
159+
@pytest.mark.asyncio
160+
async def test_store_files_is_false(
161+
mocker,
162+
session_factory: DbSessionFactory,
163+
mock_config: Config,
164+
fixture_message_directory: MessageDb,
165+
):
166+
mock_ipfs_client = mocker.MagicMock()
167+
ipfs_stats = {
168+
"Hash": "QmPZrod87ceK4yVvXQzRexDcuDgmLxBiNJ1ajLjLoMx9sU",
169+
"Size": 0,
170+
"CumulativeSize": 4560,
171+
"Blocks": 2,
172+
"Type": "file",
173+
}
174+
mock_ipfs_client.files.stat = mocker.AsyncMock(return_value=ipfs_stats)
175+
176+
mock_config.storage.store_files.value = False
177+
178+
message = fixture_message_directory
179+
storage_engine = mocker.AsyncMock()
180+
181+
storage_service = StorageService(
182+
storage_engine=storage_engine,
183+
ipfs_service=IpfsService(ipfs_client=mock_ipfs_client),
184+
node_cache=mocker.AsyncMock(),
185+
)
186+
get_hash_content_mock = mocker.patch.object(storage_service, "get_hash_content")
187+
store_message_handler = StoreMessageHandler(
188+
storage_service=storage_service, grace_period=24
189+
)
190+
191+
with session_factory() as session:
192+
await store_message_handler.fetch_related_content(
193+
session=session, message=message
194+
)
195+
session.commit()
196+
197+
with session_factory() as session:
198+
stored_files = list((session.execute(select(StoredFileDb))).scalars())
199+
200+
assert len(stored_files) == 1
201+
stored_file = stored_files[0]
202+
203+
# Check the updates to the message content
204+
assert stored_file.hash == ipfs_stats["Hash"]
205+
assert stored_file.size == -1
206+
assert stored_file.type == FileType.FILE
207+
208+
storage_engine.assert_not_called()
209+
get_hash_content_mock.assert_not_called()

0 commit comments

Comments
 (0)