From 5b166a624530477f94b2fa5cc801665eb6eba654 Mon Sep 17 00:00:00 2001 From: ghost Date: Sun, 26 Nov 2023 22:14:28 +0200 Subject: [PATCH] implement remote snap download API #2 --- src/webui/api.php | 184 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 184 insertions(+) create mode 100644 src/webui/api.php diff --git a/src/webui/api.php b/src/webui/api.php new file mode 100644 index 0000000..b45d4f3 --- /dev/null +++ b/src/webui/api.php @@ -0,0 +1,184 @@ + false, + 'message' => _('valid source required') + ] + ); + + exit; + + case isset($_GET['md5url']) && preg_match('/^[a-f0-9]{32}$/', $_GET['md5url']): + + echo json_encode( + [ + 'status' => false, + 'message' => _('valid md5url required') + ] + ); + + exit; + + case isset($_GET['time']) && preg_match('/^[\d]+$/', $_GET['time']): + + echo json_encode( + [ + 'status' => false, + 'message' => _('valid time required') + ] + ); + + exit; + } + + + // Detect remote snap source + if (preg_match('/^[\d]+$/', $_GET['source'])) + { + if (!isset($config->snap->storage->remote->ftp[$_GET['source']]) || !$config->snap->storage->remote->ftp[$_GET['source']]->enabled) + { + echo json_encode( + [ + 'status' => false, + 'message' => _('requested source not found') + ] + ); + + exit; + } + + // Connect remote + $remote = new \Yggverse\Ftp\Client(); + + $connection = $remote->connect( + $config->snap->storage->remote->ftp[$_GET['source']]->connection->host, + $config->snap->storage->remote->ftp[$_GET['source']]->connection->port, + $config->snap->storage->remote->ftp[$_GET['source']]->connection->username, + $config->snap->storage->remote->ftp[$_GET['source']]->connection->password, + $config->snap->storage->remote->ftp[$_GET['source']]->connection->directory, + $config->snap->storage->remote->ftp[$_GET['source']]->connection->timeout, + $config->snap->storage->remote->ftp[$_GET['source']]->connection->passive + ); + + // Remote host connected + if ($connection) { + + // Prepare snap path + $filename = sprintf( + '%s/%s.tar.gz', + implode( + '/', + str_split( + $_GET['md5url'] + ) + ), + $_GET['time'] + ); + + // Check snap exist + if (!$size = $remote->size($filename)) + { + echo json_encode( + [ + 'status' => false, + 'message' => _('requested snap not found') + ] + ); + + exit; + } + + // Set headers + header( + 'Content-Type: application/tar+gzip' + ); + + header( + sprintf( + 'Content-Length: %s', + $size + ) + ); + + header( + sprintf( + 'Content-Disposition: filename="%s"', + basename( + $filename + ) + ) + ); + + // Return file + $remote->get( + $filename, + 'php://output' + ); + + $remote->close(); + } + } + + // Local + else + { + // @TODO + } + + break; + + default: + + echo json_encode( + [ + 'status' => false, + 'message' => _('Undefined API method') + ] + ); + } + + break; + + default: + + echo json_encode( + [ + 'status' => false, + 'message' => _('Undefined API action') + ] + ); +}