Last active
November 13, 2018 11:16
-
-
Save jkalbhenn/889e886b52aab11808dceb06363d16e9 to your computer and use it in GitHub Desktop.
changes nextcloud 14 to determine mime-types from file content if the file exists and probably fixes some potential bugs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/apps/dav/lib/Connector/Sabre/File.php b/apps/dav/lib/Connector/Sabre/File.php | |
index e46bdcb298..2401b7158a 100644 | |
--- a/apps/dav/lib/Connector/Sabre/File.php | |
+++ b/apps/dav/lib/Connector/Sabre/File.php | |
@@ -165,7 +165,9 @@ class File extends Node implements IFile { | |
$this->changeLock(ILockingProvider::LOCK_EXCLUSIVE); | |
} | |
+ | |
$target = $partStorage->fopen($internalPartPath, 'wb'); | |
+ | |
if ($target === false) { | |
\OC::$server->getLogger()->error('\OC\Files\Filesystem::fopen() failed', ['app' => 'webdav']); | |
// because we have no clue about the cause we can only throw back a 500/Internal Server Error | |
@@ -224,6 +226,9 @@ class File extends Node implements IFile { | |
throw new Exception('Could not rename part file to final file'); | |
} | |
} catch (ForbiddenException $ex) { | |
+ # $partStorage->unlink did not remove the file | |
+ $localFile = $partStorage->getLocalFile($internalPartPath); | |
+ if(file_exists($localFile)) { unlink($localFile); } | |
throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry()); | |
} catch (\Exception $e) { | |
$partStorage->unlink($internalPartPath); | |
@@ -247,7 +252,7 @@ class File extends Node implements IFile { | |
$this->header('X-OC-MTime: accepted'); | |
} | |
} | |
- | |
+ | |
if ($view) { | |
$this->emitPostHooks($exists); | |
} | |
diff --git a/apps/workflowengine/lib/Check/FileMimeType.php b/apps/workflowengine/lib/Check/FileMimeType.php | |
index bd94ec9d5b..c44d4d0d5f 100644 | |
--- a/apps/workflowengine/lib/Check/FileMimeType.php | |
+++ b/apps/workflowengine/lib/Check/FileMimeType.php | |
@@ -72,9 +72,10 @@ class FileMimeType extends AbstractStringCheck { | |
* @return string | |
*/ | |
protected function getActualValue() { | |
- if ($this->mimeType[$this->storage->getId()][$this->path] !== null) { | |
- return $this->mimeType[$this->storage->getId()][$this->path]; | |
- } | |
+ # would read cached values. dont reuse old value after check for upload path that did not yet exist | |
+ #if ($this->mimeType[$this->storage->getId()][$this->path] !== null) { | |
+ # return $this->mimeType[$this->storage->getId()][$this->path]; | |
+ #} | |
if ($this->isWebDAVRequest()) { | |
// Creating a folder | |
@@ -85,10 +86,12 @@ class FileMimeType extends AbstractStringCheck { | |
if ($this->request->getMethod() === 'PUT' || $this->request->getMethod() === 'MOVE') { | |
if ($this->request->getMethod() === 'MOVE') { | |
- $this->mimeType[$this->storage->getId()][$this->path] = $this->mimeTypeDetector->detectPath($this->path); | |
+ $this->mimeType[$this->storage->getId()][$this->path] = $this->mimeTypeDetector->detect($this->path); | |
} else { | |
- $path = $this->request->getPathInfo(); | |
- $this->mimeType[$this->storage->getId()][$this->path] = $this->mimeTypeDetector->detectPath($path); | |
+ # path would be "webdav/filename" on upload and doesnt exist. | |
+ #$path = $this->request->getPathInfo(); | |
+ #$this->mimeType[$this->storage->getId()][$this->path] = $this->mimeTypeDetector->detect($path); | |
+ $this->mimeType[$this->storage->getId()][$this->path] = $this->detectMimetypeFromPath(); | |
} | |
return $this->mimeType[$this->storage->getId()][$this->path]; | |
} | |
@@ -99,7 +102,7 @@ class FileMimeType extends AbstractStringCheck { | |
$path = substr($path, strlen('/webdav')); | |
} | |
$path = $this->path . $path; | |
- $this->mimeType[$this->storage->getId()][$path] = $this->mimeTypeDetector->detectPath($path); | |
+ $this->mimeType[$this->storage->getId()][$path] = $this->mimeTypeDetector->detect($path); | |
return $this->mimeType[$this->storage->getId()][$path]; | |
} | |
} | |
@@ -110,7 +113,7 @@ class FileMimeType extends AbstractStringCheck { | |
$mimeType = $files['type'][0]; | |
if ($this->mimeType === 'application/octet-stream') { | |
// Maybe not... | |
- $mimeTypeTest = $this->mimeTypeDetector->detectPath($files['name'][0]); | |
+ $mimeTypeTest = $this->mimeTypeDetector->detect($files['name'][0]); | |
if ($mimeTypeTest !== 'application/octet-stream' && $mimeTypeTest !== false) { | |
$mimeType = $mimeTypeTest; | |
} else { | |
@@ -125,10 +128,7 @@ class FileMimeType extends AbstractStringCheck { | |
} | |
} | |
- $this->mimeType[$this->storage->getId()][$this->path] = $this->storage->getMimeType($this->path); | |
- if ($this->mimeType[$this->storage->getId()][$this->path] === 'application/octet-stream') { | |
- $this->mimeType[$this->storage->getId()][$this->path] = $this->detectMimetypeFromPath(); | |
- } | |
+ $this->mimeType[$this->storage->getId()][$this->path] = $this->detectMimetypeFromPath(); | |
return $this->mimeType[$this->storage->getId()][$this->path]; | |
} | |
@@ -137,11 +137,6 @@ class FileMimeType extends AbstractStringCheck { | |
* @return string | |
*/ | |
protected function detectMimetypeFromPath() { | |
- $mimeType = $this->mimeTypeDetector->detectPath($this->path); | |
- if ($mimeType !== 'application/octet-stream' && $mimeType !== false) { | |
- return $mimeType; | |
- } | |
- | |
if ($this->storage->instanceOfStorage('\OC\Files\Storage\Local') | |
|| $this->storage->instanceOfStorage('\OC\Files\Storage\Home') | |
|| $this->storage->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage')) { | |
@@ -155,15 +150,19 @@ class FileMimeType extends AbstractStringCheck { | |
return 'application/octet-stream'; | |
} else { | |
- $handle = $this->storage->fopen($this->path, 'r'); | |
- $data = fread($handle, 8024); | |
- fclose($handle); | |
- $mimeType = $this->mimeTypeDetector->detectString($data); | |
- if ($mimeType !== false) { | |
- return $mimeType; | |
+ if(file_exists($this->path)) { | |
+ $handle = $this->storage->fopen($this->path, 'r'); | |
+ $data = fread($handle, 8024); | |
+ fclose($handle); | |
+ $mimeType = $this->mimeTypeDetector->detectString($data); | |
+ if ($mimeType !== false) { | |
+ return $mimeType; | |
+ } | |
+ return 'application/octet-stream'; | |
+ } | |
+ else { | |
+ return $this->storage->getMimeType($this->path); | |
} | |
- | |
- return 'application/octet-stream'; | |
} | |
} | |
diff --git a/lib/private/Files/Type/Detection.php b/lib/private/Files/Type/Detection.php | |
index 3207562763..5fecc86406 100644 | |
--- a/lib/private/Files/Type/Detection.php | |
+++ b/lib/private/Files/Type/Detection.php | |
@@ -205,12 +205,16 @@ class Detection implements IMimeTypeDetector { | |
return "httpd/unix-directory"; | |
} | |
- $mimeType = $this->detectPath($path); | |
+ // dont try to test file content if file not existent | |
+ if (!file_exists($path)) { | |
+ return $this->detectPath($path); | |
+ } | |
- if ($mimeType === 'application/octet-stream' and function_exists('finfo_open') | |
+ if (function_exists('finfo_open') | |
and function_exists('finfo_file') and $finfo = finfo_open(FILEINFO_MIME) | |
) { | |
$info = @strtolower(finfo_file($finfo, $path)); | |
+ | |
finfo_close($finfo); | |
if ($info) { | |
$mimeType = strpos($info, ';') !== false ? substr($info, 0, strpos($info, ';')) : $info; | |
@@ -219,11 +223,11 @@ class Detection implements IMimeTypeDetector { | |
} | |
$isWrapped = (strpos($path, '://') !== false) and (substr($path, 0, 7) === 'file://'); | |
- if (!$isWrapped and $mimeType === 'application/octet-stream' && function_exists("mime_content_type")) { | |
+ if (!$isWrapped && function_exists("mime_content_type")) { | |
// use mime magic extension if available | |
$mimeType = mime_content_type($path); | |
} | |
- if (!$isWrapped and $mimeType === 'application/octet-stream' && \OC_Helper::canExecute("file")) { | |
+ if (!$isWrapped && \OC_Helper::canExecute("file")) { | |
// it looks like we have a 'file' command, | |
// lets see if it does have mime support | |
$path = escapeshellarg($path); | |
@@ -237,8 +241,13 @@ class Detection implements IMimeTypeDetector { | |
if (empty($mimeType)) { | |
$mimeType = 'application/octet-stream'; | |
} | |
+ } | |
+ // check that result is a mime type. the various tools used can set it to error strings | |
+ if (!preg_match("/^[a-zA-Z0-9-]+\/[a-zA-Z0-9-]+$/", $mimeType)) { | |
+ $mimeType = 'application/octet-stream'; | |
} | |
+ | |
return $mimeType; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment