Mattermost – Compliance export with files?

If you use Mattermost Enterprise Edition (E20) you might want to use the “Compliance Export” feature. With a simple scheduler or an ad-hoc task in the System Console this can be achieved in seconds. This week I had a customer asking: “We exported necessary but miss all the uploaded files, there’s just a FileID in the report! How can we download the files as well?”.

I asked our internal support and Martin (Thanks to Martin Kraft!) pointed me in the right direction. He mentioned that the API might be an option. So I checked the API and wrote a litte script which can also be found here.

 

import requests
import csv

#Login using: curl -i -d '{"login_id":"EMAIL","password":"PASSWORD"}' https://mattermost-server.com/api/v4/users/login
#Copy the token

auth_token = raw_input("Please enter your auth token: ")
posts = raw_input("Please enter the filename to parse: ")
hed = {'Authorization': 'Bearer ' + auth_token}

def read_csv():
    reader_m = csv.reader(open(posts),delimiter=',')
    for row_m in reader_m:
        if len(row_m[19]) > 11:
            fileid = row_m[19]
            id = fileid[2:28]
            get_file(id)

def get_file(id):
    info_url = 'https://mattermost-server.com/api/v4/files/' + id + '/info'
    response = requests.get(info_url, headers=hed)
    info = response.json()
    filename = info["name"]

    file_url = 'https://mattermost-server.com/api/v4/files/' + id
    response = requests.get(file_url, headers=hed)
    open(filename, 'wb').write(response.content)

read_csv()

 

This script asks for the token which can be created using cUrl and the CSV Compliance Export file. This can be changed into something more elegant easily but shows an option to quickly achieve this goal.

Leave a Reply