This function is now quite small, and the metadata argument is pretty specific to the format used in the preview gallery, so it is less useful as a library.  Should you just make it a function in the preview gallery controller?