Skip to content

Commit

Permalink
Merge pull request #18386 from nextcloud/backport/18236/stable17
Browse files Browse the repository at this point in the history
[stable17] Allow to detect mimetype by content
  • Loading branch information
rullzer committed Dec 12, 2019
2 parents 51dfddb + 790de0a commit 0a172d0
Show file tree
Hide file tree
Showing 12 changed files with 181 additions and 159 deletions.
3 changes: 2 additions & 1 deletion apps/workflowengine/lib/Check/AbstractStringCheck.php
Expand Up @@ -44,8 +44,9 @@ public function __construct(IL10N $l) {
/**
* @param IStorage $storage
* @param string $path
* @param bool $isDir
*/
public function setFileInfo(IStorage $storage, $path) {
public function setFileInfo(IStorage $storage, $path, $isDir = false) {
// Nothing changes here with a different path
}

Expand Down
105 changes: 21 additions & 84 deletions apps/workflowengine/lib/Check/FileMimeType.php
Expand Up @@ -58,13 +58,19 @@ public function __construct(IL10N $l, IRequest $request, IMimeTypeDetector $mime
/**
* @param IStorage $storage
* @param string $path
* @param bool $isDir
*/
public function setFileInfo(IStorage $storage, $path) {
public function setFileInfo(IStorage $storage, $path, $isDir = false) {
$this->storage = $storage;
$this->path = $path;
if (!isset($this->mimeType[$this->storage->getId()][$this->path])
|| $this->mimeType[$this->storage->getId()][$this->path] === '') {
$this->mimeType[$this->storage->getId()][$this->path] = null;

if ($isDir) {
$this->mimeType[$this->storage->getId()][$this->path] = 'httpd/unix-directory';
} else {
$this->mimeType[$this->storage->getId()][$this->path] = null;
}
}
}

Expand Down Expand Up @@ -103,93 +109,24 @@ protected function getActualValue() {
return $this->cacheAndReturnMimeType($this->storage->getId(), $this->path, 'httpd/unix-directory');
}

if ($this->isWebDAVRequest()) {
// Creating a folder
if ($this->request->getMethod() === 'MKCOL') {
return $this->cacheAndReturnMimeType($this->storage->getId(), $this->path, 'httpd/unix-directory');
}

if ($this->request->getMethod() === 'PUT' || $this->request->getMethod() === 'MOVE') {
if ($this->request->getMethod() === 'MOVE') {
$mimeType = $this->mimeTypeDetector->detectPath($this->path);
} else {
$path = $this->request->getPathInfo();
$mimeType = $this->mimeTypeDetector->detectPath($path);
}
return $this->cacheAndReturnMimeType($this->storage->getId(), $this->path, $mimeType);
}
} else if ($this->isPublicWebDAVRequest()) {
if ($this->request->getMethod() === 'PUT') {
$path = $this->request->getPathInfo();
if (strpos($path, '/webdav/') === 0) {
$path = substr($path, strlen('/webdav'));
}
$path = $this->path . $path;
$mimeType = $this->mimeTypeDetector->detectPath($path);
return $this->cacheAndReturnMimeType($this->storage->getId(), $path, $mimeType);
}
if ($this->storage->file_exists($this->path)) {
$path = $this->storage->getLocalFile($this->path);
$mimeType = $this->mimeTypeDetector->detectContent($path);
return $this->cacheAndReturnMimeType($this->storage->getId(), $this->path, $mimeType);
}

if (in_array($this->request->getMethod(), ['POST', 'PUT'])) {
$files = $this->request->getUploadedFile('files');
if (isset($files['type'][0])) {
$mimeType = $files['type'][0];
if ($mimeType === 'application/octet-stream') {
// Maybe not...
$mimeTypeTest = $this->mimeTypeDetector->detectPath($files['name'][0]);
if ($mimeTypeTest !== 'application/octet-stream' && $mimeTypeTest !== false) {
$mimeType = $mimeTypeTest;
} else {
$mimeTypeTest = $this->mimeTypeDetector->detect($files['tmp_name'][0]);
if ($mimeTypeTest !== 'application/octet-stream' && $mimeTypeTest !== false) {
$mimeType = $mimeTypeTest;
}
}
}
return $this->cacheAndReturnMimeType($this->storage->getId(), $this->path, $mimeType);
if ($this->isWebDAVRequest() || $this->isPublicWebDAVRequest()) {
// Creating a folder
if ($this->request->getMethod() === 'MKCOL') {
return 'httpd/unix-directory';
}
}

$mimeType = $this->storage->getMimeType($this->path);
if ($mimeType === 'application/octet-stream') {
$mimeType = $this->detectMimetypeFromPath();
}

return $this->cacheAndReturnMimeType($this->storage->getId(), $this->path, $mimeType);
}

/**
* @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')) {
$localFile = $this->storage->getLocalFile($this->path);
if ($localFile !== false) {
$mimeType = $this->mimeTypeDetector->detect($localFile);
if ($mimeType !== false) {
return $mimeType;
}
}

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;
}

return 'application/octet-stream';
}
// We do not cache this, as the file did not exist yet.
// In case it does in the future, we will check with detectContent()
// again to get the real mimetype of the content, rather than
// guessing it from the path.
return $this->mimeTypeDetector->detectPath($this->path);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion apps/workflowengine/lib/Check/FileName.php
Expand Up @@ -49,8 +49,9 @@ public function __construct(IL10N $l, IRequest $request) {
/**
* @param IStorage $storage
* @param string $path
* @param bool $isDir
*/
public function setFileInfo(IStorage $storage, $path) {
public function setFileInfo(IStorage $storage, $path, $isDir = false) {
$this->storage = $storage;
$this->path = $path;
}
Expand Down
3 changes: 2 additions & 1 deletion apps/workflowengine/lib/Check/FileSize.php
Expand Up @@ -51,8 +51,9 @@ public function __construct(IL10N $l, IRequest $request) {
/**
* @param IStorage $storage
* @param string $path
* @param bool $isDir
*/
public function setFileInfo(IStorage $storage, $path) {
public function setFileInfo(IStorage $storage, $path, $isDir = false) {
}

/**
Expand Down
3 changes: 2 additions & 1 deletion apps/workflowengine/lib/Check/FileSystemTags.php
Expand Up @@ -68,8 +68,9 @@ public function __construct(IL10N $l, ISystemTagManager $systemTagManager, ISyst
/**
* @param IStorage $storage
* @param string $path
* @param bool $isDir
*/
public function setFileInfo(IStorage $storage, $path) {
public function setFileInfo(IStorage $storage, $path, $isDir = false) {
$this->storage = $storage;
$this->path = $path;
}
Expand Down
3 changes: 2 additions & 1 deletion apps/workflowengine/lib/Check/RequestRemoteAddress.php
Expand Up @@ -47,8 +47,9 @@ public function __construct(IL10N $l, IRequest $request) {
/**
* @param IStorage $storage
* @param string $path
* @param bool $isDir
*/
public function setFileInfo(IStorage $storage, $path) {
public function setFileInfo(IStorage $storage, $path, $isDir = false) {
// A different path doesn't change time, so nothing to do here.
}

Expand Down
3 changes: 2 additions & 1 deletion apps/workflowengine/lib/Check/RequestTime.php
Expand Up @@ -52,8 +52,9 @@ public function __construct(IL10N $l, ITimeFactory $timeFactory) {
/**
* @param IStorage $storage
* @param string $path
* @param bool $isDir
*/
public function setFileInfo(IStorage $storage, $path) {
public function setFileInfo(IStorage $storage, $path, $isDir = false) {
// A different path doesn't change time, so nothing to do here.
}

Expand Down
3 changes: 2 additions & 1 deletion apps/workflowengine/lib/Check/UserGroupMembership.php
Expand Up @@ -60,8 +60,9 @@ public function __construct(IUserSession $userSession, IGroupManager $groupManag
/**
* @param IStorage $storage
* @param string $path
* @param bool $isDir
*/
public function setFileInfo(IStorage $storage, $path) {
public function setFileInfo(IStorage $storage, $path, $isDir = false) {
// A different path doesn't change group memberships, so nothing to do here.
}

Expand Down
82 changes: 58 additions & 24 deletions lib/private/Files/Type/Detection.php
Expand Up @@ -184,11 +184,14 @@ public function detectPath($path) {
if (strpos($fileName, '.') > 0) {

// remove versioning extension: name.v1508946057 and transfer extension: name.ocTransferId2057600214.part
$fileName = preg_replace('!((\.v\d+)|((.ocTransferId\d+)?.part))$!', '', $fileName);
$fileName = preg_replace('!((\.v\d+)|((\.ocTransferId\d+)?\.part))$!', '', $fileName);

//try to guess the type by the file extension
$extension = strtolower(strrchr($fileName, '.'));
$extension = substr($extension, 1); //remove leading .
$extension = strrchr($fileName, '.');
if ($extension !== false) {
$extension = strtolower($extension);
$extension = substr($extension, 1); //remove leading .
}
return (isset($this->mimetypes[$extension]) && isset($this->mimetypes[$extension][0]))
? $this->mimetypes[$extension][0]
: 'application/octet-stream';
Expand All @@ -198,54 +201,85 @@ public function detectPath($path) {
}

/**
* detect mimetype based on both filename and content
*
* detect mimetype only based on the content of file
* @param string $path
* @return string
* @since 18.0.0
*/
public function detect($path) {
public function detectContent(string $path): string {
$this->loadMappings();

if (@is_dir($path)) {
// directories are easy
return "httpd/unix-directory";
}

$mimeType = $this->detectPath($path);

if ($mimeType === 'application/octet-stream' and function_exists('finfo_open')
and function_exists('finfo_file') and $finfo = finfo_open(FILEINFO_MIME)
) {
$info = @strtolower(finfo_file($finfo, $path));
if (function_exists('finfo_open')
&& function_exists('finfo_file')
&& $finfo = finfo_open(FILEINFO_MIME)) {
$info = @finfo_file($finfo, $path);
finfo_close($finfo);
if ($info) {
$info = strtolower($info);
$mimeType = strpos($info, ';') !== false ? substr($info, 0, strpos($info, ';')) : $info;
return empty($mimeType) ? 'application/octet-stream' : $mimeType;
$mimeType = $this->getSecureMimeType($mimeType);
if ($mimeType !== 'application/octet-stream') {
return $mimeType;
}
}
}

if (strpos($path, '://') !== false && strpos($path, 'file://') === 0) {
// Is the file wrapped in a stream?
return 'application/octet-stream';
}
$isWrapped = (strpos($path, '://') !== false) and (substr($path, 0, 7) === 'file://');
if (!$isWrapped and $mimeType === 'application/octet-stream' && function_exists("mime_content_type")) {

if (function_exists('mime_content_type')) {
// use mime magic extension if available
$mimeType = mime_content_type($path);
if ($mimeType !== false) {
$mimeType = $this->getSecureMimeType($mimeType);
if ($mimeType !== 'application/octet-stream') {
return $mimeType;
}
}
}
if (!$isWrapped and $mimeType === 'application/octet-stream' && \OC_Helper::canExecute("file")) {

if (\OC_Helper::canExecute('file')) {
// it looks like we have a 'file' command,
// lets see if it does have mime support
$path = escapeshellarg($path);
$fp = popen("file -b --mime-type $path 2>/dev/null", "r");
$reply = fgets($fp);
$fp = popen("test -f $path && file -b --mime-type $path", 'r');
$mimeType = fgets($fp);
pclose($fp);

//trim the newline
$mimeType = trim($reply);

if (empty($mimeType)) {
$mimeType = 'application/octet-stream';
if ($mimeType !== false) {
//trim the newline
$mimeType = trim($mimeType);
$mimeType = $this->getSecureMimeType($mimeType);
if ($mimeType !== 'application/octet-stream') {
return $mimeType;
}
}

}
return $mimeType;
return 'application/octet-stream';
}

/**
* detect mimetype based on both filename and content
*
* @param string $path
* @return string
*/
public function detect($path) {
$mimeType = $this->detectPath($path);

if ($mimeType !== 'application/octet-stream') {
return $mimeType;
}

return $this->detectContent($path);
}

/**
Expand Down
10 changes: 9 additions & 1 deletion lib/public/Files/IMimeTypeDetector.php
Expand Up @@ -39,9 +39,17 @@ interface IMimeTypeDetector {
* @param string $path
* @return string
* @since 8.2.0
**/
*/
public function detectPath($path);

/**
* detect mimetype only based on the content of file
* @param string $path
* @return string
* @since 18.0.0
*/
public function detectContent(string $path): string;

/**
* detect mimetype based on both filename and content
*
Expand Down
3 changes: 2 additions & 1 deletion lib/public/WorkflowEngine/ICheck.php
Expand Up @@ -36,9 +36,10 @@ interface ICheck {
/**
* @param IStorage $storage
* @param string $path
* @param bool $isDir
* @since 9.1
*/
public function setFileInfo(IStorage $storage, $path);
public function setFileInfo(IStorage $storage, $path, $isDir = false);

/**
* @param string $operator
Expand Down

0 comments on commit 0a172d0

Please sign in to comment.