-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
62 lines (51 loc) · 1.87 KB
/
index.php
File metadata and controls
62 lines (51 loc) · 1.87 KB
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
<?php
/** @var array $config */
include 'main.php';
use Bixie\DfmApi\DfmApi;
use Bixie\DfmApi\Lime\App;
$app = new App(array_merge($config['lime'], $config['dfm_api']));
//not used in endpoints for the moment. Module helpers DO use this class
$api = new DfmApi($config['dfm_api'], $app['debug']);
$app->helpers['apikey'] = 'Bixie\DfmApi\Helpers\ApiKeyHelper';
$app->helpers['previewzip'] = 'Bixie\DfmApi\Helpers\PreviewZipHelper';
$app->helpers['logger'] = 'Bixie\DfmApi\Helpers\Logger';
$app->bind('/', function() {
return 'API client/server for DFM preview requests';
});
/**
* Receive the generated image from the server
*/
if ($app->req_is('put')) { //no shorthand function for put
$app->bind('/preview/:preview_id', function($params) {
if (!$this('apikey')->test()) {
return ['status' => 401, 'error' => 'Invalid API key',];
}
if (empty($params['preview_id'])) {
return ['status' => 400, 'error' => 'No preview id!',];
}
$preview_id = (string)$params['preview_id'];
if (!$this('previewzip')->saveZipResponse($preview_id)) {
return ['status' => 500, 'error' => 'Error writing temp-zipfile',];
}
return ['preview_id' => $preview_id,];
});
}
/**
* Error Handling
*/
$app->on('after', function() {
switch($this->response->status){
case '404':
$this->response->body = ['error' => 'Endpoint not found'];
break;
case '500':
$this->response->body = ['error' => $this->response->body];
$this('logger')->error($this->response->body);
break;
}
if (is_array($this->response->body) && !empty($this->response->body['status']) && !empty($this->response->body['error'])) {
$this->response->status = $this->response->body['status'];
unset($this->response->body['status']);
}
});
$app->run();