dfd
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user