Skip to content

Commit

Permalink
从本地存储获取图片时不再重复检查完整性
Browse files Browse the repository at this point in the history
  • Loading branch information
mokeyjay committed Nov 28, 2023
1 parent 689527e commit d1b46f9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
1 change: 1 addition & 0 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
- 完全重写了前端,更优雅的缓动效果
- 不再依赖 bootstrap,加载更快啦
- 改为使用官方 php、nginx 包
- 从本地存储获取图片时不再重复检查完整性
### 修复
- 部分环境变量在一些情况下无法被正常获取的问题
- 定时任务实际上是一小时执行一次,而非文档说的半小时一次
Expand Down
32 changes: 17 additions & 15 deletions app/Libs/Pixiv.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 => [
Expand All @@ -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;
Expand Down

0 comments on commit d1b46f9

Please sign in to comment.