util/dcp - support recursion and folders full of photos by importing files singly in separate processes - not yet tested

This commit is contained in:
zotlabs 2017-08-22 18:29:51 -07:00
parent 33528c616a
commit 9f37dbc6dc
3 changed files with 118 additions and 28 deletions

View File

@ -0,0 +1,45 @@
<?php /** @file */
namespace Zotlabs\Daemon;
class Importfile {
static public function run($argc,$argv){
if($argc < 3)
return;
$channel = channelx_by_n($argv[1]);
if(! $channel)
return;
$srcfile = $argv[2];
$folder = (($argc > 3) ? $argv[3] : '');
$dstname = (($argc > 4) ? $argv[4] : '');
$hash = random_string();
$arr = [
'src' => $srcfile,
'filename' => (($dstname) ? $dstname : basename($srcfile)),
'hash' => $hash,
'allow_cid' => $channel['channel_allow_cid'],
'allow_gid' => $channel['channel_allow_gid'],
'deny_cid' => $channel['channel_deny_cid'],
'deny_gid' => $channel['channel_deny_gid'],
'preserve_original' => true,
'replace' => true
];
if($folder)
$arr['folder'] = $folder;
attach_store($channel,$channel['channel_hash'],'import',$arr);
$sync = attach_export_data($channel,$hash);
if($sync)
build_sync_packet($channel['channel_id'],array('file' => array($sync)));
return;
}
}

View File

@ -460,6 +460,7 @@ function attach_store($channel, $observer_hash, $options = '', $arr = null) {
// By default remove $src when finished
$remove_when_processed = true;
$import_replace = false;
if($options === 'import') {
$src = $arr['src'];
@ -476,6 +477,9 @@ function attach_store($channel, $observer_hash, $options = '', $arr = null) {
if($arr['preserve_original'])
$remove_when_processed = false;
if($arr['replace'])
$import_replace = true;
// if importing a directory, just do it now and go home - we're done.
if(array_key_exists('is_dir',$arr) && intval($arr['is_dir'])) {
@ -617,8 +621,10 @@ function attach_store($channel, $observer_hash, $options = '', $arr = null) {
dbesc($folder_hash)
);
if($r) {
$overwrite = get_pconfig($channel_id,'system','overwrite_dup_files');
$overwrite = (($import_replace || get_pconfig($channel_id,'system','overwrite_dup_files')) ? true : false);
if(($overwrite) || ($options === 'import')) {
if(! array_key_exists('edited',$arr))
$arr['edited'] = datetime_convert();
$options = 'replace';
$existing_id = $x[0]['id'];
$existing_size = intval($x[0]['filesize']);

View File

@ -15,15 +15,15 @@ cli_startup();
if($argc <= 3) {
echo "Usage: " . $argv[0] . ' source destination' . "\n";
echo "Usage: " . $argv[0] . ' src dstdir' . "\n";
echo 'Always run from the toplevel web directory.' . "\n";
echo 'destination should begin with store/$nickname/desired/path or $nickname/desired/path' . "\n";
echo 'Example: util/dcp /etc/motd store/joe/etc/motd' . "\n";
echo 'Example: util/dcp /etc/motd store/joe/etc' . "\n";
exit;
}
$dstfile = $argv[$argc - 1];
$recursive = false;
$dstfile = $argv[$argc - 1];
if(strpos($dstfile,'store/') === 0)
$dstfile = substr($dstfile,6);
@ -45,6 +45,7 @@ if($argc <= 3) {
if($r && $r[0]['is_dir']) {
$isadir = true;
$basepath = $dstfile;
$folder = $r[0]['hash'];
}
else {
@ -64,29 +65,67 @@ if($argc <= 3) {
}
}
for($x = 1; $x < ($argc - 1); $x ++) {
$srcfile = $argv[$x];
if(($argv[$x] === '-r') || ($argv[$x] === '-R')) {
$recursive = true;
continue;
}
$hash = random_string();
$arr = [
'src' => $srcfile,
'filename' => basename($srcfile),
'hash' => $hash,
'allow_cid' => $channel['channel_allow_cid'],
'allow_gid' => $channel['channel_allow_gid'],
'deny_cid' => $channel['channel_deny_cid'],
'deny_gid' => $channel['channel_deny_gid'],
'preserve_original' => true,
];
if($folder)
$arr['folder'] = $folder;
attach_store($channel,$channel['channel_hash'],'import',$arr);
$sync = attach_export_data($channel,$hash);
if($sync)
build_sync_packet($channel['channel_id'],array('file' => array($sync)));
if(is_dir($argv[$x])) {
if($recursive) {
dcp_recurse($channel,$argv[$x],$basepath,$folder);
}
else {
continue;
}
}
else {
$dstname = (($isadir) ? '' : basename($dstfile));
$cmd = [ 'Importfile', $channel['channel_id'], $argv[$x], $folder, $dstname ];
\Zotlabs\Daemon\Master::Summon($cmd);
}
}
function dcp_recurse($channel,$src,$basepath,$folder) {
$dir = opendir($src);
if($dir) {
while(($entry = readdir($dir)) !== false) {
if($entry === '.' || $entry === '..')
continue;
$dstfile = $basepath . '/' . $entry;
if(is_dir($src . '/' . $entry)) {
$r = q("select * from attach where display_path = '%s' and uid = %d limit 1",
dbesc($dstfile),
intval($channel['channel_id'])
);
if($r && $r[0]['is_dir']) {
$folder = $r[0]['hash'];
}
else {
$arr = [
'pathname' => $dstfile,
'allow_cid' => $channel['channel_allow_cid'],
'allow_gid' => $channel['channel_allow_gid'],
'deny_cid' => $channel['channel_deny_cid'],
'deny_gid' => $channel['channel_deny_gid'],
];
$folder = '';
$x = attach_mkdirp($channel,$channel['channel_hash'],$arr);
if($x['success'])
$folder = $x['data']['hash'];
}
$basepath = $dstfile;
dcp_recurse($channel,$src . '/' . $entry,$basepath,$folder);
}
else {
$cmd = [ 'Importfile', $channel['channel_id'], $src . '/' . $entry, $folder ];
\Zotlabs\Daemon\Master::Summon($cmd);
}
}
closedir($dir);
}
}