chore(mastodon.php): remove image uploads in favor of oembed previews
This commit is contained in:
parent
c29105f42d
commit
2b3a15bf9b
3 changed files with 5 additions and 53 deletions
|
@ -85,76 +85,28 @@ class MastodonPostHandler extends ApiHandler
|
||||||
$items = [];
|
$items = [];
|
||||||
|
|
||||||
foreach ($rss->channel->item as $item) {
|
foreach ($rss->channel->item as $item) {
|
||||||
$imageUrl = null;
|
|
||||||
if ($item->enclosure && isset($item->enclosure['url'])) {
|
|
||||||
$imageUrl = (string) $item->enclosure['url'];
|
|
||||||
}
|
|
||||||
|
|
||||||
$items[] = [
|
$items[] = [
|
||||||
"title" => (string) $item->title,
|
"title" => (string) $item->title,
|
||||||
"link" => (string) $item->link,
|
"link" => (string) $item->link,
|
||||||
"description" => (string) $item->description,
|
"description" => (string) $item->description,
|
||||||
"image" => $imageUrl,
|
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
return $items;
|
return $items;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function uploadImageToMastodon(string $imageUrl): ?string
|
private function postToMastodon(string $content): ?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
|
|
||||||
{
|
{
|
||||||
$headers = [
|
$headers = [
|
||||||
"Authorization" => "Bearer {$this->mastodonAccessToken}",
|
"Authorization" => "Bearer {$this->mastodonAccessToken}",
|
||||||
"Content-Type" => "application/json",
|
"Content-Type" => "application/json",
|
||||||
];
|
];
|
||||||
|
|
||||||
$postData = ["status" => $content];
|
$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, [
|
$response = $this->httpClient->request("POST", self::MASTODON_API_STATUS, [
|
||||||
"headers" => $headers,
|
"headers" => $headers,
|
||||||
"json" => $postData
|
"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);
|
$body = json_decode($response->getBody()->getContents(), true);
|
||||||
return $body["url"] ?? null;
|
return $body["url"] ?? null;
|
||||||
|
|
4
package-lock.json
generated
4
package-lock.json
generated
|
@ -1,12 +1,12 @@
|
||||||
{
|
{
|
||||||
"name": "coryd.dev",
|
"name": "coryd.dev",
|
||||||
"version": "4.0.5",
|
"version": "4.0.6",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "coryd.dev",
|
"name": "coryd.dev",
|
||||||
"version": "4.0.5",
|
"version": "4.0.6",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"html-minifier-terser": "7.2.0",
|
"html-minifier-terser": "7.2.0",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "coryd.dev",
|
"name": "coryd.dev",
|
||||||
"version": "4.0.5",
|
"version": "4.0.6",
|
||||||
"description": "The source for my personal site. Built using 11ty (and other tools).",
|
"description": "The source for my personal site. Built using 11ty (and other tools).",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"engines": {
|
"engines": {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue