Update mastodonapi.php
This commit is contained in:
parent
6c6232534e
commit
101315a5ff
1 changed files with 27 additions and 15 deletions
|
|
@ -142,33 +142,45 @@ class YellowMastodonapi {
|
||||||
|
|
||||||
// Download and save image
|
// Download and save image
|
||||||
private function downloadAndSaveImage($media, $dirPath) {
|
private function downloadAndSaveImage($media, $dirPath) {
|
||||||
$imageUrl = $media["remote_url"] ?? $media["url"] ?? $media["preview_url"] ?? null;
|
$imageUrl = $media['remote_url']
|
||||||
if ( empty($imageUrl) ) return false;
|
?? $media['url']
|
||||||
|
?? $media['preview_url']
|
||||||
|
?? ($media['meta']['original']['url'] ?? null)
|
||||||
|
?? ($media['meta']['small']['url'] ?? null);
|
||||||
|
|
||||||
if ( !is_dir($dirPath) ) {
|
if (empty($imageUrl)) {
|
||||||
mkdir($dirPath, 0777, true);
|
return false;
|
||||||
chmod($dirPath, 0777);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!is_dir($dirPath)) mkdir($dirPath, 0755, true);
|
||||||
|
|
||||||
$fileName = basename(parse_url($imageUrl, PHP_URL_PATH));
|
$fileName = basename(parse_url($imageUrl, PHP_URL_PATH));
|
||||||
$fullPath = $dirPath . $fileName;
|
$fullPath = $dirPath . $fileName;
|
||||||
|
if (file_exists($fullPath)) return $fileName;
|
||||||
if ( file_exists($fullPath) ) return $fileName;
|
|
||||||
|
|
||||||
$ch = curl_init($imageUrl);
|
$ch = curl_init($imageUrl);
|
||||||
curl_setopt_array($ch, array(
|
$headers = ["User-Agent: YellowMastodonAPI"];
|
||||||
|
$accessToken = $this->yellow->system->get("mastodonapiAccessToken");
|
||||||
|
if (!empty($accessToken)) {
|
||||||
|
$headers[] = "Authorization: Bearer " . $accessToken;
|
||||||
|
}
|
||||||
|
curl_setopt_array($ch, [
|
||||||
CURLOPT_RETURNTRANSFER => true,
|
CURLOPT_RETURNTRANSFER => true,
|
||||||
CURLOPT_FOLLOWLOCATION => true,
|
CURLOPT_FOLLOWLOCATION => true,
|
||||||
CURLOPT_TIMEOUT => 30,
|
CURLOPT_TIMEOUT => 30,
|
||||||
CURLOPT_USERAGENT => "YellowMastodonapi/" . self::VERSION,
|
CURLOPT_HTTPHEADER => $headers,
|
||||||
CURLOPT_SSL_VERIFYPEER => true,
|
CURLOPT_SSL_VERIFYPEER => true,
|
||||||
));
|
CURLOPT_SSL_VERIFYHOST => 2,
|
||||||
|
]);
|
||||||
$imgData = curl_exec($ch);
|
$imgData = curl_exec($ch);
|
||||||
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||||
curl_close($ch);
|
curl_close($ch);
|
||||||
|
|
||||||
if ( !$imgData ) return false;
|
if ($imgData === false || $httpCode < 200 || $httpCode >= 300) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
file_put_contents($fullPath, $imgData);
|
file_put_contents($fullPath, $imgData);
|
||||||
chmod($fullPath, 0666);
|
|
||||||
return $fileName;
|
return $fileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue