Initial commit

Working directory listing. Hardcoded sidebar template.
This commit is contained in:
2011-01-16 22:59:18 +00:00
parent ee1707d86e
commit 65c560cdf0
20 changed files with 558 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
<?php
class MediaListing_Directory extends MediaListing_FileObject {
protected $files;
protected $parent;
public function __construct($path, $parent, $scan) {
if ( ! is_dir($path)) {
throw new MediaListing_Exception_NotADirectory($path);
}
parent::__construct(parent::TYPE_Directory, $path);
$this->parent = $parent;
if ($scan) {
$this->scan();
}
}
protected function scan() {
try {
$iterator = new MediaListing_Utility_VisibleFilesIterator(new DirectoryIterator($this->path));
foreach ($iterator as /** @var SplFileInfo */ $source) {
$filename = $source->getPathname();
$basename = basename($filename);
$this->files["{$basename}"] = MediaListing_FileObject::create($filename, $this, false);
}
} catch (Exception $e) {
echo $e->getMessage();
}
uasort($this->files, function($a, $b) {
return ($a->type() == $b->type()) ? strcasecmp($a->basename(), $b->basename()) : ($a->type() < $b->type());
}
);
}
public function parent() {
return $this->parent;
}
public function files() {
return $this->files;
}
public function file($basename) {
if ( ! key_exists($basename, $this->files)) {
throw new MediaListing_Exception_FileNotFound($basename);
}
$subdir = $this->files["{$basename}"];
$subdir->scan();
return $subdir;
}
}
?>

View File

@@ -0,0 +1,6 @@
<?php
class MediaListing_Exception_NotADirectory extends MediaListing_Exception {};
class MediaListing_Exception_NotAFile extends MediaListing_Exception {};
?>

View File

@@ -0,0 +1,23 @@
<?php
class MediaListing_File extends MediaListing_FileObject {
protected $size;
public function __construct($path) {
if ( ! is_file($path)) {
throw new MediaListing_Exception_NotAFile($path);
}
parent::__construct(parent::TYPE_File, $path);
$this->size = filesize($this->path);
}
public function size() {
return $this->size;
}
}
?>

View File

@@ -0,0 +1,56 @@
<?php
abstract class MediaListing_FileObject {
const TYPE_File = 1;
const TYPE_Directory = 2;
protected $type;
protected $basename;
protected $path;
public static function create($path, $parent = null, $scan = true) {
if ( ! file_exists($path)) {
throw new MediaListing_Exception_FileNotFound($path);
}
switch (filetype($path)) {
case "file": {
return new MediaListing_File($path);
} break;
case "dir": {
return new MediaListing_Directory($path, $parent, $scan);
} break;
}
}
protected function __construct($type, $path) {
$this->type = $type;
$this->path = $path;
$this->basename = basename($path);
}
public function isFile() {
return $this->type == self::TYPE_File;
}
public function isDirectory() {
return $this->type == self::TYPE_Directory;
}
public function type() {
return $this->type;
}
public function path() {
return $this->path;
}
public function basename() {
return $this->basename;
}
}
?>

View File

@@ -0,0 +1,36 @@
<?php
class MediaListing_Source {
protected $name;
protected $path;
protected $root;
public function __construct($name, $path, $scan = true) {
$this->name = $name;
$this->path = $path;
if ($scan) {
$this->scan();
}
}
protected function scan() {
$this->root = MediaListing_FileObject::create($this->path);
}
public function name() {
return $this->name;
}
public function path() {
return $this->path();
}
public function root() {
return $this->root;
}
}
?>

View File

@@ -0,0 +1,9 @@
<?php
class MediaListing_Utility_VideoFilesIterator extends FilterIterator {
public function accept() {
return preg_match('/\.(?:avi|ogm|m4v|mkv|mov|mp4|mpg|srt)$/i', $this->current()->getFilename());
}
}
?>