From d1b46f9ff210734cbabdd3770e04b1daa12f23f8 Mon Sep 17 00:00:00 2001 From: mokeyjay Date: Tue, 28 Nov 2023 17:32:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=8E=E6=9C=AC=E5=9C=B0=E5=AD=98=E5=82=A8?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=9B=BE=E7=89=87=E6=97=B6=E4=B8=8D=E5=86=8D?= =?UTF-8?q?=E9=87=8D=E5=A4=8D=E6=A3=80=E6=9F=A5=E5=AE=8C=E6=95=B4=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.en.md | 1 + README.md | 1 + app/Libs/Pixiv.php | 32 +++++++++++++++++--------------- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/README.en.md b/README.en.md index 2acc5dc..8e290a7 100755 --- a/README.en.md +++ b/README.en.md @@ -59,6 +59,7 @@ Both APIs automatically return the respective cross-domain header according to ` - Completely rewritten the frontend with more elegant animation effects. - Removed the dependency on Bootstrap for faster loading. - Switched to using the official PHP and Nginx packages. +- No longer repeatedly check integrity when retrieving images from local storage. ### Fixes - Some environment variables cannot be obtained normally in some cases - The scheduled task actually runs once every hour, not every half hour as stated in the documentation diff --git a/README.md b/README.md index 8228d3a..061f670 100755 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ - 完全重写了前端,更优雅的缓动效果 - 不再依赖 bootstrap,加载更快啦 - 改为使用官方 php、nginx 包 +- 从本地存储获取图片时不再重复检查完整性 ### 修复 - 部分环境变量在一些情况下无法被正常获取的问题 - 定时任务实际上是一小时执行一次,而非文档说的半小时一次 diff --git a/app/Libs/Pixiv.php b/app/Libs/Pixiv.php index f597fb0..f84dda7 100644 --- a/app/Libs/Pixiv.php +++ b/app/Libs/Pixiv.php @@ -102,9 +102,9 @@ public static function getImages() */ public static function downloadImage($url) { - $fileName = pathinfo($url, PATHINFO_BASENAME); - // 如果 storage 里存了有,就不再重新下载了 - $image = Storage::getImage($fileName); + // 如果 local storage 已经存有这张图(每日榜上的图片是可能存在重复的),就不再重新下载了 + $image = Storage::getImage(pathinfo($url, PATHINFO_BASENAME)); + $shouldCheckComplete = !$image; if ($image === false) { $image = Curl::get($url, [ CURLOPT_HTTPHEADER => [ @@ -118,23 +118,25 @@ public static function downloadImage($url) if ($image) { $file = explode('/', $url); $file = array_pop($file); - $file = sys_get_temp_dir() . '/' . $file; + $file = sys_get_temp_dir() . '/' . Str::random(16) . $file; $bytes = file_put_contents($file, $image); Log::write("写入文件 {$file} 大小:{$bytes} 字节"); // 检查文件是否下载完整 - $response = Curl::get($url, [ - CURLOPT_NOBODY => true, - CURLOPT_HEADER => true, - CURLOPT_HTTPHEADER => [ - 'Referer: https://www.pixiv.net/ranking.php?mode=daily', - ], - ]); - $contentLength = Response::getContentLength($response); - if ($bytes != $contentLength) { - Log::write("写入的文件大小与目标 content-length: {$contentLength} 不符"); - return false; + if ($shouldCheckComplete) { + $response = Curl::get($url, [ + CURLOPT_NOBODY => true, + CURLOPT_HEADER => true, + CURLOPT_HTTPHEADER => [ + 'Referer: https://www.pixiv.net/ranking.php?mode=daily', + ], + ]); + $contentLength = Response::getContentLength($response); + if ($bytes != $contentLength) { + Log::write("写入的文件大小与目标 content-length: {$contentLength} 不符"); + return false; + } } return $bytes > 0 ? $file : false;