Files
MoYuBan/api/save_avatar.php

80 lines
3.0 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
// 保存头像到本地(避免重复),不进行过期清理
header('Content-Type: application/json; charset=utf-8');
function respond($ok, $message = '', $path = '') {
echo json_encode(['success' => $ok, 'message' => $message, 'path' => $path], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
exit;
}
$raw = file_get_contents('php://input');
$data = json_decode($raw, true);
$url = $data['url'] ?? '';
if (!$url || !filter_var($url, FILTER_VALIDATE_URL)) {
respond(false, 'URL无效');
}
$scheme = parse_url($url, PHP_URL_SCHEME);
if (!in_array(strtolower($scheme), ['http','https'])) {
respond(false, '仅支持http/https图片地址');
}
$avatarsRoot = __DIR__ . '/../avatars';
if (!is_dir($avatarsRoot)) { @mkdir($avatarsRoot, 0777, true); }
$today = (new DateTime('now'))->format('Y-m-d');
$targetDir = $avatarsRoot . '/' . $today;
if (!is_dir($targetDir)) { @mkdir($targetDir, 0777, true); }
// 推断扩展名
$pathExt = strtolower(pathinfo(parse_url($url, PHP_URL_PATH) ?? '', PATHINFO_EXTENSION));
$allowed = ['jpg','jpeg','png','gif','webp'];
if (!$pathExt || !in_array($pathExt, $allowed)) { $pathExt = 'jpg'; }
// 使用URL的basename作为文件名若没有则用hash
$basename = basename(parse_url($url, PHP_URL_PATH) ?: '');
if (!$basename || $basename === '/' || $basename === '\\') {
$basename = md5($url) . '.' . $pathExt;
}
if (strpos($basename, '.') === false) { $basename .= '.' . $pathExt; }
$safeName = preg_replace('/[^\w\.-]+/u', '_', $basename);
$targetPath = $targetDir . '/' . $safeName;
// 如果已存在,跳过下载
if (file_exists($targetPath)) {
respond(true, '文件已存在,跳过下载', str_replace($_SERVER['DOCUMENT_ROOT'], '', $targetPath));
}
// 下载保存
try {
$read = @fopen($url, 'rb');
if (!$read) { respond(false, '无法读取远程图片'); }
$write = @fopen($targetPath, 'wb');
if (!$write) { respond(false, '无法写入文件'); }
stream_copy_to_stream($read, $write);
fclose($read);
fclose($write);
} catch (Throwable $e) {
respond(false, '保存失败: ' . $e->getMessage());
}
$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));