streams/Zotlabs/Daemon/File_importer.php

54 lines
1.4 KiB
PHP
Raw Normal View History

<?php
namespace Zotlabs\Daemon;
use Zotlabs\Web\HTTPSig;
2022-01-25 01:26:12 +00:00
use Zotlabs\Lib\Channel;
require_once('include/cli_startup.php');
require_once('include/attach.php');
require_once('include/import.php');
2021-12-03 03:01:39 +00:00
class File_importer
{
2021-12-03 03:01:39 +00:00
public static function run($argc, $argv)
{
2021-12-03 03:01:39 +00:00
cli_startup();
2021-12-03 03:01:39 +00:00
$attach_id = $argv[1];
$channel_address = $argv[2];
$hz_server = urldecode($argv[3]);
2021-12-03 03:01:39 +00:00
$m = parse_url($hz_server);
2022-01-25 01:26:12 +00:00
$channel = Channel::from_username($channel_address);
2021-12-03 03:01:39 +00:00
if (! $channel) {
logger('filehelper: channel not found');
killme();
}
2021-12-03 03:01:39 +00:00
$headers = [
'X-API-Token' => random_string(),
'X-API-Request' => $hz_server . '/api/z/1.0/file/export?f=&zap_compat=1&file_id=' . $attach_id,
'Host' => $m['host'],
'(request-target)' => 'get /api/z/1.0/file/export?f=&zap_compat=1&file_id=' . $attach_id,
];
2022-01-25 01:26:12 +00:00
$headers = HTTPSig::create_sig($headers, $channel['channel_prvkey'], Channel::url($channel), true, 'sha512');
2021-12-03 03:01:39 +00:00
$x = z_fetch_url($hz_server . '/api/z/1.0/file/export?f=&zap_compat=1&file_id=' . $attach_id, false, $redirects, [ 'headers' => $headers ]);
2021-12-03 03:01:39 +00:00
if (! $x['success']) {
logger('no API response', LOGGER_DEBUG);
return;
}
2021-12-03 03:01:39 +00:00
$j = json_decode($x['body'], true);
2021-12-03 03:01:39 +00:00
$r = sync_files($channel, [$j]);
2021-12-03 03:01:39 +00:00
killme();
}
}