Skip to content

Instantly share code, notes, and snippets.

@icelander
Last active January 5, 2022 21:08
Show Gist options
  • Save icelander/9e638f897e6df4b2b4a31b0bc7b5d38c to your computer and use it in GitHub Desktop.
Save icelander/9e638f897e6df4b2b4a31b0bc7b5d38c to your computer and use it in GitHub Desktop.
Removes attachments and emoji from Mattermost bulk export file
#!/usr/bin/env ruby
require 'json'
## How to use
#
# 1. Generate a Mattermost bulk export file with attachments
# 2. Unzip the file and find the .jsonl file with the post contents and put its filename on the next line
filename = 'import.jsonl'
# 3. Run it and direct the output to a new file, e.g.
#
# ruby remove-attachments.rb > import_no_attachments.jsonl
posts = []
emoji = []
other = []
File.readlines(filename).each do |line|
data = JSON.parse(line)
if data['type'] == 'post' or data['type'] == 'direct_post'
$stderr.puts "Found #{data['type']}"
type = data['type']
if data[type].key?('attachments')
$stderr.puts "#{type} has an attachments key "
begin
if !data[type]['attachments'].nil? and data[type]['attachments'].length > 0
$stderr.puts "#{type} has #{data[type]['attachments'].length} attachment(s). Resetting"
data[type]['attachments'] = []
end
rescue Exception => e
$stderr.puts e.to_s
$stderr.puts data[type].to_json
end
end
posts << data.to_json
else
other << line
end
end
other.each do |line|
puts line
end
posts.each do |post|
puts post
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment