Skip to content

Commit 874374c

Browse files
committed
Use index and last modified date
1 parent 24b093a commit 874374c

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

src/pyflask/app.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
import json
44
import multiprocessing
5-
import random
6-
import string
5+
import os
76
import sys
87
from datetime import datetime
98
from logging import DEBUG, Formatter
@@ -35,6 +34,7 @@
3534
system_namespace,
3635
)
3736

37+
# Dictionary to store file paths with their IDs
3838
neurosift_file_registry = dict()
3939

4040
flask_app = Flask(__name__)
@@ -97,19 +97,26 @@ def handle_file_request(file_path) -> Union[str, None]:
9797
if not file_path.endswith(".nwb"):
9898
raise ValueError("This endpoint must be called on an NWB file that ends with '.nwb'!")
9999

100-
file_id = "".join(random.choices(string.ascii_uppercase + string.digits, k=8))
100+
# Get the last modified time of the file
101+
try:
102+
last_modified = os.path.getmtime(file_path)
103+
last_modified_str = datetime.fromtimestamp(last_modified).strftime("%Y%m%d%H%M%S")
104+
except (OSError, ValueError):
105+
# If we can't get the last modified time, use current time
106+
last_modified_str = datetime.now().strftime("%Y%m%d%H%M%S")
107+
108+
# Use the number of items in the dictionary as the index
109+
index = len(neurosift_file_registry) + 1
110+
111+
# Create a file_id that combines the index and last modified date
112+
file_id = f"{index}_{last_modified_str}"
101113
neurosift_file_registry[file_id] = file_path
102114

103115
# files/<file_id> is the URL that can be used to access the file
104-
# NOTE: This endpoint used to be files/<file_path> but file_path would become URL-decoded
105-
# by Neurosift which changed + signs to spaces before making the GET request. This broke local reading
106-
# of files with + signs in the filename. Other symbols may be affected as well.
107-
# This is why we use the safer, but less transparent, files/<file_id> instead.
108-
# We do not use an incremental index because Neurosift caches information about the file in persistent
109-
# Chrome storage (IndexedDB) that is based on the URL and cannot be cleared by NWB GUIDE. Subsequent loads
110-
# of a file would return cached information from host/files/0. So, we use a random string instead.
111-
# We also want to make sure multiple loads of the same file do not return the same URL because cached
112-
# information about the file that is no longer valid may be returned.
116+
# NOTE: We use an index joined with the last modified date to ensure uniqueness for
117+
# each request (avoiding Neurosift caching issues) and avoid issues with URL-decoding
118+
# by the external Neurosift app, while providing somewhat meaningful IDs that include
119+
# file modification information.
113120
return request.host_url + "/files/" + file_id
114121

115122

0 commit comments

Comments
 (0)