Created
September 18, 2019 12:26
-
-
Save chonz0/9ffd2861699675cbecf5ace06173e115 to your computer and use it in GitHub Desktop.
Copy attachments from specific Slack channel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "slack", | |
"version": "1.0.0", | |
"main": "index.js", | |
"license": "MIT", | |
"dependencies": { | |
"fs": "^0.0.1-security", | |
"glob": "^7.1.4" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Prerequisites: | |
// 1. Have exported your workspace data from Slack | |
// 2. Have run [Slack Advanced Exporter](https://github.com/grundleborg/slack-advanced-exporter) with attachment download | |
const fs = require('fs') | |
const glob = require("glob") | |
const options = {}; | |
const channelName = 'general'; | |
// options is optional | |
glob("../../Downloads/workspace-slack-attachments/"+channelName+"/*.json", options, function (er, files) { | |
// files is an array of filenames. | |
// If the `nonull` option is set, and nothing | |
// was found, then files is ["**/*.js"] | |
// er is an error object or null. | |
files.forEach(file => { | |
fs.readFile(file, 'utf8', (err, fileContents) => { | |
if (err) { | |
console.error(err) | |
return | |
} | |
try { | |
const messages = JSON.parse(fileContents) | |
if (messages && messages.length) { | |
messages.forEach(message => { | |
if (message && message.files) { | |
message.files.forEach(file => { | |
glob("../../Downloads/workspace-slack-attachments/__uploads/"+file.id+"/*.*", options, function (er, files) { | |
files.forEach(f => { | |
fs.copyFile(f, '../../Downloads/'+channelName+'/' + file.id + '_' + file.name, (err) => { | |
if (err) throw err; | |
console.log('Ok!'); | |
}); | |
}); | |
}); | |
}); | |
} | |
}) | |
} | |
} catch (err) { | |
console.error(err) | |
} | |
}) | |
}); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment