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));
}
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
}
}
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());
}
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;
}
}
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());
}
Aggregations