Commit 2dc80be6 authored by Zot's avatar Zot

New addon: hzfiles for syncing files across hubzilla servers. The selective...

New addon: hzfiles for syncing files across hubzilla servers. The selective timestamp option will only work on 2.9 or greater.
parent b3aee016
<?php
require_once('include/cli_startup.php');
require_once('include/attach.php');
cli_startup();
$attach_id = $argv[1];
$channel_address = $argv[2];
$hz_server = urldecode($argv[3]);
// define('FILESYNCTEST', 1);
$channel = channelx_by_nick($channel_address);
if(! $channel) {
logger('redfilehelper: channel not found');
killme();
}
$headers = [];
$headers['X-API-Token'] = random_string();
$headers['X-API-Request'] = $hz_server . '/api/z/1.0/file/export?f=&file_id=' . $attach_id;
$headers = \Zotlabs\Web\HTTPSig::create_sig('',$headers,$channel['channel_prvkey'],
'acct:' . $channel['channel_address'] . '@' . \App::get_hostname(),false,true,'sha512');
$x = z_fetch_url($hz_server . '/api/z/1.0/file/export?f=&file_id=' . $attach_id,false,$redirects,[ 'headers' => $headers ]);
if(! $x['success']) {
logger('no API response');
return;
}
$j = json_decode($x['body'],true);
if(defined('FILESYNCTEST')) {
logger('data: ' . print_r($j,true));
}
else {
$r = sync_files($channel,$j);
}
killme();
<?php
/**
* Name: Hubzilla Cloud File copy
* Description: import hubzilla file storage from another site
* Version: 1.0
* Author: Mike Macgirvin
* Maintainer: Mike Macgirvin <mike@macgirvin.com>
*/
function hzfiles_install() {}
function hzfiles_uninstall() {}
function hzfiles_module() {}
function hzfiles_post(&$a) {
if(! local_channel())
return;
$channel = App::get_channel();
$hz_server = $_REQUEST['hz_server'];
// The API will convert these to UTC.
$since = datetime_convert(date_default_timezone_get(),date_default_timezone_get(),$_REQUEST['since']);
$until = datetime_convert(date_default_timezone_get(),date_default_timezone_get(),$_REQUEST['until']);
$headers = [];
$headers['X-API-Token'] = random_string();
$headers['X-API-Request'] = $hz_server . '/api/z/1.0/files?f=&since=' . urlencode($since) . '&until=' . urlencode($until);
$headers = \Zotlabs\Web\HTTPSig::create_sig('',$headers,$channel['channel_prvkey'],
'acct:' . $channel['channel_address'] . '@' . \App::get_hostname(),false,true,'sha512');
$x = z_fetch_url($hz_server . '/api/z/1.0/files?f=&since=' . urlencode($since) . '&until=' . urlencode($until),false,$redirects,[ 'headers' => $headers ]);
if(! $x['success']) {
logger('no API response');
return;
}
$j = json_decode($x['body'],true);
if(! $j['success'])
return;
$poll_interval = get_config('system','poll_interval',3);
if(count($j['results'])) {
$todo = count($j['results']);
logger('total to process: ' . $todo);
foreach($j['results'] as $jj) {
// logger('json data: ' . print_r($jj,true));
proc_run('php','addon/hzfiles/hzfilehelper.php',$jj['hash'], $channel['channel_address'], urlencode($hz_server));
sleep($poll_interval);
}
goaway(z_root() . '/cloud/' . $channel['channel_address']);
}
}
function hzfiles_content(&$a) {
if(! local_channel()) {
notice( t('Permission denied') . EOL);
return;
}
$o = replace_macros(get_markup_template('hzfiles.tpl','addon/hzfiles'),array(
'$header' => t('Hubzilla File Storage Import'),
'$desc' => t('This will import all your cloud files from another server.'),
'$fr_server' => array('hz_server', t('Hubzilla Server base URL'),'',''),
'$since' => array('since', t('Since modified date yyyy-mm-dd'),'0001-01-01',''),
'$until' => array('until', t('Until modified date yyyy-mm-dd'),datetime_convert('UTC',date_default_timezone_get(),'now','Y-m-d'),''),
'$submit' => t('Submit'),
));
return $o;
}
<h3>{{$header}}</h3>
<p class="descriptive-text">{{$desc}}</p>
<form action="hzfiles" method="post" >
{{include file="field_input.tpl" field=$fr_server}}
{{include file="field_input.tpl" field=$since}}
{{include file="field_input.tpl" field=$until}}
<input type="submit" name="submit" value="{{$submit}}" />
</form>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment