From 2407fc5ab84da24cbc46e42cb62f8f6ea6f2eb11 Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Wed, 8 May 2019 09:42:25 +0100 Subject: [PATCH 1/2] Use set mimetype --- src/MemoryAdapter.php | 5 +++-- tests/MemoryAdapterTest.php | 12 ++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/MemoryAdapter.php b/src/MemoryAdapter.php index 1331380..86afe38 100644 --- a/src/MemoryAdapter.php +++ b/src/MemoryAdapter.php @@ -94,7 +94,7 @@ public function deleteDir($dirname) */ public function getMetadata($path) { - $metadata = $this->storage[$path] + ['path' => $path]; + $metadata = array_filter($this->storage[$path]) + ['path' => $path]; unset($metadata['contents']); return $metadata; @@ -105,7 +105,7 @@ public function getMetadata($path) */ public function getMimetype($path) { - $mimetype = Util::guessMimeType($path, $this->storage[$path]['contents']); + $mimetype = $this->storage[$path]['mimetype'] ?: Util::guessMimeType($path, $this->storage[$path]['contents']); return [ 'mimetype' => $mimetype, @@ -223,6 +223,7 @@ public function update($path, $contents, Config $config) $this->storage[$path]['timestamp'] = $config->get('timestamp', time()); $this->storage[$path]['size'] = Util::contentSize($contents); $this->storage[$path]['visibility'] = $config->get('visibility', $this->storage[$path]['visibility']); + $this->storage[$path]['mimetype'] = $config->get('mimetype'); return $this->getMetadata($path); } diff --git a/tests/MemoryAdapterTest.php b/tests/MemoryAdapterTest.php index a53b206..dbe0c2e 100644 --- a/tests/MemoryAdapterTest.php +++ b/tests/MemoryAdapterTest.php @@ -82,12 +82,24 @@ public function testGetMetadata() $this->assertSame(8, $meta['size']); $this->assertSame('public', $meta['visibility']); $this->assertTrue(is_int($meta['timestamp'])); + + $this->adapter->write('dir/file.txt', 'contents', new Config(['mimetype' => 'mime/type'])); + + $meta = $this->adapter->getMetadata('dir/file.txt'); + + $this->assertCount(6, $meta); + $this->assertSame('mime/type', $meta['mimetype']); } public function testGetMimetype() { + $this->adapter->write('dir/file.txt', 'contents', new Config(['mimetype' => 'mime/type'])); + $meta = $this->adapter->getMimetype('file.txt'); $this->assertSame('text/plain', $meta['mimetype']); + + $meta = $this->adapter->getMimetype('dir/file.txt'); + $this->assertSame('mime/type', $meta['mimetype']); } public function testGetSize() From d62907006bb3f6da40f0f848b33878ea5a900db0 Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Wed, 8 May 2019 10:17:08 +0100 Subject: [PATCH 2/2] Avoid accidental filtering --- src/MemoryAdapter.php | 9 ++++++--- tests/MemoryAdapterTest.php | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/MemoryAdapter.php b/src/MemoryAdapter.php index 86afe38..f5fe0e3 100644 --- a/src/MemoryAdapter.php +++ b/src/MemoryAdapter.php @@ -94,7 +94,7 @@ public function deleteDir($dirname) */ public function getMetadata($path) { - $metadata = array_filter($this->storage[$path]) + ['path' => $path]; + $metadata = $this->storage[$path] + ['path' => $path]; unset($metadata['contents']); return $metadata; @@ -105,7 +105,8 @@ public function getMetadata($path) */ public function getMimetype($path) { - $mimetype = $this->storage[$path]['mimetype'] ?: Util::guessMimeType($path, $this->storage[$path]['contents']); + $mimetype = isset($this->storage[$path]['mimetype']) ? $this->storage[$path]['mimetype'] : + Util::guessMimeType($path, $this->storage[$path]['contents']); return [ 'mimetype' => $mimetype, @@ -223,7 +224,9 @@ public function update($path, $contents, Config $config) $this->storage[$path]['timestamp'] = $config->get('timestamp', time()); $this->storage[$path]['size'] = Util::contentSize($contents); $this->storage[$path]['visibility'] = $config->get('visibility', $this->storage[$path]['visibility']); - $this->storage[$path]['mimetype'] = $config->get('mimetype'); + if ($config->has('mimetype')) { + $this->storage[$path]['mimetype'] = $config->get('mimetype'); + } return $this->getMetadata($path); } diff --git a/tests/MemoryAdapterTest.php b/tests/MemoryAdapterTest.php index dbe0c2e..640a9a4 100644 --- a/tests/MemoryAdapterTest.php +++ b/tests/MemoryAdapterTest.php @@ -83,7 +83,7 @@ public function testGetMetadata() $this->assertSame('public', $meta['visibility']); $this->assertTrue(is_int($meta['timestamp'])); - $this->adapter->write('dir/file.txt', 'contents', new Config(['mimetype' => 'mime/type'])); + $this->adapter->write('dir/file.txt', '', new Config(['mimetype' => 'mime/type'])); $meta = $this->adapter->getMetadata('dir/file.txt');