Skip to content

Commit

Permalink
fix broken extension, make pathinfo handle .tar.(gz|bz2)
Browse files Browse the repository at this point in the history
  • Loading branch information
butonic authored and DeepDiver1975 committed Sep 29, 2016
1 parent 071e2eb commit ba40861
Show file tree
Hide file tree
Showing 12 changed files with 214 additions and 37 deletions.
16 changes: 4 additions & 12 deletions apps/files_external/lib/Lib/Storage/SMB.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,8 @@ public function fopen($path, $mode) {
if (!$this->isCreatable(dirname($path))) {
break;
}
$tmpFile = \OC::$server->getTempManager()->getTemporaryFile();
$ext = \OCP\Files::pathinfo($path, PATHINFO_EXTENSION);
$tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext);
}
$source = fopen($tmpFile, $mode);
$share = $this->share;
Expand Down Expand Up @@ -512,18 +513,9 @@ public function filetype($path) {
public function mkdir($path) {
$this->log('enter: '.__FUNCTION__."($path)");
$result = false;
$dirs = explode('/', $path);
$currDir = '';
$path = $this->buildPath($path);
try {
foreach ($dirs as $dir) {
$currDir .= '/'.$dir;
try {
$path = $this->buildPath($currDir);
$result = $this->share->mkdir($path);
} catch (AlreadyExistsException $e) {
$this->swallow(__FUNCTION__, $e);
}
}
$result = $this->share->mkdir($path);
} catch (ConnectException $e) {
$ex = new StorageNotAvailableException(
$e->getMessage(), $e->getCode(), $e);
Expand Down
2 changes: 1 addition & 1 deletion apps/files_sharing/lib/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ public static function stripUserFilesPath($path) {
* @return string $path
*/
public static function generateUniqueTarget($path, $excludeList, $view) {
$pathinfo = pathinfo($path);
$pathinfo = \OCP\Files::pathinfo($path);
$ext = (isset($pathinfo['extension'])) ? '.'.$pathinfo['extension'] : '';
$name = $pathinfo['filename'];
$dir = $pathinfo['dirname'];
Expand Down
2 changes: 1 addition & 1 deletion apps/files_sharing/lib/SharedMount.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private function updateFileTarget($newPath, &$share) {
* @return mixed
*/
private function generateUniqueTarget($path, $view, array $mountpoints) {
$pathinfo = pathinfo($path);
$pathinfo = \OCP\Files::pathinfo($path);
$ext = (isset($pathinfo['extension'])) ? '.'.$pathinfo['extension'] : '';
$name = $pathinfo['filename'];
$dir = $pathinfo['dirname'];
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Files/Cache/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public function renameFromStorage(IStorage $sourceStorage, $source, $target) {
}
}

if (pathinfo($source, PATHINFO_EXTENSION) !== pathinfo($target, PATHINFO_EXTENSION)) {
if (\OCP\Files::pathinfo($source, PATHINFO_EXTENSION) !== \OCP\Files::pathinfo($target, PATHINFO_EXTENSION)) {
// handle mime type change
$mimeType = $this->storage->getMimeType($target);
$fileId = $this->cache->getId($target);
Expand Down
6 changes: 1 addition & 5 deletions lib/private/Files/ObjectStore/ObjectStoreStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,7 @@ public function fopen($path, $mode) {
case 'x+':
case 'c':
case 'c+':
if (strrpos($path, '.') !== false) {
$ext = substr($path, strrpos($path, '.'));
} else {
$ext = '';
}
$ext = \OCP\Files::pathinfo($path, PATHINFO_EXTENSION);
$tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext);
\OC\Files\Stream\Close::registerCallback($tmpFile, [$this, 'writeBack']);
if ($this->file_exists($path)) {
Expand Down
7 changes: 2 additions & 5 deletions lib/private/Files/Storage/DAV.php
Original file line number Diff line number Diff line change
Expand Up @@ -388,16 +388,12 @@ public function fopen($path, $mode) {
case 'c+':
//emulate these
$tempManager = \OC::$server->getTempManager();
if (strrpos($path, '.') !== false) {
$ext = substr($path, strrpos($path, '.'));
} else {
$ext = '';
}
if ($this->file_exists($path)) {
if (!$this->isUpdatable($path)) {
return false;
}
if ($mode === 'w' or $mode === 'w+') {
$ext = \OCP\Files::pathinfo($path, PATHINFO_EXTENSION);
$tmpFile = $tempManager->getTemporaryFile($ext);
} else {
$tmpFile = $this->getCachedFile($path);
Expand All @@ -406,6 +402,7 @@ public function fopen($path, $mode) {
if (!$this->isCreatable(dirname($path))) {
return false;
}
$ext = \OCP\Files::pathinfo($path, PATHINFO_EXTENSION);
$tmpFile = $tempManager->getTemporaryFile($ext);
}
Close::registerCallback($tmpFile, [$this, 'writeBack']);
Expand Down
3 changes: 2 additions & 1 deletion lib/private/Files/Storage/LocalTempFileTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ protected function toTmpFile($path) { //no longer in the storage api, still usef
if (!$source) {
return false;
}
$tmpFile = \OC::$server->getTempManager()->getTemporaryFile();
$ext = \OCP\Files::pathinfo($path, PATHINFO_EXTENSION);
$tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext);
$target = fopen($tmpFile, 'w');
\OC_Helper::streamCopy($source, $target);
fclose($target);
Expand Down
4 changes: 2 additions & 2 deletions lib/private/Files/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -971,8 +971,8 @@ public function toTmpFile($path) {
if (Filesystem::isValidPath($path)) {
$source = $this->fopen($path, 'r');
if ($source) {
$extension = pathinfo($path, PATHINFO_EXTENSION);
$tmpFile = \OC::$server->getTempManager()->getTemporaryFile($extension);
$ext = \OCP\Files::pathinfo($path, PATHINFO_EXTENSION);
$tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext);
file_put_contents($tmpFile, $source);
return $tmpFile;
} else {
Expand Down
15 changes: 7 additions & 8 deletions lib/private/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,25 +271,24 @@ public static function updateAppByOCSId($ocsId) {
public static function downloadApp($data = []) {
$l = \OC::$server->getL10N('lib');

if(!isset($data['source'])) {
if (!isset($data['source'])) {
throw new \Exception($l->t("No source specified when installing app"));
}

//download the file if necessary
if($data['source']=='http') {
$pathInfo = pathinfo($data['href']);
$extension = isset($pathInfo['extension']) ? '.' . $pathInfo['extension'] : '';
$path = \OC::$server->getTempManager()->getTemporaryFile($extension);
if ($data['source'] === 'http') {
if(!isset($data['href'])) {
throw new \Exception($l->t("No href specified when installing app from http"));
}
$ext = \OCP\Files::pathinfo($data['href'], PATHINFO_EXTENSION);
$path = \OC::$server->getTempManager()->getTemporaryFile($ext);
$client = \OC::$server->getHTTPClientService()->newClient();
$client->get($data['href'], ['save_to' => $path]);
} else {
if(!isset($data['path'])) {
throw new \Exception($l->t("No path specified when installing app from local file"));
}
$path=$data['path'];
$path = $data['path'];
}

//detect the archive type
Expand All @@ -302,11 +301,11 @@ public static function downloadApp($data = []) {
$extractDir = \OC::$server->getTempManager()->getTemporaryFolder();
OC_Helper::rmdirr($extractDir);
mkdir($extractDir);
if($archive=\OC\Archive\Archive::open($path)) {
if ($archive=\OC\Archive\Archive::open($path)) {
$archive->extract($extractDir);
} else {
OC_Helper::rmdirr($extractDir);
if($data['source']=='http') {
if ($data['source']=='http') {
unlink($path);
}
throw new \Exception($l->t("Failed to open archive when installing app"));
Expand Down
40 changes: 39 additions & 1 deletion lib/public/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static function rmdirr( $dir ) {
}

/**
* Get the mimetype form a local file
* Get the mimetype of a local file
* @param string $path
* @return string
* does NOT work for ownClouds filesystem, use OC_FileSystem::getMimeType instead
Expand All @@ -64,6 +64,44 @@ static function getMimeType( $path ) {
return \OC::$server->getMimeTypeDetector()->detect($path);
}

/**
* Get the pathinfo of a path with special handling for tar.(gz|bz2) which
* have different mimetypes
* @param string $path
* @return array|string|null
* @since 9.2.0
*/
static function pathinfo( $path, $options = PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME ) {
$result = pathinfo($path, $options);
// handle .tar.(gz|bz2)
if (is_array($result)) {
if (isset($result['extension'])) {
if ($result['extension'] === 'gz' && substr($path, -7) === '.tar.gz') {
$result['extension'] = 'tar.gz';
if (isset($result['filename'])) {
$result['filename'] = substr($result['filename'], 0, -4);
}
} else if ($result['extension'] === 'bz2' && substr($path, -8) === '.tar.bz2') {
$result['extension'] = 'tar.bz2';
if (isset($result['filename'])) {
$result['filename'] = substr($result['filename'], 0, -4);
}
}
}
} else if ($options === PATHINFO_EXTENSION) {
if ($result === 'gz' && substr($path, -7) === '.tar.gz') {
$result = 'tar.gz';
} else if ($result === 'bz2' && substr($path, -8) === '.tar.bz2') {
$result = 'tar.bz2';
}
} else if ($options === PATHINFO_FILENAME) {
if (substr($path, -7) === '.tar.gz' || substr($path, -8) === '.tar.bz2' ) {
$result = substr($result, 0, -4);
}
}
return $result;
}

/**
* Search for files by mimetype
* @param string $mimetype
Expand Down
6 changes: 6 additions & 0 deletions tests/lib/Files/Storage/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ public function testWriteWithDotInPath() {
// filename does not contain a dot / extension, but parent dirs do ...
$path = '/something/that/is/not.supposed/to happen/lorem';
$lorem = 'lorem ipsum dolor sit ...';

//FIXME test and implement recursive mkdir
self::assertTrue($this->instance->mkdir('/something'));
self::assertTrue($this->instance->mkdir('/something/that'));
self::assertTrue($this->instance->mkdir('/something/that/is'));
self::assertTrue($this->instance->mkdir('/something/that/is/not.supposed'));
self::assertTrue($this->instance->mkdir('/something/that/is/not.supposed/to happen'));

//use fopen with w+ to walk affected code path
Expand Down
148 changes: 148 additions & 0 deletions tests/lib/FilesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,152 @@ public function testSetUploadLimitWrite(
);
$this->assertEquals($userIniSize + $userIniSizeMod, filesize($files['.user.ini']));
}



public function pathinfoProvider() {
return [
[
'/www/htdocs/inc/lib.inc.php',
PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME,
[
'dirname' => '/www/htdocs/inc',
'basename' => 'lib.inc.php',
'extension' => 'php',
'filename' => 'lib.inc',
]
],
[
'/something/that/is/not.supposed/to happen/lorem.gz',
PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME,
[
'dirname' => '/something/that/is/not.supposed/to happen',
'basename' => 'lorem.gz',
'extension' => 'gz',
'filename' => 'lorem',
]
],
[
'/something/that/is/not.supposed/to happen/lorem.tar.gz',
PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME,
[
'dirname' => '/something/that/is/not.supposed/to happen',
'basename' => 'lorem.tar.gz',
'extension' => 'tar.gz',
'filename' => 'lorem',
]
],
[
'/something/that/is/not.supposed/to happen/lorem.bz2',
PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME,
[
'dirname' => '/something/that/is/not.supposed/to happen',
'basename' => 'lorem.bz2',
'extension' => 'bz2',
'filename' => 'lorem',
]
],
[
'/something/that/is/not.supposed/to happen/lorem.tar.bz2',
PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME,
[
'dirname' => '/something/that/is/not.supposed/to happen',
'basename' => 'lorem.tar.bz2',
'extension' => 'tar.bz2',
'filename' => 'lorem',
]
],

[
'/www/htdocs/inc/lib.inc.php',
PATHINFO_DIRNAME, '/www/htdocs/inc',
],
[
'/something/that/is/not.supposed/to happen/lorem.gz',
PATHINFO_DIRNAME, '/something/that/is/not.supposed/to happen'
],
[
'/something/that/is/not.supposed/to happen/lorem.tar.gz',
PATHINFO_DIRNAME, '/something/that/is/not.supposed/to happen'
],
[
'/something/that/is/not.supposed/to happen/lorem.bz2',
PATHINFO_DIRNAME, '/something/that/is/not.supposed/to happen'
],
[
'/something/that/is/not.supposed/to happen/lorem.tar.bz2',
PATHINFO_DIRNAME, '/something/that/is/not.supposed/to happen'
],

[
'/www/htdocs/inc/lib.inc.php',
PATHINFO_BASENAME, 'lib.inc.php',
],
[
'/something/that/is/not.supposed/to happen/lorem.gz',
PATHINFO_BASENAME, 'lorem.gz'
],
[
'/something/that/is/not.supposed/to happen/lorem.tar.gz',
PATHINFO_BASENAME, 'lorem.tar.gz'
],
[
'/something/that/is/not.supposed/to happen/lorem.bz2',
PATHINFO_BASENAME, 'lorem.bz2'
],
[
'/something/that/is/not.supposed/to happen/lorem.tar.bz2',
PATHINFO_BASENAME, 'lorem.tar.bz2'
],

[
'/www/htdocs/inc/lib.inc.php',
PATHINFO_EXTENSION, 'php',
],
[
'/something/that/is/not.supposed/to happen/lorem.gz',
PATHINFO_EXTENSION, 'gz',
],
[
'/something/that/is/not.supposed/to happen/lorem.tar.gz',
PATHINFO_EXTENSION, 'tar.gz',
],
[
'/something/that/is/not.supposed/to happen/lorem.bz2',
PATHINFO_EXTENSION, 'bz2',
],
[
'/something/that/is/not.supposed/to happen/lorem.tar.bz2',
PATHINFO_EXTENSION, 'tar.bz2',
],

[
'/www/htdocs/inc/lib.inc.php',
PATHINFO_FILENAME, 'lib.inc',
],
[
'/something/that/is/not.supposed/to happen/lorem.gz',
PATHINFO_FILENAME, 'lorem',
],
[
'/something/that/is/not.supposed/to happen/lorem.tar.gz',
PATHINFO_FILENAME, 'lorem',
],
[
'/something/that/is/not.supposed/to happen/lorem.bz2',
PATHINFO_FILENAME, 'lorem',
],
[
'/something/that/is/not.supposed/to happen/lorem.tar.bz2',
PATHINFO_FILENAME, 'lorem',
],
];
}
/**
* @dataProvider pathinfoProvider
*/
public function testPathinfo($path, $options, $expectedResult) {
$result = \OCP\Files::pathinfo($path, $options);
self::assertEquals($expectedResult, $result);
}
}

0 comments on commit ba40861

Please sign in to comment.