Search in sources :

Example 1 with EmptyMediaException

use of com.ichi2.libanki.exception.EmptyMediaException in project Anki-Android by ankidroid.

the class MediaTest method testAdd.

@Test
public void testAdd() throws IOException, EmptyMediaException {
    // open new empty collection
    File dir = getTestDir();
    BackupManager.removeDir(dir);
    assertTrue(dir.mkdirs());
    File path = new File(dir, "foo.jpg");
    FileOutputStream os = new FileOutputStream(path, false);
    os.write("hello".getBytes());
    os.close();
    // new file, should preserve name
    String r = mTestCol.getMedia().addFile(path);
    assertEquals("foo.jpg", r);
    // adding the same file again should not create a duplicate
    assertEquals("foo.jpg", mTestCol.getMedia().addFile(path));
    // but if it has a different md5, it should
    os = new FileOutputStream(path, false);
    os.write("world".getBytes());
    os.close();
    assertNotEquals("foo.jpg", mTestCol.getMedia().addFile(path));
}
Also used : FileOutputStream(java.io.FileOutputStream) File(java.io.File) Test(org.junit.Test) InstrumentedTest(com.ichi2.anki.tests.InstrumentedTest)

Example 2 with EmptyMediaException

use of com.ichi2.libanki.exception.EmptyMediaException in project Anki-Android by ankidroid.

the class MediaTest method testAddEmptyFails.

@Test
public void testAddEmptyFails() throws IOException {
    // open new empty collection
    File dir = getTestDir();
    BackupManager.removeDir(dir);
    assertTrue(dir.mkdirs());
    File path = new File(dir, "foo.jpg");
    assertTrue(path.createNewFile());
    // new file, should preserve name
    try {
        mTestCol.getMedia().addFile(path);
        fail("exception should be thrown");
    } catch (EmptyMediaException mediaException) {
    // all good
    }
}
Also used : File(java.io.File) EmptyMediaException(com.ichi2.libanki.exception.EmptyMediaException) Test(org.junit.Test) InstrumentedTest(com.ichi2.anki.tests.InstrumentedTest)

Example 3 with EmptyMediaException

use of com.ichi2.libanki.exception.EmptyMediaException in project Anki-Android by ankidroid.

the class MediaTest method testChanges.

@Test
public void testChanges() throws IOException, EmptyMediaException {
    assertNotNull(mTestCol.getMedia()._changed());
    assertEquals(0, added(mTestCol).size());
    assertEquals(0, removed(mTestCol).size());
    // add a file
    File dir = getTestDir();
    File path = new File(dir, "foo.jpg");
    FileOutputStream os = new FileOutputStream(path, false);
    os.write("hello".getBytes());
    os.close();
    path = new File(mTestCol.getMedia().dir(), mTestCol.getMedia().addFile(path));
    // should have been logged
    mTestCol.getMedia().findChanges();
    assertThat(added(mTestCol).size(), is(greaterThan(0)));
    assertEquals(0, removed(mTestCol).size());
    // if we modify it, the cache won't notice
    os = new FileOutputStream(path, true);
    os.write("world".getBytes());
    os.close();
    assertEquals(1, added(mTestCol).size());
    assertEquals(0, removed(mTestCol).size());
    // but if we add another file, it will
    path = new File(path.getAbsolutePath() + "2");
    os = new FileOutputStream(path, true);
    os.write("yo".getBytes());
    os.close();
    mTestCol.getMedia().findChanges(true);
    assertEquals(2, added(mTestCol).size());
    assertEquals(0, removed(mTestCol).size());
    // deletions should get noticed too
    assertTrue(path.delete());
    mTestCol.getMedia().findChanges(true);
    assertEquals(1, added(mTestCol).size());
    assertEquals(1, removed(mTestCol).size());
}
Also used : FileOutputStream(java.io.FileOutputStream) File(java.io.File) Test(org.junit.Test) InstrumentedTest(com.ichi2.anki.tests.InstrumentedTest)

Example 4 with EmptyMediaException

use of com.ichi2.libanki.exception.EmptyMediaException in project Anki-Android by ankidroid.

the class CardContentProvider method insertMediaFile.

