Search in sources :

Example 1 with AnkiPackageExporter

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);
}
Also used : FileNotFoundException(java.io.FileNotFoundException) JSONException(com.ichi2.utils.JSONException) IOException(java.io.IOException) AnkiPackageExporter(com.ichi2.libanki.AnkiPackageExporter) ImportExportException(com.ichi2.anki.exception.ImportExportException) Collection(com.ichi2.libanki.Collection) JSONObject(com.ichi2.utils.JSONObject)

Example 2 with AnkiPackageExporter

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, "{}");
}
Also used : File(java.io.File) UnzipFile(com.ichi2.utils.UnzipFile) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

Example 3 with AnkiPackageExporter

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, "{}");
}
Also used : Path(java.nio.file.Path) File(java.io.File) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

Example 4 with AnkiPackageExporter

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);
}
Also used : Path(java.nio.file.Path) File(java.io.File) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

Example 5 with AnkiPackageExporter

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);
}
Also used : File(java.io.File) UnzipFile(com.ichi2.utils.UnzipFile) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

Aggregations

RobolectricTest (com.ichi2.anki.RobolectricTest)4 File (java.io.File)4 Test (org.junit.Test)4 UnzipFile (com.ichi2.utils.UnzipFile)2 Path (java.nio.file.Path)2 ImportExportException (com.ichi2.anki.exception.ImportExportException)1 AnkiPackageExporter (com.ichi2.libanki.AnkiPackageExporter)1 Collection (com.ichi2.libanki.Collection)1 JSONException (com.ichi2.utils.JSONException)1 JSONObject (com.ichi2.utils.JSONObject)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1