[ActivityPub] Attachment fetch should happen on StoreRemoteMedia
This commit is contained in:
parent
e51520bd63
commit
f9290705f8
@ -236,14 +236,40 @@ class Activitypub_notice
|
|||||||
$attachments = [];
|
$attachments = [];
|
||||||
if (isset($object['attachment']) && is_array($object['attachment'])) {
|
if (isset($object['attachment']) && is_array($object['attachment'])) {
|
||||||
foreach ($object['attachment'] as $attachment) {
|
foreach ($object['attachment'] as $attachment) {
|
||||||
if (array_key_exists('type', $attachment) && $attachment['type'] == 'Document') {
|
if (array_key_exists('type', $attachment)
|
||||||
|
&& $attachment['type'] === 'Document'
|
||||||
|
&& array_key_exists('url', $attachment)) {
|
||||||
try {
|
try {
|
||||||
// throws exception on failure
|
$file = new File();
|
||||||
$attachment = MediaFile::fromUrl($attachment['url'], $actor_profile, $attachment['name']);
|
$file->url = $attachment['url'];
|
||||||
$act->enclosures[] = $attachment->getEnclosure();
|
$file->title = array_key_exists('type', $attachment) ? $attachment['name'] : null;
|
||||||
$attachments[] = $attachment;
|
if (array_key_exists('type', $attachment)) {
|
||||||
|
$file->mimetype = $attachment['mediaType'];
|
||||||
|
} else {
|
||||||
|
$http = new HTTPClient();
|
||||||
|
common_debug(
|
||||||
|
'Performing HEAD request for incoming activity '
|
||||||
|
. 'to avoid unnecessarily downloading too '
|
||||||
|
. 'large files. URL: ' . $file->url
|
||||||
|
);
|
||||||
|
$head = $http->head($file->url);
|
||||||
|
$headers = $head->getHeader();
|
||||||
|
$headers = array_change_key_case($headers, CASE_LOWER);
|
||||||
|
if (array_key_exists('content-type', $headers)) {
|
||||||
|
$file->mimetype = $headers['content-type'];
|
||||||
|
} else {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (array_key_exists('content-length', $headers)) {
|
||||||
|
$file->size = $headers['content-length'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$file->saveFile();
|
||||||
|
$act->enclosures[] = $file->getEnclosure();
|
||||||
|
$attachments[] = $file;
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
// Whatever.
|
// Whatever.
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -259,8 +285,8 @@ class Activitypub_notice
|
|||||||
$note = Notice::saveActivity($act, $actor_profile, $options);
|
$note = Notice::saveActivity($act, $actor_profile, $options);
|
||||||
|
|
||||||
// Attachments (last part)
|
// Attachments (last part)
|
||||||
foreach($attachments as $attachment) {
|
foreach ($attachments as $file) {
|
||||||
$attachment->attachToNotice($note);
|
File_to_post::processNew($file, $note);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $note;
|
return $note;
|
||||||
|
Loading…
Reference in New Issue
Block a user