Skip to content

Commit

Permalink
if author has avatar then don't replace avatar; continue if request f…
Browse files Browse the repository at this point in the history
…ailed
  • Loading branch information
star8ks committed Apr 1, 2023
1 parent c883e30 commit 2492bda
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions app/Console/Author/MoveImageToCOS.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public function handle() {
try {
$authors->each(function ($author) {
$this->process($author);
logger()->info('process end:[author->id=' . $author->id . ']');
});
} catch (\Exception $e) {
$this->error('error while put file to COS ' . $e->getMessage());
Expand Down Expand Up @@ -93,10 +94,18 @@ public function process(Author $author): int {
$ext = $pathInfo['extension'];

logger()->info('fetching url:' . $url);
$response = \Illuminate\Support\Facades\Http::withOptions($options)->timeout(10)->retry(3, 1)->get($url);
if ($response->status() !== 200) {
return -1;

try {
$response = \Illuminate\Support\Facades\Http::withOptions($options)->timeout(10)->retry(3, 1)->get($url);
if ($response->status() !== 200) {
continue;
}
} catch (\Exception $e) {
logger()->warning('fetchImage failed:' . $e->getMessage());

continue;
}

$imgContent = $response->body();

$toFormat = TX::SUPPORTED_FORMAT['webp'];
Expand All @@ -110,9 +119,13 @@ public function process(Author $author): int {
}

// 获取 wikimedia 链接及版权信息,保存至 image 表
$wikimediaPicInfo = collect(get_wikimedia_pic_info([
$wikiMediaData = get_wikimedia_pic_info([
'title' => $pathInfo['basename'],
])->query->pages)->first();
]);
if (!$wikiMediaData) {
continue;
}
$wikimediaPicInfo = collect($wikiMediaData->query->pages)->first();

if (isset($wikimediaPicInfo->imageinfo[0]->extmetadata->Artist)) {
$MediaFile->setProp('wikimediaPicInfo', $wikimediaPicInfo->imageinfo[0]->extmetadata->Artist->value)->save();
Expand Down Expand Up @@ -178,7 +191,7 @@ protected function upload(Author $author, string $imgContent, string $ext, strin

$MediaFile = $this->authorRepo->saveAuthorMediaFile($author, MediaFile::TYPE['image'], $compressedKey, $name, $toFormat, $compressed['Size']);

if ($index === 0) {
if ($index === 0 && !$author->avatar) {
$scropSize = min(600, $compressed['Width'], $compressed['Height']);
$avatarResult = $this->scropAvatar($compressedKey, $author->fakeId, $toFormat, $scropSize);
$author->avatar = 'https://' . $avatarResult['Location'];
Expand Down

0 comments on commit 2492bda

Please sign in to comment.