加入流量查看,访客与视频数量图片数量查看

This commit is contained in:
LL
2025-11-18 15:10:00 +08:00
parent 3c348195b7
commit 558188765e
9 changed files with 257 additions and 3 deletions

View File

@@ -57,5 +57,23 @@ try {
respond(false, '保存失败: ' . $e->getMessage());
}
respond(true, '保存成功', str_replace($_SERVER['DOCUMENT_ROOT'], '', $targetPath));
$bytes = @filesize($targetPath) ?: 0;
try {
$statsRoot = __DIR__ . '/../stats';
if (!is_dir($statsRoot)) { @mkdir($statsRoot, 0777, true); }
$dailyPath = $statsRoot . '/daily.json';
$today = (new DateTime('now'))->format('Y-m-d');
$daily = [];
if (file_exists($dailyPath)) {
$rawDaily = @file_get_contents($dailyPath);
$decoded = json_decode($rawDaily, true);
if (is_array($decoded)) { $daily = $decoded; }
}
if (!isset($daily[$today]) || !is_array($daily[$today])) {
$daily[$today] = ['visits' => 0, 'video_plays' => 0, 'picture_shows' => 0, 'uv' => 0, 'bandwidth_in' => 0, 'bandwidth_out' => 0];
}
$daily[$today]['bandwidth_out'] += (int)$bytes;
@file_put_contents($dailyPath, json_encode($daily, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT), LOCK_EX);
} catch (Throwable $e) {}
respond(true, '保存成功', str_replace($_SERVER['DOCUMENT_ROOT'], '', $targetPath));

View File

@@ -62,5 +62,23 @@ try {
respond(false, '保存失败: ' . $e->getMessage());
}
respond(true, '保存成功', str_replace($_SERVER['DOCUMENT_ROOT'], '', $targetPath));
$bytes = @filesize($targetPath) ?: 0;
try {
$statsRoot = __DIR__ . '/../stats';
if (!is_dir($statsRoot)) { @mkdir($statsRoot, 0777, true); }
$dailyPath = $statsRoot . '/daily.json';
$today = (new DateTime('now'))->format('Y-m-d');
$daily = [];
if (file_exists($dailyPath)) {
$rawDaily = @file_get_contents($dailyPath);
$decoded = json_decode($rawDaily, true);
if (is_array($decoded)) { $daily = $decoded; }
}
if (!isset($daily[$today]) || !is_array($daily[$today])) {
$daily[$today] = ['visits' => 0, 'video_plays' => 0, 'picture_shows' => 0, 'uv' => 0, 'bandwidth_in' => 0, 'bandwidth_out' => 0];
}
$daily[$today]['bandwidth_out'] += (int)$bytes;
@file_put_contents($dailyPath, json_encode($daily, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT), LOCK_EX);
} catch (Throwable $e) {}
respond(true, '保存成功', str_replace($_SERVER['DOCUMENT_ROOT'], '', $targetPath));

View File

