|
|
<?php |
|
|
|
|
|
namespace Kanboard\Core\ObjectStorage; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FileStorage implements ObjectStorageInterface |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private $path = ''; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function __construct($path) |
|
|
{ |
|
|
$this->path = $path; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function get($key) |
|
|
{ |
|
|
$filename = $this->path.DIRECTORY_SEPARATOR.$key; |
|
|
|
|
|
if (! file_exists($filename)) { |
|
|
throw new ObjectStorageException('File not found: '.$filename); |
|
|
} |
|
|
|
|
|
return file_get_contents($filename); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function put($key, &$blob) |
|
|
{ |
|
|
$this->createFolder($key); |
|
|
|
|
|
if (file_put_contents($this->path.DIRECTORY_SEPARATOR.$key, $blob) === false) { |
|
|
throw new ObjectStorageException('Unable to write the file: '.$this->path.DIRECTORY_SEPARATOR.$key); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function output($key) |
|
|
{ |
|
|
$filename = $this->path.DIRECTORY_SEPARATOR.$key; |
|
|
|
|
|
if (! file_exists($filename)) { |
|
|
throw new ObjectStorageException('File not found: '.$filename); |
|
|
} |
|
|
|
|
|
readfile($filename); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function moveFile($src_filename, $key) |
|
|
{ |
|
|
$this->createFolder($key); |
|
|
$dst_filename = $this->path.DIRECTORY_SEPARATOR.$key; |
|
|
|
|
|
if (! rename($src_filename, $dst_filename)) { |
|
|
throw new ObjectStorageException('Unable to move the file: '.$src_filename.' to '.$dst_filename); |
|
|
} |
|
|
|
|
|
return true; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function moveUploadedFile($filename, $key) |
|
|
{ |
|
|
$this->createFolder($key); |
|
|
return move_uploaded_file($filename, $this->path.DIRECTORY_SEPARATOR.$key); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function remove($key) |
|
|
{ |
|
|
$filename = $this->path.DIRECTORY_SEPARATOR.$key; |
|
|
$result = false; |
|
|
|
|
|
if (file_exists($filename)) { |
|
|
$result = unlink($filename); |
|
|
|
|
|
|
|
|
$parentFolder = dirname($filename); |
|
|
$files = glob($parentFolder.DIRECTORY_SEPARATOR.'*'); |
|
|
|
|
|
if ($files !== false && is_dir($parentFolder) && count($files) === 0) { |
|
|
rmdir($parentFolder); |
|
|
} |
|
|
} |
|
|
|
|
|
return $result; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private function createFolder($key) |
|
|
{ |
|
|
$folder = strpos($key, DIRECTORY_SEPARATOR) !== false ? $this->path.DIRECTORY_SEPARATOR.dirname($key) : $this->path; |
|
|
|
|
|
if (! is_dir($folder) && ! mkdir($folder, 0755, true)) { |
|
|
throw new ObjectStorageException('Unable to create folder: '.$folder); |
|
|
} |
|
|
} |
|
|
} |
|
|
|