Skip to content

Commit e9bf327

Browse files
committed
bugfix(upload): cannot get rid of temp files right away
1 parent c5c48af commit e9bf327

4 files changed

Lines changed: 39 additions & 22 deletions

File tree

‎QA/py/tests/test_admin.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from pathlib import Path
77

88
from API_operations.helpers.Service import Service
9+
from FS.UserFilesDir import get_tus_files_dir
910
from helpers.AppConfig import Config
1011
from sqlalchemy import text
1112
from starlette import status
@@ -112,6 +113,16 @@ def mock_getctime(path):
112113
recent_trash_file.write_text("recent trashed file")
113114
os.utime(recent_trash_file, (recent_trash_time, recent_trash_time))
114115

116+
# 6. TUS uploads directory (old should be removed, recent should NOT be removed)
117+
tus_dir = Path(get_tus_files_dir())
118+
old_tus_file = tus_dir / "old_tus_upload.tmp"
119+
old_tus_file.write_text("old tus file")
120+
os.utime(old_tus_file, (old_time, old_time))
121+
122+
recent_tus_file = tus_dir / "recent_tus_upload.tmp"
123+
recent_tus_file.write_text("recent tus file")
124+
os.utime(recent_tus_file, (recent_time, recent_time))
125+
115126
return {
116127
"users_files_dir": users_files_dir,
117128
"user_dir": user_dir,
@@ -122,6 +133,8 @@ def mock_getctime(path):
122133
"recent_in_mixed": recent_in_mixed,
123134
"recent_dir": recent_dir,
124135
"recent_trash_file": recent_trash_file,
136+
"old_tus_file": old_tus_file,
137+
"recent_tus_file": recent_tus_file,
125138
}
126139

127140

@@ -140,6 +153,8 @@ def test_nightly_job(fastapi, caplog, tstlogs, mock_taxoserver, monkeypatch):
140153
recent_in_mixed = fake_dirs["recent_in_mixed"]
141154
recent_dir = fake_dirs["recent_dir"]
142155
recent_trash_file = fake_dirs["recent_trash_file"]
156+
old_tus_file = fake_dirs["old_tus_file"]
157+
recent_tus_file = fake_dirs["recent_tus_file"]
143158

144159
# Generate a few jobs however, and warp them back in time
145160
test_export_roundtrip(fastapi, tstlogs) # Import/Export/Import
@@ -166,11 +181,13 @@ def test_nightly_job(fastapi, caplog, tstlogs, mock_taxoserver, monkeypatch):
166181
assert not old_dir.exists()
167182
assert not old_trash_subdir.exists()
168183
assert not old_trash_file.exists()
184+
assert not old_tus_file.exists()
169185
assert mixed_dir.exists()
170186
assert recent_in_mixed.exists()
171187
assert recent_dir.exists()
172188
assert (recent_dir / "recent_file.txt").exists()
173189
assert recent_trash_file.exists()
190+
assert recent_tus_file.exists()
174191

