* Mail/REST API: support an "X-No-Location: true" header to avoid getting a "Location" header when uploading attachments

Also change HTTP Status from "200 Ok" to "201 Created" for a "Location" header,
and send a correct URL to download the attachment again with a GET request.
This commit is contained in:
ralf 2023-11-10 17:07:06 +02:00
parent 6529956da2
commit ef43d7298b
2 changed files with 24 additions and 7 deletions

View File

@ -125,16 +125,18 @@ to use in further requests, instead of the attachment.
``` ```
curl -i https://example.org/egroupware/groupdav.php/mail/attachments/<filename> --user <user> \ curl -i https://example.org/egroupware/groupdav.php/mail/attachments/<filename> --user <user> \
--data-binary @<file> -H 'Content-Type: <content-type-of-file>' --data-binary @<file> -H 'Content-Type: <content-type-of-file>'
HTTP/1.1 302 Found HTTP/1.1 201 Created
Location: https://example.org/egroupware/groupdav.php/mail/attachment/<token> Location: https://example.org/egroupware/groupdav.php/mail/attachments/<token>
{ {
"status": 200, "status": 201,
"message": "Attachment stored", "message": "Attachment stored",
"location": "/mail/attachments/<filename>--xM35lY" "location": "/mail/attachments/<token>"
} }
``` ```
> When using curl to upload attachments it's important to use ```--data-binary```, just ```-d``` or ```--data``` is NOT sufficient! > When using curl to upload attachments it's important to use ```--data-binary```, just ```-d``` or ```--data``` is NOT sufficient!
> Use a `X-No-Location: true` header to get NO `Location: <url>` header with HTTP status `201 Created` back, but a simple `200 Ok`!
</details> </details>
- ```POST /mail[/<id>]/view``` view an eml file - ```POST /mail[/<id>]/view``` view an eml file

View File

@ -287,13 +287,19 @@ class ApiHandler extends Api\CalDAV\Handler
file_put_contents($attachment_path, $content)) file_put_contents($attachment_path, $content))
{ {
if (isset($fp)) fclose($fp); if (isset($fp)) fclose($fp);
header('Location: '.($location = '/mail/attachments/'.substr(basename($attachment_path), 8))); $location = '/mail/attachments/'.substr(basename($attachment_path), 8);
// allow to suppress location header with an "X-No-Location: true" header
if (($location_header = empty($_SERVER['HTTP_X_NO_LOCATION'])))
{
header('Location: '.Api\Framework::getUrl(Api\Framework::link('/groupdav.php'.$location)));
}
$ret = $location_header ? '201 Created' : '200 Ok';
echo json_encode([ echo json_encode([
'status' => 200, 'status' => (int)$ret,
'message' => 'Attachment stored', 'message' => 'Attachment stored',
'location' => $location, 'location' => $location,
], self::JSON_RESPONSE_OPTIONS); ], self::JSON_RESPONSE_OPTIONS);
return '200 Ok'; return $ret;
} }
throw new \Exception('Error storing attachment'); throw new \Exception('Error storing attachment');
} }
@ -528,6 +534,15 @@ class ApiHandler extends Api\CalDAV\Handler
$account = self::getMailAccount($user, $matches[2] ?? null); $account = self::getMailAccount($user, $matches[2] ?? null);
echo json_encode(self::returnVacation(self::getVacation($account->imapServer(), $user)), self::JSON_RESPONSE_OPTIONS); echo json_encode(self::returnVacation(self::getVacation($account->imapServer(), $user)), self::JSON_RESPONSE_OPTIONS);
return true; return true;
case preg_match('#^/mail/attachments/(([^/]+)--[^/.-]{6,})$#', $path, $matches) === 1:
if (!file_exists($tmp=$GLOBALS['egw_info']['server']['temp_dir'].'/attach--'.$matches[1]))
{
throw new \Exception("Attachment $path NOT found", 404);
}
Api\Header\Content::type($matches[2], '', filesize($tmp));
readfile($tmp);
exit;
} }
} }
catch (\Throwable $e) { catch (\Throwable $e) {