-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_service.php
359 lines (321 loc) · 12.9 KB
/
file_service.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<?php
namespace SmartHistoryTourManager;
require_once(dirname(__FILE__) . '/message_service.php');
require_once(dirname(__FILE__) . '/post_service.php');
require_once(dirname(__FILE__) . '/views/view.php');
require_once(dirname(__FILE__) . '/view_helper.php');
require_once(dirname(__FILE__) . '/models/tour_records.php');
require_once(dirname(__FILE__) . '/models/tours.php');
require_once(dirname(__FILE__) . '/logging.php');
/**
* A class used to handle the keeping of record files for tours.
*
* NOTE: Uses a lot of wordpress internal functions to determine file paths.
*/
class FileServiceResponse {
// a boolean to indicate if everything went okay
public $ok = false;
// All files created are collected in this array
// NOTE: These may not exist anymore
public $files_created = array();
// An array of MessageService Messages that may be shown to the user
public $messages = array();
public function add_file_created($path) {
array_push($this->files_created, $path);
}
public function get_last_file_created() {
$n = count($this->files_created);
if($n <= 0) {
return null;
} else {
return $this->files_created[$n - 1];
}
}
public function add_error($msg_str) {
$this->ok = false;
$msg = new Message($msg_str, MessageService::ERROR);
array_push($this->messages, $msg);
}
public function add_warning($msg_str) {
$msg = new Message($msg_str, MessageService::WARNING);
array_push($this->messages, $msg);
}
}
class FileService {
const TOUR_PREFIX = 'shtm-tour-';
// path of the tour folder (for all tour files) below the wordpress upload
// directory
const TOUR_FOLDER_PATH = '/smart-history-tours';
/**
* Create files for the tour. Expects a valid tour as input with all
* related objects set on the tour.
*
* @return FileServiceResponse Always a response object
*/
public static function create_files($record, $tour) {
$response = new FileServiceResponse();
// check that the record has an associated timestamp and content
if(empty($record->published_at)) {
$msg = "Cannot create tour files without publish timestamp.";
return self::handle_error($response, $msg);
}
if(empty($record->content)) {
$msg = "Cannot create tour files without tour record content.";
return self::handle_error($response, $msg);
}
// Get the files of mediaitems linked to the tour as first entries to
// zip up
$files_to_zip = self::get_files_for_tour($response, $tour);
if(!$response->ok) {
$msg = 'Failed to determine files for tour.';
return self::handle_error($response, $msg);
}
// if a tour content file already exists, it should be save to remove,
// but make sure to log it as this should not be the case normally
$path = self::get_temporary_path_for_record($record, $tour->id, '.yaml');
if(file_exists($path)) {
debug_log("Removing previously created tour content at: $path");
unlink($path);
}
// write the record content to disk
$handle = fopen($path, 'w');
$result = fwrite($handle, $record->content);
$result_close = fclose($handle);
if(!$result || !$result_close) {
$msg = "Error creating the content file at: $path";
return self::handle_error($response, $msg);
} else {
$response->add_file_created($path);
array_push($files_to_zip, $path);
}
// create the zip archive from all files collected so far
$path = self::get_temporary_path_for_record($record, $tour->id, '.zip');
if(file_exists($path)) {
debug_log("Removing previously created tour zip at: $path");
unlink($path);
}
$response = self::create_zip_archive($response, $files_to_zip, $path);
$zip_file = $response->get_last_file_created();
if(!$response->ok || !file_exists($zip_file)) {
$msg = 'Error packing the zip file.';
return self::handle_error($response, $msg);
}
// get or create the tour folder
$tour_folder = self::get_or_create_tour_folder($response);
if(!$tour_folder || !$response->ok) {
return self::handle_error($response, 'Failed to get tour folder.');
}
// copy the zip file to its destination
$destination = $tour_folder . '/' . basename($zip_file);
$result = copy($zip_file, $destination);
if(!$result) {
$msg = "Error copying the tour package to: $destination";
$response->add_file_created($destination);
return self::handle_error($response, $msg);
} else {
// remove the files previously created
$response = self::remove_files_created($response);
if(!$response->ok) {
$msg = 'Critical error in removing created files';
return $response;
}
}
// all went well, return with the zip as last file created
$response->add_file_created($destination);
$response->ok = true;
return $response;
}
/**
* Returns the upload url if the file exists below the tour folder.
*
* @return String|null The url on success else false.
*/
public static function get_upload_url($path) {
// get the tour folder name using a new response
$response = new FileServiceResponse();
$base_path = self::get_or_create_tour_folder($response);
if(!$response->ok) {
return null;
}
$base_url = wp_get_upload_dir()['baseurl'];
$base_name = basename($path);
if(file_exists("$base_path/$base_name")) {
return ($base_url . self::TOUR_FOLDER_PATH . '/' . $base_name) ;
} else {
return null;
}
}
// TODO: This belongs to an api endpoint, no file should be written
public static function write_as_publish_list($str) {
// get the tour folder name using a new response
$response = new FileServiceResponse();
$base_path = self::get_or_create_tour_folder($response);
if(!$response->ok) {
return null;
}
// simply write everything to file
$path = $base_path . '/tours.yaml';
$handle = fopen($path, 'w');
$result = fwrite($handle, $str);
debug_log("Wrote $result bytes to $path");
return fclose($handle);
}
public static function write_as_publish_list_v2($str) {
// get the tour folder name using a new response
$response = new FileServiceResponse();
$base_path = self::get_or_create_tour_folder($response);
if(!$response->ok) {
return null;
}
// simply write everything to file
$path = $base_path . '/tours.v2.yaml';
$handle = fopen($path, 'w');
$result = fwrite($handle, $str);
debug_log("Wrote $result bytes to $path");
return fclose($handle);
}
// return an array of mediaitems that belong to posts of a tour's mapstops
private static function get_files_for_tour($response, $tour) {
$result = array();
foreach($tour->mapstops as $mapstop) {
foreach($mapstop->post_ids as $post_id) {
$media = PostService::get_post_media($post_id);
foreach($media as $medium) {
$path = get_attached_file($medium->ID);
if(!file_exists($path)) {
$msg = "File does not exist: $medium->guid";
$msg .= " (mapstop: $mapstop->id, page: $post_id)";
$response->add_error($msg);
return $result;
}
array_push($result, $path);
}
}
}
if ($tour->is_indoor()) {
foreach($tour->scenes as $scene) {
$path = get_attached_file($scene->id);
if(!file_exists($path)) {
$msg = "File does not exist: $scene->src";
$msg .= " (scene: $scene->id, page: $scene->id)";
$response->add_error($msg);
return $result;
}
array_push($result, $path);
}
}
$response->ok = true;
return $result;
}
/**
* Get the tour folder below the wordpress upload dir or create it if it is
* not present. Set errors on the response object if anything bad happens.
*
* @return String|false The path to the folder or false on error.
*/
private static function get_or_create_tour_folder($response) {
$upload_dir = self::get_wp_upload_base_dir();
if(!is_dir($upload_dir)) {
$response->add_error('Failed to determine the base upload dir.');
return false;
}
// the tour folder is directly below the upload folder
$tour_folder = $upload_dir . self::TOUR_FOLDER_PATH;
$result = wp_mkdir_p($tour_folder);
if(!$result) {
$msg = "Failed to get/create the tour folder at: $path";
$response->add_error($msg);
return false;
}
$response->ok = true;
return $tour_folder;
}
private static function create_zip_archive($response, $files, $path) {
$zip = new \ZipArchive();
$result = $zip->open($path,
(\ZipArchive::CREATE | \ZipArchive::OVERWRITE));
if(!$result) {
$msg = "Failed to open zip archive (code: '$result') at: '$path'.";
return self::handle_error($response, $msg);
}
// add all files to the zip archive setting the relative path to
// their basename
foreach ($files as $file) {
$result = $zip->addFile($file, basename($file));
if(!$result) {
$msg = "Failed to add file to zip archive: $file";
return self::handle_error($response, $msg);
}
}
// Create the archive by calling close()
$result = $zip->close();
if(!$result) {
$msg = "Failed to close zip archive on path: $path.";
return self::handle_error($response, $msg);
}
// Set the created archive path and return ok
$response->add_file_created($path);
$response->ok = true;
return $response;
}
/**
* Set the error on the response object, clean up all files created and
* return the response object.
*
* @return FileServiceResponse
*/
private static function handle_error($response, $msg_str) {
$response->add_error($msg_str);
$response = self::remove_files_created($response);
return $response;
}
/**
* Remove all files that are listed as created. (Only files below /tmp or
* below the wp-content folder can be deleted this way).
*
* @return FileServiceResponse
*/
public static function remove_files_created($response) {
// We need to determine the wordpress upload dir and the tmp folder
// to check the created file paths
$ul_dir = self::get_wp_upload_base_dir();
$tmp_dir = sys_get_temp_dir();
if(!is_dir($ul_dir) || !is_dir($tmp_dir)) {
$msg = 'Failed to get viable directories for deletion on error.';
$response->add_error($msg);
return $response;
}
// remove the created files, check paths to make sure that we do not
// delete anywhere where we are not supposed to
$might_still_exist = array();
foreach($response->files_created as $path) {
$in_ul_dir = (substr($path, 0, strlen($ul_dir)) === $ul_dir);
$in_tmp_dir = (substr($path, 0, strlen($tmp_dir)) === $tmp_dir);
if($in_ul_dir || $in_tmp_dir) {
$result = unlink($path);
if(!$result) {
array_push($might_still_exist, $path);
$response->add_warning("Failed to remove: $path");
}
} else {
array_push($might_still_exist, $path);
$response->add_warning("Invalid path to delete: $path");
}
}
$response->files_created = $might_still_exist;
return $response;
}
// a records file name depends on the published_at-field (always present)
// and the tour id, which might not be present on the record and thus must
// be given as a param
private static function get_temporary_path_for_record($record, $tour_id, $suffix) {
$path = sys_get_temp_dir() . '/' . self::TOUR_PREFIX;
$path .= $tour_id . '-' . $record->published_at . $suffix;
return $path;
}
// return the base dir where wp uploads are stored
private static function get_wp_upload_base_dir() {
return wp_get_upload_dir()['basedir'];
}
}
?>