@@ -77,6 +77,25 @@ if ($skipDownload) {
}
}
$bytes = @filesize($targetPath) ?: 0;
try {
$statsRoot = __DIR__ . '/../stats';
if (!is_dir($statsRoot)) { @mkdir($statsRoot, 0777, true); }
$dailyPath = $statsRoot . '/daily.json';
$today = (new DateTime('now'))->format('Y-m-d');
$daily = [];
if (file_exists($dailyPath)) {
$rawDaily = @file_get_contents($dailyPath);
$decoded = json_decode($rawDaily, true);
if (is_array($decoded)) { $daily = $decoded; }
}
if (!isset($daily[$today]) || !is_array($daily[$today])) {
$daily[$today] = ['visits' => 0, 'video_plays' => 0, 'picture_shows' => 0, 'uv' => 0, 'bandwidth_in' => 0, 'bandwidth_out' => 0];
}
$daily[$today]['bandwidth_out'] += (int)$bytes;
@file_put_contents($dailyPath, json_encode($daily, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT), LOCK_EX);
} catch (Throwable $e) {}
// 恢复清理逻辑:仅保留最近两天的视频目录,删除更早的目录
try {
$dirs = @scandir($videosRoot);
@@ -93,7 +112,7 @@ try {
// 按日期升序排序
sort($dateDirs);
// 保留最近两天(末尾两项),其它全部删除
if (count($dateDirs) > 30) {
if (count($dateDirs) > 2) {
$toDelete = array_slice($dateDirs, 0, count($dateDirs) - 2);
foreach ($toDelete as $del) {
rrmdir($videosRoot . DIRECTORY_SEPARATOR . $del);

99
api/stats.php Normal file
View File

@@ -0,0 +1,99 @@
<?php
header('Content-Type: application/json; charset=utf-8');
$method = $_SERVER['REQUEST_METHOD'] ?? 'GET';
$statsRoot = __DIR__ . '/../stats';
if (!is_dir($statsRoot)) { @mkdir($statsRoot, 0777, true); }
$eventsRoot = $statsRoot . '/events';
if (!is_dir($eventsRoot)) { @mkdir($eventsRoot, 0777, true); }
$uvRoot = $statsRoot . '/uv';
if (!is_dir($uvRoot)) { @mkdir($uvRoot, 0777, true); }
$dailyPath = $statsRoot . '/daily.json';
$today = (new DateTime('now'))->format('Y-m-d');
if ($method === 'GET') {
$daily = [];
if (file_exists($dailyPath)) {
$rawDaily = @file_get_contents($dailyPath);
$decoded = json_decode($rawDaily, true);
if (is_array($decoded)) { $daily = $decoded; }
}
$keys = array_keys($daily);
sort($keys);
$last7 = array_slice($keys, max(0, count($keys) - 7));
$out = [];
foreach ($last7 as $k) { $out[$k] = $daily[$k]; }
$todayData = $daily[$today] ?? ['visits' => 0, 'video_plays' => 0, 'picture_shows' => 0, 'uv' => 0, 'bandwidth_in' => 0, 'bandwidth_out' => 0];
echo json_encode(['success' => true, 'today' => $todayData, 'daily' => $out], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
exit;
}
$raw = file_get_contents('php://input');
$data = json_decode($raw, true);
$event = isset($data['event']) ? trim((string)$data['event']) : '';
$page = isset($data['page']) ? trim((string)$data['page']) : '';
$url = isset($data['url']) ? trim((string)$data['url']) : '';
$title = isset($data['title']) ? trim((string)$data['title']) : '';
$allowed = ['visit','video_play','picture_show','bandwidth'];
if (!in_array($event, $allowed, true)) {
echo json_encode(['success' => false, 'message' => 'invalid event']);
exit;
}
$daily = [];
if (file_exists($dailyPath)) {
$rawDaily = @file_get_contents($dailyPath);
$decoded = json_decode($rawDaily, true);
if (is_array($decoded)) { $daily = $decoded; }
}
if (!isset($daily[$today]) || !is_array($daily[$today])) {
$daily[$today] = ['visits' => 0, 'video_plays' => 0, 'picture_shows' => 0, 'uv' => 0, 'bandwidth_in' => 0, 'bandwidth_out' => 0];
}
if ($event === 'visit') { $daily[$today]['visits']++; }
if ($event === 'video_play') { $daily[$today]['video_plays']++; }
if ($event === 'picture_show') { $daily[$today]['picture_shows']++; }
if ($event === 'visit') {
$uvPath = $uvRoot . '/' . $today . '.json';
$ips = [];
if (file_exists($uvPath)) {
$rawIps = @file_get_contents($uvPath);
$decodedIps = json_decode($rawIps, true);
if (is_array($decodedIps)) { $ips = $decodedIps; }
}
$ip = $_SERVER['REMOTE_ADDR'] ?? '';
if ($ip !== '') {
if (!in_array($ip, $ips, true)) { $ips[] = $ip; }
}
@file_put_contents($uvPath, json_encode($ips, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), LOCK_EX);
$daily[$today]['uv'] = count($ips);
}
@file_put_contents($dailyPath, json_encode($daily, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT), LOCK_EX);
$eventsPath = $eventsRoot . '/' . $today . '.json';
$events = [];
if (file_exists($eventsPath)) {
$rawEvents = @file_get_contents($eventsPath);
$decoded = json_decode($rawEvents, true);
if (is_array($decoded)) { $events = $decoded; }
}
$events[] = [
'type' => $event,
'page' => $page,
'url' => $url,
'title' => $title,
'direction' => $direction ?? '',
'bytes' => $bytes ?? 0,
'ip' => $_SERVER['REMOTE_ADDR'] ?? '',
'ua' => $_SERVER['HTTP_USER_AGENT'] ?? '',
'ts' => (new DateTime('now'))->format(DateTime::ATOM),
];
@file_put_contents($eventsPath, json_encode($events, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT), LOCK_EX);
echo json_encode(['success' => true, 'today' => $daily[$today]], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$direction = isset($data['direction']) ? (string)$data['direction'] : '';
$bytes = isset($data['bytes']) ? intval($data['bytes']) : 0;
if ($event === 'bandwidth' && $bytes > 0) {
if ($direction === 'in') { $daily[$today]['bandwidth_in'] += $bytes; }
else if ($direction === 'out') { $daily[$today]['bandwidth_out'] += $bytes; }
}

View File

@@ -71,6 +71,25 @@ if (!move_uploaded_file($file['tmp_name'], $target)) {
exit;
}
$bytes = (int)($file['size'] ?? 0);
try {
$statsRoot = __DIR__ . '/../stats';
if (!is_dir($statsRoot)) { @mkdir($statsRoot, 0777, true); }
$dailyPath = $statsRoot . '/daily.json';
$today = (new DateTime('now'))->format('Y-m-d');
$daily = [];
if (file_exists($dailyPath)) {
$rawDaily = @file_get_contents($dailyPath);
$decoded = json_decode($rawDaily, true);
if (is_array($decoded)) { $daily = $decoded; }
}
if (!isset($daily[$today]) || !is_array($daily[$today])) {
$daily[$today] = ['visits' => 0, 'video_plays' => 0, 'picture_shows' => 0, 'uv' => 0, 'bandwidth_in' => 0, 'bandwidth_out' => 0];
}
$daily[$today]['bandwidth_in'] += (int)$bytes;
@file_put_contents($dailyPath, json_encode($daily, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT), LOCK_EX);
} catch (Throwable $e) {}
$meta = [
'original_name' => $origName,
'saved_name' => basename($target),

View File

@@ -73,6 +73,25 @@ if (!move_uploaded_file($file['tmp_name'], $target)) {
exit;
}
$bytes = (int)($file['size'] ?? 0);
try {
$statsRoot = __DIR__ . '/../stats';
if (!is_dir($statsRoot)) { @mkdir($statsRoot, 0777, true); }
$dailyPath = $statsRoot . '/daily.json';
$today = (new DateTime('now'))->format('Y-m-d');
$daily = [];
if (file_exists($dailyPath)) {
$rawDaily = @file_get_contents($dailyPath);
$decoded = json_decode($rawDaily, true);
if (is_array($decoded)) { $daily = $decoded; }
}
if (!isset($daily[$today]) || !is_array($daily[$today])) {
$daily[$today] = ['visits' => 0, 'video_plays' => 0, 'picture_shows' => 0, 'uv' => 0, 'bandwidth_in' => 0, 'bandwidth_out' => 0];
}
$daily[$today]['bandwidth_in'] += (int)$bytes;
@file_put_contents($dailyPath, json_encode($daily, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT), LOCK_EX);
} catch (Throwable $e) {}
// 可选:记录一个简单的元信息文件
$meta = [
'original_name' => $origName,