-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
118 lines (104 loc) · 2.85 KB
/
api.php
File metadata and controls
118 lines (104 loc) · 2.85 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
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
<?php
require_once "auth.php";
define("OBJECT_DIR", __DIR__ . "/objects/");
function getUrl($path) {
$proto = (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] !== "off") ? "https" : "http";
$host = $_SERVER["HTTP_HOST"];
$script_dir = rtrim(dirname($_SERVER["SCRIPT_NAME"]), "/");
return "{$proto}://{$host}{$script_dir}{$path}";
}
function isValidOid($oid) {
return preg_match('/^[a-f0-9]{64}$/', $oid);
}
header("Content-Type: application/vnd.git-lfs+json");
if ($_SERVER["REQUEST_METHOD"] !== "POST") {
http_response_code(405);
exit;
}
$request_body = file_get_contents("php://input");
$request_json = json_decode($request_body, true);
if (json_last_error() !== JSON_ERROR_NONE) {
http_response_code(400);
echo json_encode(["message" => "Invalid JSON body"]);
exit;
}
if (
!isset($request_json["operation"]) ||
gettype($request_json["operation"]) !== "string" ||
!isset($request_json["objects"]) ||
gettype($request_json["objects"]) !== "array"
) {
http_response_code(400);
echo json_encode(["message" => "Invalid JSON body"]);
exit;
}
$operation = $request_json["operation"];
if ($operation !== "download" && $operation !== "upload") {
http_response_code(400);
echo json_encode(["message" => "Invalid JSON body"]);
exit;
}
if ($operation !== "download") {
if (
!isset($_SERVER["PHP_AUTH_USER"]) ||
!isset($_SERVER["PHP_AUTH_PW"]) ||
!verify_password($_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"])
) {
http_response_code(401);
exit;
}
}
$response = ["objects" => []];
foreach ($request_json["objects"] as $object) {
if (
!isset($object["oid"]) ||
gettype($object["oid"]) !== "string" ||
!isset($object["size"]) ||
gettype($object["size"]) !== "integer"
) {
http_response_code(400);
echo json_encode(["message" => "Invalid JSON body"]);
exit;
}
$oid = $object["oid"];
if (!isValidOid($oid)) {
http_response_code(400);
echo json_encode(["message" => "Invalid OID"]);
exit;
}
$size = $object["size"];
$file_path = OBJECT_DIR . $oid;
if (
$operation === "download" &&
(
!file_exists($file_path) ||
filesize($file_path) !== $size
)
) {
$response["objects"][] = [
"oid" => $oid,
"size" => $size,
"error" => [
"code" => 404,
"message" => "Object not found"
]
];
} else {
$response["objects"][] = [
"oid" => $oid,
"size" => $size,
"actions" => $operation === "upload" ? [
"upload" => [
"href" => getUrl("/objects/" . $oid),
"header" => (object)[],
]
] : [
"download" => [
"href" => getUrl("/objects/" . $oid),
"header" => (object)[],
]
]
];
}
}
echo json_encode($response);