mirror of
https://github.com/YGGverse/gemini-dl.git
synced 2026-03-31 09:05:31 +00:00
make absolute filename in links optional and disabled by default
This commit is contained in:
parent
4c478c2e88
commit
84e3d223d5
6 changed files with 102 additions and 21 deletions
|
|
@ -64,6 +64,7 @@ src/gemini-dl.php --source gemini://.. --target /path/to/download
|
|||
|
||||
# Optional
|
||||
|
||||
-a, --absolute - no value, absolute filepath in links (ignored on --keep), disabled by default
|
||||
-c, --crawl - no value, crawl document links (entire capsule download), disabled by default
|
||||
-d, --delay - integer, pause between requests (seconds), 1 by default
|
||||
-i, --index - string, index filename of directory listing, index.gmi by default
|
||||
|
|
|
|||
1
help.gmi
1
help.gmi
|
|
@ -17,6 +17,7 @@
|
|||
|
||||
## Optional
|
||||
|
||||
-a, --absolute - no value, absolute filepath in links (ignored on --keep), disabled by default
|
||||
-c, --crawl - no value, crawl document links (entire capsule download), disabled by default
|
||||
-d, --delay - integer, pause between requests (seconds), 1 by default
|
||||
-i, --index - string, index filename of directory listing, index.gmi by default
|
||||
|
|
|
|||
|
|
@ -102,6 +102,12 @@ class Cli
|
|||
$this->source[$offset]
|
||||
);
|
||||
|
||||
// Build filesystem location
|
||||
$filename = $this->filesystem->getFilenameFromNetAddress(
|
||||
$source,
|
||||
$this->option->index
|
||||
);
|
||||
|
||||
// Build request
|
||||
$request = new Request(
|
||||
$source->get()
|
||||
|
|
@ -188,7 +194,7 @@ class Cli
|
|||
)
|
||||
);
|
||||
|
||||
// Set downloader mode
|
||||
// Set data mode
|
||||
$raw = ($this->option->raw || !str_contains((string) $response->getMeta(), 'text/gemini'));
|
||||
|
||||
// Parse gemtext
|
||||
|
|
@ -228,14 +234,27 @@ class Cli
|
|||
// Address --keep not requested
|
||||
if (!$this->option->keep)
|
||||
{
|
||||
// Replace link to local path
|
||||
$link->setAddress(
|
||||
$this->filesystem->getFilenameFromNetAddress(
|
||||
// Generate absolute local file name
|
||||
$local = $this->filesystem->getFilenameFromNetAddress(
|
||||
$address,
|
||||
$this->option->index,
|
||||
);
|
||||
|
||||
// Absolute option skipped, make local path relative
|
||||
if (!$this->option->absolute)
|
||||
{
|
||||
$local = Filesystem::getFilenameRelativeToDirname(
|
||||
$local,
|
||||
dirname(
|
||||
$filename
|
||||
)
|
||||
);
|
||||
}
|
||||
// Replace link to local path
|
||||
$link->setAddress(
|
||||
$local
|
||||
);
|
||||
}
|
||||
|
||||
// Append new address to crawler pool
|
||||
$this->addSource(
|
||||
|
|
@ -244,22 +263,10 @@ class Cli
|
|||
}
|
||||
}
|
||||
|
||||
// Build document filesystem location
|
||||
$filename = $this->filesystem->getFilenameFromNetAddress(
|
||||
$source,
|
||||
$this->option->index
|
||||
);
|
||||
|
||||
// Save document to file
|
||||
$result = $this->filesystem->save(
|
||||
$filename,
|
||||
$raw || empty($document) ? $response->getBody()
|
||||
: $document->toString()
|
||||
);
|
||||
|
||||
// Debug FS
|
||||
if ($result)
|
||||
{
|
||||
if ($this->filesystem->save($filename, $raw || empty($document) ? $response->getBody()
|
||||
: $document->toString())
|
||||
) {
|
||||
print(
|
||||
Message::green(
|
||||
_("\tsave: ") . $filename
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ namespace Yggverse\GeminiDL\Model\Cli;
|
|||
|
||||
class Option
|
||||
{
|
||||
public bool $absolute = false;
|
||||
public bool $crawl = false;
|
||||
public int $delay = 1;
|
||||
public bool $external = false;
|
||||
|
|
@ -32,6 +33,10 @@ class Option
|
|||
}
|
||||
|
||||
// Define variables
|
||||
$this->absolute = boolval(
|
||||
isset($options['absolute']) || isset($options['a']) || $this->absolute
|
||||
);
|
||||
|
||||
$this->crawl = boolval(
|
||||
isset($options['crawl']) || isset($options['c']) || $this->crawl
|
||||
);
|
||||
|
|
|
|||
|
|
@ -174,4 +174,70 @@ class Filesystem
|
|||
$data
|
||||
);
|
||||
}
|
||||
|
||||
// Helpers
|
||||
public static function getFilenameRelativeToDirname(
|
||||
string $filename,
|
||||
string $dirname
|
||||
): string
|
||||
{
|
||||
// Validate paths
|
||||
if (empty($filename))
|
||||
{
|
||||
throw new \Exception(
|
||||
'Filename is could not be empty'
|
||||
);
|
||||
}
|
||||
|
||||
if (empty($dirname))
|
||||
{
|
||||
throw new \Exception(
|
||||
'Dirname is could not be empty'
|
||||
);
|
||||
}
|
||||
|
||||
if (str_starts_with($filename, $dirname))
|
||||
{
|
||||
return ltrim(
|
||||
str_replace(
|
||||
$dirname,
|
||||
DIRECTORY_SEPARATOR,
|
||||
$filename
|
||||
),
|
||||
DIRECTORY_SEPARATOR
|
||||
);
|
||||
}
|
||||
|
||||
$filepath = explode(
|
||||
DIRECTORY_SEPARATOR,
|
||||
dirname(
|
||||
$filename
|
||||
)
|
||||
);
|
||||
|
||||
$segments = [];
|
||||
|
||||
foreach (
|
||||
explode(
|
||||
DIRECTORY_SEPARATOR,
|
||||
$dirname
|
||||
) as $level => $directory)
|
||||
{
|
||||
if (isset($filepath[$level]) && $filepath[$level] == $directory)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$segments[] = '..';
|
||||
}
|
||||
|
||||
$segments[] = basename(
|
||||
$filename
|
||||
);
|
||||
|
||||
return implode(
|
||||
DIRECTORY_SEPARATOR,
|
||||
$segments
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -15,8 +15,9 @@ try
|
|||
// Start application
|
||||
$cli = new Cli(
|
||||
getopt(
|
||||
'cd:ef:hi:kl:m:rs:t:u',
|
||||
'acd:ef:hi:kl:m:rs:t:u',
|
||||
[
|
||||
'absolute:',
|
||||
'crawl:',
|
||||
'delay:',
|
||||
'external',
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue