Skip to content

Commit

Permalink
Merge pull request #12288 from owncloud/backport-mapper-fixes
Browse files Browse the repository at this point in the history
Backport mapper fixes
  • Loading branch information
nickvergessen committed Nov 19, 2014
2 parents 6a6b645 + f235e8b commit 54607a2
Showing 1 changed file with 49 additions and 26 deletions.
75 changes: 49 additions & 26 deletions lib/private/files/mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ public function removePath($path, $isLogicPath, $recursive) {
*/
public function copy($path1, $path2)
{
$path1 = $this->stripLast($path1);
$path2 = $this->stripLast($path2);
$path1 = $this->resolveRelativePath($path1);
$path2 = $this->resolveRelativePath($path2);
$physicPath1 = $this->logicToPhysical($path1, true);
$physicPath2 = $this->logicToPhysical($path2, true);

Expand Down Expand Up @@ -113,18 +113,11 @@ public function stripRootFolder($path, $root) {
return '';
}

private function stripLast($path) {
if (substr($path, -1) == '/') {
$path = substr_replace($path, '', -1);
}
return $path;
}

/**
* @param string $logicPath
*/
private function resolveLogicPath($logicPath) {
$logicPath = $this->stripLast($logicPath);
$logicPath = $this->resolveRelativePath($logicPath);
$sql = 'SELECT * FROM `*PREFIX*file_map` WHERE `logic_path_hash` = ?';
$result = \OC_DB::executeAudited($sql, array(md5($logicPath)));
$result = $result->fetchRow();
Expand All @@ -136,20 +129,43 @@ private function resolveLogicPath($logicPath) {
}

private function resolvePhysicalPath($physicalPath) {
$physicalPath = $this->stripLast($physicalPath);
$physicalPath = $this->resolveRelativePath($physicalPath);
$sql = \OC_DB::prepare('SELECT * FROM `*PREFIX*file_map` WHERE `physic_path_hash` = ?');
$result = \OC_DB::executeAudited($sql, array(md5($physicalPath)));
$result = $result->fetchRow();

return $result['logic_path'];
}

private function resolveRelativePath($path) {
$explodedPath = explode('/', $path);
$pathArray = array();
foreach ($explodedPath as $pathElement) {
if (empty($pathElement) || ($pathElement == '.')) {
continue;
} elseif ($pathElement == '..') {
if (count($pathArray) == 0) {
return false;
}
array_pop($pathArray);
} else {
array_push($pathArray, $pathElement);
}
}
if (substr($path, 0, 1) == '/') {
$path = '/';
} else {
$path = '';
}
return $path.implode('/', $pathArray);
}

/**
* @param string $logicPath
* @param boolean $store
*/
private function create($logicPath, $store) {
$logicPath = $this->stripLast($logicPath);
$logicPath = $this->resolveRelativePath($logicPath);
$index = 0;

// create the slugified path
Expand Down Expand Up @@ -177,14 +193,12 @@ private function insert($logicPath, $physicalPath) {
/**
* @param integer $index
*/
public function slugifyPath($path, $index=null) {
public function slugifyPath($path, $index = null) {
$path = $this->stripRootFolder($path, $this->unchangedPhysicalRoot);

$pathElements = explode('/', $path);
$sluggedElements = array();

$last= end($pathElements);


foreach ($pathElements as $pathElement) {
// remove empty elements
if (empty($pathElement)) {
Expand All @@ -196,20 +210,19 @@ public function slugifyPath($path, $index=null) {

// apply index to file name
if ($index !== null) {
$last= array_pop($sluggedElements);
$last = array_pop($sluggedElements);

// if filename contains periods - add index number before last period
if (preg_match('~\.[^\.]+$~i',$last,$extension)){
array_push($sluggedElements, substr($last,0,-(strlen($extension[0]))).'-'.$index.$extension[0]);
if (preg_match('~\.[^\.]+$~i', $last, $extension)) {
array_push($sluggedElements, substr($last, 0, -(strlen($extension[0]))) . '-' . $index . $extension[0]);
} else {
// if filename doesn't contain periods add index ofter the last char
array_push($sluggedElements, $last.'-'.$index);
}

array_push($sluggedElements, $last . '-' . $index);
}
}

$sluggedPath = $this->unchangedPhysicalRoot.implode('/', $sluggedElements);
return $this->stripLast($sluggedPath);
return $this->resolveRelativePath($sluggedPath);
}

/**
Expand All @@ -218,8 +231,8 @@ public function slugifyPath($path, $index=null) {
* @param string $text
* @return string
*/
private function slugify($text)
{
private function slugify($text) {
$originalText = $text;
// replace non letter or digits or dots by -
$text = preg_replace('~[^\\pL\d\.]+~u', '-', $text);

Expand All @@ -241,7 +254,17 @@ private function slugify($text)
$text = preg_replace('~\.+$~', '', $text);

if (empty($text)) {
return uniqid();
/**
* Item slug would be empty. Previously we used uniqid() here.
* However this means that the behaviour is not reproducible, so
* when uploading files into a "empty" folder, the folders name is
* different.
*
* If there would be a md5() hash collision, the deduplicate check
* will spot this and append an index later, so this should not be
* a problem.
*/
return md5($originalText);
}

return $text;
Expand Down

0 comments on commit 54607a2

Please sign in to comment.