0
0
mirror of https://github.com/ankidroid/Anki-Android.git synced 2024-09-20 12:02:16 +02:00

Merged 'e35a52b' from hotfix-2.5.3: Don't export media files with _ unless they are referenced in model

dae/anki#114
timrae/anki@6d850c9
This commit is contained in:
Timothy Rae 2015-12-10 02:51:04 +09:00
commit 28c75efa9b

View File

@ -205,7 +205,13 @@ class AnkiExporter extends Exporter {
for (File f : new File(mMediaDir).listFiles()) {
String fname = f.getName();
if (fname.startsWith("_")) {
media.put(fname, true);
// Loop through every model that will be exported, and check if it contains a reference to f
for (int idx = 0; idx < mid.size(); idx++) {
if (_modelHasMedia(mSrc.getModels().get(idx), fname)) {
media.put(fname, true);
break;
}
}
}
}
}
@ -225,6 +231,31 @@ class AnkiExporter extends Exporter {
dst.close();
}
/**
* Returns whether or not the specified model contains a reference to the given media file.
* In order to ensure relatively fast operation we only check if the styling, front, back templates *contain* fname,
* and thus must allow for occasional false positives.
* @param model the model to scan
* @param fname the name of the media file to check for
* @return
* @throws JSONException
*/
private boolean _modelHasMedia(JSONObject model, String fname) throws JSONException {
// First check the styling
if (model.getString("css").contains(fname)) {
return true;
}
// If not there then check the templates
JSONArray tmpls = model.getJSONArray("tmpls");
for (int idx = 0; idx < tmpls.length(); idx++) {
JSONObject tmpl = tmpls.getJSONObject(idx);
if (tmpl.getString("qfmt").contains(fname) || tmpl.getString("afmt").contains(fname)) {
return true;
}
}
return false;
}
/**
* overwrite to apply customizations to the deck before it's closed, such as update the deck description