mirror of
https://github.com/ankitects/anki.git
synced 2025-09-20 06:52:21 -04:00
only copy used media on import/export
This commit is contained in:
parent
e010ef8062
commit
8df9111b50
1 changed files with 20 additions and 6 deletions
26
anki/sync.py
26
anki/sync.py
|
@ -33,6 +33,7 @@ from anki.stats import Stats, globalStats
|
||||||
from anki.history import CardHistoryEntry
|
from anki.history import CardHistoryEntry
|
||||||
from anki.stats import globalStats
|
from anki.stats import globalStats
|
||||||
from anki.utils import ids2str, hexifyID
|
from anki.utils import ids2str, hexifyID
|
||||||
|
from anki.media import mediaRefs
|
||||||
from anki.lang import _
|
from anki.lang import _
|
||||||
from hooks import runHook
|
from hooks import runHook
|
||||||
|
|
||||||
|
@ -1166,14 +1167,27 @@ class HttpSyncServer(SyncServer):
|
||||||
##########################################################################
|
##########################################################################
|
||||||
|
|
||||||
def copyLocalMedia(src, dst):
|
def copyLocalMedia(src, dst):
|
||||||
src = src.mediaDir()
|
srcDir = src.mediaDir()
|
||||||
if not src:
|
if not srcDir:
|
||||||
return
|
return
|
||||||
dst = dst.mediaDir(create=True)
|
dstDir = dst.mediaDir(create=True)
|
||||||
files = os.listdir(src)
|
files = os.listdir(srcDir)
|
||||||
|
# find media references; ignore latex cache
|
||||||
|
used = {}
|
||||||
|
for col in ("question", "answer"):
|
||||||
|
txt = dst.s.column0("""
|
||||||
|
select %(c)s from cards where
|
||||||
|
%(c)s like '%%<img %%'
|
||||||
|
or %(c)s like '%%[sound:%%'""" % {'c': col})
|
||||||
|
for entry in txt:
|
||||||
|
for (full, fname, repl) in mediaRefs(entry):
|
||||||
|
used[fname] = True
|
||||||
|
# copy only used media
|
||||||
for file in files:
|
for file in files:
|
||||||
srcfile = os.path.join(src, file)
|
if file not in used:
|
||||||
dstfile = os.path.join(dst, file)
|
continue
|
||||||
|
srcfile = os.path.join(srcDir, file)
|
||||||
|
dstfile = os.path.join(dstDir, file)
|
||||||
if not os.path.exists(dstfile):
|
if not os.path.exists(dstfile):
|
||||||
try:
|
try:
|
||||||
shutil.copy2(srcfile, dstfile)
|
shutil.copy2(srcfile, dstfile)
|
||||||
|
|
Loading…
Reference in a new issue