Skip to content

Instantly share code, notes, and snippets.

@maxmpz
Created January 4, 2024 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxmpz/6e83a36e16c3fd4fd7cfe1d5cd440f29 to your computer and use it in GitHub Desktop.
Save maxmpz/6e83a36e16c3fd4fd7cfe1d5cd440f29 to your computer and use it in GitHub Desktop.
/**
* For given track/album/etc. parameters or just artist or just title, build a filename to store AA:<br></br>
* - for streams return path<br></br>
* - for just title, returns getNameWithoutNumber for path if > 10 (thus, title is ignored)<br></br>
* - if we have title + artists, returns "artist - title"<br></br>
* - if we have just artist, returns "artist"<br></br>
* - if we have just album, return "album"<br></br>
* - otherwise returns "artist - album"<br></br>
* @param artistOrName either artist name or any entity name as needed. This may be the album name for Unknown album (album=null in this case)
* @param fileType used to check if this is stream
* @param sb should be set to zero length and should be returned with sanitization
* @return sanitized base filename, or null on failures
*/
// THREADING: any
fun buildAAFileName(
title: CharSequence?, album: CharSequence?, artistOrName: CharSequence?, path: String?,
fileType: Int, sb: StringBuilder
): StringBuilder? {
if(LOG) Log.w(TAG, "buildAAFileName title=$title album=$album artistOrName=$artistOrName path=$path fileType=$fileType")
sb.setLength(0)
if(isStream(fileType)) { // Streams always use own art name approach (sanitized uri)
if(aa_per_stream_track && !title.isNullOrEmpty() && !artistOrName.isNullOrEmpty()) {
// Use track based logic
} else {
if(path.isNullOrEmpty()) {
if(LOG) Log.e(TAG, "buildAAFileName FAIL isStream !path")
return null
}
sb.append(path)
if(LOG) Log.w(TAG, "buildAAFileName isStream =>$sb")
return PathUtils.sanitizeFilenameMutate(sb)
}
}
val albumEmpty = album.isNullOrEmpty()
val artistOrNameEmpty = artistOrName.isNullOrEmpty()
val titleEmpty = title.isNullOrEmpty()
if(albumEmpty && artistOrNameEmpty) {
if(titleEmpty && path.isNullOrEmpty()) {
if(LOG) Log.e(TAG, "buildAAFileName FAIL albumEmpty artistOrNameEmpty titleEmpty path is empty")
return null
}
// Try to extract larger name from path as we have nothing in album/artist
// NOTE: title is ignored here. Keeping it this way for legacy
val pathWithoutNumber = PathUtils.getNameWithoutNumber(path)
if(!pathWithoutNumber.isNullOrEmpty()) {
sb.append(title)
return PathUtils.sanitizeFilenameMutate(sb)
}
// We may have path made just from numbers. In this case use it as is. It's guaranteed to be non-empty
sb.append(path)
return PathUtils.sanitizeFilenameMutate(sb)
} else if(albumEmpty) {
// artistEmpty=true can't be here
if(titleEmpty) { // We have just artist. It's pointless to add " - " to just artist
sb.append(artistOrName)
return PathUtils.sanitizeFilenameMutate(sb)
}
sb.append(artistOrName).append(" - ").append(title) // REVISIT: title may be empty
return PathUtils.sanitizeFilenameMutate(sb)
} else if(artistOrNameEmpty) {
sb.append(album)
return PathUtils.sanitizeFilenameMutate(sb)
}
sb.append(artistOrName).append(" - ").append(album) // Album is not empty - checked above
return PathUtils.sanitizeFilenameMutate(sb)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment