use of com.ichi2.libanki.AnkiPackageExporter in project AnkiChinaAndroid by ankichinateam.
the class CollectionTask method doInBackgroundExportApkg.
private TaskData doInBackgroundExportApkg(TaskData param) {
Timber.d("doInBackgroundExportApkg");
Object[] data = param.getObjArray();
Collection col = (Collection) data[0];
String apkgPath = (String) data[1];
Long did = (Long) data[2];
boolean includeSched = (Boolean) data[3];
boolean includeMedia = (Boolean) data[4];
boolean exportApkg = (Boolean) data[5];
boolean exportCard = (Boolean) data[6];
try {
AnkiPackageExporter exporter = new AnkiPackageExporter(col);
exporter.setIncludeSched(includeSched);
exporter.setIncludeMedia(includeMedia);
exporter.setExportCard(exportCard);
exporter.setExportApkg(exportApkg);
exporter.setDid(did);
exporter.exportInto(apkgPath, mContext);
} catch (FileNotFoundException e) {
Timber.e(e, "FileNotFoundException in doInBackgroundExportApkg");
return new TaskData(false);
} catch (IOException e) {
Timber.e(e, "IOException in doInBackgroundExportApkg");
return new TaskData(false);
} catch (JSONException e) {
Timber.e(e, "JSOnException in doInBackgroundExportApkg");
return new TaskData(false);
} catch (ImportExportException e) {
Timber.e(e, "ImportExportException in doInBackgroundExportApkg");
return new TaskData(e.getMessage(), true);
}
return new TaskData(exportCard ? apkgPath.replace(".apkg", ".card") : apkgPath);
}
use of com.ichi2.libanki.AnkiPackageExporter 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.libanki.AnkiPackageExporter in project AnkiChinaAndroid by ankichinateam.
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();
Path tempExportDir = Files.createTempDirectory("AnkiDroid-missingFileInExportDoesNotThrowException-export");
File exportedFile = new File(tempExportDir.toFile(), "export.apkg");
// act
exporter.exportInto(exportedFile.getAbsolutePath(), getTargetContext());
// assert
Path unzipDirectory = unzipFilesTo(tempExportDir, exportedFile);
File[] files = unzipDirectory.toFile().listFiles();
// confirm the files
List<String> fileNames = Arrays.stream(files).map(File::getName).collect(Collectors.toList());
assertThat(fileNames, containsInAnyOrder("collection.anki2", "media"));
assertThat("Only two files should exist", fileNames, hasSize(2));
checkMediaExportStringIs(files, "{}");
}
use of com.ichi2.libanki.AnkiPackageExporter in project AnkiChinaAndroid by ankichinateam.
the class AnkiPackageExporterTest method fileInExportIsCopied.
@Test
public void fileInExportIsCopied() throws IOException, ImportExportException {
// arrange
File tempFileInCollection = addTempFileToMediaAndNote();
AnkiPackageExporter exporter = getExporterForDeckWithMedia();
Path tempExportDir = Files.createTempDirectory("AnkiDroid-missingFileInExportDoesNotThrowException-export");
File exportedFile = new File(tempExportDir.toFile(), "export.apkg");
// act
exporter.exportInto(exportedFile.getAbsolutePath(), getTargetContext());
// assert
Path unzipDirectory = unzipFilesTo(tempExportDir, exportedFile);
File[] files = unzipDirectory.toFile().listFiles();
// confirm the files
List<String> fileNames = Arrays.stream(files).map(File::getName).collect(Collectors.toList());
assertThat(fileNames, containsInAnyOrder("collection.anki2", "media", "0"));
assertThat("Three files should exist", fileNames, hasSize(3));
// {"0":"filename.txt"}
String expected = String.format("{\"0\":\"%s\"}", tempFileInCollection.getName());
checkMediaExportStringIs(files, expected);
}
use of com.ichi2.libanki.AnkiPackageExporter in project Anki-Android by ankidroid.
the class AnkiPackageExporterTest method fileInExportIsCopied.
@Test
public void fileInExportIsCopied() throws IOException, ImportExportException {
// Arrange
File tempFileInCollection = addTempFileToMediaAndNote();
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[] fileNames = new File[3];
int i = 0;
for (String x : files) {
File f = new File(unzipDirectory + "/" + x);
fileNames[i++] = f;
}
// Checking the unzipped files
assertThat(files, containsInAnyOrder("collection.anki2", "media", "0"));
assertThat("Three files should exist", files, hasSize(3));
// {"0":"filename.txt"}
String expected = String.format("{\"0\":\"%s\"}", tempFileInCollection.getName());
checkMediaExportStringIs(fileNames, expected);
}
Aggregations