musikr: reformat

This commit is contained in:
Alexander Capehart 2025-02-25 16:13:55 -07:00
parent 584af83a07
commit b388474655
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47

View file

@ -90,7 +90,7 @@ private class ExtractStepImpl(
} }
.flattenMerge() .flattenMerge()
.buffer(Channel.UNLIMITED) .buffer(Channel.UNLIMITED)
// Divert cache hits and misses // Divert cache hits and misses
val cacheFlow = val cacheFlow =
cacheResults.divert { cacheResults.divert {
@ -99,50 +99,47 @@ private class ExtractStepImpl(
is CacheResult.Miss -> Divert.Right(it.file) is CacheResult.Miss -> Divert.Right(it.file)
} }
} }
// Cache hits can be directly converted to valid songs // Cache hits can be directly converted to valid songs
val cachedSongs = cacheFlow.left.map { ExtractedMusic.Valid.Song(it) } val cachedSongs = cacheFlow.left.map { ExtractedMusic.Valid.Song(it) }
// Process uncached files in parallel // Process uncached files in parallel
val uncachedFiles = cacheFlow.right val uncachedFiles = cacheFlow.right
val processingDistributedFlow = uncachedFiles.distribute(8) val processingDistributedFlow = uncachedFiles.distribute(8)
// Process each uncached file in parallel flows // Process each uncached file in parallel flows
val processedSongs = val processedSongs =
processingDistributedFlow.flows processingDistributedFlow.flows
.map { flow -> .map { flow ->
flow flow
.mapNotNull { file -> .mapNotNull { file ->
wrap(file) { f -> wrap(file) { f ->
// Open file descriptor withContext(Dispatchers.IO) {
val fd = withContext(Dispatchers.IO) { context.contentResolver.openFileDescriptor(f.uri, "r")
context.contentResolver.openFileDescriptor(f.uri, "r") }
} ?: return@wrap null ?.use {
val extractedMetadata = metadataExtractor.extract(f, fd)
try {
// Extract metadata if (extractedMetadata != null) {
val extractedMetadata = metadataExtractor.extract(f, fd) val tags = tagParser.parse(extractedMetadata)
val cover =
if (extractedMetadata != null) { extractedMetadata.cover?.let {
// Parse tags storedCovers.write(it)
val tags = tagParser.parse(extractedMetadata) }
val rawSong =
// Store cover if present RawSong(
val cover = extractedMetadata.cover?.let { f,
storedCovers.write(it) extractedMetadata.properties,
} tags,
cover,
// Create and write the raw song to cache addingMs)
val rawSong = RawSong(f, extractedMetadata.properties, tags, cover, addingMs) cache.write(rawSong)
wrap(rawSong, cache::write)
ExtractedMusic.Valid.Song(rawSong)
ExtractedMusic.Valid.Song(rawSong) } else {
} else { ExtractedMusic.Invalid
ExtractedMusic.Invalid }
} }
} finally {
withContext(Dispatchers.IO) { fd.close() }
}
} }
} }
.flowOn(Dispatchers.IO) .flowOn(Dispatchers.IO)
@ -150,16 +147,17 @@ private class ExtractStepImpl(
} }
.flattenMerge() .flattenMerge()
.buffer(Channel.UNLIMITED) .buffer(Channel.UNLIMITED)
// Separate valid processed songs from invalid ones // Separate valid processed songs from invalid ones
val processedFlow = processedSongs.divert { val processedFlow =
when (it) { processedSongs.divert {
is ExtractedMusic.Valid.Song -> Divert.Left(it) when (it) {
is ExtractedMusic.Invalid -> Divert.Right(it) is ExtractedMusic.Valid.Song -> Divert.Left(it)
else -> Divert.Right(ExtractedMusic.Invalid) is ExtractedMusic.Invalid -> Divert.Right(it)
else -> Divert.Right(ExtractedMusic.Invalid)
}
} }
}
val processedValidSongs = processedFlow.left val processedValidSongs = processedFlow.left
val invalidSongs = processedFlow.right val invalidSongs = processedFlow.right