Update mastodonapi.php
This commit is contained in:
parent
128570ddc1
commit
15f3da1b62
1 changed files with 24 additions and 14 deletions
|
|
@ -2,7 +2,7 @@
|
|||
// Mastodonapi extension, https://codeberg.org/simonpipe/yellow-mastodonapi
|
||||
|
||||
class YellowMastodonapi {
|
||||
const VERSION = "0.9.5";
|
||||
const VERSION = "0.9.6";
|
||||
public $yellow; // access to API
|
||||
|
||||
// Initialize extension
|
||||
|
|
@ -147,39 +147,49 @@ class YellowMastodonapi {
|
|||
?? $media['preview_url']
|
||||
?? ($media['meta']['original']['url'] ?? null)
|
||||
?? ($media['meta']['small']['url'] ?? null);
|
||||
|
||||
if (empty($imageUrl)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
if (empty($imageUrl)) return false;
|
||||
|
||||
if (!is_dir($dirPath)) mkdir($dirPath, 0755, true);
|
||||
|
||||
|
||||
// Dateiname etwas robuster ermitteln
|
||||
$fileName = basename(parse_url($imageUrl, PHP_URL_PATH));
|
||||
|
||||
// Fallback falls Dateiname keine Endung hat (Mastodon-spezifisch)
|
||||
if (!strpos($fileName, '.')) {
|
||||
$fileName .= ".jpg";
|
||||
}
|
||||
|
||||
$fullPath = $dirPath . $fileName;
|
||||
if (file_exists($fullPath)) return $fileName;
|
||||
|
||||
|
||||
// Bestehende Datei nur akzeptieren, wenn sie nicht leer ist
|
||||
if (file_exists($fullPath) && filesize($fullPath) > 0) return $fileName;
|
||||
|
||||
$ch = curl_init($imageUrl);
|
||||
$headers = ["User-Agent: YellowMastodonAPI"];
|
||||
// Browser-ähnlicher User-Agent hilft gegen CDNs
|
||||
$headers = ["User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) 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_HTTPHEADER => $headers,
|
||||
CURLOPT_SSL_VERIFYPEER => true,
|
||||
CURLOPT_SSL_VERIFYHOST => 2,
|
||||
CURLOPT_SSL_VERIFYPEER => false, // Erhöht die Kompatibilität massiv
|
||||
]);
|
||||
|
||||
$imgData = curl_exec($ch);
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
|
||||
if ($imgData === false || $httpCode < 200 || $httpCode >= 300) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
file_put_contents($fullPath, $imgData);
|
||||
return $fileName;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue