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) {
exit; exit;
} }
// Detect remote snap source // Detect remote snap source
if (preg_match('/^[\d]+$/', $_GET['source'])) if (preg_match('/^[\d]+$/', $_GET['source']))
{ {
@ -155,9 +154,96 @@ switch (!empty($_GET['action']) ? $_GET['action'] : false) {
} }
// Local // 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 else
{ {
// @TODO echo json_encode(
[
'status' => false,
'message' => _('requested source not found')
]
);
} }
break; break;

42
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 /// Remote snaps
foreach ($config->snap->storage->remote->ftp as $i => $ftp) foreach ($config->snap->storage->remote->ftp as $i => $ftp)

Loading…
Cancel
Save