Add image attachements

merge-requests/5/head
Terence Eden 2024-02-18 15:48:33 +00:00
rodzic 3006e1f284
commit 206c46514a
1 zmienionych plików z 48 dodań i 2 usunięć

Wyświetl plik

@ -7,7 +7,8 @@
* For educational purposes only.
* The Server produces an Actor who can be followed.
* The Actor can send messages to followers.
* Those messages can have linkable URls, hashtags, and mentions.
* The message can have linkable URls, hashtags, and mentions.
* An image and alt text can be attached to the message.
* The Server saves logs about requests it receives and sends.
* This code is NOT suitable for production use.
* SPDX-License-Identifier: AGPL-3.0-or-later
@ -381,6 +382,10 @@ echo <<< HTML
<form action="/send" method="post" enctype="multipart/form-data">
<label for="content">Your message:</label><br>
<textarea id="content" name="content" rows="5" cols="32"></textarea><br>
<label for="image">Attach an image</label><br>
<input type="file" name="image" id="image" accept="image/*"><br>
<label for="alt">Alt Text</label>
<input type="text" name="alt" id="alt" size="32" /><br>
<label for="password">Password</label><br>
<input type="password" name="password" id="password" size="32"><br>
<input type="submit" value="Post Message">
@ -406,6 +411,46 @@ HTML;
// Process the content into HTML to get hashtags etc
list( "HTML" => $content, "TagArray" => $tags ) = process_content( $content );
// Is there an image attached?
if ( isset( $_FILES['image']['tmp_name'] ) && ("" != $_FILES['image']['tmp_name'] ) ) {
// Get information about the image
$image = $_FILES['image']['tmp_name'];
$image_info = getimagesize( $image );
$image_ext = image_type_to_extension( $image_info[2] );
$image_mime = $image_info["mime"];
// Files are stored according to their hash
// A hash of "abc123" is stored in "/images/abc123.jpg"
$sha1 = sha1_file( $image );
$image_path = "images";
$image_full_path = "{$image_path}/{$sha1}.{$image_ext}";
// Move media to the correct location
// Create a directory if it doesn't exist
if( ! is_dir( $image_path ) ) {
mkdir( $image_path );
}
move_uploaded_file($image, $image_full_path );
// Get the alt text
if ( isset( $_POST["alt"] ) ) {
$alt = $_POST["alt"];
} else {
$alt = "";
}
// Construct the attachment value for the post
$attachment = [
"type" => "Image",
"mediaType" => "{$image_mime}",
"url" => "https://{$server}/{$image_full_path}",
"name" => $alt
];
} else {
$attachment = [];
}
// Current time - ISO8601
$timestamp = date( "c" );
@ -426,7 +471,8 @@ HTML;
"content" => $content,
"contentMap" => ["en" => $content],
"to" => ["https://www.w3.org/ns/activitystreams#Public"],
"tag" => $tags
"tag" => $tags,
"attachment" => $attachment
];
// Construct the Message