Skip to content

Commit

Permalink
Cleanup parent folders when deleting downloaded chapters (#776)
Browse files Browse the repository at this point in the history
Currently, the download folder never gets cleaned up which leads to having a lot of empty folders (sources, mangas folders)
  • Loading branch information
schroda authored Nov 20, 2023
1 parent 50cd0c4 commit 909bd76
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,21 @@ import org.apache.commons.compress.archivers.zip.ZipArchiveEntry
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream
import org.apache.commons.compress.archivers.zip.ZipFile
import org.kodein.di.DI
import org.kodein.di.conf.global
import org.kodein.di.instance
import suwayomi.tachidesk.manga.impl.download.fileProvider.ChaptersFilesProvider
import suwayomi.tachidesk.manga.impl.download.model.DownloadChapter
import suwayomi.tachidesk.manga.impl.util.getChapterCachePath
import suwayomi.tachidesk.manga.impl.util.getChapterCbzPath
import suwayomi.tachidesk.manga.impl.util.getMangaDownloadDir
import suwayomi.tachidesk.manga.impl.util.storage.FileDeletionHelper
import suwayomi.tachidesk.server.ApplicationDirs
import java.io.File
import java.io.InputStream

private val applicationDirs by DI.global.instance<ApplicationDirs>()

class ArchiveProvider(mangaId: Int, chapterId: Int) : ChaptersFilesProvider(mangaId, chapterId) {
override fun getImageImpl(index: Int): Pair<InputStream, String> {
val cbzPath = getChapterCbzPath(mangaId, chapterId)
Expand Down Expand Up @@ -67,8 +74,13 @@ class ArchiveProvider(mangaId: Int, chapterId: Int) : ChaptersFilesProvider(mang

override fun delete(): Boolean {
val cbzFile = File(getChapterCbzPath(mangaId, chapterId))
if (cbzFile.exists()) return cbzFile.delete()
return false
if (!cbzFile.exists()) {
return true
}

val cbzDeleted = cbzFile.delete()
FileDeletionHelper.cleanupParentFoldersFor(cbzFile, applicationDirs.mangaDownloadsRoot)
return cbzDeleted
}

private fun handleExistingCbzFile(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package suwayomi.tachidesk.manga.impl.download.fileProvider.impl

import kotlinx.coroutines.CoroutineScope
import org.kodein.di.DI
import org.kodein.di.conf.global
import org.kodein.di.instance
import suwayomi.tachidesk.manga.impl.download.fileProvider.ChaptersFilesProvider
import suwayomi.tachidesk.manga.impl.download.model.DownloadChapter
import suwayomi.tachidesk.manga.impl.util.getChapterCachePath
import suwayomi.tachidesk.manga.impl.util.getChapterDownloadPath
import suwayomi.tachidesk.manga.impl.util.storage.FileDeletionHelper
import suwayomi.tachidesk.server.ApplicationDirs
import java.io.File
import java.io.FileInputStream
import java.io.InputStream

private val applicationDirs by DI.global.instance<ApplicationDirs>()

/*
* Provides downloaded files when pages were downloaded into folders
* */
Expand Down Expand Up @@ -42,18 +49,14 @@ class FolderProvider(mangaId: Int, chapterId: Int) : ChaptersFilesProvider(manga
}

override fun delete(): Boolean {
val chapterDir = getChapterDownloadPath(mangaId, chapterId)
return File(chapterDir).deleteRecursively()
}
val chapterDirPath = getChapterDownloadPath(mangaId, chapterId)
val chapterDir = File(chapterDirPath)
if (!chapterDir.exists()) {
return true
}

private fun isExistingFile(
folder: File,
fileName: String,
): Boolean {
val existingFile =
folder.listFiles { file ->
file.isFile && file.name.startsWith(fileName)
}?.firstOrNull()
return existingFile?.exists() == true
val chapterDirDeleted = chapterDir.deleteRecursively()
FileDeletionHelper.cleanupParentFoldersFor(chapterDir, applicationDirs.mangaDownloadsRoot)
return chapterDirDeleted
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package suwayomi.tachidesk.manga.impl.util.storage

import java.io.File

object FileDeletionHelper {
/**
* Recursively deletes all parent folders for the given deleted file until the parent folder is not empty, or it's the root folder
*/
fun cleanupParentFoldersFor(
file: File,
rootPath: String,
) {
val folder = file.parentFile
if (!folder.isDirectory) {
return
}

if (folder.absolutePath == rootPath) {
return
}

if (folder.listFiles()?.isEmpty() != true) {
return
}

folder.delete()
cleanupParentFoldersFor(folder, rootPath)
}
}

0 comments on commit 909bd76

Please sign in to comment.