Merge pull request #18236 from nextcloud/bugfix/noid/always-detect-mimetype-by-content-in-workflows
Allow to detect mimetype by content
This commit is contained in:
commit
c6e51924c8
|
@ -57,12 +57,18 @@ class FileMimeType extends AbstractStringCheck implements IFileCheck {
|
||||||
/**
|
/**
|
||||||
* @param IStorage $storage
|
* @param IStorage $storage
|
||||||
* @param string $path
|
* @param string $path
|
||||||
|
* @param bool $isDir
|
||||||
*/
|
*/
|
||||||
public function setFileInfo(IStorage $storage, string $path) {
|
public function setFileInfo(IStorage $storage, string $path, bool $isDir = false): void {
|
||||||
$this->_setFileInfo($storage, $path);
|
$this->_setFileInfo($storage, $path, $isDir);
|
||||||
if (!isset($this->mimeType[$this->storage->getId()][$this->path])
|
if (!isset($this->mimeType[$this->storage->getId()][$this->path])
|
||||||
|| $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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -101,93 +107,24 @@ class FileMimeType extends AbstractStringCheck implements IFileCheck {
|
||||||
return $this->cacheAndReturnMimeType($this->storage->getId(), $this->path, 'httpd/unix-directory');
|
return $this->cacheAndReturnMimeType($this->storage->getId(), $this->path, 'httpd/unix-directory');
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->isWebDAVRequest()) {
|
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 ($this->isWebDAVRequest() || $this->isPublicWebDAVRequest()) {
|
||||||
// Creating a folder
|
// Creating a folder
|
||||||
if ($this->request->getMethod() === 'MKCOL') {
|
if ($this->request->getMethod() === 'MKCOL') {
|
||||||
return $this->cacheAndReturnMimeType($this->storage->getId(), $this->path, 'httpd/unix-directory');
|
return '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 (in_array($this->request->getMethod(), ['POST', 'PUT'])) {
|
// We do not cache this, as the file did not exist yet.
|
||||||
$files = $this->request->getUploadedFile('files');
|
// In case it does in the future, we will check with detectContent()
|
||||||
if (isset($files['type'][0])) {
|
// again to get the real mimetype of the content, rather than
|
||||||
$mimeType = $files['type'][0];
|
// guessing it from the path.
|
||||||
if ($mimeType === 'application/octet-stream') {
|
return $this->mimeTypeDetector->detectPath($this->path);
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$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';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -37,14 +37,19 @@ trait TFileCheck {
|
||||||
/** @var string */
|
/** @var string */
|
||||||
protected $path;
|
protected $path;
|
||||||
|
|
||||||
|
/** @var bool */
|
||||||
|
protected $isDir;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param IStorage $storage
|
* @param IStorage $storage
|
||||||
* @param string $path
|
* @param string $path
|
||||||
|
* @param bool $isDir
|
||||||
* @since 18.0.0
|
* @since 18.0.0
|
||||||
*/
|
*/
|
||||||
public function setFileInfo(IStorage $storage, string $path) {
|
public function setFileInfo(IStorage $storage, string $path, bool $isDir = false): void {
|
||||||
$this->storage = $storage;
|
$this->storage = $storage;
|
||||||
$this->path = $path;
|
$this->path = $path;
|
||||||
|
$this->isDir = $isDir;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -71,9 +71,10 @@ class RuleMatcher implements IRuleMatcher {
|
||||||
$this->l = $l;
|
$this->l = $l;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setFileInfo(IStorage $storage, string $path): void {
|
public function setFileInfo(IStorage $storage, string $path, bool $isDir = false): void {
|
||||||
$this->fileInfo['storage'] = $storage;
|
$this->fileInfo['storage'] = $storage;
|
||||||
$this->fileInfo['path'] = $path;
|
$this->fileInfo['path'] = $path;
|
||||||
|
$this->fileInfo['isDir'] = $isDir;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setEntitySubject(IEntity $entity, $subject): void {
|
public function setEntitySubject(IEntity $entity, $subject): void {
|
||||||
|
@ -168,7 +169,7 @@ class RuleMatcher implements IRuleMatcher {
|
||||||
if (empty($this->fileInfo)) {
|
if (empty($this->fileInfo)) {
|
||||||
throw new RuntimeException('Must set file info before running the check');
|
throw new RuntimeException('Must set file info before running the check');
|
||||||
}
|
}
|
||||||
$checkInstance->setFileInfo($this->fileInfo['storage'], $this->fileInfo['path']);
|
$checkInstance->setFileInfo($this->fileInfo['storage'], $this->fileInfo['path'], $this->fileInfo['isDir']);
|
||||||
} elseif ($checkInstance instanceof IEntityCheck) {
|
} elseif ($checkInstance instanceof IEntityCheck) {
|
||||||
foreach($this->contexts as $entityInfo) {
|
foreach($this->contexts as $entityInfo) {
|
||||||
list($entity, $subject) = $entityInfo;
|
list($entity, $subject) = $entityInfo;
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
<?php
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
/**
|
/**
|
||||||
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
||||||
*
|
*
|
||||||
|
@ -77,8 +78,8 @@ class Detection implements IMimeTypeDetector {
|
||||||
*/
|
*/
|
||||||
public function __construct(IURLGenerator $urlGenerator,
|
public function __construct(IURLGenerator $urlGenerator,
|
||||||
ILogger $logger,
|
ILogger $logger,
|
||||||
$customConfigDir,
|
string $customConfigDir,
|
||||||
$defaultConfigDir) {
|
string $defaultConfigDir) {
|
||||||
$this->urlGenerator = $urlGenerator;
|
$this->urlGenerator = $urlGenerator;
|
||||||
$this->logger = $logger;
|
$this->logger = $logger;
|
||||||
$this->customConfigDir = $customConfigDir;
|
$this->customConfigDir = $customConfigDir;
|
||||||
|
@ -96,9 +97,9 @@ class Detection implements IMimeTypeDetector {
|
||||||
* @param string $mimetype
|
* @param string $mimetype
|
||||||
* @param string|null $secureMimeType
|
* @param string|null $secureMimeType
|
||||||
*/
|
*/
|
||||||
public function registerType($extension,
|
public function registerType(string $extension,
|
||||||
$mimetype,
|
string $mimetype,
|
||||||
$secureMimeType = null) {
|
?string $secureMimeType = null): void {
|
||||||
$this->mimetypes[$extension] = array($mimetype, $secureMimeType);
|
$this->mimetypes[$extension] = array($mimetype, $secureMimeType);
|
||||||
$this->secureMimeTypes[$mimetype] = $secureMimeType ?: $mimetype;
|
$this->secureMimeTypes[$mimetype] = $secureMimeType ?: $mimetype;
|
||||||
}
|
}
|
||||||
|
@ -112,12 +113,12 @@ class Detection implements IMimeTypeDetector {
|
||||||
*
|
*
|
||||||
* @param array $types
|
* @param array $types
|
||||||
*/
|
*/
|
||||||
public function registerTypeArray($types) {
|
public function registerTypeArray(array $types): void {
|
||||||
$this->mimetypes = array_merge($this->mimetypes, $types);
|
$this->mimetypes = array_merge($this->mimetypes, $types);
|
||||||
|
|
||||||
// Update the alternative mimetypes to avoid having to look them up each time.
|
// Update the alternative mimetypes to avoid having to look them up each time.
|
||||||
foreach ($this->mimetypes as $mimeType) {
|
foreach ($this->mimetypes as $mimeType) {
|
||||||
$this->secureMimeTypes[$mimeType[0]] = isset($mimeType[1]) ? $mimeType[1]: $mimeType[0];
|
$this->secureMimeTypes[$mimeType[0]] = $mimeType[1] ?? $mimeType[0];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -136,7 +137,7 @@ class Detection implements IMimeTypeDetector {
|
||||||
/**
|
/**
|
||||||
* Add the mimetype aliases if they are not yet present
|
* Add the mimetype aliases if they are not yet present
|
||||||
*/
|
*/
|
||||||
private function loadAliases() {
|
private function loadAliases(): void {
|
||||||
if (!empty($this->mimeTypeAlias)) {
|
if (!empty($this->mimeTypeAlias)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -148,12 +149,12 @@ class Detection implements IMimeTypeDetector {
|
||||||
/**
|
/**
|
||||||
* @return string[]
|
* @return string[]
|
||||||
*/
|
*/
|
||||||
public function getAllAliases() {
|
public function getAllAliases(): array {
|
||||||
$this->loadAliases();
|
$this->loadAliases();
|
||||||
return $this->mimeTypeAlias;
|
return $this->mimeTypeAlias;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getOnlyDefaultAliases() {
|
public function getOnlyDefaultAliases(): array {
|
||||||
$this->loadMappings();
|
$this->loadMappings();
|
||||||
$this->mimeTypeAlias = json_decode(file_get_contents($this->defaultConfigDir . '/mimetypealiases.dist.json'), true);
|
$this->mimeTypeAlias = json_decode(file_get_contents($this->defaultConfigDir . '/mimetypealiases.dist.json'), true);
|
||||||
return $this->mimeTypeAlias;
|
return $this->mimeTypeAlias;
|
||||||
|
@ -162,7 +163,7 @@ class Detection implements IMimeTypeDetector {
|
||||||
/**
|
/**
|
||||||
* Add mimetype mappings if they are not yet present
|
* Add mimetype mappings if they are not yet present
|
||||||
*/
|
*/
|
||||||
private function loadMappings() {
|
private function loadMappings(): void {
|
||||||
if (!empty($this->mimetypes)) {
|
if (!empty($this->mimetypes)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -176,7 +177,7 @@ class Detection implements IMimeTypeDetector {
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function getAllMappings() {
|
public function getAllMappings(): array {
|
||||||
$this->loadMappings();
|
$this->loadMappings();
|
||||||
return $this->mimetypes;
|
return $this->mimetypes;
|
||||||
}
|
}
|
||||||
|
@ -187,7 +188,7 @@ class Detection implements IMimeTypeDetector {
|
||||||
* @param string $path
|
* @param string $path
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function detectPath($path) {
|
public function detectPath($path): string {
|
||||||
$this->loadMappings();
|
$this->loadMappings();
|
||||||
|
|
||||||
$fileName = basename($path);
|
$fileName = basename($path);
|
||||||
|
@ -199,17 +200,84 @@ class Detection implements IMimeTypeDetector {
|
||||||
if (strpos($fileName, '.') > 0) {
|
if (strpos($fileName, '.') > 0) {
|
||||||
|
|
||||||
// remove versioning extension: name.v1508946057 and transfer extension: name.ocTransferId2057600214.part
|
// 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
|
//try to guess the type by the file extension
|
||||||
$extension = strtolower(strrchr($fileName, '.'));
|
$extension = strrchr($fileName, '.');
|
||||||
$extension = substr($extension, 1); //remove leading .
|
if ($extension !== false) {
|
||||||
return (isset($this->mimetypes[$extension]) && isset($this->mimetypes[$extension][0]))
|
$extension = strtolower($extension);
|
||||||
? $this->mimetypes[$extension][0]
|
$extension = substr($extension, 1); //remove leading .
|
||||||
: 'application/octet-stream';
|
return $this->mimetypes[$extension][0] ?? 'application/octet-stream';
|
||||||
} else {
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 'application/octet-stream';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* detect mimetype only based on the content of file
|
||||||
|
* @param string $path
|
||||||
|
* @return string
|
||||||
|
* @since 18.0.0
|
||||||
|
*/
|
||||||
|
public function detectContent(string $path): string {
|
||||||
|
$this->loadMappings();
|
||||||
|
|
||||||
|
if (@is_dir($path)) {
|
||||||
|
// directories are easy
|
||||||
|
return 'httpd/unix-directory';
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
$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';
|
return 'application/octet-stream';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 (\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("test -f $path && file -b --mime-type $path", 'r');
|
||||||
|
$mimeType = fgets($fp);
|
||||||
|
pclose($fp);
|
||||||
|
|
||||||
|
if ($mimeType !== false) {
|
||||||
|
//trim the newline
|
||||||
|
$mimeType = trim($mimeType);
|
||||||
|
$mimeType = $this->getSecureMimeType($mimeType);
|
||||||
|
if ($mimeType !== 'application/octet-stream') {
|
||||||
|
return $mimeType;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return 'application/octet-stream';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -218,49 +286,14 @@ class Detection implements IMimeTypeDetector {
|
||||||
* @param string $path
|
* @param string $path
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function detect($path) {
|
public function detect($path): string {
|
||||||
$this->loadMappings();
|
|
||||||
|
|
||||||
if (@is_dir($path)) {
|
|
||||||
// directories are easy
|
|
||||||
return "httpd/unix-directory";
|
|
||||||
}
|
|
||||||
|
|
||||||
$mimeType = $this->detectPath($path);
|
$mimeType = $this->detectPath($path);
|
||||||
|
|
||||||
if ($mimeType === 'application/octet-stream' and function_exists('finfo_open')
|
if ($mimeType !== 'application/octet-stream') {
|
||||||
and function_exists('finfo_file') and $finfo = finfo_open(FILEINFO_MIME)
|
return $mimeType;
|
||||||
) {
|
|
||||||
$info = @strtolower(finfo_file($finfo, $path));
|
|
||||||
finfo_close($finfo);
|
|
||||||
if ($info) {
|
|
||||||
$mimeType = strpos($info, ';') !== false ? substr($info, 0, strpos($info, ';')) : $info;
|
|
||||||
return empty($mimeType) ? 'application/octet-stream' : $mimeType;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
$isWrapped = (strpos($path, '://') !== false) and (substr($path, 0, 7) === 'file://');
|
|
||||||
if (!$isWrapped and $mimeType === 'application/octet-stream' && 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")) {
|
|
||||||
// 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);
|
|
||||||
pclose($fp);
|
|
||||||
|
|
||||||
//trim the newline
|
return $this->detectContent($path);
|
||||||
$mimeType = trim($reply);
|
|
||||||
|
|
||||||
if (empty($mimeType)) {
|
|
||||||
$mimeType = 'application/octet-stream';
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
return $mimeType;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -269,20 +302,20 @@ class Detection implements IMimeTypeDetector {
|
||||||
* @param string $data
|
* @param string $data
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function detectString($data) {
|
public function detectString($data): string {
|
||||||
if (function_exists('finfo_open') and function_exists('finfo_file')) {
|
if (function_exists('finfo_open') && function_exists('finfo_file')) {
|
||||||
$finfo = finfo_open(FILEINFO_MIME);
|
$finfo = finfo_open(FILEINFO_MIME);
|
||||||
$info = finfo_buffer($finfo, $data);
|
$info = finfo_buffer($finfo, $data);
|
||||||
return strpos($info, ';') !== false ? substr($info, 0, strpos($info, ';')) : $info;
|
return strpos($info, ';') !== false ? substr($info, 0, strpos($info, ';')) : $info;
|
||||||
} else {
|
|
||||||
$tmpFile = \OC::$server->getTempManager()->getTemporaryFile();
|
|
||||||
$fh = fopen($tmpFile, 'wb');
|
|
||||||
fwrite($fh, $data, 8024);
|
|
||||||
fclose($fh);
|
|
||||||
$mime = $this->detect($tmpFile);
|
|
||||||
unset($tmpFile);
|
|
||||||
return $mime;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$tmpFile = \OC::$server->getTempManager()->getTemporaryFile();
|
||||||
|
$fh = fopen($tmpFile, 'wb');
|
||||||
|
fwrite($fh, $data, 8024);
|
||||||
|
fclose($fh);
|
||||||
|
$mime = $this->detect($tmpFile);
|
||||||
|
unset($tmpFile);
|
||||||
|
return $mime;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -291,12 +324,10 @@ class Detection implements IMimeTypeDetector {
|
||||||
* @param string $mimeType
|
* @param string $mimeType
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getSecureMimeType($mimeType) {
|
public function getSecureMimeType($mimeType): string {
|
||||||
$this->loadMappings();
|
$this->loadMappings();
|
||||||
|
|
||||||
return isset($this->secureMimeTypes[$mimeType])
|
return $this->secureMimeTypes[$mimeType] ?? 'application/octet-stream';
|
||||||
? $this->secureMimeTypes[$mimeType]
|
|
||||||
: 'application/octet-stream';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -304,7 +335,7 @@ class Detection implements IMimeTypeDetector {
|
||||||
* @param string $mimetype the MIME type
|
* @param string $mimetype the MIME type
|
||||||
* @return string the url
|
* @return string the url
|
||||||
*/
|
*/
|
||||||
public function mimeTypeIcon($mimetype) {
|
public function mimeTypeIcon($mimetype): string {
|
||||||
$this->loadAliases();
|
$this->loadAliases();
|
||||||
|
|
||||||
while (isset($this->mimeTypeAlias[$mimetype])) {
|
while (isset($this->mimeTypeAlias[$mimetype])) {
|
||||||
|
@ -315,8 +346,7 @@ class Detection implements IMimeTypeDetector {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Replace slash and backslash with a minus
|
// Replace slash and backslash with a minus
|
||||||
$icon = str_replace('/', '-', $mimetype);
|
$icon = str_replace(['/', '\\'], '-', $mimetype);
|
||||||
$icon = str_replace('\\', '-', $icon);
|
|
||||||
|
|
||||||
// Is it a dir?
|
// Is it a dir?
|
||||||
if ($mimetype === 'dir') {
|
if ($mimetype === 'dir') {
|
||||||
|
|
|
@ -40,9 +40,17 @@ interface IMimeTypeDetector {
|
||||||
* @param string $path
|
* @param string $path
|
||||||
* @return string
|
* @return string
|
||||||
* @since 8.2.0
|
* @since 8.2.0
|
||||||
**/
|
*/
|
||||||
public function detectPath($path);
|
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
|
* detect mimetype based on both filename and content
|
||||||
*
|
*
|
||||||
|
|
|
@ -37,8 +37,11 @@ use OCP\Files\Storage\IStorage;
|
||||||
*/
|
*/
|
||||||
interface IFileCheck extends IEntityCheck {
|
interface IFileCheck extends IEntityCheck {
|
||||||
/**
|
/**
|
||||||
|
* @param IStorage $storage
|
||||||
|
* @param string $path
|
||||||
|
* @param bool $isDir
|
||||||
* @since 18.0.0
|
* @since 18.0.0
|
||||||
*/
|
*/
|
||||||
public function setFileInfo(IStorage $storage, string $path);
|
public function setFileInfo(IStorage $storage, string $path, bool $isDir = false): void;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,61 +39,96 @@ class DetectionTest extends \Test\TestCase {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testDetect() {
|
public function dataDetectPath(): array {
|
||||||
$dir = \OC::$SERVERROOT.'/tests/data';
|
return [
|
||||||
|
['foo.txt', 'text/plain'],
|
||||||
$result = $this->detection->detect($dir."/");
|
['foo.png', 'image/png'],
|
||||||
$expected = 'httpd/unix-directory';
|
['foo.bar.png', 'image/png'],
|
||||||
$this->assertEquals($expected, $result);
|
['.hidden.png', 'image/png'],
|
||||||
|
['.hidden.foo.png', 'image/png'],
|
||||||
$result = $this->detection->detect($dir."/data.tar.gz");
|
['.hidden/foo.png', 'image/png'],
|
||||||
$expected = 'application/x-gzip';
|
['.hidden/.hidden.png', 'image/png'],
|
||||||
$this->assertEquals($expected, $result);
|
['test.jpg/foo.png', 'image/png'],
|
||||||
|
['.png', 'application/octet-stream'],
|
||||||
$result = $this->detection->detect($dir."/data.zip");
|
['..hidden', 'application/octet-stream'],
|
||||||
$expected = 'application/zip';
|
['foo', 'application/octet-stream'],
|
||||||
$this->assertEquals($expected, $result);
|
['', 'application/octet-stream'],
|
||||||
|
['foo.png.ocTransferId123456789.part', 'image/png'],
|
||||||
$result = $this->detection->detect($dir."/testimagelarge.svg");
|
['foo.png.v1234567890', 'image/png'],
|
||||||
$expected = 'image/svg+xml';
|
];
|
||||||
$this->assertEquals($expected, $result);
|
|
||||||
|
|
||||||
$result = $this->detection->detect($dir."/testimage.png");
|
|
||||||
$expected = 'image/png';
|
|
||||||
$this->assertEquals($expected, $result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetSecureMimeType() {
|
/**
|
||||||
$result = $this->detection->getSecureMimeType('image/svg+xml');
|
* @dataProvider dataDetectPath
|
||||||
|
*
|
||||||
|
* @param string $path
|
||||||
|
* @param string $expected
|
||||||
|
*/
|
||||||
|
public function testDetectPath(string $path, string $expected): void {
|
||||||
|
$this->assertEquals($expected, $this->detection->detectPath($path));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function dataDetectContent(): array {
|
||||||
|
return [
|
||||||
|
['/', 'httpd/unix-directory'],
|
||||||
|
// ['/data.tar.gz', 'application/x-gzip'], TODO: fix as it fails hard on php7.4 now
|
||||||
|
['/data.zip', 'application/zip'],
|
||||||
|
['/testimage.mp3', 'audio/mpeg'],
|
||||||
|
['/testimage.png', 'image/png'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider dataDetectContent
|
||||||
|
*
|
||||||
|
* @param string $path
|
||||||
|
* @param string $expected
|
||||||
|
*/
|
||||||
|
public function testDetectContent(string $path, string $expected): void {
|
||||||
|
$this->assertEquals($expected, $this->detection->detectContent(\OC::$SERVERROOT . '/tests/data' . $path));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function dataDetect(): array {
|
||||||
|
return [
|
||||||
|
['/', 'httpd/unix-directory'],
|
||||||
|
['/data.tar.gz', 'application/x-gzip'],
|
||||||
|
['/data.zip', 'application/zip'],
|
||||||
|
['/testimagelarge.svg', 'image/svg+xml'],
|
||||||
|
['/testimage.png', 'image/png'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider dataDetect
|
||||||
|
*
|
||||||
|
* @param string $path
|
||||||
|
* @param string $expected
|
||||||
|
*/
|
||||||
|
public function testDetect(string $path, string $expected): void {
|
||||||
|
$this->assertEquals($expected, $this->detection->detect(\OC::$SERVERROOT . '/tests/data' . $path));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDetectString(): void {
|
||||||
|
$result = $this->detection->detectString('/data/data.tar.gz');
|
||||||
$expected = 'text/plain';
|
$expected = 'text/plain';
|
||||||
$this->assertEquals($expected, $result);
|
$this->assertEquals($expected, $result);
|
||||||
|
|
||||||
$result = $this->detection->getSecureMimeType('image/png');
|
|
||||||
$expected = 'image/png';
|
|
||||||
$this->assertEquals($expected, $result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testDetectPath() {
|
public function dataGetSecureMimeType(): array {
|
||||||
$this->assertEquals('text/plain', $this->detection->detectPath('foo.txt'));
|
return [
|
||||||
$this->assertEquals('image/png', $this->detection->detectPath('foo.png'));
|
['image/svg+xml', 'text/plain'],
|
||||||
$this->assertEquals('image/png', $this->detection->detectPath('foo.bar.png'));
|
['image/png', 'image/png'],
|
||||||
$this->assertEquals('image/png', $this->detection->detectPath('.hidden.png'));
|
];
|
||||||
$this->assertEquals('image/png', $this->detection->detectPath('.hidden.foo.png'));
|
|
||||||
$this->assertEquals('image/png', $this->detection->detectPath('.hidden/foo.png'));
|
|
||||||
$this->assertEquals('image/png', $this->detection->detectPath('.hidden/.hidden.png'));
|
|
||||||
$this->assertEquals('image/png', $this->detection->detectPath('test.jpg/foo.png'));
|
|
||||||
$this->assertEquals('application/octet-stream', $this->detection->detectPath('.png'));
|
|
||||||
$this->assertEquals('application/octet-stream', $this->detection->detectPath('..hidden'));
|
|
||||||
$this->assertEquals('application/octet-stream', $this->detection->detectPath('foo'));
|
|
||||||
$this->assertEquals('application/octet-stream', $this->detection->detectPath(''));
|
|
||||||
$this->assertEquals('image/png', $this->detection->detectPath('foo.png.ocTransferId123456789.part'));
|
|
||||||
$this->assertEquals('image/png', $this->detection->detectPath('foo.png.v1234567890'));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testDetectString() {
|
/**
|
||||||
$result = $this->detection->detectString("/data/data.tar.gz");
|
* @dataProvider dataGetSecureMimeType
|
||||||
$expected = 'text/plain';
|
*
|
||||||
$this->assertEquals($expected, $result);
|
* @param string $mimeType
|
||||||
|
* @param string $expected
|
||||||
|
*/
|
||||||
|
public function testGetSecureMimeType(string $mimeType, string $expected): void {
|
||||||
|
$this->assertEquals($expected, $this->detection->getSecureMimeType($mimeType));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testMimeTypeIcon() {
|
public function testMimeTypeIcon() {
|
||||||
|
|
Loading…
Reference in New Issue