mirror of
https://github.com/YGGverse/gemini-dl.git
synced 2026-03-31 17:15:32 +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
|
# 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
|
-c, --crawl - no value, crawl document links (entire capsule download), disabled by default
|
||||||
-d, --delay - integer, pause between requests (seconds), 1 by default
|
-d, --delay - integer, pause between requests (seconds), 1 by default
|
||||||
-i, --index - string, index filename of directory listing, index.gmi 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
|
## 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
|
-c, --crawl - no value, crawl document links (entire capsule download), disabled by default
|
||||||
-d, --delay - integer, pause between requests (seconds), 1 by default
|
-d, --delay - integer, pause between requests (seconds), 1 by default
|
||||||
-i, --index - string, index filename of directory listing, index.gmi by default
|
-i, --index - string, index filename of directory listing, index.gmi by default
|
||||||
|
|
|
||||||
|
|
@ -102,6 +102,12 @@ class Cli
|
||||||
$this->source[$offset]
|
$this->source[$offset]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Build filesystem location
|
||||||
|
$filename = $this->filesystem->getFilenameFromNetAddress(
|
||||||
|
$source,
|
||||||
|
$this->option->index
|
||||||
|
);
|
||||||
|
|
||||||
// Build request
|
// Build request
|
||||||
$request = new Request(
|
$request = new Request(
|
||||||
$source->get()
|
$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'));
|
$raw = ($this->option->raw || !str_contains((string) $response->getMeta(), 'text/gemini'));
|
||||||
|
|
||||||
// Parse gemtext
|
// Parse gemtext
|
||||||
|
|
@ -228,12 +234,25 @@ class Cli
|
||||||
// Address --keep not requested
|
// Address --keep not requested
|
||||||
if (!$this->option->keep)
|
if (!$this->option->keep)
|
||||||
{
|
{
|
||||||
|
// 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
|
// Replace link to local path
|
||||||
$link->setAddress(
|
$link->setAddress(
|
||||||
$this->filesystem->getFilenameFromNetAddress(
|
$local
|
||||||
$address,
|
|
||||||
$this->option->index,
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -244,22 +263,10 @@ class Cli
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build document filesystem location
|
|
||||||
$filename = $this->filesystem->getFilenameFromNetAddress(
|
|
||||||
$source,
|
|
||||||
$this->option->index
|
|
||||||
);
|
|
||||||
|
|
||||||
// Save document to file
|
// Save document to file
|
||||||
$result = $this->filesystem->save(
|
if ($this->filesystem->save($filename, $raw || empty($document) ? $response->getBody()
|
||||||
$filename,
|
: $document->toString())
|
||||||
$raw || empty($document) ? $response->getBody()
|
) {
|
||||||
: $document->toString()
|
|
||||||
);
|
|
||||||
|
|
||||||
// Debug FS
|
|
||||||
if ($result)
|
|
||||||
{
|
|
||||||
print(
|
print(
|
||||||
Message::green(
|
Message::green(
|
||||||
_("\tsave: ") . $filename
|
_("\tsave: ") . $filename
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ namespace Yggverse\GeminiDL\Model\Cli;
|
||||||
|
|
||||||
class Option
|
class Option
|
||||||
{
|
{
|
||||||
|
public bool $absolute = false;
|
||||||
public bool $crawl = false;
|
public bool $crawl = false;
|
||||||
public int $delay = 1;
|
public int $delay = 1;
|
||||||
public bool $external = false;
|
public bool $external = false;
|
||||||
|
|
@ -32,6 +33,10 @@ class Option
|
||||||
}
|
}
|
||||||
|
|
||||||
// Define variables
|
// Define variables
|
||||||
|
$this->absolute = boolval(
|
||||||
|
isset($options['absolute']) || isset($options['a']) || $this->absolute
|
||||||
|
);
|
||||||
|
|
||||||
$this->crawl = boolval(
|
$this->crawl = boolval(
|
||||||
isset($options['crawl']) || isset($options['c']) || $this->crawl
|
isset($options['crawl']) || isset($options['c']) || $this->crawl
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -174,4 +174,70 @@ class Filesystem
|
||||||
$data
|
$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
|
// Start application
|
||||||
$cli = new Cli(
|
$cli = new Cli(
|
||||||
getopt(
|
getopt(
|
||||||
'cd:ef:hi:kl:m:rs:t:u',
|
'acd:ef:hi:kl:m:rs:t:u',
|
||||||
[
|
[
|
||||||
|
'absolute:',
|
||||||
'crawl:',
|
'crawl:',
|
||||||
'delay:',
|
'delay:',
|
||||||
'external',
|
'external',
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue