Ported code by @onli to emit nested media db directories. Yay.