MediaLibrary: added cleanup behaviour, support for disc numbers when importing media.

This commit is contained in:
Jonathan Bernard 2016-03-12 21:17:17 -06:00
parent 400d3d1774
commit cf8aad8775

View File

@ -3,6 +3,7 @@ package com.jdbernard.wdiwtlt
import com.jdbernard.wdiwtlt.db.ORM
import com.jdbernard.wdiwtlt.db.models.*
import java.sql.Timestamp
import java.util.regex.Pattern
import org.apache.commons.codec.digest.DigestUtils
@ -22,6 +23,8 @@ public class MediaLibrary {
@Delegate ORM orm
private File libraryRoot
public long autoDeletePeriod = 1000 * 60 * 60 * 24 * 7 // one week
public MediaLibrary(ORM orm, File rootDir) {
logger.debug("Creating a MediaLibrary rooted at: {}",
rootDir.canonicalPath)
@ -33,7 +36,16 @@ public class MediaLibrary {
orm.removeEmptyAlbums()
orm.removeEmptyArtists()
orm.removeEmptyPlaylists()
}
Timestamp staleTS = new Timestamp(new Date().time - autoDeletePeriod)
orm.withTransaction {
def stalePlaylists = orm.getPlaylistsWhere(
userCreated: false, lastUsedBefore: staleTS)
stalePlaylists.each { orm.delete(it) } }
orm.withTransaction {
def staleBookmarks = orm.getBookmarksWhere(
userCreated: false, lastUsedBefore: staleTS)
staleBookmarks.each { orm.delete(it) } } }
public def rescanLibrary() {
def results = [ total: 0, ignored: 0, new: 0]
@ -78,8 +90,8 @@ public class MediaLibrary {
mf.name = fileTag?.getFirst(TITLE)?.trim() ?: f.name
mf.filePath = relPath
mf.comment = fileTag?.getAll(COMMENT)?.collect { it.trim() }.join('\n\n')
mf.discNumber = safeToInteger(fileTag?.getFirst(DISC_NO))
mf.comment = fileTag?.getAll(COMMENT)?.collect { it.trim() }?.join('\n\n')
mf.discNumber = safeToInteger(fileTag?.getFirst(DISC_NO)) ?: 1
mf.trackNumber = safeToInteger(fileTag?.getFirst(TRACK))
def folderParts = mf.filePath.split("[\\\\/]")[1..<-1] as LinkedList
@ -102,8 +114,7 @@ public class MediaLibrary {
orm.withTransaction {
orm.create(mf)
associateWithArtistsAndAlbums(mf, artistNames, albumNames,
safeToInteger(fileTag.getFirst(YEAR)))
}
safeToInteger(fileTag?.getFirst(YEAR))) }
return mf
}
@ -197,8 +208,7 @@ public class MediaLibrary {
artists.each { artist ->
def albumsForArtist = orm.getAlbumsWhere(artistId: artist.id)
def albumsMissing = albums - albumsForArtist
albumsMissing.each { album -> orm.associate(artist, album) } }
}
albumsMissing.each { album -> orm.associate(artist, album) } } }
/** #### `getRelativePath`
* Given a parent path and a child path, assuming the child path is