kopia lustrzana https://github.com/nextcloud/social
cache uploaded images
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>pull/657/head
rodzic
82e87b8aff
commit
00d88d8ab9
|
@ -48,9 +48,9 @@ use OCA\Social\Service\FollowService;
|
|||
use OCA\Social\Service\HashtagService;
|
||||
use OCA\Social\Service\LikeService;
|
||||
use OCA\Social\Service\MiscService;
|
||||
use OCA\Social\Service\StreamService;
|
||||
use OCA\Social\Service\PostService;
|
||||
use OCA\Social\Service\SearchService;
|
||||
use OCA\Social\Service\StreamService;
|
||||
use OCP\AppFramework\Controller;
|
||||
use OCP\AppFramework\Http;
|
||||
use OCP\AppFramework\Http\DataResponse;
|
||||
|
@ -174,6 +174,7 @@ class LocalController extends Controller {
|
|||
$post->addTo($this->get('to', $data, ''));
|
||||
$post->setType($this->get('type', $data, Stream::TYPE_PUBLIC));
|
||||
$post->setHashtags($this->getArray('hashtags', $data, []));
|
||||
$post->setAttachments($this->getArray('attachments', $data, []));
|
||||
|
||||
$activity = $this->postService->createPost($post, $token);
|
||||
|
||||
|
|
|
@ -303,8 +303,7 @@ class ACore extends Item implements JsonSerializable {
|
|||
$base = $this->withoutEndSlash($this->withBeginSlash($base));
|
||||
}
|
||||
|
||||
$uuid = $this->uuid();
|
||||
$this->setId($url . $base . '/' . $uuid);
|
||||
$this->setId($url . $base . '/' . $this->uuid());
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -33,6 +33,7 @@ namespace OCA\Social\Model;
|
|||
use daita\MySmallPhpTools\Traits\TArrayTools;
|
||||
use JsonSerializable;
|
||||
use OCA\Social\Model\ActivityPub\Actor\Person;
|
||||
use OCA\Social\Model\ActivityPub\Object\Document;
|
||||
|
||||
|
||||
/**
|
||||
|
@ -64,6 +65,11 @@ class Post implements JsonSerializable {
|
|||
/** @var array */
|
||||
private $hashtags = [];
|
||||
|
||||
/** @var string[] */
|
||||
private $attachments = [];
|
||||
|
||||
/** @var Document[] */
|
||||
private $documents = [];
|
||||
|
||||
/**
|
||||
* Post constructor.
|
||||
|
@ -170,6 +176,44 @@ class Post implements JsonSerializable {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getAttachments(): array {
|
||||
return $this->attachments;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $attachments
|
||||
*
|
||||
* @return Post
|
||||
*/
|
||||
public function setAttachments(array $attachments): Post {
|
||||
$this->attachments = $attachments;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Document[]
|
||||
*/
|
||||
public function getDocuments(): array {
|
||||
return $this->documents;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Document[] $documents
|
||||
*
|
||||
* @return Post
|
||||
*/
|
||||
public function setDocuments(array $documents): Post {
|
||||
$this->documents = $documents;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
|
@ -190,11 +234,13 @@ class Post implements JsonSerializable {
|
|||
*/
|
||||
public function jsonSerialize(): array {
|
||||
return [
|
||||
'actor' => $this->getActor(),
|
||||
'to' => $this->getTo(),
|
||||
'replyTo' => $this->getReplyTo(),
|
||||
'content' => $this->getContent(),
|
||||
'type' => $this->getType()
|
||||
'actor' => $this->getActor(),
|
||||
'to' => $this->getTo(),
|
||||
'replyTo' => $this->getReplyTo(),
|
||||
'content' => $this->getContent(),
|
||||
'attachments' => $this->getAttachments(),
|
||||
'hashtags' => $this->getHashtags(),
|
||||
'type' => $this->getType()
|
||||
];
|
||||
}
|
||||
|
||||
|
|
|
@ -39,6 +39,7 @@ use OCA\Social\Db\StreamRequest;
|
|||
use OCA\Social\Exceptions\ActorDoesNotExistException;
|
||||
use OCA\Social\Exceptions\EmptyQueueException;
|
||||
use OCA\Social\Exceptions\InvalidResourceException;
|
||||
use OCA\Social\Exceptions\ItemAlreadyExistsException;
|
||||
use OCA\Social\Exceptions\ItemUnknownException;
|
||||
use OCA\Social\Exceptions\NoHighPriorityRequestException;
|
||||
use OCA\Social\Exceptions\QueueStatusException;
|
||||
|
@ -437,17 +438,17 @@ class ActivityService {
|
|||
|
||||
|
||||
/**
|
||||
* @param ACore $activity
|
||||
* @param ACore $item
|
||||
*/
|
||||
private function saveObject(ACore $activity) {
|
||||
private function saveObject(ACore $item) {
|
||||
try {
|
||||
if ($activity->hasObject()) {
|
||||
$this->saveObject($activity->getObject());
|
||||
if ($item->hasObject()) {
|
||||
$this->saveObject($item->getObject());
|
||||
}
|
||||
|
||||
$service = AP::$activityPub->getInterfaceForItem($activity);
|
||||
$service->save($activity);
|
||||
} catch (ItemUnknownException $e) {
|
||||
$service = AP::$activityPub->getInterfaceForItem($item);
|
||||
$service->save($item);
|
||||
} catch (ItemUnknownException | ItemAlreadyExistsException $e) {
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -96,6 +96,22 @@ class CacheDocumentService {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Document $document
|
||||
* @param string $uploaded
|
||||
* @param string $mime
|
||||
*
|
||||
* @throws CacheContentMimeTypeException
|
||||
* @throws NotFoundException
|
||||
* @throws NotPermittedException
|
||||
*/
|
||||
public function saveLocalUploadToCache(Document $document, string $uploaded, string &$mime = '') {
|
||||
$content = $uploaded;
|
||||
|
||||
$this->saveContentToCache($document, $content, $mime);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Document $document
|
||||
* @param string $mime
|
||||
|
@ -112,9 +128,23 @@ class CacheDocumentService {
|
|||
* @throws SocialAppConfigException
|
||||
* @throws UnauthorizedFediverseException
|
||||
*/
|
||||
public function saveRemoteFileToCache(Document $document, &$mime = '') {
|
||||
public function saveRemoteFileToCache(Document $document, string &$mime = '') {
|
||||
$content = $this->retrieveContent($document->getUrl());
|
||||
|
||||
$this->saveContentToCache($document, $content, $mime);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Document $document
|
||||
* @param string $content
|
||||
* @param string $mime
|
||||
*
|
||||
* @throws CacheContentMimeTypeException
|
||||
* @throws NotFoundException
|
||||
* @throws NotPermittedException
|
||||
*/
|
||||
public function saveContentToCache(Document $document, string $content, string &$mime = '') {
|
||||
// To get the mime type, we create a temp file
|
||||
$tmpFile = tmpfile();
|
||||
$tmpPath = stream_get_meta_data($tmpFile)['uri'];
|
||||
|
@ -124,10 +154,10 @@ class CacheDocumentService {
|
|||
|
||||
$this->filterMimeTypes($mime);
|
||||
|
||||
$filename = $this->saveContentToCache($content);
|
||||
$filename = $this->generateFileFromContent($content);
|
||||
$document->setLocalCopy($filename);
|
||||
$this->resizeImage($content);
|
||||
$resized = $this->saveContentToCache($content);
|
||||
$resized = $this->generateFileFromContent($content);
|
||||
$document->setResizedCopy($resized);
|
||||
}
|
||||
|
||||
|
@ -139,7 +169,7 @@ class CacheDocumentService {
|
|||
* @throws NotPermittedException
|
||||
* @throws NotFoundException
|
||||
*/
|
||||
private function saveContentToCache(string $content): string {
|
||||
private function generateFileFromContent(string $content): string {
|
||||
|
||||
$filename = $this->uuid();
|
||||
// creating a path aa/bb/cc/dd/ from the filename aabbccdd-0123-[...]
|
||||
|
|
|
@ -31,10 +31,12 @@ namespace OCA\Social\Service;
|
|||
|
||||
|
||||
use daita\MySmallPhpTools\Exceptions\MalformedArrayException;
|
||||
use Exception;
|
||||
use OCA\Social\AP;
|
||||
use OCA\Social\Exceptions\CacheContentMimeTypeException;
|
||||
use OCA\Social\Exceptions\InvalidOriginException;
|
||||
use OCA\Social\Exceptions\InvalidResourceException;
|
||||
use OCA\Social\Exceptions\ItemUnknownException;
|
||||
use OCA\Social\Exceptions\StreamNotFoundException;
|
||||
use OCA\Social\Exceptions\RedundancyLimitException;
|
||||
use OCA\Social\Exceptions\RequestContentException;
|
||||
use OCA\Social\Exceptions\RequestNetworkException;
|
||||
|
@ -42,9 +44,15 @@ use OCA\Social\Exceptions\RequestResultNotJsonException;
|
|||
use OCA\Social\Exceptions\RequestResultSizeException;
|
||||
use OCA\Social\Exceptions\RequestServerException;
|
||||
use OCA\Social\Exceptions\SocialAppConfigException;
|
||||
use OCA\Social\Exceptions\StreamNotFoundException;
|
||||
use OCA\Social\Exceptions\UnauthorizedFediverseException;
|
||||
use OCA\Social\Exceptions\UrlCloudException;
|
||||
use OCA\Social\Model\ActivityPub\ACore;
|
||||
use OCA\Social\Model\ActivityPub\Object\Document;
|
||||
use OCA\Social\Model\ActivityPub\Object\Note;
|
||||
use OCA\Social\Model\Post;
|
||||
use OCP\Files\NotFoundException;
|
||||
use OCP\Files\NotPermittedException;
|
||||
|
||||
class PostService {
|
||||
|
||||
|
@ -58,6 +66,9 @@ class PostService {
|
|||
/** @var ActivityService */
|
||||
private $activityService;
|
||||
|
||||
/** @var CacheDocumentService */
|
||||
private $cacheDocumentService;
|
||||
|
||||
/** @var ConfigService */
|
||||
private $configService;
|
||||
|
||||
|
@ -71,16 +82,18 @@ class PostService {
|
|||
* @param StreamService $streamService
|
||||
* @param AccountService $accountService
|
||||
* @param ActivityService $activityService
|
||||
* @param CacheDocumentService $cacheDocumentService
|
||||
* @param ConfigService $configService
|
||||
* @param MiscService $miscService
|
||||
*/
|
||||
public function __construct(
|
||||
StreamService $streamService, AccountService $accountService, ActivityService $activityService,
|
||||
ConfigService $configService, MiscService $miscService
|
||||
CacheDocumentService $cacheDocumentService, ConfigService $configService, MiscService $miscService
|
||||
) {
|
||||
$this->streamService = $streamService;
|
||||
$this->accountService = $accountService;
|
||||
$this->activityService = $activityService;
|
||||
$this->cacheDocumentService = $cacheDocumentService;
|
||||
$this->configService = $configService;
|
||||
$this->miscService = $miscService;
|
||||
}
|
||||
|
@ -95,7 +108,6 @@ class PostService {
|
|||
* @throws InvalidResourceException
|
||||
* @throws ItemUnknownException
|
||||
* @throws MalformedArrayException
|
||||
* @throws StreamNotFoundException
|
||||
* @throws RedundancyLimitException
|
||||
* @throws RequestContentException
|
||||
* @throws RequestNetworkException
|
||||
|
@ -103,6 +115,8 @@ class PostService {
|
|||
* @throws RequestResultSizeException
|
||||
* @throws RequestServerException
|
||||
* @throws SocialAppConfigException
|
||||
* @throws StreamNotFoundException
|
||||
* @throws UnauthorizedFediverseException
|
||||
*/
|
||||
public function createPost(Post $post, &$token = ''): ACore {
|
||||
$note = new Note();
|
||||
|
@ -114,9 +128,12 @@ class PostService {
|
|||
|
||||
$note->setContent(htmlentities($post->getContent(), ENT_QUOTES));
|
||||
|
||||
$this->generateDocumentsFromAttachments($note, $post);
|
||||
|
||||
$this->streamService->replyTo($note, $post->getReplyTo());
|
||||
$this->streamService->addRecipients($note, $post->getType(), $post->getTo());
|
||||
$this->streamService->addHashtags($note, $post->getHashtags());
|
||||
$this->streamService->addAttachments($note, $post->getDocuments());
|
||||
|
||||
$token = $this->activityService->createActivity($actor, $note, $activity);
|
||||
$this->accountService->cacheLocalActorDetailCount($actor);
|
||||
|
@ -125,5 +142,57 @@ class PostService {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Note $note
|
||||
* @param Post $post
|
||||
*/
|
||||
private function generateDocumentsFromAttachments(Note $note, Post $post) {
|
||||
$documents = [];
|
||||
foreach ($post->getAttachments() as $attachment) {
|
||||
try {
|
||||
$document = $this->generateDocumentFromAttachment($note, $attachment);
|
||||
|
||||
$service = AP::$activityPub->getInterfaceForItem($document);
|
||||
$service->save($document);
|
||||
|
||||
$documents[] = $document;
|
||||
} catch (Exception $e) {
|
||||
}
|
||||
}
|
||||
$post->setDocuments($documents);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Note $note
|
||||
* @param string $attachment
|
||||
*
|
||||
* @return Document
|
||||
* @throws CacheContentMimeTypeException
|
||||
* @throws NotFoundException
|
||||
* @throws NotPermittedException
|
||||
* @throws SocialAppConfigException
|
||||
* @throws UrlCloudException
|
||||
*/
|
||||
private function generateDocumentFromAttachment(Note $note, string $attachment): Document {
|
||||
list(, $data) = explode(';', $attachment);
|
||||
list(, $data) = explode(',', $data);
|
||||
$content = base64_decode($data);
|
||||
|
||||
$document = new Document();
|
||||
$document->setUrlCloud($this->configService->getCloudUrl());
|
||||
$document->generateUniqueId('/documents/local');
|
||||
$document->setParentId($note->getId());
|
||||
$document->setPublic(true);
|
||||
|
||||
$mime = '';
|
||||
$this->cacheDocumentService->saveLocalUploadToCache($document, $content, $mime);
|
||||
$document->setMediaType($mime);
|
||||
$document->setMimeType($mime);
|
||||
|
||||
return $document;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -47,6 +47,7 @@ use OCA\Social\Exceptions\SocialAppConfigException;
|
|||
use OCA\Social\Exceptions\UnauthorizedFediverseException;
|
||||
use OCA\Social\Model\ActivityPub\ACore;
|
||||
use OCA\Social\Model\ActivityPub\Actor\Person;
|
||||
use OCA\Social\Model\ActivityPub\Object\Document;
|
||||
use OCA\Social\Model\ActivityPub\Object\Note;
|
||||
use OCA\Social\Model\ActivityPub\Stream;
|
||||
use OCA\Social\Model\InstancePath;
|
||||
|
@ -316,6 +317,15 @@ class StreamService {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Note $note
|
||||
* @param Document[] $documents
|
||||
*/
|
||||
public function addAttachments(Note $note, array $documents) {
|
||||
$note->setAttachments($documents);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Note $note
|
||||
* @param string $replyTo
|
||||
|
|
Ładowanie…
Reference in New Issue