more cloud updates - upgrade the DAV structures as well.

This commit is contained in:
zotlabs 2017-03-22 19:31:43 -07:00
parent 0fb0241901
commit 0499deea69
3 changed files with 79 additions and 41 deletions

View File

@ -49,7 +49,7 @@ class Directory extends DAV\Node implements DAV\ICollection, DAV\IQuota {
* @param BasicAuth &$auth_plugin * @param BasicAuth &$auth_plugin
*/ */
public function __construct($ext_path, &$auth_plugin) { public function __construct($ext_path, &$auth_plugin) {
// $ext_path = urldecode($ext_path); // $ext_path = urldecode($ext_path);
logger('directory ' . $ext_path, LOGGER_DATA); logger('directory ' . $ext_path, LOGGER_DATA);
$this->ext_path = $ext_path; $this->ext_path = $ext_path;
// remove "/cloud" from the beginning of the path // remove "/cloud" from the beginning of the path
@ -167,6 +167,14 @@ class Directory extends DAV\Node implements DAV\ICollection, DAV\IQuota {
intval($this->auth->owner_id) intval($this->auth->owner_id)
); );
$x = attach_syspaths($this->auth->owner_id,$this->folder_hash);
$y = q("update attach set display_path = '%s where hash = '%s' and uid = %d",
dbesc($x['path']),
dbesc($this->folder_hash),
intval($this->auth->owner_id)
);
$ch = channelx_by_n($this->auth->owner_id); $ch = channelx_by_n($this->auth->owner_id);
if ($ch) { if ($ch) {
$sync = attach_export_data($ch, $this->folder_hash); $sync = attach_export_data($ch, $this->folder_hash);
@ -260,14 +268,18 @@ class Directory extends DAV\Node implements DAV\ICollection, DAV\IQuota {
dbesc($f), dbesc($f),
dbesc(datetime_convert()), dbesc(datetime_convert()),
dbesc(datetime_convert()), dbesc(datetime_convert()),
'', //TODO: use os_path '',
'', //TODO: use display_path '',
dbesc($allow_cid), dbesc($allow_cid),
dbesc($allow_gid), dbesc($allow_gid),
dbesc($deny_cid), dbesc($deny_cid),
dbesc($deny_gid) dbesc($deny_gid)
); );
// fetch the actual storage paths
$xpath = attach_syspaths($this->auth->owner_id, $hash);
// returns the number of bytes that were written to the file, or FALSE on failure // returns the number of bytes that were written to the file, or FALSE on failure
$size = file_put_contents($f, $data); $size = file_put_contents($f, $data);
// delete attach entry if file_put_contents() failed // delete attach entry if file_put_contents() failed
@ -281,15 +293,17 @@ class Directory extends DAV\Node implements DAV\ICollection, DAV\IQuota {
$edited = datetime_convert(); $edited = datetime_convert();
$is_photo = 0; $is_photo = 0;
$x = @getimagesize($f); $gis = @getimagesize($f);
logger('getimagesize: ' . print_r($x,true), LOGGER_DATA); logger('getimagesize: ' . print_r($gis,true), LOGGER_DATA);
if (($x) && ($x[2] === IMAGETYPE_GIF || $x[2] === IMAGETYPE_JPEG || $x[2] === IMAGETYPE_PNG)) { if (($gis) && ($gis[2] === IMAGETYPE_GIF || $gis[2] === IMAGETYPE_JPEG || $gis[2] === IMAGETYPE_PNG)) {
$is_photo = 1; $is_photo = 1;
} }
// updates entry with filesize and timestamp // updates entry with filesize and timestamp
$d = q("UPDATE attach SET filesize = '%s', is_photo = %d, edited = '%s' WHERE hash = '%s' AND uid = %d", $d = q("UPDATE attach SET filesize = '%s', os_path = '%s', display_path = '%s', is_photo = %d, edited = '%s' WHERE hash = '%s' AND uid = %d",
dbesc($size), dbesc($size),
dbesc($xpath['os_path']),
dbesc($xpath['display_path']),
intval($is_photo), intval($is_photo),
dbesc($edited), dbesc($edited),
dbesc($hash), dbesc($hash),
@ -312,29 +326,29 @@ class Directory extends DAV\Node implements DAV\ICollection, DAV\IQuota {
// check against service class quota // check against service class quota
$limit = engr_units_to_bytes(service_class_fetch($c[0]['channel_id'], 'attach_upload_limit')); $limit = engr_units_to_bytes(service_class_fetch($c[0]['channel_id'], 'attach_upload_limit'));
if ($limit !== false) { if ($limit !== false) {
$x = q("SELECT SUM(filesize) AS total FROM attach WHERE aid = %d ", $z = q("SELECT SUM(filesize) AS total FROM attach WHERE aid = %d ",
intval($c[0]['channel_account_id']) intval($c[0]['channel_account_id'])
); );
if (($x) && ($x[0]['total'] + $size > $limit)) { if (($z) && ($z[0]['total'] + $size > $limit)) {
logger('service class limit exceeded for ' . $c[0]['channel_name'] . ' total usage is ' . $x[0]['total'] . ' limit is ' . userReadableSize($limit)); logger('service class limit exceeded for ' . $c[0]['channel_name'] . ' total usage is ' . $z[0]['total'] . ' limit is ' . userReadableSize($limit));
attach_delete($c[0]['channel_id'], $hash); attach_delete($c[0]['channel_id'], $hash);
return; return;
} }
} }
if ($is_photo) { if($is_photo) {
$album = ''; $album = '';
if ($this->folder_hash) { if ($this->folder_hash) {
$f1 = q("select filename from attach WHERE hash = '%s' AND uid = %d", $f1 = q("select filename, display_path from attach WHERE hash = '%s' AND uid = %d",
dbesc($this->folder_hash), dbesc($this->folder_hash),
intval($c[0]['channel_id']) intval($c[0]['channel_id'])
); );
if ($f1) if ($f1)
$album = $f1[0]['filename']; $album = (($f1[0]['display_path']) ? $f1[0]['display_path'] : $f1[0]['filename']);
} }
require_once('include/photos.php'); require_once('include/photos.php');
$args = array( 'resource_id' => $hash, 'album' => $album, 'os_path' => $f, 'filename' => $name, 'getimagesize' => $x, 'directory' => $direct); $args = array( 'resource_id' => $hash, 'album' => $album, 'os_syspath' => $f, 'os_path' => $xpath['os_path'], 'display_path' => $xpath['path'], 'filename' => $name, 'getimagesize' => $gis, 'directory' => $direct);
$p = photo_upload($c[0], \App::get_observer(), $args); $p = photo_upload($c[0], \App::get_observer(), $args);
} }
@ -646,20 +660,24 @@ class Directory extends DAV\Node implements DAV\ICollection, DAV\IQuota {
logger("Path mismatch: $path !== /$file"); logger("Path mismatch: $path !== /$file");
return NULL; return NULL;
} }
if(ACTIVE_DBTYPE == DBTYPE_POSTGRES) {
$prefix = 'DISTINCT ON (filename)';
$suffix = 'ORDER BY filename';
}
else {
$prefix = ''; $prefix = '';
$suffix = 'GROUP BY filename'; $suffix = '';
}
$r = q("select $prefix id, uid, hash, filename, filetype, filesize, revision, folder, flags, is_dir, created, edited from attach where folder = '%s' and uid = %d $perms $suffix", $r = q("select $prefix id, uid, hash, filename, filetype, filesize, revision, folder, flags, is_dir, created, edited from attach where folder = '%s' and uid = %d $perms $suffix",
dbesc($folder), dbesc($folder),
intval($channel_id) intval($channel_id)
); );
foreach ($r as $rr) { foreach ($r as $rr) {
// @FIXME I don't think we use revisions currently in attach structures.
// In case we see any in the wild provide a unique filename. This
// name may or may not be accessible
if($rr['revision'])
$rr['filename'] .= '-' . $rr['revision'];
//logger('filename: ' . $rr['filename'], LOGGER_DEBUG); //logger('filename: ' . $rr['filename'], LOGGER_DEBUG);
if (intval($rr['is_dir'])) { if (intval($rr['is_dir'])) {
$ret[] = new Directory($path . '/' . $rr['filename'], $auth); $ret[] = new Directory($path . '/' . $rr['filename'], $auth);

View File

@ -85,13 +85,23 @@ class File extends DAV\Node implements DAV\IFile {
intval($this->data['id']) intval($this->data['id'])
); );
$x = attach_syspaths($this->auth->owner_id,$this->data['hash']);
$y = q("update attach set display_path = '%s where hash = '%s' and uid = %d",
dbesc($x['path']),
dbesc($this->data['hash']),
intval($this->auth->owner_id)
);
if($this->data->is_photo) { if($this->data->is_photo) {
$r = q("update photo set filename = '%s' where resource_id = '%s' and uid = %d", $r = q("update photo set filename = '%s', display_path = '%s' where resource_id = '%s' and uid = %d",
dbesc($newName), dbesc($newName),
dbesc($x['path']),
dbesc($this->data['hash']), dbesc($this->data['hash']),
intval($this->auth->owner_id) intval($this->auth->owner_id)
); );
} }
$ch = channelx_by_n($this->auth->owner_id); $ch = channelx_by_n($this->auth->owner_id);
if($ch) { if($ch) {
$sync = attach_export_data($ch,$this->data['hash']); $sync = attach_export_data($ch,$this->data['hash']);

View File

@ -811,7 +811,7 @@ function attach_store($channel, $observer_hash, $options = '', $arr = null) {
if($is_photo) { if($is_photo) {
$args = array( 'source' => $source, 'visible' => $visible, 'resource_id' => $hash, 'album' => basename($pathname), 'os_syspath' => $os_basepath . $os_relpath, 'os_path' => $os_path, 'display_path' => $display_path, 'filename' => $filename, 'getimagesize' => $gis, 'directory' => $direct, 'options' => $options ); $args = array( 'source' => $source, 'visible' => $visible, 'resource_id' => $hash, 'album' => $pathname, 'os_syspath' => $os_basepath . $os_relpath, 'os_path' => $os_path, 'display_path' => $display_path, 'filename' => $filename, 'getimagesize' => $gis, 'directory' => $direct, 'options' => $options );
if($arr['contact_allow']) if($arr['contact_allow'])
$args['contact_allow'] = $arr['contact_allow']; $args['contact_allow'] = $arr['contact_allow'];
if($arr['group_allow']) if($arr['group_allow'])
@ -915,7 +915,7 @@ function z_readdir($channel_id, $observer_hash, $pathname, $parent_hash = '') {
if(count($paths) > 1) { if(count($paths) > 1) {
$curpath = array_shift($paths); $curpath = array_shift($paths);
$r = q("select hash, id, is_dir from attach where uid = %d and filename = '%s' and is_dir != 0 " . permissions_sql($channel_id) . " limit 1", $r = q("select hash, id, is_dir from attach where uid = %d and filename = '%s' and is_dir != 0 " . permissions_sql($channel_id,$observer_hash) . " limit 1",
intval($channel_id), intval($channel_id),
dbesc($curpath) dbesc($curpath)
); );
@ -969,12 +969,15 @@ function attach_mkdir($channel, $observer_hash, $arr = null) {
$sql_options = ''; $sql_options = '';
$basepath = 'store/' . $channel['channel_address']; $os_basepath = 'store/' . $channel['channel_address'];
logger('attach_mkdir: basepath: ' . $basepath); logger('attach_mkdir: basepath: ' . $os_basepath);
if(! is_dir($basepath)) if(! is_dir($os_basepath))
os_mkdir($basepath,STORAGE_DEFAULT_PERMISSIONS, true); os_mkdir($os_basepath,STORAGE_DEFAULT_PERMISSIONS, true);
$os_basepath .= '/';
if(! perm_is_allowed($channel_id, $observer_hash, 'write_storage')) { if(! perm_is_allowed($channel_id, $observer_hash, 'write_storage')) {
$ret['message'] = t('Permission denied.'); $ret['message'] = t('Permission denied.');
@ -1020,10 +1023,13 @@ function attach_mkdir($channel, $observer_hash, $arr = null) {
$lpath = ''; $lpath = '';
$lfile = $arr['folder']; $lfile = $arr['folder'];
$dpath = '';
$sql_options = permissions_sql($channel['channel_id']); $sql_options = permissions_sql($channel['channel_id']);
do { do {
$r = q("select filename, hash, flags, is_dir, folder from attach where uid = %d and hash = '%s' and is_dir != 0 $r = q("select filename, hash, flags, is_dir, folder, display_path from attach where uid = %d and hash = '%s' and is_dir = 1
$sql_options limit 1", $sql_options limit 1",
intval($channel['channel_id']), intval($channel['channel_id']),
dbesc($lfile) dbesc($lfile)
@ -1033,22 +1039,26 @@ function attach_mkdir($channel, $observer_hash, $arr = null) {
$ret['message'] = t('Path not found.'); $ret['message'] = t('Path not found.');
return $ret; return $ret;
} }
$dpath = $r[0]['filename'] . (($dpath) ? '/' . $dpath : '');
if($lfile) if($lfile)
$lpath = $r[0]['hash'] . '/' . $lpath; $lpath = $r[0]['hash'] . (($lpath) ? '/' . $lpath : '');
$lfile = $r[0]['folder']; $lfile = $r[0]['folder'];
} while ( ($r[0]['folder']) && intval($r[0]['is_dir'])) ; } while ( ($r[0]['folder']) && intval($r[0]['is_dir'])) ;
$path = $basepath . '/' . $lpath;
$path = $lpath;
} }
else else
$path = $basepath . '/'; $path = '';
$path .= $arr['hash'];
$created = datetime_convert(); $created = datetime_convert();
// not yet used $os_path = ltrim($path . '/' . $arr['hash'],'/');
$os_path = ''; $display_path = ltrim($dpath . '/' . $arr['filename'],'/');
$display_path = '';
$r = q("INSERT INTO attach ( aid, uid, hash, creator, filename, filetype, filesize, revision, folder, os_storage, is_dir, content, created, edited, os_path, display_path, allow_cid, allow_gid, deny_cid, deny_gid ) $r = q("INSERT INTO attach ( aid, uid, hash, creator, filename, filetype, filesize, revision, folder, os_storage, is_dir, content, created, edited, os_path, display_path, allow_cid, allow_gid, deny_cid, deny_gid )
VALUES ( %d, %d, '%s', '%s', '%s', '%s', %d, %d, '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' ) ", VALUES ( %d, %d, '%s', '%s', '%s', '%s', %d, %d, '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' ) ",
@ -1063,7 +1073,7 @@ function attach_mkdir($channel, $observer_hash, $arr = null) {
dbesc($arr['folder']), dbesc($arr['folder']),
intval(1), intval(1),
intval(1), intval(1),
dbescbin($path), dbescbin($os_basepath . $os_path),
dbesc($created), dbesc($created),
dbesc($created), dbesc($created),
dbesc($os_path), dbesc($os_path),
@ -1075,7 +1085,7 @@ function attach_mkdir($channel, $observer_hash, $arr = null) {
); );
if($r) { if($r) {
if(os_mkdir($path, STORAGE_DEFAULT_PERMISSIONS, true)) { if(os_mkdir($os_basepath . $os_path, STORAGE_DEFAULT_PERMISSIONS, true)) {
$ret['success'] = true; $ret['success'] = true;
// update the parent folder's lastmodified timestamp // update the parent folder's lastmodified timestamp
@ -1093,7 +1103,7 @@ function attach_mkdir($channel, $observer_hash, $arr = null) {
$ret['data'] = $z[0]; $ret['data'] = $z[0];
} }
else { else {
logger('attach_mkdir: ' . mkdir . ' ' . $path . ' failed.'); logger('attach_mkdir: ' . mkdir . ' ' . $os_basepath . $os_path . ' failed.');
$ret['message'] = t('mkdir failed.'); $ret['message'] = t('mkdir failed.');
} }
} }