Implement deletion of source image
This commit is contained in:
@@ -180,6 +180,7 @@ class ImageRepository(
|
|||||||
|
|
||||||
fun delete(id: String) {
|
fun delete(id: String) {
|
||||||
File(scanDir, id).delete()
|
File(scanDir, id).delete()
|
||||||
|
File(sourceDir, id).delete()
|
||||||
getThumbnailFile(id).delete()
|
getThumbnailFile(id).delete()
|
||||||
pages.removeAll { it.file == id }
|
pages.removeAll { it.file == id }
|
||||||
saveMetadata()
|
saveMetadata()
|
||||||
|
|||||||
@@ -76,9 +76,13 @@ class ImageRepositoryTest {
|
|||||||
val repo = repo()
|
val repo = repo()
|
||||||
val bytes = byteArrayOf(101, 102, 103)
|
val bytes = byteArrayOf(101, 102, 103)
|
||||||
repo.add(bytes, byteArrayOf(51), metadata1)
|
repo.add(bytes, byteArrayOf(51), metadata1)
|
||||||
|
assertThat(jpegFiles(scanDir())).hasSize(1)
|
||||||
|
assertThat(jpegFiles(sourceDir())).hasSize(1)
|
||||||
assertThat(repo.imageIds()).hasSize(1)
|
assertThat(repo.imageIds()).hasSize(1)
|
||||||
repo.delete(repo.imageIds()[0])
|
repo.delete(repo.imageIds()[0])
|
||||||
assertThat(repo.imageIds()).isEmpty()
|
assertThat(repo.imageIds()).isEmpty()
|
||||||
|
assertThat(jpegFiles(scanDir())).hasSize(0)
|
||||||
|
assertThat(jpegFiles(sourceDir())).hasSize(0)
|
||||||
val repo2 = repo()
|
val repo2 = repo()
|
||||||
assertThat(repo2.imageIds()).isEmpty()
|
assertThat(repo2.imageIds()).isEmpty()
|
||||||
}
|
}
|
||||||
@@ -92,19 +96,17 @@ class ImageRepositoryTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `should find existing files at initialization with no json`() {
|
fun `should find existing files at initialization with no json`() {
|
||||||
val scanDir = File(getFilesDir(), SCAN_DIR_NAME)
|
scanDir().mkdirs()
|
||||||
scanDir.mkdirs()
|
File(scanDir(), "1.jpg").writeBytes(byteArrayOf(101, 102, 103))
|
||||||
File(scanDir, "1.jpg").writeBytes(byteArrayOf(101, 102, 103))
|
|
||||||
assertThat(repo().imageIds()).containsExactly("1.jpg")
|
assertThat(repo().imageIds()).containsExactly("1.jpg")
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `should filter pages in json at initialization`() {
|
fun `should filter pages in json at initialization`() {
|
||||||
val scanDir = File(getFilesDir(), SCAN_DIR_NAME)
|
scanDir().mkdirs()
|
||||||
scanDir.mkdirs()
|
|
||||||
val json = """{"pages":[{"file":"1.jpg"}, {"file":"2.jpg"}]}"""
|
val json = """{"pages":[{"file":"1.jpg"}, {"file":"2.jpg"}]}"""
|
||||||
File(scanDir, "document.json").writeText(json)
|
File(scanDir(), "document.json").writeText(json)
|
||||||
File(scanDir, "2.jpg").writeBytes(byteArrayOf(101, 102, 103))
|
File(scanDir(), "2.jpg").writeBytes(byteArrayOf(101, 102, 103))
|
||||||
assertThat(repo().imageIds()).containsExactly("2.jpg")
|
assertThat(repo().imageIds()).containsExactly("2.jpg")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,12 +125,9 @@ class ImageRepositoryTest {
|
|||||||
assertThat(repo1.imageIds()).isNotEmpty()
|
assertThat(repo1.imageIds()).isNotEmpty()
|
||||||
repo1.clear()
|
repo1.clear()
|
||||||
assertThat(repo1.imageIds()).isEmpty()
|
assertThat(repo1.imageIds()).isEmpty()
|
||||||
assertThat(File(getFilesDir(), SCAN_DIR_NAME)
|
assertThat(jpegFiles(scanDir())).isEmpty()
|
||||||
.listFiles { f -> f.name.endsWith(".jpg") })
|
assertThat(jpegFiles(sourceDir())).isEmpty()
|
||||||
.isEmpty()
|
assertThat(jpegFiles(File(getFilesDir(), THUMBNAIL_DIR_NAME))).isEmpty()
|
||||||
assertThat(File(getFilesDir(), THUMBNAIL_DIR_NAME)
|
|
||||||
.listFiles { f -> f.name.endsWith(".jpg") })
|
|
||||||
.isEmpty()
|
|
||||||
val repo2 = repo()
|
val repo2 = repo()
|
||||||
assertThat(repo2.imageIds()).isEmpty()
|
assertThat(repo2.imageIds()).isEmpty()
|
||||||
}
|
}
|
||||||
@@ -189,4 +188,10 @@ class ImageRepositoryTest {
|
|||||||
assertThat(metadata!!.isColored).isEqualTo(isColored)
|
assertThat(metadata!!.isColored).isEqualTo(isColored)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun scanDir(): File = File(getFilesDir(), SCAN_DIR_NAME)
|
||||||
|
private fun sourceDir(): File = File(getFilesDir(), SOURCE_DIR_NAME)
|
||||||
|
|
||||||
|
private fun jpegFiles(dir: File): Array<out File?>?
|
||||||
|
= dir.listFiles { f -> f.name.endsWith(".jpg") }
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user