delete old files

This commit is contained in:
Kieran 2019-04-04 21:40:13 +08:00
parent 0f96b26f31
commit 6a5395f244
5 changed files with 80 additions and 5 deletions

View File

@ -7,7 +7,7 @@
public static function LoadHeader($path) : ?BlobFile { public static function LoadHeader($path) : ?BlobFile {
$input = fopen($path, "rb"); $input = fopen($path, "rb");
$version = ord(fread($input, 1)); $version = ord(fread($input, 1));
error_log($version); //error_log($version);
$bf = new BlobFile(); $bf = new BlobFile();
if($version == 1) { if($version == 1) {
@ -24,7 +24,7 @@
$header_data = unpack("H14magic/Vuploaded", $header); $header_data = unpack("H14magic/Vuploaded", $header);
fclose($input); fclose($input);
error_log("Magic is: " . $header_data["magic"]); //error_log("Magic is: " . $header_data["magic"]);
if($header_data["magic"] == "4f4944f09f90b1") { //OID🐱 as hex (UTF-8) if($header_data["magic"] == "4f4944f09f90b1") { //OID🐱 as hex (UTF-8)
$bf->Version = 2; $bf->Version = 2;
$bf->Uploaded = $header_data["uploaded"]; $bf->Uploaded = $header_data["uploaded"];

View File

@ -4,11 +4,58 @@
if(StaticRedis::Connect()) { if(StaticRedis::Connect()) {
echo "Connected to redis..\n"; echo "Connected to redis..\n";
Config::LoadConfig(array("upload_folder")); Config::LoadConfig(array("upload_folder", "discord_webhook_pub"));
$fs = new FileStore(Config::$Instance->upload_folder, $_SERVER["cron_root"]);
//delete expired files
$pmsg = "`" . gethostname() . ":\n";
$redis = StaticRedis::ReadOp();
$deleted = false;
foreach($fs->ListFiles() as $file) {
$id = basename($file);
$file_key = REDIS_PREFIX . $id;
$lv = $redis->hGet($file_key, "lastview");
$expire = time() - (30 * 24 * 60 * 60);
//use the file upload timestamp if there is no view data recorded
//if the file upload time is greater than the current timestamp, mark as old (!!abuse!!)
//this will also force legacy file uploads with no views to be deleted (header will always fail to load)
if($lv === false) {
$file_header = BlobFile::LoadHeader($file);
if($file_header !== null && $file_header->Uploaded <= time()){
$lv = $file_header->Uploaded;
} else {
//cant read file header or upload timestamp is invalid, mark as old
$lv = 0;
}
}
if($lv !== false && intval($lv) < $expire) {
$nmsg = "Deleting expired file: " . $id . " (lastview=" . date("Y-m-d h:i:s", intval($lv)) . ")\n";
if(strlen($pmsg) + strlen($nmsg) >= 2000){
//send to discord public hook
$pmsg = $pmsg . "`";
Discord::SendPublic(array(
"content" => $pmsg
));
$pmsg = "`";
}
$pmsg = $pmsg . $nmsg;
unlink($file);
$deleted = true;
}
}
//send last message if any
if(strlen($pmsg) > 0 && $deleted) {
$pmsg = $pmsg . "`";
Discord::SendPublic(array(
"content" => $pmsg
));
}
if(StaticRedis::$IsConnectedToSlave == False) { if(StaticRedis::$IsConnectedToSlave == False) {
echo "Runing master node tasks..\n"; echo "Runing master node tasks..\n";
$fs = new FileStore(Config::$Instance->upload_folder, $_SERVER["cron_root"]);
Stats::Collect($fs); Stats::Collect($fs);
} }
} }

26
src/php/discord.php Normal file
View File

@ -0,0 +1,26 @@
<?php
class Discord {
public static function SendPublic($msg) : void {
self::CallWebhook(Config::$Instance->discord_webhook_pub, $msg);
}
private static function CallWebhook($url, $data) : void {
self::CurlPost($url, json_encode($data));
}
private static function CurlPost($url, $data) : ?string {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}
?>

View File

@ -73,7 +73,7 @@ static int curl_upload_read(void *ptr, size_t size, size_t nmemb, void *stream)
VBFPayloadHeader h; VBFPayloadHeader h;
h.len = flen; h.len = flen;
h.mime = "application/x-msdownload"; h.mime = DEFAULT_MIME;
h.filename = state->filename; h.filename = state->filename;
state->bo->len = state->bi->len -= ENC_ALGO::BLOCKSIZE - sizeof(VBFHeader); //reduce by 1 block to allow space for header state->bo->len = state->bi->len -= ENC_ALGO::BLOCKSIZE - sizeof(VBFHeader); //reduce by 1 block to allow space for header

View File

@ -5,6 +5,8 @@
#include <argtable2.h> #include <argtable2.h>
#define CLI_VERSION "void_util v0.1" #define CLI_VERSION "void_util v0.1"
#define DEFAULT_MIME "application/octet-stream"
#define DEFAULT_HOST "v3.void.cat"
struct arg_lit *verb, *help; struct arg_lit *verb, *help;
struct arg_file *upload, *pack; struct arg_file *upload, *pack;