This repository has been archived by the owner on May 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.php
271 lines (247 loc) · 8.44 KB
/
api.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
<?php
require ("vendor/slim/slim/Slim/Slim.php");
require ("Database.php");
\Slim\Slim::registerAutoloader();
define('PATH', $_SERVER['SERVER_NAME']);
date_default_timezone_set('UTC');
$app = new \Slim\Slim();
/**
* @api {get} /api/v1/jobs/:id Get Job Status
* @apiParam {Number} id Unique job ID.
* @apiDescription Returns the status of the current job.
* @apiGroup jobs
* @apiName GetJob
* @apiVersion 1.0.0
* @apiExample {curl} Example usage:
* curl -X GET 'http://pyro.demo/api/v1/jobs/1'
* @apiError (400 Bad Request) {Number} response 400
* @apiError (400 Bad Request) {String} message The ID supplied is invalid or does not exist.
* @apiSuccess (200 OK) {Number} id ID of current job.
* @apiSuccess (200 OK) {String} name Name of the uploaded file.
* @apiSuccess (200 OK) {Number} timestamp Timestamp of when file was uploaded.
* @apiSuccess (200 OK) {String} status Status of the current job.
* @apiSuccess (200 OK) {Number} progress Current progress of the job.
*/
$app->get('/api/v1/jobs/:id', function ($id) use ($app)
{
DB::FindJob($id, $app);
});
/**
* @api {get} /api/v1/stop/:id Stop (Pause) Job
* @apiParam {Number} id Unique job ID.
* @apiDescription Stop (Pause) the job specified.
* @apiGroup jobs
* @apiName StopJob
* @apiVersion 1.0.0
* @apiExample {curl} Example usage:
* curl -X GET 'http://pyro.demo/api/v1/stop/1'
* @apiError (400 Bad Request) {Number} response 400
* @apiError (400 Bad Request) {String} message The ID supplied is invalid or does not exist.
* @apiSuccess (200 OK) {Number} id ID of current job.
* @apiSuccess (200 OK) {String} status Status of the current job.
*/
$app->get('/api/v1/stop/:id', function ($id) use ($app)
{
DB::StopJob($id, $app);
});
/**
* @api {get} /api/v1/start/:id Start (Resume) Job
* @apiParam {Number} id Unique job ID.
* @apiDescription Start (Resume) the job specified.
* @apiGroup jobs
* @apiName StartJob
* @apiVersion 1.0.0
* @apiExample {curl} Example usage:
* curl -X GET 'http://pyro.demo/api/v1/start/1'
* @apiError (400 Bad Request) {Number} response 400
* @apiError (400 Bad Request) {String} message Error message.
* @apiSuccess (200 OK) {Number} id ID of current job.
* @apiSuccess (200 OK) {String} status Status of the current job.
*/
$app->get('/api/v1/start/:id', function ($id) use ($app)
{
DB::StartJob($id, $app);
});
/**
* @api {get} /api/v1/list/ Get All Jobs
* @apiDescription Returns a list of running and completed jobs.
* @apiGroup list
* @apiName GetJobs
* @apiVersion 1.0.0
* @apiExample {curl} Example usage:
* curl -X GET 'http://pyro.demo/api/v1/list/'
* @apiError (400 Bad Request) {Number} response 400
* @apiSuccess (200 OK) {String} No jobs in your table if there are no jobs.
* @apiSuccess (200 OK) {Number} id The unique job ID.
* @apiSuccess (200 OK) {String} name The job's name.
* @apiSuccess (200 OK) {Number} timestamp The timestamp of when the job was uploaded.
* @apiSuccess (200 OK) {Number} progress The progress state of the job.
* @apiSuccess (200 OK) {Number} is_zipped If the job has been completed and zipped for download.
* @apiSuccess (200 OK) {String} filename The name of the uploaded file.
*/
// Show a list of running and completed tasks.
$app->get('/api/v1/list/', function () use ($app)
{
// Grab the list of files.
//$db->ListJobs();
DB::ListJobs($app);
// Display the list.
});
/**
* @api {delete} /api/v1/delete/:id Delete a Job
* @apiDescription Deletes the specified job.
* @apiGroup delete
* @apiName DeleteJob
* @apiParam {Number} id The ID of the job to delete.
* @apiVersion 1.0.0
* @apiExample {curl} Example usage:
* curl -X DELETE 'http://pyro.demo/api/v1/delete/1'
* @apiError (400 Bad Request) {Number} response 400
* @apiSuccess (200 OK) {Number} response 200
*/
$app->delete('/api/v1/delete/:id', function ($id) use ($app)
{
return DB::DeleteJob($id, $app);
});
/**
* @api {get} /api/v1/download/:id Download a finished job
* @apiDescription Downloads a finished job.
* @apiGroup download
* @apiName DownloadJob
* @apiParam {Number} id The ID of the job to download.
* @apiVersion 1.0.0
* @apiExample {curl} Example usage:
* curl -X GET 'http://pyro.demo/api/v1/download/1'
*/
$app->get('/api/v1/download/:id', function($id)
{
$job = DB::GetJob($id);
//print $id;
// Get the job to be downloaded.
// $job = DB::FindJob($id, $app);
// echo $id . "<br />";
// $job["timestamp"] = 1446044514;
// // Create the zip.
// $zip = new ZipArchive();
// echo "uploads/" . $job["timestamp"] . "/" . $job["timestamp"] . ".zip<br />";
// // Add everything except the original FDS file to the list of stuff to zip.
// $zip->open("uploads/" . $job["timestamp"] . "/" . $job["timestamp"] . ".zip", ZipArchive::CREATE);
// $files = scandir(".");
// foreach($files as $f){
// echo $f;
// if(substr($f, -4) !== ".fds"){ // Don't include the original FDS file.
// $zip->addFile($f);
// }
// }
// $zip->close();
//
// // Now make it available for download.
// return $zip;
chmod(".", 0777);
$zip = new ZipArchive();
$files = scandir(".");
$zip->open("zip.zip", ZipArchive::CREATE);
// var_dump($zip);
foreach ($files as $f)
{
$zip->addFile($f);
}
$res = $zip->close();
return $zip;
// var_dump($res);
});
/**
* @api {post} /api/v1/jobs Upload job
* @apiDescription Uploads a new job.
* @apiGroup jobs
* @apiName UploadJob
* @apiVersion 1.0.0
* @apiExample {curl} Example usage:
* curl -X POST -F '[email protected]' 'http://pyro.demo/api/v1/jobs'
* @apiSuccess (200 OK) {Number} id The unique job ID.
* @apiSuccess (200 OK) {String} name The job's name.
* @apiSuccess (200 OK) {Number} timestamp The timestamp of when the job was uploaded.
* @apiSuccess (200 OK) {Number} progress The progress state of the job.
* @apiSuccess (200 OK) {Number} is_zipped If the job has been completed and zipped for download.
* @apiSuccess (200 OK) {String} filename The name of the uploaded file.
*/
$app->post('/api/v1/jobs/', function () use ($app)
{
$ext = pathinfo($_FILES["file"]["name"], PATHINFO_EXTENSION);
if ($_FILES["file"]["type"] != "application/octet-stream" || $ext != "fds")
{
echo "You have uploaded an invalid file.\n";
}
else
{
// Make sure the uploads file exists.
if (!file_exists('uploads'))
{
mkdir('uploads', 0777, true);
}
// Make sure the completed file exists.
if (!file_exists('completed'))
{
mkdir('completed', 0777, true);
}
// Create the folder for the simulation and put it in there.
$target = "uploads/" . time();
mkdir($target);
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target . "/" . basename($_FILES["file"]["name"])))
{
echo DB::AddJob($_FILES["file"]["name"]);
//echo json_encode(array("message" => "The file " . $_FILES["file"]["name"] . " has been uploaded."));
}
else
{
return FALSE;
}
}
});
/**
* @api {get} /api/v1/wipe/ Erases all jobs.
* @apiDescription Erases all jobs (Testing purposes only).
* @apiGroup wipe
* @apiName WipeJobs
* @apiVersion 1.0.0
* @apiExample {curl} Example usage:
* curl 'http://pyro.demo/api/v1/wipe'
*/
$app->get('/api/v1/wipe/', function() use($app){
R::wipe("job");
echo "The jobs have been wiped.";
});
/**
* @api {get} /api/v1/kill/:id Kills a job.
* @apiDescription Kills a currently running job.
* @apiGroup kill
* @apiName KillJob
* @apiVersion 1.0.0
* @apiExample {curl} Example usage:
* curl 'http://pyro.demo/api/v1/kill/1
* @apiSuccess (200 OK) {String} A string indicating kill and delete were successful.
* @apiError (400 Bad Request) {String) A string indicating the job does not exist.
*/
$app->get('/api/v1/kill/:id', function($id) use($app){
$job = DB::GetJob($id);
if($job['id'] == 0){
echo 'The job does not exist';
}else{
// For now we just straight up kill fds.exe
// If we want to kill a specific job, we'll need to store what job is associated with what pid.
shell_exec('taskkill /f /im fds.exe');
// Get the file it's in and remove everything except the .out file
$directoryOfFile = 'uploads/' . $job['timestamp'] . '/*';
$files = glob($directoryOfFile);
foreach($files as $f){
if(substr($f, -4) !== ".out"){
unlink($f);
}
}
// Remove the job from the database.
DB::DeleteJob($id, $app);
}
});
// Run the code.
$app->run();
?>