|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Storgman - Student Organizations Management |
|
5
|
|
|
* Copyright (C) 2014-2016, Dejan Angelov <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* This file is part of Storgman. |
|
8
|
|
|
* |
|
9
|
|
|
* Storgman is free software: you can redistribute it and/or modify |
|
10
|
|
|
* it under the terms of the GNU General Public License as published by |
|
11
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
12
|
|
|
* (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* Storgman is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU General Public License |
|
20
|
|
|
* along with Storgman. If not, see <http://www.gnu.org/licenses/>. |
|
21
|
|
|
* |
|
22
|
|
|
* @package Storgman |
|
23
|
|
|
* @copyright Copyright (C) 2014-2016, Dejan Angelov <[email protected]> |
|
24
|
|
|
* @license https://github.com/angelov/storgman/blob/master/LICENSE |
|
25
|
|
|
* @author Dejan Angelov <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
|
|
28
|
|
|
namespace Angelov\Storgman\Core\FileSystem; |
|
29
|
|
|
|
|
30
|
|
|
use Gaufrette\Adapter\Local; |
|
31
|
|
|
use Gaufrette\Filesystem; |
|
32
|
|
|
|
|
33
|
|
|
// @todo can be improved |
|
34
|
|
|
class LocalFileSystem implements FileSystemInterface |
|
35
|
|
|
{ |
|
36
|
|
|
protected $filesystem; |
|
37
|
|
|
protected $basePath; |
|
38
|
|
|
|
|
39
|
|
|
public function __construct($basePath = "/") |
|
40
|
|
|
{ |
|
41
|
|
|
$this->basePath = $basePath; |
|
42
|
|
|
$this->filesystem = new Filesystem(new Local($basePath)); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function store(File $file, $preserveFilename = false) |
|
46
|
|
|
{ |
|
47
|
|
|
$filename = $this->generateFilename($file, $preserveFilename); |
|
48
|
|
|
$content = file_get_contents($file->getPath()); |
|
49
|
|
|
|
|
50
|
|
|
$this->filesystem->write($filename, $content); |
|
51
|
|
|
|
|
52
|
|
|
$path = $this->basePath . DIRECTORY_SEPARATOR . $filename; |
|
53
|
|
|
|
|
54
|
|
|
return new File($filename, $path); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
protected function generateFilename(File $file, $preserveOriginal = false) |
|
58
|
|
|
{ |
|
59
|
|
|
if ($preserveOriginal) { |
|
60
|
|
|
$prefix = 1; |
|
61
|
|
|
$filename = $file->getFilename(); |
|
62
|
|
|
while (true) { |
|
63
|
|
|
if (! $this->filesystem->has($filename)) { |
|
64
|
|
|
return $filename; |
|
65
|
|
|
} |
|
66
|
|
|
$filename = $this->addFilenamePrefix($filename, $prefix++); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return md5($file->getFilename()) . "_" . md5(rand(0, 100000)) . "." . $file->getExtension(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
protected function addFilenamePrefix($filename, $prefix) |
|
74
|
|
|
{ |
|
75
|
|
|
$parts = explode(".", $filename); |
|
76
|
|
|
$filename = array_slice($parts, 0, count($parts) - 1); |
|
77
|
|
|
$extension = $parts[count($parts) - 1]; |
|
78
|
|
|
$filename = join(".", $filename); |
|
79
|
|
|
$filename .= sprintf(" (%s).%s", $prefix, $extension); |
|
80
|
|
|
|
|
81
|
|
|
return $filename; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function find($filename) |
|
85
|
|
|
{ |
|
86
|
|
|
if (! $this->filesystem->has($filename)) { |
|
87
|
|
|
throw new FileNotFoundException(sprintf( |
|
88
|
|
|
"Could not find file %s", |
|
89
|
|
|
$filename |
|
90
|
|
|
)); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$path = $this->basePath . DIRECTORY_SEPARATOR . $filename; |
|
94
|
|
|
|
|
95
|
|
|
return new File($filename, $path); |
|
96
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function read(File $file) |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->filesystem->read($file->getFilename()); |
|
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function setBasePath($path) |
|
105
|
|
|
{ |
|
106
|
|
|
$this->basePath = $path; |
|
107
|
|
|
$this->filesystem = new Filesystem(new Local($path)); |
|
108
|
|
|
} |
|
109
|
|
|
} |