use of com.ichi2.anki.exception.ImportExportException in project AnkiChinaAndroid by ankichinateam.
the class ZipFile method exportFiltered.
private JSONObject exportFiltered(ZipFile z, String path, Context context) throws IOException, JSONException, ImportExportException {
// export into the anki2 file
String colfile = path.replace(".apkg", ".anki2");
super.exportInto(colfile, context);
z.write(colfile, CollectionHelper.COLLECTION_FILENAME);
// and media
prepareMedia();
JSONObject media = _exportMedia(z, mMediaFiles, mCol.getMedia().dir());
// tidy up intermediate files
SQLiteDatabase.deleteDatabase(new File(colfile));
SQLiteDatabase.deleteDatabase(new File(path.replace(".apkg", ".media.ad.db2")));
String tempPath = path.replace(".apkg", ".media");
File file = new File(tempPath);
if (file.exists()) {
String deleteCmd = "rm -r " + tempPath;
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec(deleteCmd);
} catch (IOException e) {
}
}
return media;
}
use of com.ichi2.anki.exception.ImportExportException in project Anki-Android by ankidroid.
the class ImportTest method testAnki2Mediadupes.
@Test
public void testAnki2Mediadupes() throws IOException, JSONException, ImportExportException {
// add a note that references a sound
Note n = mTestCol.newNote();
n.setField(0, "[sound:foo.mp3]");
long mid = n.model().getLong("id");
mTestCol.addNote(n);
// add that sound to the media directory
FileOutputStream os = new FileOutputStream(new File(mTestCol.getMedia().dir(), "foo.mp3"), false);
os.write("foo".getBytes());
os.close();
mTestCol.close();
// it should be imported correctly into an empty deck
Collection empty = getEmptyCol();
Importer imp = new Anki2Importer(empty, mTestCol.getPath());
imp.run();
List<String> expected = Collections.singletonList("foo.mp3");
List<String> actual = Arrays.asList(new File(empty.getMedia().dir()).list());
actual.retainAll(expected);
assertEquals(expected.size(), actual.size());
// and importing again will not duplicate, as the file content matches
empty.remCards(empty.getDb().queryLongList("select id from cards"));
imp = new Anki2Importer(empty, mTestCol.getPath());
imp.run();
expected = Collections.singletonList("foo.mp3");
actual = Arrays.asList(new File(empty.getMedia().dir()).list());
actual.retainAll(expected);
assertEquals(expected.size(), actual.size());
n = empty.getNote(empty.getDb().queryLongScalar("select id from notes"));
assertTrue(n.getFields()[0].contains("foo.mp3"));
// if the local file content is different, and import should trigger a rename
empty.remCards(empty.getDb().queryLongList("select id from cards"));
os = new FileOutputStream(new File(empty.getMedia().dir(), "foo.mp3"), false);
os.write("bar".getBytes());
os.close();
imp = new Anki2Importer(empty, mTestCol.getPath());
imp.run();
expected = Arrays.asList("foo.mp3", String.format("foo_%s.mp3", mid));
actual = Arrays.asList(new File(empty.getMedia().dir()).list());
actual.retainAll(expected);
assertEquals(expected.size(), actual.size());
n = empty.getNote(empty.getDb().queryLongScalar("select id from notes"));
assertTrue(n.getFields()[0].contains("_"));
// if the localized media file already exists, we rewrite the note and media
empty.remCards(empty.getDb().queryLongList("select id from cards"));
os = new FileOutputStream(new File(empty.getMedia().dir(), "foo.mp3"));
os.write("bar".getBytes());
os.close();
imp = new Anki2Importer(empty, mTestCol.getPath());
imp.run();
expected = Arrays.asList("foo.mp3", String.format("foo_%s.mp3", mid));
actual = Arrays.asList(new File(empty.getMedia().dir()).list());
actual.retainAll(expected);
assertEquals(expected.size(), actual.size());
n = empty.getNote(empty.getDb().queryLongScalar("select id from notes"));
assertTrue(n.getFields()[0].contains("_"));
empty.close();
}
use of com.ichi2.anki.exception.ImportExportException in project Anki-Android by ankidroid.
the class ImportTest method testApkg.
@Test
public void testApkg() throws IOException, ImportExportException {
String apkg = Shared.getTestFilePath(getTestContext(), "media.apkg");
Importer imp = new AnkiPackageImporter(mTestCol, apkg);
List<String> expected = Collections.emptyList();
List<String> actual = Arrays.asList(new File(mTestCol.getMedia().dir()).list());
actual.retainAll(expected);
assertEquals(actual.size(), expected.size());
imp.run();
expected = Collections.singletonList("foo.wav");
actual = Arrays.asList(new File(mTestCol.getMedia().dir()).list());
actual.retainAll(expected);
assertEquals(expected.size(), actual.size());
// import again should be idempotent in terms of media
mTestCol.remCards(mTestCol.getDb().queryLongList("select id from cards"));
imp = new AnkiPackageImporter(mTestCol, apkg);
imp.run();
expected = Collections.singletonList("foo.wav");
actual = Arrays.asList(new File(mTestCol.getMedia().dir()).list());
actual.retainAll(expected);
assertEquals(expected.size(), actual.size());
// but if the local file has different data, it will rename
mTestCol.remCards(mTestCol.getDb().queryLongList("select id from cards"));
FileOutputStream os = new FileOutputStream(new File(mTestCol.getMedia().dir(), "foo.wav"), false);
os.write("xyz".getBytes());
os.close();
imp = new AnkiPackageImporter(mTestCol, apkg);
imp.run();
assertEquals(2, new File(mTestCol.getMedia().dir()).list().length);
}
use of com.ichi2.anki.exception.ImportExportException in project Anki-Android by ankidroid.
the class AnkiPackageExporterTest method missingFileInDeckExportDoesSkipsFile.
@Test
public void missingFileInDeckExportDoesSkipsFile() throws IOException, ImportExportException {
// Arrange
File mediaFilePath = addTempFileToMediaAndNote();
if (!mediaFilePath.delete()) {
throw new IllegalStateException("need to delete temp file for test to pass");
}
AnkiPackageExporter exporter = getExporterForDeckWithMedia();
File temp = CreateTempDir.tempDir("/AnkiDroid-missingFileInExportDoesNotThrowException-export");
File exportedFile = new File(temp.getAbsolutePath() + "/export.apkg");
// Exporting
exporter.exportInto(exportedFile.getAbsolutePath(), getTargetContext());
// Unzipping the export.apkg file
UnzipFile.unzip(exportedFile, temp.getAbsolutePath() + "/unzipped");
String unzipDirectory = temp.getAbsolutePath() + "/unzipped";
// Storing paths of unzipped files in a list
List<String> files = Arrays.asList(new File(unzipDirectory).list());
File[] file_names = new File[2];
int i = 0;
for (String x : files) {
File f = new File(unzipDirectory + "/" + x);
file_names[i++] = f;
}
// Checking the unzipped files
assertThat(files, containsInAnyOrder("collection.anki2", "media"));
assertThat("Only two files should exist", files, hasSize(2));
checkMediaExportStringIs(file_names, "{}");
}
use of com.ichi2.anki.exception.ImportExportException in project AnkiChinaAndroid by ankichinateam.
the class ImportTest method testAnki2DiffmodelTemplates.
@Test
public void testAnki2DiffmodelTemplates() throws IOException, JSONException, ImportExportException {
// different from the above as this one tests only the template text being
// changed, not the number of cards/fields
// import the first version of the model
String tmp = Shared.getTestFilePath(InstrumentationRegistry.getInstrumentation().getTargetContext(), "diffmodeltemplates-1.apkg");
AnkiPackageImporter imp = new AnkiPackageImporter(testCol, tmp);
imp.setDupeOnSchemaChange(true);
imp.run();
// then the version with updated template
tmp = Shared.getTestFilePath(InstrumentationRegistry.getInstrumentation().getTargetContext(), "diffmodeltemplates-2.apkg");
imp = new AnkiPackageImporter(testCol, tmp);
imp.setDupeOnSchemaChange(true);
imp.run();
// collection should contain the note we imported
assertEquals(1, testCol.noteCount());
// the front template should contain the text added in the 2nd package
Long tcid = testCol.findCards("").get(0);
Note tnote = testCol.getCard(tcid).note();
assertTrue(testCol.findTemplates(tnote).get(0).getString("qfmt").contains("Changed Front Template"));
}
Aggregations