Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/workflowengine/js/filemimetypeplugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
getCheck: function() {
return {
'class': 'OCA\\WorkflowEngine\\Check\\FileMimeType',
'name': t('workflowengine', 'File mime type (upload)'),
'name': t('workflowengine', 'File mime type'),
'operators': [
{'operator': 'is', 'name': t('workflowengine', 'is')},
{'operator': '!is', 'name': t('workflowengine', 'is not')},
Expand Down
93 changes: 85 additions & 8 deletions apps/workflowengine/lib/Check/FileMimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@


use OCP\Files\IMimeTypeDetector;
use OCP\Files\Storage\IStorage;
use OCP\IL10N;
use OCP\IRequest;

class FileMimeType extends AbstractStringCheck {

/** @var string */
/** @var array */
protected $mimeType;

/** @var IRequest */
Expand All @@ -37,6 +38,12 @@ class FileMimeType extends AbstractStringCheck {
/** @var IMimeTypeDetector */
protected $mimeTypeDetector;

/** @var IStorage */
protected $storage;

/** @var string */
protected $path;

/**
* @param IL10N $l
* @param IRequest $request
Expand All @@ -48,27 +55,97 @@ public function __construct(IL10N $l, IRequest $request, IMimeTypeDetector $mime
$this->mimeTypeDetector = $mimeTypeDetector;
}

/**
* @param IStorage $storage
* @param string $path
*/
public function setFileInfo(IStorage $storage, $path) {
$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;
}
}

/**
* @return string
*/
protected function getActualValue() {
if ($this->mimeType !== null) {
return $this->mimeType;
if ($this->mimeType[$this->storage->getId()][$this->path] !== null) {
return $this->mimeType[$this->storage->getId()][$this->path];
}

$this->mimeType = '';
$this->mimeType[$this->storage->getId()][$this->path] = '';
if ($this->isWebDAVRequest()) {
if ($this->request->getMethod() === 'PUT') {
$path = $this->request->getPathInfo();
$this->mimeType = $this->mimeTypeDetector->detectPath($path);
$this->mimeType[$this->storage->getId()][$this->path] = $this->mimeTypeDetector->detectPath($path);
return $this->mimeType[$this->storage->getId()][$this->path];
}
} else if (in_array($this->request->getMethod(), ['POST', 'PUT'])) {
}

if (in_array($this->request->getMethod(), ['POST', 'PUT'])) {
$files = $this->request->getUploadedFile('files');
if (isset($files['type'][0])) {
$this->mimeType = $files['type'][0];
$mimeType = $files['type'][0];
if ($this->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;
}
}
}
$this->mimeType[$this->storage->getId()][$this->path] = $mimeType;
return $mimeType;
}
}

$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();
}

return $this->mimeType[$this->storage->getId()][$this->path];
}

/**
* @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';
}
return $this->mimeType;
}

/**
Expand Down
5 changes: 3 additions & 2 deletions lib/private/Files/Type/Detection.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public function detect($path) {
$info = @strtolower(finfo_file($finfo, $path));
finfo_close($finfo);
if ($info) {
$mimeType = substr($info, 0, strpos($info, ';'));
$mimeType = strpos($info, ';') !== false ? substr($info, 0, strpos($info, ';')) : $info;
return empty($mimeType) ? 'application/octet-stream' : $mimeType;
}

Expand Down Expand Up @@ -238,7 +238,8 @@ public function detect($path) {
public function detectString($data) {
if (function_exists('finfo_open') and function_exists('finfo_file')) {
$finfo = finfo_open(FILEINFO_MIME);
return finfo_buffer($finfo, $data);
$info = finfo_buffer($finfo, $data);
return strpos($info, ';') !== false ? substr($info, 0, strpos($info, ';')) : $info;
} else {
$tmpFile = \OC::$server->getTempManager()->getTemporaryFile();
$fh = fopen($tmpFile, 'wb');
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/Files/Type/DetectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function testDetectPath() {

public function testDetectString() {
$result = $this->detection->detectString("/data/data.tar.gz");
$expected = 'text/plain; charset=us-ascii';
$expected = 'text/plain';
$this->assertEquals($expected, $result);
}

Expand Down