Skip to content

Instantly share code, notes, and snippets.

@ozero
Last active March 12, 2018 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ozero/75a2c3005a272d0a5427acb5d19093b7 to your computer and use it in GitHub Desktop.
Save ozero/75a2c3005a272d0a5427acb5d19093b7 to your computer and use it in GitHub Desktop.
Delete old and big files on Slack.
<?php
$token ='******PASTE YOUR TOKEN HERE******';
$count = 100;
/*
Delete old and big files on Slack.
- 10MB over: can be stored up to 3 months
- 5MB over: can be stored up to 6 months
- 1MB over: can be stored up to 12months
ref:
Slackのファイルを一気に削除する - Qiita
https://qiita.com/9umaske/items/49bc1bd89330fe946ff3
*/
//query for file count
$res_init = queryForFiles($token, $count);
for($i = 1; $i <= $res_init['paging']['pages']; $i++){
//query for files with offset
$result = queryForFiles($token, $count, $i);
//delete files
processResponse($token, $result['files']);
}
print "done\n";
//---------------------
// Query slack.com/api/files.list
function queryForFiles($token, $count, $page = 1){
# API Reference:
# https://api.slack.com/methods/files.list
# Ex: Get files by channel ID
# $op ='token=' . $token . "&count=$count" . "&channel=ABCDEF";
$op ="token={$token}&count={$count}&page={$page}";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://slack.com/api/files.list?'.$op);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
$result = json_decode($response, true);
$n = $result['paging']['total'];
curl_close($curl);
print_r($result['paging']);
//throttling
sleep(1);
return $result;
}
// scan files on result and delete files.
function processResponse($token, $files){
$json_data = array('file' => 'XXX');
$i = 0;
$_time_now = time();
foreach ($files as $file) {
$sec_morethan3month = 60*60*24 * 30 * 3;
$sec_morethan6month = 60*60*24 * 30 * 6;
$sec_morethan12month = 60*60*24 * 30 * 12;
$_flag_time = "_";
$tmp_age = $_time_now - $file['created'];
if($tmp_age > $sec_morethan3month){
$_flag_time = "morethan3m";
if($tmp_age > $sec_morethan6month){
$_flag_time = "morethan6m";
if($tmp_age > $sec_morethan12month){
$_flag_time = "morethan12m";
}
}
}
$size_morethan1mb = 1024 * 1024 * 1;
$size_morethan5mb = 1024 * 1024 * 5;
$size_morethan10mb = 1024 * 1024 * 10;
$_flag_size = "_";
$file['size_cri'] = "";
if( intval($file['size']) > $size_morethan1mb){
$_flag_size = "over1MB";
$file['size_cri'] = $size_morethan1mb;
if(intval($file['size']) > $size_morethan5mb){
$_flag_size = "over5MB";
$file['size_cri'] = $size_morethan5mb;
if( intval($file['size']) > $size_morethan10mb){
$_flag_size = "over10MB";
$file['size_cri'] = $size_morethan10mb;
}
}
}
$_flag_del = 0;
//- 10MB over: up to 3 months
//- 5MB over: up to 6 months
//- 1MB over: up to 12months
if($_flag_time == "morethan3m"){
if($_flag_size == "over10MB"){
$_flag_del=10;
}
}else if($_flag_time == "morethan6m"){
if($_flag_size == "over10MB"){
$_flag_del=20;
}
if($_flag_size == "over5MB"){
$_flag_del=21;
}
}else if($_flag_time == "morethan12m"){
if($_flag_size == "over10MB"){
$_flag_del=30;
}
if($_flag_size == "over5MB"){
$_flag_del=31;
}
if($_flag_size == "over1MB"){
$_flag_del=32;
}
}
if($_flag_del == 0){
continue;
}
$tmp=[
'ftime'=>$_flag_time,
'fsize'=>$_flag_size,
'del'=>$_flag_del,
'id'=>$file['id'],
'name'=>$file['name'],
'type'=>$file['filetype'],
'size'=>$file['size'],
'size_c'=>$file['size_cri'],
'size(MB)'=>$file['size']/(1024*1024),
'created'=>date('Y/m/d',$file['created'])
];
print_r($tmp);
//
print "PLS comment out Line 151.\n\n";
continue;
//
echo "Process: " . $file['id'].":".$file['name'];
$json_data['file'] = $file['id'];
$data = json_encode($json_data);
$ret = postFromHTTP('https://slack.com/api/files.delete',$data,$token);
if($ret['ok']){
echo ' -> Deleted!';
}else {
echo ' -> ERROR.';
}
echo "\n";
$i++;
}
}
//execute file delete via slack file api
function postFromHTTP($url, $data, $token) {
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_AUTOREFERER => true,
);
$authorization = "Authorization: Bearer ".$token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array('Content-Type: application/json; charset=utf-8' , $authorization ));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
curl_close($ch);
$ret = json_decode($result, true);
//Throttling
sleep(1);
return $ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment