set lastview on upload

This commit is contained in:
Kieran 2019-05-01 02:17:37 +08:00
parent 6a5395f244
commit 196bec4412
21 changed files with 993 additions and 983 deletions

View File

@ -6,6 +6,7 @@
public static function LoadHeader($path) : ?BlobFile {
$input = fopen($path, "rb");
if ($input !== false) {
$version = ord(fread($input, 1));
//error_log($version);
@ -33,7 +34,7 @@
} else {
fclose($input);
}
}
return null;
}
}

View File

@ -26,11 +26,11 @@
$lv = $file_header->Uploaded;
} else {
//cant read file header or upload timestamp is invalid, mark as old
$lv = 0;
$lv = $file_header !== null ? $file_header->Uploaded : 0;
}
}
if($lv !== false && intval($lv) < $expire) {
if($lv !== false && (intval($lv) < $expire) || intval($lv) > time()) {
$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

View File

@ -26,7 +26,7 @@
//check the ip of the host submitting the file for sync
if(in_array(USER_IP, $sync_hosts_ips)) {
$fs->StoreFile("php://input", $id);
$fs->StoreFile(fopen("php://input", "rb"), $id);
http_response_code(201);
} else {
http_response_code(401);

View File

@ -1,15 +1,10 @@
<?php
class Tracking {
public function TrackDownload($fs, $id) : void {
$redis = StaticRedis::WriteOp();
$file_key = REDIS_PREFIX . $id;
$file_size = $fs->GetFileSize($id);
if(!$this->IsRangeRequest()) {
$redis->hIncrBy($file_key, 'views', 1);
$redis->hSet($file_key, 'lastview', time());
$this->TrackView($id);
Stats::TrackTransfer($id, $file_size);
} else {
$range = $this->GetRequestRange($file_size);
@ -17,6 +12,13 @@
}
}
public function TrackView($id) : void {
$redis = StaticRedis::WriteOp();
$file_key = REDIS_PREFIX . $id;
$redis->hIncrBy($file_key, 'views', 1);
$redis->hSet($file_key, 'lastview', time());
}
function GetRequestRange($len) : ?object {
if(isset($_SERVER['HTTP_RANGE'])) {
$rby = explode('=', $_SERVER['HTTP_RANGE']);

View File

@ -40,6 +40,7 @@
$rsp->msg = "File is too large";
} else {
$auth = new Auth();
$tracking = new Tracking();
$token = $auth->GetBearerToken();
if($token !== null) {
@ -51,6 +52,9 @@
$rsp->sync = $this->SyncFileUpload($id);
$rsp->status = 200;
$rsp->id = $id;
//finally set the last view to now
$tracking->TrackView($id);
} else {
$rsp->status = 3;
$rsp->msg = "Legacy upload error";
@ -75,6 +79,9 @@
$rsp->sync = $this->SyncFileUpload($id);
$rsp->status = 200;
$rsp->id = $id;
//finally set the last view to now
$tracking->TrackView($id);
}
} else {
$rsp->status = 2;