Merge pull request #1695 from gokadzev/tagger_fix

Refactor tag editor code and fix related issues
This commit is contained in:
Thomas Möller 2024-12-10 17:00:48 +01:00 committed by GitHub
commit 5586f94d4f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -74,9 +74,16 @@ class TagWriter {
val audioFile = AudioFileIO.read(File(filePath))
val tag = audioFile.tagOrCreateAndSetDefault
if (info.fieldKeyValueMap != null) {
for ((key, value) in info.fieldKeyValueMap) {
for ((key, newValue) in info.fieldKeyValueMap) {
try {
tag.setField(key, value)
val currentValue = tag.getFirst(key)
if (currentValue != newValue) {
if (newValue.isEmpty()) {
tag.deleteField(key)
} else {
tag.setField(key, newValue)
}
}
} catch (e: FieldDataInvalidException) {
withContext(Dispatchers.Main) {
context.showToast(R.string.could_not_write_tags_to_file)
@ -127,6 +134,7 @@ class TagWriter {
val cacheFiles = mutableListOf<File>()
var artwork: Artwork? = null
var albumArtFile: File? = null
if (info.artworkInfo?.artwork != null) {
try {
albumArtFile = createAlbumArtFile(context).canonicalFile
@ -140,24 +148,36 @@ class TagWriter {
e.printStackTrace()
}
}
var wroteArtwork = false
var deletedArtwork = false
for (filePath in info.filePaths!!) {
try {
val originFile = File(filePath)
val cacheFile = File(context.cacheDir, originFile.name)
cacheFiles.add(cacheFile)
originFile.inputStream().use { input ->
cacheFile.outputStream().use { output ->
input.copyTo(output)
}
}
val audioFile = AudioFileIO.read(cacheFile)
val tag = audioFile.tagOrCreateAndSetDefault
if (info.fieldKeyValueMap != null) {
for ((key, value) in info.fieldKeyValueMap) {
for ((key, newValue) in info.fieldKeyValueMap) {
try {
tag.setField(key, value)
val currentValue = tag.getFirst(key)
if (currentValue != newValue) {
if (newValue.isEmpty()) {
tag.deleteField(key)
} else {
tag.setField(key, newValue)
}
}
} catch (e: FieldDataInvalidException) {
withContext(Dispatchers.Main) {
context.showToast(R.string.could_not_write_tags_to_file)