Twitter: Improve uploading of images
This commit is contained in:
parent
d1d3c73d25
commit
5da3cf67ce
|
@ -88,6 +88,7 @@ use Friendica\Model\User;
|
||||||
use Friendica\Protocol\Activity;
|
use Friendica\Protocol\Activity;
|
||||||
use Friendica\Core\Config\Util\ConfigFileLoader;
|
use Friendica\Core\Config\Util\ConfigFileLoader;
|
||||||
use Friendica\Core\System;
|
use Friendica\Core\System;
|
||||||
|
use Friendica\Model\Photo;
|
||||||
use Friendica\Util\DateTimeFormat;
|
use Friendica\Util\DateTimeFormat;
|
||||||
use Friendica\Util\Images;
|
use Friendica\Util\Images;
|
||||||
use Friendica\Util\Strings;
|
use Friendica\Util\Strings;
|
||||||
|
@ -781,37 +782,29 @@ function twitter_post_hook(App $a, array &$b)
|
||||||
// and now tweet it :-)
|
// and now tweet it :-)
|
||||||
$post = [];
|
$post = [];
|
||||||
|
|
||||||
if (!empty($msgarr['images'])) {
|
if (!empty($msgarr['images']) || !empty($msgarr['remote_images'])) {
|
||||||
Logger::info('Got images', ['id' => $b['id'], 'images' => $msgarr['images']]);
|
Logger::info('Got images', ['id' => $b['id'], 'images' => $msgarr['images'], 'remote_images' => $msgarr['remote_images']]);
|
||||||
try {
|
try {
|
||||||
$media_ids = [];
|
$media_ids = [];
|
||||||
foreach ($msgarr['images'] as $image) {
|
foreach ($msgarr['images'] ?? [] as $image) {
|
||||||
if (count($media_ids) == 4) {
|
if (count($media_ids) == 4) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
$media_ids[] = twitter_upload_image($connection, $cb, $image, $b);
|
||||||
|
} catch (\Throwable $th) {
|
||||||
|
Logger::warning('Error while uploading image', ['code' => $th->getCode(), 'message' => $th->getMessage()]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$img_str = DI::httpClient()->fetch($image['url']);
|
foreach ($msgarr['remote_images'] ?? [] as $image) {
|
||||||
|
if (count($media_ids) == 4) {
|
||||||
$tempfile = tempnam(System::getTempPath(), 'cache');
|
continue;
|
||||||
file_put_contents($tempfile, $img_str);
|
}
|
||||||
|
try {
|
||||||
Logger::info('Uploading', ['id' => $b['id'], 'image' => $image['url']]);
|
$media_ids[] = twitter_upload_image($connection, $cb, $image, $b);
|
||||||
$media = $connection->upload('media/upload', ['media' => $tempfile]);
|
} catch (\Throwable $th) {
|
||||||
|
Logger::warning('Error while uploading image', ['code' => $th->getCode(), 'message' => $th->getMessage()]);
|
||||||
unlink($tempfile);
|
|
||||||
|
|
||||||
if (isset($media->media_id_string)) {
|
|
||||||
$media_ids[] = $media->media_id_string;
|
|
||||||
|
|
||||||
if (!empty($image['description'])) {
|
|
||||||
$data = ['media_id' => $media->media_id_string,
|
|
||||||
'alt_text' => ['text' => substr($image['description'], 0, 420)]];
|
|
||||||
$ret = $cb->media_metadata_create($data);
|
|
||||||
Logger::info('Metadata create', ['id' => $b['id'], 'data' => $data, 'return' => $ret]);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Logger::error('Failed upload', ['id' => $b['id'], 'image' => $image['url'], 'return' => $media]);
|
|
||||||
throw new Exception('Failed upload of ' . $image['url']);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$post['media_ids'] = implode(',', $media_ids);
|
$post['media_ids'] = implode(',', $media_ids);
|
||||||
|
@ -888,6 +881,39 @@ function twitter_post_hook(App $a, array &$b)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function twitter_upload_image($connection, $cb, array $image, array $item)
|
||||||
|
{
|
||||||
|
if (!empty($image['id'])) {
|
||||||
|
$photo = Photo::selectFirst([], ['id' => $image['id']]);
|
||||||
|
} else {
|
||||||
|
$photo = Photo::createPhotoForExternalResource($image['url']);
|
||||||
|
}
|
||||||
|
|
||||||
|
$tempfile = tempnam(System::getTempPath(), 'cache');
|
||||||
|
file_put_contents($tempfile, Photo::getImageForPhoto($photo));
|
||||||
|
|
||||||
|
Logger::info('Uploading', ['id' => $item['id'], 'image' => $image]);
|
||||||
|
$media = $connection->upload('media/upload', ['media' => $tempfile]);
|
||||||
|
|
||||||
|
unlink($tempfile);
|
||||||
|
|
||||||
|
if (isset($media->media_id_string)) {
|
||||||
|
$media_id = $media->media_id_string;
|
||||||
|
|
||||||
|
if (!empty($image['description'])) {
|
||||||
|
$data = ['media_id' => $media->media_id_string,
|
||||||
|
'alt_text' => ['text' => substr($image['description'], 0, 420)]];
|
||||||
|
$ret = $cb->media_metadata_create($data);
|
||||||
|
Logger::info('Metadata create', ['id' => $item['id'], 'data' => $data, 'return' => $ret]);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Logger::error('Failed upload', ['id' => $item['id'], 'image' => $image['url'], 'return' => $media]);
|
||||||
|
throw new Exception('Failed upload of ' . $image['url']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $media_id;
|
||||||
|
}
|
||||||
|
|
||||||
function twitter_delete_item(array $item)
|
function twitter_delete_item(array $item)
|
||||||
{
|
{
|
||||||
if (!$item['deleted']) {
|
if (!$item['deleted']) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user