From 100806af027f70486db003d0f220d1023e659034 Mon Sep 17 00:00:00 2001 From: ghost Date: Wed, 13 Dec 2023 00:29:34 +0200 Subject: [PATCH] complete local snaps feature #2 --- src/webui/api.php | 90 ++++++++++++++++++++++++++++++++++++++++++- src/webui/explore.php | 42 +++++++++++++++++++- 2 files changed, 129 insertions(+), 3 deletions(-) diff --git a/src/webui/api.php b/src/webui/api.php index f9d243b..db6346f 100644 --- a/src/webui/api.php +++ b/src/webui/api.php @@ -65,7 +65,6 @@ switch (!empty($_GET['action']) ? $_GET['action'] : false) { exit; } - // Detect remote snap source if (preg_match('/^[\d]+$/', $_GET['source'])) { @@ -155,9 +154,96 @@ switch (!empty($_GET['action']) ? $_GET['action'] : false) { } // Local + else if ($config->snap->storage->local->enabled) + { + // Prefix absolute + if ('/' === substr($config->snap->storage->local->directory, 0, 1)) + { + $prefix = $config->snap->storage->local->directory; + } + + // Prefix relative + else + { + $prefix = __DIR__ . '/../../' . $config->snap->storage->local->directory; + } + + // Prepare snap path + $filename = sprintf( + '%s/%s/%s.tar.gz', + $prefix, + implode( + '/', + str_split( + $_GET['md5url'] + ) + ), + $_GET['time'] + ); + + // Check snap exist + if (!file_exists($filename) || !is_readable($filename)) + { + echo json_encode( + [ + 'status' => false, + 'message' => _('requested snap not found') + ] + ); + + exit; + } + + // Check snap has valid size + if (!$size = filesize($filename)) + { + echo json_encode( + [ + 'status' => false, + 'message' => _('requested snap has invalid size') + ] + ); + + exit; + } + + // Set headers + header( + 'Content-Type: application/tar+gzip' + ); + + header( + sprintf( + 'Content-Length: %s', + $size + ) + ); + + header( + sprintf( + 'Content-Disposition: filename="snap.%s.%s"', + $_GET['md5url'], + basename( + $filename + ) + ) + ); + + readfile( + $filename + ); + + exit; + } + else { - // @TODO + echo json_encode( + [ + 'status' => false, + 'message' => _('requested source not found') + ] + ); } break; diff --git a/src/webui/explore.php b/src/webui/explore.php index af9c8d1..997aecc 100644 --- a/src/webui/explore.php +++ b/src/webui/explore.php @@ -86,7 +86,47 @@ $filepath = implode( ) ); -/// Local snaps @TODO +/// Local snaps +if ($config->snap->storage->local->enabled) +{ + /// absolute + if ('/' === substr($config->snap->storage->local->directory, 0, 1)) + { + $prefix = $config->snap->storage->local->directory; + } + + /// relative + else + { + $prefix = __DIR__ . '/../../' . $config->snap->storage->local->directory; + } + + foreach ((array) scandir(sprintf('%s/%s', $prefix, $filepath)) as $filename) + { + if (in_array($filename, ['.', '..'])) + { + continue; + } + + $basename = basename($filename); + $time = preg_replace('/\D/', '', $basename); + + $snaps[_('Local')][] = (object) + [ + 'source' => 'local', + 'md5url' => $md5url, + 'name' => $basename, + 'time' => $time, + 'size' => filesize( + sprintf( + '%s/%s', + $prefix, + $filepath + ) + ), + ]; + } +} /// Remote snaps foreach ($config->snap->storage->remote->ftp as $i => $ftp)