chore(mastodon.php): remove image uploads in favor of oembed previews

This commit is contained in:
Cory Dransfeldt 2025-05-12 14:45:05 -07:00
parent c29105f42d
commit 2b3a15bf9b
No known key found for this signature in database
3 changed files with 5 additions and 53 deletions

View file

@ -85,76 +85,28 @@ class MastodonPostHandler extends ApiHandler
$items = [];
foreach ($rss->channel->item as $item) {
$imageUrl = null;
if ($item->enclosure && isset($item->enclosure['url'])) {
$imageUrl = (string) $item->enclosure['url'];
}
$items[] = [
"title" => (string) $item->title,
"link" => (string) $item->link,
"description" => (string) $item->description,
"image" => $imageUrl,
];
}
return $items;
}
private function uploadImageToMastodon(string $imageUrl): ?string
{
$tempFile = tempnam(sys_get_temp_dir(), "mastodon_img");
file_put_contents($tempFile, file_get_contents($imageUrl));
$response = $this->httpClient->request("POST", "https://follow.coryd.dev/api/v2/media", [
"headers" => [
"Authorization" => "Bearer {$this->mastodonAccessToken}"
],
"multipart" => [
[
"name" => "file",
"contents" => fopen($tempFile, "r"),
"filename" => basename($imageUrl)
]
]
]);
unlink($tempFile);
if ($response->getStatusCode() !== 200) {
throw new \Exception("Image upload failed with status {$response->getStatusCode()}");
}
$json = json_decode($response->getBody(), true);
return $json["id"] ?? null;
}
private function postToMastodon(string $content, ?string $imageUrl = null): ?string
private function postToMastodon(string $content): ?string
{
$headers = [
"Authorization" => "Bearer {$this->mastodonAccessToken}",
"Content-Type" => "application/json",
];
$postData = ["status" => $content];
if ($imageUrl) {
try {
$mediaId = $this->uploadImageToMastodon($imageUrl);
if ($mediaId) $postData["media_ids"] = [$mediaId];
} catch (\Exception $e) {
echo "Image upload failed: {$e->getMessage()}\n";
}
}
$response = $this->httpClient->request("POST", self::MASTODON_API_STATUS, [
"headers" => $headers,
"json" => $postData
]);
if ($response->getStatusCode() >= 400) {
throw new \Exception("Mastodon post failed: {$response->getBody()}");
}
if ($response->getStatusCode() >= 400) throw new \Exception("Mastodon post failed: {$response->getBody()}");
$body = json_decode($response->getBody()->getContents(), true);
return $body["url"] ?? null;

4
package-lock.json generated
View file

@ -1,12 +1,12 @@
{
"name": "coryd.dev",
"version": "4.0.5",
"version": "4.0.6",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "coryd.dev",
"version": "4.0.5",
"version": "4.0.6",
"license": "MIT",
"dependencies": {
"html-minifier-terser": "7.2.0",

View file

@ -1,6 +1,6 @@
{
"name": "coryd.dev",
"version": "4.0.5",
"version": "4.0.6",
"description": "The source for my personal site. Built using 11ty (and other tools).",
"type": "module",
"engines": {