Skip to content

Commit

Permalink
fix: handle exceptions during deletion of outdated recordings due to …
Browse files Browse the repository at this point in the history
…non-existent directories experience
  • Loading branch information
CerealAxis committed Apr 15, 2024
1 parent 113f406 commit 0c53dfc
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions src/main/java/cn/xor7/iseeyou/ISeeYou.kt
Original file line number Diff line number Diff line change
Expand Up @@ -213,25 +213,19 @@ class ISeeYou : JavaPlugin(), CommandExecutor {
private fun cleanOutdatedRecordings() {
try {
val recordPathA: String = toml!!.data.recordPath
val recordPathB: String = toml!!.data.recordSuspiciousPlayer.recordPath
val recordingsDirA = Paths.get(recordPathA).parent
val recordingsDirB = Paths.get(recordPathB).parent
val recordingsDirB: Path? = if (toml!!.data.recordSuspiciousPlayer.enableMatrixIntegration || toml!!.data.recordSuspiciousPlayer.enableThemisIntegration) {
Paths.get(toml!!.data.recordSuspiciousPlayer.recordPath).parent
} else {
null
}

logger.info("Start to delete outdated recordings in $recordingsDirA and $recordingsDirB")
var deletedCount = 0

Files.walk(recordingsDirA).use { paths ->
paths.filter { Files.isDirectory(it) && it.parent == recordingsDirA }
.forEach { folder ->
deletedCount += deleteRecordingFiles(folder)
}
}

Files.walk(recordingsDirB).use { paths ->
paths.filter { Files.isDirectory(it) && it.parent == recordingsDirB }
.forEach { folder ->
deletedCount += deleteRecordingFiles(folder)
}
deletedCount += deleteFilesInDirectory(recordingsDirA)
recordingsDirB?.let {
deletedCount += deleteFilesInDirectory(it)
}

logger.info("Finished deleting outdated recordings, deleted $deletedCount files")
Expand All @@ -241,6 +235,18 @@ class ISeeYou : JavaPlugin(), CommandExecutor {
}
}

private fun deleteFilesInDirectory(directory: Path): Int {
var count = 0
Files.walk(directory).use { paths ->
paths.filter { Files.isDirectory(it) && it.parent == directory }
.forEach { folder ->
count += deleteRecordingFiles(folder)
}
}
return count
}


private fun deleteRecordingFiles(folderPath: Path): Int {
var deletedCount = 0
var fileCount = 0
Expand Down

0 comments on commit 0c53dfc

Please sign in to comment.