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
Fallback to filename based detection if the remote dav server doesn't…
… know the mimetype

Signed-off-by: Robin Appelman <[email protected]>
  • Loading branch information
icewind1991 committed Sep 28, 2017
commit b36dd8b71fb8751b07ef98443d739d0b1cda9c7d
13 changes: 12 additions & 1 deletion apps/files_external/tests/Storage/WebdavTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
namespace OCA\Files_External\Tests\Storage;

use \OC\Files\Storage\DAV;
use OC\Files\Type\Detection;

/**
* Class WebdavTest
Expand All @@ -43,7 +44,7 @@ protected function setUp() {

$id = $this->getUniqueID();
$config = include('files_external/tests/config.webdav.php');
if ( ! is_array($config) or !$config['run']) {
if (!is_array($config) or !$config['run']) {
$this->markTestSkipped('WebDAV backend not configured');
}
if (isset($config['wait'])) {
Expand All @@ -61,4 +62,14 @@ protected function tearDown() {

parent::tearDown();
}

public function testMimetypeFallback() {
$this->instance->file_put_contents('foo.bar', 'asd');

/** @var Detection $mimeDetector */
$mimeDetector = \OC::$server->getMimeTypeDetector();
$mimeDetector->registerType('bar', 'application/x-bar');

$this->assertEquals('application/x-bar', $this->instance->getMimeType('foo.bar'));
}
}
26 changes: 17 additions & 9 deletions lib/private/Files/Storage/DAV.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ protected function init() {
}

$proxy = \OC::$server->getConfig()->getSystemValue('proxy', '');
if($proxy !== '') {
if ($proxy !== '') {
$settings['proxy'] = $proxy;
}

Expand Down Expand Up @@ -343,11 +343,11 @@ public function fopen($path, $mode) {
case 'rb':
try {
$response = $this->httpClientService
->newClient()
->get($this->createBaseUri() . $this->encodePath($path), [
'auth' => [$this->user, $this->password],
'stream' => true
]);
->newClient()
->get($this->createBaseUri() . $this->encodePath($path), [
'auth' => [$this->user, $this->password],
'stream' => true
]);
} catch (RequestException $e) {
if ($e->getResponse() instanceof ResponseInterface
&& $e->getResponse()->getStatusCode() === 404) {
Expand Down Expand Up @@ -590,6 +590,15 @@ public function stat($path) {

/** {@inheritdoc} */
public function getMimeType($path) {
$remoteMimetype = $this->getMimeTypeFromRemote($path);
if ($remoteMimetype === 'application/octet-stream') {
return \OC::$server->getMimeTypeDetector()->detectPath($path);
} else {
return $remoteMimetype;
}
}

public function getMimeTypeFromRemote($path) {
try {
$response = $this->propfind($path);
if ($response === false) {
Expand All @@ -606,12 +615,11 @@ public function getMimeType($path) {
} elseif (isset($response['{DAV:}getcontenttype'])) {
return $response['{DAV:}getcontenttype'];
} else {
return false;
return 'application/octet-stream';
}
} catch (\Exception $e) {
$this->convertException($e, $path);
return false;
}
return false;
}

/**
Expand Down