Browse Source

complete local snaps feature #2

main
ghost 12 months ago
parent
commit
100806af02
  1. 90
      src/webui/api.php
  2. 42
      src/webui/explore.php

90
src/webui/api.php

@ -65,7 +65,6 @@ switch (!empty($_GET['action']) ? $_GET['action'] : false) { @@ -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) { @@ -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;

42
src/webui/explore.php

@ -86,7 +86,47 @@ $filepath = implode( @@ -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)

Loading…
Cancel
Save