-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathFiles.php
executable file
·146 lines (127 loc) · 3.4 KB
/
Files.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/**
* Created by PhpStorm.
* User: shady
* Date: 12/26/18
* Time: 9:31 AM
*/
class Files
{
const FOLDER_NAME="upload";
private $app;
private $repo;
/**
* Files constructor.
*/
public function __construct(SfsApplication $app)
{
$this->app = $app;
$config = new \JamesMoss\Flywheel\Config($this->app->getBaseDir() . '/db');
$this->repo = new \JamesMoss\Flywheel\Repository('files', $config);
}
/**
* @return \JamesMoss\Flywheel\Repository
*/
public function getRepo()
{
return $this->repo;
}
/**
* @param \JamesMoss\Flywheel\Repository $repo
*/
public function setRepo($repo)
{
$this->repo = $repo;
}
public function createFile($filename, $path, $url){
$file = new \JamesMoss\Flywheel\Document(array(
'name' => $filename,
'dateAdded' => date("Y-m-d h:i:s"),
'path' => $path,
'url' => $url,
"user" => $this->app->getUserName()
));
$this->saveFile($file);
}
public function saveFile( $file )
{
$this->getRepo()->store($file);
}
public function getAllFiles()
{
$settings = $this->app->getSettings();
if($settings["orderBy"] === "name"){
return $this->getFilesByNameAsc();
}else if($settings["orderBy"]==="date"){
return $this->getFilesByDate();
}else{
return $this->getFilesByDate();
}
}
private function getFilesByNameAsc()
{
$files = $this->repo->query()
->orderBy('name ASC')
->execute();
return $files;
}
private function getFilesByDate()
{
$files = $this->repo->query()
->orderBy('dateAdded DESC')
->execute();
return $files;
}
public function getDatedFolder()
{
$year = date('Y');
$day = date('m');
$this->createCurrentFolder($year, $day);
return $this->app->getBaseDir() . '/' . self::FOLDER_NAME . '/' . $year . '/' . $day;
}
public function getDatedUrl( $file )
{
$year = date('Y');
$day = date('m');
$baseUrl = $this->app->getBaseUrl();
return $baseUrl . '/'
. self::FOLDER_NAME . '/'
. $year . '/' . $day . '/' . $file;
}
private function createCurrentFolder($year, $day)
{
$new_path = $this->createPath(array(self::FOLDER_NAME,$year, $day));
if(!file_exists($new_path)){
mkdir($new_path,0755,true);
}
}
private function createPath($params)
{
$base = $this->app->getBaseDir() . '/';
$path ='';
foreach($params as $folder ){
$path .= $folder . '/';
}
return $base . $path;
}
public function randomName($num = 6)
{
$characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
$string = '';
$max = strlen($characters) - 1;
for ($i = 0; $i < $num; $i++) {
$string .= $characters[mt_rand(0, $max)];
}
return $string;
}
public function getAllowedFiles()
{
$settings = $this->app->getSettings();
return $settings["files"]["allowed"];
}
public function getMaxUploadFileSize()
{
$settings = $this->app->getSettings();
return $settings["files"]["maxUploadSize"];
}
}