|
2 | 2 |
|
3 | 3 | import json |
4 | 4 | import multiprocessing |
5 | | -import random |
6 | | -import string |
| 5 | +import os |
7 | 6 | import sys |
8 | 7 | from datetime import datetime |
9 | 8 | from logging import DEBUG, Formatter |
|
35 | 34 | system_namespace, |
36 | 35 | ) |
37 | 36 |
|
| 37 | +# Dictionary to store file paths with their IDs |
38 | 38 | neurosift_file_registry = dict() |
39 | 39 |
|
40 | 40 | flask_app = Flask(__name__) |
@@ -97,19 +97,26 @@ def handle_file_request(file_path) -> Union[str, None]: |
97 | 97 | if not file_path.endswith(".nwb"): |
98 | 98 | raise ValueError("This endpoint must be called on an NWB file that ends with '.nwb'!") |
99 | 99 |
|
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}" |
101 | 113 | neurosift_file_registry[file_id] = file_path |
102 | 114 |
|
103 | 115 | # 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. |
113 | 120 | return request.host_url + "/files/" + file_id |
114 | 121 |
|
115 | 122 |
|
|
0 commit comments