private Uri insertMediaFile(ContentValues values, Collection col) {
    // Insert media file using libanki.Media.addFile and return Uri for the inserted file.
    Uri fileUri = Uri.parse(values.getAsString(FlashCardsContract.AnkiMedia.FILE_URI));
    String preferredName = values.getAsString(FlashCardsContract.AnkiMedia.PREFERRED_NAME);
    try {
        ContentResolver cR = mContext.getContentResolver();
        Media media = col.getMedia();
        // idea, open input stream and save to cache directory, then
        // pass this (hopefully temporary) file to the media.addFile function.
        // return eg "jpeg"
        String fileMimeType = MimeTypeMap.getSingleton().getExtensionFromMimeType(cR.getType(fileUri));
        // should we be enforcing strict mimetypes? which types?
        File tempFile;
        File externalCacheDir = mContext.getExternalCacheDir();
        if (externalCacheDir == null) {
            Timber.e("createUI() unable to get external cache directory");
            return null;
        }
        File tempMediaDir = new File(externalCacheDir.getAbsolutePath() + "/temp-media");
        if (!tempMediaDir.exists() && !tempMediaDir.mkdir()) {
            Timber.e("temp-media dir did not exist and could not be created");
            return null;
        }
        try {
            tempFile = File.createTempFile(// the beginning of the filename.
            preferredName + "_", // this is the extension, if null, '.tmp' is used, need to get the extension from MIME type?
            "." + fileMimeType, tempMediaDir);
            tempFile.deleteOnExit();
        } catch (Exception e) {
            Timber.w(e, "Could not create temporary media file. ");
            return null;
        }
        FileUtil.internalizeUri(fileUri, tempFile, cR);
        String fname = media.addFile(tempFile);
        Timber.d("insert -> MEDIA: fname = %s", fname);
        File f = new File(fname);
        Timber.d("insert -> MEDIA: f = %s", f);
        Uri uriFromF = Uri.fromFile(f);
        Timber.d("insert -> MEDIA: uriFromF = %s", uriFromF);
        return Uri.fromFile(new File(fname));
    } catch (IOException | EmptyMediaException e) {
        Timber.w(e, "insert failed from %s", fileUri);
        return null;
    }
}
Also used : Media(com.ichi2.libanki.Media) IOException(java.io.IOException) Uri(android.net.Uri) File(java.io.File) EmptyMediaException(com.ichi2.libanki.exception.EmptyMediaException) JSONException(com.ichi2.utils.JSONException) DeckRenameException(com.ichi2.libanki.backend.exception.DeckRenameException) ConfirmModSchemaException(com.ichi2.anki.exception.ConfirmModSchemaException) IOException(java.io.IOException) ContentResolver(android.content.ContentResolver) EmptyMediaException(com.ichi2.libanki.exception.EmptyMediaException)

Example 5 with EmptyMediaException

use of com.ichi2.libanki.exception.EmptyMediaException in project Anki-Android by ankidroid.

the class MediaTest method testDeckIntegration.

@Test
public void testDeckIntegration() throws IOException, EmptyMediaException {
    // create a media dir
    mTestCol.getMedia().dir();
    // Put a file into it
    File file = createNonEmptyFile("fake.png");
    mTestCol.getMedia().addFile(file);
    // add a note which references it
    Note f = mTestCol.newNote();
    f.setField(0, "one");
    f.setField(1, "<img src='fake.png'>");
    mTestCol.addNote(f);
    // and one which references a non-existent file
    f = mTestCol.newNote();
    f.setField(0, "one");
    f.setField(1, "<img src='fake2.png'>");
    mTestCol.addNote(f);
    // and add another file which isn't used
    FileOutputStream os = new FileOutputStream(new File(mTestCol.getMedia().dir(), "foo.jpg"), false);
    os.write("test".getBytes());
    os.close();
    // check media
    List<List<String>> ret = mTestCol.getMedia().check();
    List<String> expected = Collections.singletonList("fake2.png");
    List<String> actual = ret.get(0);
    actual.retainAll(expected);
    assertEquals(expected.size(), actual.size());
    expected = Collections.singletonList("foo.jpg");
    actual = ret.get(1);
    actual.retainAll(expected);
    assertEquals(expected.size(), actual.size());
}
Also used : Note(com.ichi2.libanki.Note) FileOutputStream(java.io.FileOutputStream) List(java.util.List) File(java.io.File) Test(org.junit.Test) InstrumentedTest(com.ichi2.anki.tests.InstrumentedTest)

Aggregations

File (java.io.File)5 InstrumentedTest (com.ichi2.anki.tests.InstrumentedTest)4 Test (org.junit.Test)4 FileOutputStream (java.io.FileOutputStream)3 EmptyMediaException (com.ichi2.libanki.exception.EmptyMediaException)2 ContentResolver (android.content.ContentResolver)1 Uri (android.net.Uri)1 ConfirmModSchemaException (com.ichi2.anki.exception.ConfirmModSchemaException)1 Media (com.ichi2.libanki.Media)1 Note (com.ichi2.libanki.Note)1 DeckRenameException (com.ichi2.libanki.backend.exception.DeckRenameException)1 JSONException (com.ichi2.utils.JSONException)1 IOException (java.io.IOException)1 List (java.util.List)1