Commit bffb9a04 authored by Mario's avatar Mario
Browse files

files_ng: implement copy and fix issue with moving photo thumbnails

parent 9414fe49
Pipeline #367593 passed with stage
in 2 minutes and 46 seconds
......@@ -248,9 +248,10 @@ class Browser extends DAV\Browser\Plugin {
$attachIcon = ""; // "<a href=\"attach/".$attachHash."\" title=\"".$displayName."\"><i class=\"fa fa-arrow-circle-o-down\"></i></a>";
$lockstate = (($r[0]['allow_cid'] || $r[0]['allow_gid'] || $r[0]['deny_cid'] || $r[0]['deny_gid']) ? 'lock' : 'unlock');
$id = $this->findAttachIdByHash($attachHash);
// put the array for this file together
$ft['attachId'] = $this->findAttachIdByHash($attachHash);
$ft['attachId'] = $id;
$ft['fileStorageUrl'] = substr($relPath, 0, strpos($relPath, "cloud/")) . "filestorage/" . $this->auth->owner_nick;
$ft['icon'] = $icon;
$ft['photo_icon'] = $photo_icon;
......@@ -276,8 +277,12 @@ class Browser extends DAV\Browser\Plugin {
$ft['resource'] = $r[0]['hash'];
$ft['folder'] = $r[0]['folder'];
$ft['revision'] = $r[0]['revision'];
$ft['newfilename'] = ['newfilename', t('Change filename to'), $displayName];
$ft['newfolder'] = ['newfolder', t('Move to directory'), $r[0]['folder'], '', $folder_list];
$ft['newfilename'] = ['newfilename_' . $id, t('Change filename to'), $displayName];
$ft['newfolder'] = ['newfolder_' . $id, t('Move to directory'), $r[0]['folder'], '', $folder_list];
$ft['copy'] = array('copy_' . $id, t('Copy instead of move'), 0, '', array(t('No'), t('Yes')));
$ft['recurse'] = array('recurse_' . $id, t('Change permission fo all files and sub folders'), 0, '', array(t('No'), t('Yes')));
$ft['notify'] = array('notify_edit_' . $id, t('Show in your contacts shared folder'), 0, '', array(t('No'), t('Yes')));
$f[] = $ft;
......@@ -317,8 +322,6 @@ class Browser extends DAV\Browser\Plugin {
'$delete' => t('Delete'),
'$nick' => $this->auth->getCurrentUser(),
'$recurse' => array('recurse', t('Change permission fo all files and sub folders'), 0, '', array(t('No'), t('Yes'))),
'$notify' => array('notify_edit', t('Show in your contacts shared folder'), 0, '', array(t('No'), t('Yes'))),
'$cpdesc' => t('Copy/paste this code to attach file to a post'),
'$cpldesc' => t('Copy/paste this URL to link file from a web page'),
......
......@@ -2634,7 +2634,6 @@ function attach_move($channel_id, $resource_id, $new_folder_hash, $newname = '')
intval($r[0]['id'])
);
if($r[0]['is_photo']) {
q("update photo set album = '%s', filename = '%s', os_path = '%s', display_path = '%s'
where resource_id = '%s' and uid = %d",
......@@ -2646,11 +2645,22 @@ function attach_move($channel_id, $resource_id, $new_folder_hash, $newname = '')
intval($channel_id)
);
q("update photo set content = '%s' where resource_id = '%s' and uid = %d and imgscale = 0",
q("update photo set content = CASE imgscale WHEN 0 THEN '%s' ELSE CONCAT('%s', '-', imgscale) END where resource_id = '%s' and uid = %d and os_storage = 1",
dbescbin($newstorepath),
dbescbin($newstorepath),
dbesc($resource_id),
intval($channel_id)
);
// now rename the thumbnails in os_storage - the original should have been copied before already
$ps = q("SELECT content, imgscale FROM photo WHERE uid = %d AND resource_id = '%s' and imgscale > 0 and os_storage = 1",
intval($channel_id),
dbesc($resource_id)
);
foreach($ps as $p) {
rename($oldstorepath . '-' . $p['imgscale'], $p['content']);
}
}
if($r[0]['is_dir']) {
......@@ -2675,6 +2685,195 @@ function attach_move($channel_id, $resource_id, $new_folder_hash, $newname = '')
}
function attach_copy($channel_id, $resource_id, $new_folder_hash, $newname = '') {
$c = channelx_by_n($channel_id);
if(! ($c && $resource_id))
return false;
// find the resource to be moved
$r = q("select * from attach where hash = '%s' and uid = %d limit 1",
dbesc($resource_id),
intval($channel_id)
);
if(! $r) {
logger('resource_id not found');
return false;
}
$a = $r[0];
$new_resource_id = new_uuid();
$oldstorepath = dbunescbin($r[0]['content']);
// find the resource we are copying to
if($new_folder_hash) {
$n = q("select * from attach where hash = '%s' and uid = %d and is_dir = 1 limit 1",
dbesc($new_folder_hash),
intval($channel_id)
);
if(! $n) {
logger('new_folder_hash not found');
return false;
}
$newdirname = $n[0]['filename'];
$newalbumname = $n[0]['display_path'];
$newstorepath = dbunescbin($n[0]['content']) . '/' . $new_resource_id;
}
else {
// root directory
$newdirname = EMPTY_STR;
$newalbumname = EMPTY_STR;
$newstorepath = 'store/' . $c['channel_address'] . '/' . $new_resource_id;
}
if(is_dir($oldstorepath))
os_mkdir($newstorepath,STORAGE_DEFAULT_PERMISSIONS,true);
else
copy($oldstorepath,$newstorepath);
// duplicate detection. If 'overwrite' is specified, return false because we can't yet do that.
$oldfilename = $r[0]['filename'];
$filename = (($newname) ? basename($newname) : $oldfilename);
// don't do duplicate check unless our parent folder has changed.
if($r[0]['folder'] !== $new_folder_hash) {
$s = q("select filename, id, hash, filesize from attach where filename = '%s' and folder = '%s' ",
dbesc($filename),
dbesc($new_folder_hash)
);
if($s) {
$overwrite = get_pconfig($channel_id,'system','overwrite_dup_files');
if($overwrite) {
/// @fixme
return;
}
else {
if(strpos($filename,'.') !== false) {
$basename = substr($filename,0,strrpos($filename,'.'));
$ext = substr($filename,strrpos($filename,'.'));
}
else {
$basename = $filename;
$ext = '';
}
$matches = false;
if(preg_match('/(.*?)\([0-9]{1,}\)$/',$basename,$matches))
$basename = $matches[1];
$v = q("select filename from attach where uid = %d and ( filename = '%s' OR filename like '%s' ) and folder = '%s' ",
intval($channel_id),
dbesc($basename . $ext),
dbesc($basename . '(%)' . $ext),
dbesc($new_folder_hash)
);
if($v) {
$x = 1;
do {
$found = false;
foreach($v as $vv) {
if($vv['filename'] === $basename . '(' . $x . ')' . $ext) {
$found = true;
break;
}
}
if($found)
$x++;
}
while($found);
$filename = $basename . '(' . $x . ')' . $ext;
}
else
$filename = $basename . $ext;
}
}
}
/*
q("update attach set content = '%s', folder = '%s', filename = '%s' where id = %d",
dbescbin($newstorepath),
dbesc($new_folder_hash),
dbesc($filename),
intval($r[0]['id'])
);
*/
unset($a['id']);
$a['hash'] = $new_resource_id;
$a['content'] = $newstorepath;
$a['folder'] = $new_folder_hash;
$a['filename'] = $filename;
create_table_from_array('attach', $a, ['content']);
$x = attach_syspaths($channel_id, $new_resource_id);
q("update attach set os_path = '%s', display_path = '%s' where hash = '%s'",
dbesc($x['os_path']),
dbesc($x['path']),
dbesc($new_resource_id)
);
if($a['is_photo']) {
$ps = q("SELECT * FROM photo WHERE uid = %d AND resource_id = '%s'",
intval($channel_id),
dbesc($resource_id)
);
foreach($ps as $p) {
unset($p['id']);
$p['resource_id'] = $new_resource_id;
$p['album'] = $newalbumname;
$p['filename'] = $filename;
$p['os_path'] = $x['os_path'];
$p['display_path'] = $x['path'];
if($p['os_storage']) {
$p['content'] = (($p['imgscale'] == 0) ? $newstorepath : $newstorepath . '-' . $p['imgscale']);
//the original should have been copied before already
if($p['imgscale'] > 0)
copy($oldstorepath, $p['content']);
}
create_table_from_array('photo', $p, ['content']);
}
}
if($r[0]['is_dir']) {
$copy_success = true;
$x = q("select hash from attach where folder = '%s' and uid = %d",
dbesc($r[0]['hash']),
intval($channel_id)
);
if($x) {
foreach($x as $xv) {
$rs = attach_copy($channel_id,$xv['hash'],$new_resource_id);
if(! $rs) {
$copy_success = false;
break;
}
}
}
return $copy_success;
}
return true;
}
/**
* Used to generate a select input box of all your folders
*/
......
......@@ -77,13 +77,15 @@
<tr id="cloud-tools-{{$item.attachId}}" class="cloud-tools">
<td id="attach-edit-panel-{{$item.attachId}}" colspan="9">
<form id="attach_edit_form_{{$item.attachId}}" action="attach_edit/{{$nick}}/{{$item.attachId}}" method="post" class="acl-form" data-form_id="attach_edit_form_{{$item.attachId}}" data-allow_cid='{{$item.allow_cid}}' data-allow_gid='{{$item.allow_gid}}' data-deny_cid='{{$item.deny_cid}}' data-deny_gid='{{$item.deny_gid}}'>
<input type="hidden" name="attach_id" value="{{$item.attachId}}" />
<input type="hidden" name="resource" value="{{$item.resource}}" />
<input type="hidden" name="filename" value="{{$item.displayName}}" />
<input type="hidden" name="folder" value="{{$item.folder}}" />
{{include file="field_input.tpl" field=$item.newfilename}}
{{include file="field_select.tpl" field=$item.newfolder}}
{{if !$item.collection}}{{include file="field_checkbox.tpl" field=$notify}}{{/if}}
{{if $item.collection}}{{include file="field_checkbox.tpl" field=$recurse}}{{/if}}
{{include file="field_checkbox.tpl" field=$item.copy}}
{{if !$item.collection}}{{include file="field_checkbox.tpl" field=$item.notify}}{{/if}}
{{if $item.collection}}{{include file="field_checkbox.tpl" field=$item.recurse}}{{/if}}
<div id="attach-edit-tools-share" class="btn-group form-group">
{{if !$item.collection}}
<a href="/rpost?attachment=[attachment]{{$item.resource}},{{$item.revision}}[/attachment]" id="attach-btn" class="btn btn-outline-secondary btn-sm" title="{{$attach_btn_title}}">
......
Supports Markdown
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