Created
November 3, 2022 12:14
-
-
Save aka-apri/2b03bd00b6fcbd7bdea46abfa3bad635 to your computer and use it in GitHub Desktop.
primedbfix routine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.akworks.prime.primedbfix.routines.v2 | |
import net.akworks.prime.primedbfix.config.appProps | |
import net.akworks.prime.primedbfix.db.v2.Playlist | |
import net.akworks.prime.primedbfix.db.v2.PlaylistEntity | |
import net.akworks.prime.primedbfix.db.v2.PlaylistPath | |
import net.akworks.prime.primedbfix.db.v2.Track | |
import net.akworks.prime.primedbfix.routines.UnitPrimeRoutine | |
import net.akworks.prime.primedbfix.routines.databaseUuid | |
import net.akworks.prime.primedbfix.routines.playlistPathToFilePath | |
import net.akworks.prime.primedbfix.routines.primeDbConnect | |
import org.jetbrains.exposed.sql.and | |
import org.jetbrains.exposed.sql.select | |
import org.jetbrains.exposed.sql.transactions.transaction | |
import java.io.File | |
import java.nio.file.Files | |
import java.nio.file.Paths | |
import java.nio.file.StandardCopyOption | |
class PlaylistMoveFilesRoutine( | |
val playlistPath: String | |
) : UnitPrimeRoutine { | |
override fun doWork() { | |
val destDir = File( | |
listOf( | |
appProps.musicCollectionHome, | |
"WORK", | |
playlistPath.playlistPathToFilePath() | |
).joinToString(appProps.fileSep) | |
) | |
println("Destination DIR: ${destDir.absolutePath}") | |
if (!destDir.exists()) { | |
destDir.mkdirs() | |
} | |
if (!destDir.canWrite()) { | |
throw IllegalStateException("Can't write to dest dir ${destDir.absolutePath}") | |
} | |
primeDbConnect(false) | |
transaction { | |
val dbUuid = databaseUuid() | |
val persPlaylists = | |
Playlist.select { Playlist.isPersisted.eq(true) }.filter { it[Playlist.title] != appProps.titleDeleted } | |
.map { it[Playlist.id] } | |
val playlistId = PlaylistPath.select { | |
PlaylistPath.path eq playlistPath | |
} | |
.filter { it[PlaylistPath.id] in persPlaylists }.map { it[PlaylistPath.id] } | |
.apply { | |
if (size != 1) { | |
println("Invalid playlistId for path: $playlistPath") | |
} | |
} | |
.first() | |
val trackIds = | |
PlaylistEntity.select { PlaylistEntity.listId.eq(playlistId) and PlaylistEntity.databaseUuid.eq(dbUuid) } | |
.map { it[PlaylistEntity.trackId] } | |
val tracks = Track.select { Track.id inList trackIds } | |
val files = tracks.map { | |
val path = it[Track.path].orEmpty() | |
appProps.pathToLibFile(path) | |
} | |
val dirMap = files.groupBy { it.parentFile.path } | |
dirMap.keys.forEach { | |
println("Src Dir: $it") | |
println("Files:") | |
dirMap[it]?.forEach { | |
println("${if (it.exists()) "OK" else "NA"}. ${it.path}") | |
} | |
} | |
val moveFiles = files.filter { it.exists() && it.parentFile.path != destDir.path }.map { | |
it to File(destDir.path + File.separator + it.name) | |
} | |
moveFiles.forEach { | |
println("srcFile: ${it.first.path}") | |
println("destFile: ${it.second.path}") | |
val src = Paths.get(it.first.toURI()) | |
val dest = Paths.get(it.second.toURI()) | |
Files.move(src, dest, StandardCopyOption.REPLACE_EXISTING) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment