From f9290705f8533c971885d85559395b8cd226f795 Mon Sep 17 00:00:00 2001 From: Diogo Peralta Cordeiro Date: Mon, 21 Sep 2020 21:54:14 +0100 Subject: [PATCH] [ActivityPub] Attachment fetch should happen on StoreRemoteMedia --- .../lib/models/Activitypub_notice.php | 42 +++++++++++++++---- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/plugins/ActivityPub/lib/models/Activitypub_notice.php b/plugins/ActivityPub/lib/models/Activitypub_notice.php index dc31565558..11ecd853d2 100644 --- a/plugins/ActivityPub/lib/models/Activitypub_notice.php +++ b/plugins/ActivityPub/lib/models/Activitypub_notice.php @@ -85,7 +85,7 @@ class Activitypub_notice 'id' => self::getUri($notice), 'type' => 'Delete', // XXX: A bit of ugly code here - 'object' => array_merge(Activitypub_tombstone::tombstone_to_array((int)substr(explode(':', $notice->getUri())[2],9)), ['deleted' => str_replace(' ', 'T', $notice->getCreated()) . 'Z']), + 'object' => array_merge(Activitypub_tombstone::tombstone_to_array((int)substr(explode(':', $notice->getUri())[2], 9)), ['deleted' => str_replace(' ', 'T', $notice->getCreated()) . 'Z']), 'url' => $notice->getUrl(), 'actor' => $profile->getUri(), 'to' => $to, @@ -236,14 +236,40 @@ class Activitypub_notice $attachments = []; if (isset($object['attachment']) && is_array($object['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 { - // throws exception on failure - $attachment = MediaFile::fromUrl($attachment['url'], $actor_profile, $attachment['name']); - $act->enclosures[] = $attachment->getEnclosure(); - $attachments[] = $attachment; + $file = new File(); + $file->url = $attachment['url']; + $file->title = array_key_exists('type', $attachment) ? $attachment['name'] : null; + 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) { // Whatever. + continue; } } } @@ -259,8 +285,8 @@ class Activitypub_notice $note = Notice::saveActivity($act, $actor_profile, $options); // Attachments (last part) - foreach($attachments as $attachment) { - $attachment->attachToNotice($note); + foreach ($attachments as $file) { + File_to_post::processNew($file, $note); } return $note;