1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* File Class |
5
|
|
|
* |
6
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
7
|
|
|
* @author Omar El Gabry <[email protected]> |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
class File extends Model{ |
|
|
|
|
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* get all files. |
14
|
|
|
* |
15
|
|
|
* @access public |
16
|
|
|
* @param integer $pageNum |
17
|
|
|
* @return array |
18
|
|
|
* |
19
|
|
|
*/ |
20
|
|
View Code Duplication |
public function getAll($pageNum = 1){ |
|
|
|
|
21
|
|
|
|
22
|
|
|
// get pagination object |
23
|
|
|
$pagination = Pagination::pagination("files", "", [], $pageNum); |
24
|
|
|
$offset = $pagination->getOffset(); |
25
|
|
|
$limit = $pagination->perPage; |
26
|
|
|
|
27
|
|
|
$database = Database::openConnection(); |
28
|
|
|
$query = "SELECT files.id AS id, files.filename, users.id AS user_id, users.name AS user_name, files.extension AS format, files.hashed_filename, files.date "; |
29
|
|
|
$query .= "FROM users, files "; |
30
|
|
|
$query .= "WHERE users.id = files.user_id "; |
31
|
|
|
$query .= "ORDER BY files.date DESC "; |
32
|
|
|
$query .= "LIMIT $limit OFFSET $offset"; |
33
|
|
|
|
34
|
|
|
$database->prepare($query); |
35
|
|
|
$database->execute(); |
36
|
|
|
$files = $database->fetchAllAssociative(); |
37
|
|
|
|
38
|
|
|
return array("files" => $files, "pagination" => $pagination); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* get file by Id. |
43
|
|
|
* |
44
|
|
|
* @access public |
45
|
|
|
* @param string $fileId |
46
|
|
|
* @return array Array holds the data of the file |
47
|
|
|
*/ |
48
|
|
View Code Duplication |
public function getById($fileId){ |
|
|
|
|
49
|
|
|
|
50
|
|
|
$database = Database::openConnection(); |
51
|
|
|
$query = "SELECT files.id AS id, files.filename, users.id AS user_id, users.name AS user_name, files.extension AS format, files.hashed_filename, files.date "; |
52
|
|
|
$query .= "FROM users, files "; |
53
|
|
|
$query .= "WHERE files.id = :id "; |
54
|
|
|
$query .= "AND users.id = files.user_id LIMIT 1 "; |
55
|
|
|
|
56
|
|
|
$database->prepare($query); |
57
|
|
|
$database->bindValue(':id', (int)$fileId); |
58
|
|
|
$database->execute(); |
59
|
|
|
|
60
|
|
|
$file = $database->fetchAllAssociative(); |
61
|
|
|
return $file; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* get file by hashed name. |
66
|
|
|
* files are unique by the hashed file name(= hash(original filename . extension)). |
67
|
|
|
* |
68
|
|
|
* @access public |
69
|
|
|
* @param string $hashedFileName |
70
|
|
|
* @return array Array holds the data of the file |
71
|
|
|
*/ |
72
|
|
View Code Duplication |
public function getByHashedName($hashedFileName){ |
|
|
|
|
73
|
|
|
|
74
|
|
|
$database = Database::openConnection(); |
75
|
|
|
|
76
|
|
|
$query = "SELECT files.id AS id, files.filename, files.extension, files.hashed_filename "; |
77
|
|
|
$query .= "FROM files "; |
78
|
|
|
$query .= "WHERE hashed_filename = :hashed_filename "; |
79
|
|
|
$query .= "LIMIT 1 "; |
80
|
|
|
|
81
|
|
|
$database->prepare($query); |
82
|
|
|
$database->bindValue(':hashed_filename', $hashedFileName); |
83
|
|
|
$database->execute(); |
84
|
|
|
|
85
|
|
|
$file = $database->fetchAssociative(); |
86
|
|
|
return $file; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* create file. |
91
|
|
|
* |
92
|
|
|
* @access public |
93
|
|
|
* @param integer $userId |
94
|
|
|
* @param array $fileData |
95
|
|
|
* @return array Array holds the created file |
96
|
|
|
* @throws Exception If file couldn't be created |
97
|
|
|
*/ |
98
|
|
|
public function create($userId, $fileData){ |
99
|
|
|
|
100
|
|
|
// upload |
101
|
|
|
$file = Uploader::uploadFile($fileData); |
102
|
|
|
|
103
|
|
|
if(!$file) { |
104
|
|
|
$this->errors = Uploader::errors(); |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$database = Database::openConnection(); |
109
|
|
|
|
110
|
|
|
$query = "INSERT INTO files (user_id, filename, hashed_filename, extension) VALUES (:user_id, :filename, :hashed_filename, :extension)"; |
111
|
|
|
|
112
|
|
|
$database->prepare($query); |
113
|
|
|
$database->bindValue(':user_id', $userId); |
114
|
|
|
$database->bindValue(':filename', $file["filename"]); |
115
|
|
|
$database->bindValue(':hashed_filename', $file["hashed_filename"]); |
116
|
|
|
$database->bindValue(':extension', strtolower($file["extension"])); |
117
|
|
|
$database->execute(); |
118
|
|
|
|
119
|
|
|
// if insert failed, then delete the file |
120
|
|
|
if($database->countRows() !== 1){ |
121
|
|
|
Uploader::deleteFile(APP ."uploads/" . $file["basename"]); |
122
|
|
|
throw new Exception ("Couldn't upload file"); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$fileId = $database->lastInsertedId(); |
126
|
|
|
$file = $this->getById($fileId); |
127
|
|
|
return $file; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* deletes file. |
132
|
|
|
* This method overrides the deleteById() method in Model class. |
133
|
|
|
* |
134
|
|
|
* @access public |
135
|
|
|
* @param array $id |
136
|
|
|
* @throws Exception If failed to delete the file |
137
|
|
|
* |
138
|
|
|
*/ |
139
|
|
|
public function deleteById($id){ |
140
|
|
|
|
141
|
|
|
$database = Database::openConnection(); |
142
|
|
|
|
143
|
|
|
$database->getById("files", $id); |
144
|
|
|
$file = $database->fetchAssociative(); |
145
|
|
|
|
146
|
|
|
// start a transaction to guarantee the file will be deleted from both; database and filesystem |
147
|
|
|
$database->beginTransaction(); |
148
|
|
|
$database->deleteById("files", $id); |
149
|
|
|
|
150
|
|
|
if($database->countRows() !== 1){ |
151
|
|
|
$database->rollBack(); |
152
|
|
|
throw new Exception ("Couldn't delete file"); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$basename = $file["hashed_filename"] . "." . $file["extension"]; |
156
|
|
|
Uploader::deleteFile(APP ."uploads/" . $basename); |
157
|
|
|
|
158
|
|
|
$database->commit(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.