This commit is contained in:
nose
2025-12-19 15:20:08 -08:00
parent d3edd6420c
commit 900a37e210
13 changed files with 729 additions and 32 deletions

View File

@@ -354,12 +354,49 @@ class Folder(Store):
self.add_url(file_hash, url)
return file_hash
# Move or copy file
# Move or copy file (with progress bar on actual byte transfer).
# Note: a same-volume move may be a fast rename and won't show progress.
def _copy_with_progress(src: Path, dst: Path, *, label: str) -> None:
from models import ProgressFileReader
total_bytes = None
try:
total_bytes = int(src.stat().st_size)
except Exception:
total_bytes = None
with src.open("rb") as r, dst.open("wb") as w:
reader = ProgressFileReader(r, total_bytes=total_bytes, label=label)
while True:
chunk = reader.read(1024 * 1024)
if not chunk:
break
w.write(chunk)
# Preserve file metadata similar to shutil.copy2
try:
shutil.copystat(str(src), str(dst))
except Exception:
pass
if move_file:
shutil.move(str(file_path), str(save_file))
debug(f"Local move: {save_file}", file=sys.stderr)
# Prefer native move; fall back to copy+delete with progress on failure.
try:
shutil.move(str(file_path), str(save_file))
debug(f"Local move: {save_file}", file=sys.stderr)
except Exception:
_copy_with_progress(file_path, save_file, label=f"folder:{self._name} move")
try:
file_path.unlink(missing_ok=True) # type: ignore[arg-type]
except Exception:
try:
if file_path.exists():
file_path.unlink()
except Exception:
pass
debug(f"Local move (copy+delete): {save_file}", file=sys.stderr)
else:
shutil.copy2(str(file_path), str(save_file))
_copy_with_progress(file_path, save_file, label=f"folder:{self._name} copy")
debug(f"Local copy: {save_file}", file=sys.stderr)
# Best-effort: capture duration for media