175192
maintenance_log_file = (
176193
users_files_dir
@@ -181,6 +198,7 @@ def test_nightly_job(fastapi, caplog, tstlogs, mock_taxoserver, monkeypatch):
181198
assert "old_folder" in maintenance_log_content
182199
assert "old_trash_sub" in maintenance_log_content
183200
assert "old_trash_file.txt" in maintenance_log_content
201+
assert "old_tus_upload.tmp" in maintenance_log_content
184202

185203
# Second cleanup must do nothing
186204
log = do_nightly(fastapi)
@@ -191,7 +209,9 @@ def test_nightly_job(fastapi, caplog, tstlogs, mock_taxoserver, monkeypatch):
191209
assert not old_dir.exists()
192210
assert not old_trash_subdir.exists()
193211
assert not old_trash_file.exists()
212+
assert not old_tus_file.exists()
194213
assert mixed_dir.exists()
195214
assert recent_in_mixed.exists()
196215
assert recent_dir.exists()
197216
assert recent_trash_file.exists()
217+
assert recent_tus_file.exists()

‎py/API_operations/BigFiles.py‎

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from tuspyserver.lock import acquire_upload_lock
1515
from tuspyserver.storage.local import LocalFileStorage
1616

17-
from FS.UserFilesDir import UserFilesDirectory
17+
from FS.UserFilesDir import UserFilesDirectory, get_tus_files_dir
1818
from helpers.AppConfig import Config
1919
from helpers.DynamicLogs import get_logger
2020
from helpers.fastApiUtils import get_current_user
@@ -27,17 +27,6 @@
2727
background_tasks: Set[asyncio.Task[Any]] = set()
2828

2929

30-
def get_tus_files_dir() -> str:
31-
"""
32-
Get the directory where big files are stored.
33-
Use a subfolder of the users files area.
34-
"""
35-
base_dir = Config().get_users_files_dir()
36-
tus_dir = Path(base_dir, "tus_uploads")
37-
tus_dir.mkdir(parents=True, exist_ok=True)
38-
return str(tus_dir)
39-
40-
4130
async def tus_auth(
4231
request: Request, current_user: int = Depends(get_current_user)
4332
) -> None:
@@ -64,14 +53,6 @@ async def _background_unpack(
6453
"Error unpacking %s in background: %s", file_path, str(e), exc_info=True
6554
)
6655
raise
67-
finally:
68-
# Always release the TUS upload.
69-
try:
70-
os.unlink(orig_path)
71-
except FileNotFoundError:
72-
pass
73-
except OSError as e:
74-
logger.warning("Could not remove uploaded file %s: %s", orig_path, e)
7556

7657

7758
async def on_tus_upload_complete(
@@ -184,7 +165,6 @@ async def get_on_complete_handler(
184165
Dependency that returns the completion handler with the current user id.
185166
"""
186167
current_user_id = getattr(request.state, "current_user_id", None)
187-
logger.info("get_on_complete_handler: current_user_id=%s", current_user_id)
188168
if current_user_id is None:
189169
# This should not happen if tus_auth was called
190170
logger.error(

‎py/API_operations/admin/NightlyJob.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from DB.helpers import Session
3535
from DB.helpers.Direct import text
3636
from FS.TempDirForTasks import TempDirForTasks
37-
from FS.UserFilesDir import UserFilesDirectory
37+
from FS.UserFilesDir import UserFilesDirectory, get_tus_files_dir
3838
from helpers.DynamicLogs import get_logger, LogsSwitcher
3939

4040
logger = get_logger(__name__)
@@ -487,6 +487,12 @@ def users_files_maintenance(self, start: int, end: int) -> None:
487487
tf = int(os.path.getctime(item))
488488
if tf < trashtime:
489489
self._delete_definitely(loggerfilename, item, tf, users_files_dir)
490+
# remove old files in TUS uploads directory
491+
for entry in glob(get_tus_files_dir() + os.path.sep + "*"):
492+
item = Path(entry)
493+
tf = int(os.path.getctime(item))
494+
if tf < old:
495+
self._delete_definitely(loggerfilename, item, tf, users_files_dir)
490496
# bottom to top scandir
491497
for entry in glob(users_files_dir + os.path.sep + userdirpattern + "*"):
492498
item = Path(entry)

‎py/FS/UserFilesDir.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,3 +392,14 @@ def _log_exception_throw(e: Exception, context: Optional[str] = None):
392392
code = 500
393393
logger.error(str(code) + " " + message + " " + str(context))
394394
raise
395+
396+
397+
def get_tus_files_dir() -> str:
398+
"""
399+
Get the directory where uploaded files (via TUS) are stored.
400+
Use a subfolder of the users files area.
401+
"""
402+
base_dir = Config().get_users_files_dir()
403+
tus_dir = Path(base_dir, "tus_uploads")
404+
tus_dir.mkdir(parents=True, exist_ok=True)
405+
return str(tus_dir)

0 commit comments

Comments
 (0)