Commit 7427f0b3 authored by Malcolm Blaney's avatar Malcolm Blaney

Micropub endpoint now includes uploaded photos in photo properties

for the post. Required changing return format from Browser module.
parent eb289e17
Pipeline #37866125 passed with stage
in 1 minute and 23 seconds
......@@ -118,7 +118,9 @@ class Browser extends Base {
}
public function Factory($fn, $p = NULL) {
if ($fn === 'Upload') return $this->Upload();
// Upload is called from micropub.php where the required return value is
// the location of the uploaded file.
if ($fn === 'Upload') return $this->Upload($p, 'location');
}
public function Group() {
......@@ -191,7 +193,7 @@ class Browser extends Base {
return ['error' => 'Filename does not have correct format.'];
}
private function Upload() {
private function Upload($name = 'file', $return = 'content') {
// First check if the user's upload directory is at capacity.
$handle = popen('/usr/bin/du -sm ' . $this->PublicDirectory(), 'r');
$size = fgets($handle);
......@@ -206,28 +208,36 @@ class Browser extends Base {
if ($remaining <= 0) {
return ['error' => 'Upload directory full.'];
}
if (!isset($_FILES['file']['size'])) {
if (!isset($_FILES[$name]['size'])) {
return ['error' => 'Could not check uploaded file size.'];
}
if (is_array($_FILES['file']['size'])) {
if (is_array($_FILES[$name]['size'])) {
$content = '';
for ($i = 0; $i < count($_FILES['file']['size']); $i++) {
$result = $this->UploadFile($_FILES['file']['name'][$i],
$_FILES['file']['tmp_name'][$i],
$_FILES['file']['size'][$i], $remaining);
$location = [];
for ($i = 0; $i < count($_FILES[$name]['size']); $i++) {
$result = $this->UploadFile($_FILES[$name]['name'][$i],
$_FILES[$name]['tmp_name'][$i],
$_FILES[$name]['size'][$i],
$remaining, $return);
if (isset($result['error'])) return $result;
$content .= $result['content'];
$remaining -= (int)$_FILES['file']['size'][$i] / 1000000;
if ($return === 'content') {
$content .= $result['content'];
}
else if ($return === 'location') {
$location[] = $result;
}
$remaining -= (int)$_FILES[$name]['size'][$i] / 1000000;
}
return ['content' => $content];
if ($return === 'content') return ['content' => $content];
if ($return === 'location') return $location;
}
return $this->UploadFile($_FILES['file']['name'],
$_FILES['file']['tmp_name'],
$_FILES['file']['size'], $remaining);
return $this->UploadFile($_FILES[$name]['name'], $_FILES[$name]['tmp_name'],
$_FILES[$name]['size'], $remaining, $return);
}
private function UploadFile($filename, $tmp, $size, $remaining) {
private function UploadFile($filename, $tmp, $size, $remaining, $return) {
if (!isset($size)) {
return ['error' => 'Could not check uploaded file size.'];
}
......@@ -259,7 +269,8 @@ class Browser extends Base {
return ['error' => 'File: ' . $filename . ' was not uploaded.'];
}
// If this user has a media endpoint configured transfer the file to it.
if (isset($this->user->settings['micropub']['config'])) {
if ($return === 'content' &&
isset($this->user->settings['micropub']['config'])) {
$config = json_decode($this->user->settings['micropub']['config'], true);
if (isset($config['media-endpoint'])) {
return $this->TransferMedia($path, $config['media-endpoint']);
......@@ -308,9 +319,12 @@ class Browser extends Base {
else {
$thumbnail = '<span title="' . $filename . '">' . $type . '</span>';
}
return ['content' =>
$this->ImageContent($thumbnail, $html_path . $filename)];
if ($return === 'content') {
return ['content' =>
$this->ImageContent($thumbnail, $html_path . $filename)];
}
if ($return === 'location') return $html_path . $filename;
return ['error' => 'Unknown return format.'];
}
private function ImageContent($thumbnail, $filename, $local = true) {
......
......@@ -159,7 +159,7 @@ if (!$post->IsInstalled()) {
exit;
}
$content = [];
$content = ['data' => ''];
if ($content_type === 'application/json') {
$use_content = true;
if (isset($data['properties']['summary'][0])) {
......@@ -245,16 +245,6 @@ else {
$content['category'] = $_POST['category'];
}
}
if (isset($_POST['photo'])) {
$photo_list = $_POST['photo'];
if (is_string($photo_list)) {
$content['data'] .=
'<p><img class="u-photo" src="' . $_POST['photo'] . '"></p>';
}
else {
$content['data'] .= photo_html($photo_list);
}
}
if (isset($_POST['in-reply-to'])) {
$content['webactionType'] = 'reply';
$content['webactionUrl'] = $_POST['in-reply-to'];
......@@ -277,9 +267,9 @@ else {
}
}
}
// If this is multipart/form-data then $_FILES will be set with name="file".
if (isset($_FILES['file'])) {
$photo_list = isset($_POST['photo']) ? $_POST['photo'] : [];
// If this is multipart/form-data then $_FILES will be set with name="photo".
if (isset($_FILES['photo'])) {
$browser = new Module($user, $owner, 'browser');
if (!$browser->IsInstalled()) {
header('HTTP/1.1 500 Internal Server Error');
......@@ -287,9 +277,20 @@ else {
exit;
}
$result = $browser->Factory('Upload');
if (isset($result['error'])) log_db('micropub.php 7: ' . $result['error']);
$result = $browser->Factory('Upload', 'photo');
if (isset($result['error'])) {
log_db('micropub.php 7: ' . $result['error']);
}
else if (is_string($result)) {
if (!in_array($location, $photo_list)) $photo_list[] = $result;
}
else {
foreach ($result as $location) {
if (!in_array($location, $photo_list)) $photo_list[] = $location;
}
}
}
$content['data'] .= photo_html($photo_list);
}
if (count($content) === 0) {
log_db('micropub.php 7: No content found.');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment