1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8namespace Tiki\FileGallery\Handler;
9
10use Tiki\FileGallery\FileWrapper\PhysicalFile;
11
12class FileSystem implements HandlerInterface
13{
14	private $directory;
15
16	function __construct($directory)
17	{
18		$this->directory = $directory;
19		$this->directory = rtrim($directory, '/\\');
20	}
21
22	function getFileWrapper($file)
23	{
24		return new PhysicalFile($this->directory, $file->path);
25	}
26
27	function delete($file)
28	{
29		$full = "{$this->directory}/$file->path";
30
31		if ($file->path && is_writable($full)) {
32			unlink($full);
33		}
34	}
35
36	function uniquePath($file) {
37		if (! empty($file->path)) {
38			return $file->path;
39		}
40		$fhash = md5($file->name);
41		while (file_exists($this->directory . '/' . $fhash)) {
42			$fhash = md5(uniqid($fhash));
43		}
44		return $fhash;
45	}
46
47	function isWritable() {
48		return is_writable($this->directory);
49	}
50}
51