Update mastodonapi.php

This commit is contained in:
simonpipe 2026-02-22 17:43:11 +01:00
parent 6c6232534e
commit 101315a5ff

View file

@ -142,33 +142,45 @@ class YellowMastodonapi {
// Download and save image
private function downloadAndSaveImage($media, $dirPath) {
$imageUrl = $media["remote_url"] ?? $media["url"] ?? $media["preview_url"] ?? null;
if ( empty($imageUrl) ) return false;
$imageUrl = $media['remote_url']
?? $media['url']
?? $media['preview_url']
?? ($media['meta']['original']['url'] ?? null)
?? ($media['meta']['small']['url'] ?? null);
if ( !is_dir($dirPath) ) {
mkdir($dirPath, 0777, true);
chmod($dirPath, 0777);
if (empty($imageUrl)) {
return false;
}
if (!is_dir($dirPath)) mkdir($dirPath, 0755, true);
$fileName = basename(parse_url($imageUrl, PHP_URL_PATH));
$fullPath = $dirPath . $fileName;
if ( file_exists($fullPath) ) return $fileName;
if (file_exists($fullPath)) return $fileName;
$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_FOLLOWLOCATION => true,
CURLOPT_TIMEOUT => 30,
CURLOPT_USERAGENT => "YellowMastodonapi/" . self::VERSION,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_SSL_VERIFYPEER => true,
));
CURLOPT_SSL_VERIFYHOST => 2,
]);
$imgData = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ( !$imgData ) return false;
if ($imgData === false || $httpCode < 200 || $httpCode >= 300) {
return false;
}
file_put_contents($fullPath, $imgData);
chmod($fullPath, 0666);
return $fileName;
}