From 2b3a15bf9b528c6ede296c1e6f9e9309245e5a11 Mon Sep 17 00:00:00 2001 From: Cory Dransfeldt Date: Mon, 12 May 2025 14:45:05 -0700 Subject: [PATCH] chore(mastodon.php): remove image uploads in favor of oembed previews --- api/mastodon.php | 52 ++--------------------------------------------- package-lock.json | 4 ++-- package.json | 2 +- 3 files changed, 5 insertions(+), 53 deletions(-) diff --git a/api/mastodon.php b/api/mastodon.php index 6019dd7..df96a7c 100644 --- a/api/mastodon.php +++ b/api/mastodon.php @@ -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; diff --git a/package-lock.json b/package-lock.json index 0dbeac6..79dcd7e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 2b41dc3..b711843 100644 --- a/package.json +++ b/package.json @@ -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": {