'Allows the downloading of metadata files associated with datasets (eg. bai index for bam files)'

@web.expose
def get_metadata_file(self, trans, hda_id, metadata_name):

    data = trans.sa_session.query(trans.app.model.HistoryDatasetAssociation).get(self.decode_id(hda_id))
    if ((not data) or (not self._can_access_dataset(trans, data))):
        return trans.show_error_message('You    are    not    allowed    to    access    this    dataset')
    fname = ''.join(((((c in util.FILENAME_VALID_CHARS) and c) or '_') for c in data.name))[0:150]
    file_ext = data.metadata.spec.get(metadata_name).get('file_ext', metadata_name)
    trans.response.headers['Content-Type'] = 'application/octet-stream'
    trans.response.headers['Content-Disposition'] = ('attachment;    filename="Galaxy%s-[%s].%s"' % (data.hid, fname, file_ext))
    return open(data.metadata.get(metadata_name).file_name)
