mirror of
https://github.com/sissbruecker/linkding
synced 2025-02-16 12:28:23 +00:00
Add option for passing arguments to single-file command (#691)
* Promoting singlefile timeout to env variable * Promoting singlefile timeout to env variable * add tests * Add LD_SINGLEFILE_OPTIONS support * add tests --------- Co-authored-by: Sascha Ißbrücker <sascha.issbruecker@gmail.com>
This commit is contained in:
parent
3ffec72d3e
commit
2b342c0d56
3 changed files with 54 additions and 4 deletions
|
@ -1,6 +1,7 @@
|
||||||
import gzip
|
import gzip
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import shlex
|
||||||
import shutil
|
import shutil
|
||||||
import signal
|
import signal
|
||||||
import subprocess
|
import subprocess
|
||||||
|
@ -17,10 +18,11 @@ logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
def create_snapshot(url: str, filepath: str):
|
def create_snapshot(url: str, filepath: str):
|
||||||
singlefile_path = settings.LD_SINGLEFILE_PATH
|
singlefile_path = settings.LD_SINGLEFILE_PATH
|
||||||
# singlefile_options = settings.LD_SINGLEFILE_OPTIONS
|
# parse string to list of arguments
|
||||||
|
singlefile_options = shlex.split(settings.LD_SINGLEFILE_OPTIONS)
|
||||||
temp_filepath = filepath + ".tmp"
|
temp_filepath = filepath + ".tmp"
|
||||||
|
# concat lists
|
||||||
args = [singlefile_path, url, temp_filepath]
|
args = [singlefile_path] + singlefile_options + [url, temp_filepath]
|
||||||
try:
|
try:
|
||||||
# Use start_new_session=True to create a new process group
|
# Use start_new_session=True to create a new process group
|
||||||
process = subprocess.Popen(args, start_new_session=True)
|
process = subprocess.Popen(args, start_new_session=True)
|
||||||
|
|
|
@ -51,6 +51,44 @@ class SingleFileServiceTestCase(TestCase):
|
||||||
with self.assertRaises(singlefile.SingeFileError):
|
with self.assertRaises(singlefile.SingeFileError):
|
||||||
singlefile.create_snapshot("http://example.com", self.html_filepath)
|
singlefile.create_snapshot("http://example.com", self.html_filepath)
|
||||||
|
|
||||||
|
def test_create_snapshot_empty_options(self):
|
||||||
|
mock_process = mock.Mock()
|
||||||
|
mock_process.wait.return_value = 0
|
||||||
|
self.create_test_file()
|
||||||
|
|
||||||
|
with mock.patch("subprocess.Popen") as mock_popen:
|
||||||
|
singlefile.create_snapshot("http://example.com", self.html_filepath)
|
||||||
|
|
||||||
|
expected_args = [
|
||||||
|
"single-file",
|
||||||
|
"http://example.com",
|
||||||
|
self.html_filepath + ".tmp",
|
||||||
|
]
|
||||||
|
mock_popen.assert_called_with(expected_args, start_new_session=True)
|
||||||
|
|
||||||
|
@override_settings(
|
||||||
|
LD_SINGLEFILE_OPTIONS='--some-option "some value" --another-option "another value" --third-option="third value"'
|
||||||
|
)
|
||||||
|
def test_create_snapshot_custom_options(self):
|
||||||
|
mock_process = mock.Mock()
|
||||||
|
mock_process.wait.return_value = 0
|
||||||
|
self.create_test_file()
|
||||||
|
|
||||||
|
with mock.patch("subprocess.Popen") as mock_popen:
|
||||||
|
singlefile.create_snapshot("http://example.com", self.html_filepath)
|
||||||
|
|
||||||
|
expected_args = [
|
||||||
|
"single-file",
|
||||||
|
"--some-option",
|
||||||
|
"some value",
|
||||||
|
"--another-option",
|
||||||
|
"another value",
|
||||||
|
"--third-option=third value",
|
||||||
|
"http://example.com",
|
||||||
|
self.html_filepath + ".tmp",
|
||||||
|
]
|
||||||
|
mock_popen.assert_called_with(expected_args, start_new_session=True)
|
||||||
|
|
||||||
def test_create_snapshot_default_timeout_setting(self):
|
def test_create_snapshot_default_timeout_setting(self):
|
||||||
mock_process = mock.Mock()
|
mock_process = mock.Mock()
|
||||||
mock_process.wait.return_value = 0
|
mock_process.wait.return_value = 0
|
||||||
|
|
|
@ -252,4 +252,14 @@ Alternative favicon providers:
|
||||||
|
|
||||||
Values: `Float` | Default = 60.0
|
Values: `Float` | Default = 60.0
|
||||||
|
|
||||||
When creating archive snapshots, control the timeout for how long to wait for `single-file` to complete, in `seconds`. Defaults to 60 seconds; on lower-powered hardware you may need to increase this value.
|
When creating HTML archive snapshots, control the timeout for how long to wait for the snapshot to complete, in `seconds`.
|
||||||
|
Defaults to 60 seconds; on lower-powered hardware you may need to increase this value.
|
||||||
|
|
||||||
|
### `LD_SINGLEFILE_OPTIONS`
|
||||||
|
|
||||||
|
Values: `String` | Default = None
|
||||||
|
|
||||||
|
When creating HTML archive snapshots, pass additional options to the `single-file` application that is used to create snapshots.
|
||||||
|
See `single-file --help` for complete list of arguments, or browse source: https://github.com/gildas-lormeau/single-file-cli/blob/master/options.js
|
||||||
|
|
||||||
|
Example: `LD_SINGLEFILE_OPTIONS=--user-agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0"`
|
||||||
|
|
Loading…
Add table
Reference in a new issue