-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.php
62 lines (58 loc) · 2.25 KB
/
convert.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
<?php
namespace biomcs;
/**
* This file converts base64 encoded "content" "to" the desired format (hdf5 or json)
* The output format is json that contains biom data base64 encoded as part of the output array:
* Example:
* {
* content: "iUhERg0KGgoAAAAAAAgIAAQAEAAAAAAAAAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/zCEAAAAAAAA...AAAAAAAAAAAAAAAAAAA",
* error: null
* }
*/
// Allow CORS
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Content-Type");
// Set own content type
header("Content-Type: application/json");
require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
// explicitly set CONTENT_TYPE if empty (required for testing)
if (empty($_SERVER['CONTENT_TYPE'])) {
$_SERVER['CONTENT_TYPE'] = "application/x-www-form-urlencoded";
}
// load json data into $_REQUEST if content type is application/json
if ($_SERVER["CONTENT_TYPE"] === 'application/json') {
$_REQUEST = json_decode(file_get_contents('php://input'), true);
}
$to = isset($_REQUEST["to"]) ? $_REQUEST["to"] : false;
$content = isset($_REQUEST["content"]) ? $_REQUEST["content"] : false;
$content = base64_decode($content);
if (!$to) {
echo json_encode(array("error" => "Missing parameter 'to' please set to 'json' or 'hdf5'"));
} elseif ($to !== 'json' && $to !== 'hdf5') {
echo json_encode(array("error" => "Illegal value of 'to' parameter please set to 'json' or 'hdf5'"));
} elseif (!$content) {
echo json_encode(array("error" => "Missing parameter 'content' please send the content to convert as base64 encoded string"));
} else {
$biomcs = new BiomCS();
try {
if ($to === "json") {
echo json_encode(
array(
"content" => base64_encode($biomcs->convertToJSON($content)),
"error" => null
),
JSON_PRETTY_PRINT
);
} elseif ($to === "hdf5") {
print json_encode(
array(
"content" => base64_encode($biomcs->convertToHDF5($content)),
"error" => null
),
JSON_PRETTY_PRINT
);
}
} catch (\Exception $e) {
echo json_encode(array("error" => $e->getMessage()), JSON_PRETTY_PRINT);
}
}