-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.ab
More file actions
64 lines (55 loc) · 1.6 KB
/
http.ab
File metadata and controls
64 lines (55 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Amber-HTTP2 © Karim Vergnes <me@thesola.io>
// Licensed under GNU GPLv2
// Improved HTTP downloader with range support
import { is_command } from "std/env"
/// Downloads a file (or portion of a file) from a given URL to standard output
///
/// ### Arguments
/// - `url`: The URL to download from
/// - `start`: The start offset within the file
/// - `length`: The length of data to download
pub fun file_stream(url: Text, start: Int = 0, length: Int = -1): Text?
{
let end = ""
let wget_post = "cat"
if length >= 0 {
end = "{length}"
wget_post = "head -c {length}"
}
if {
is_command("curl"):
return $ curl -L -r {start}-{end} "{url}" $?
is_command("wget"):
return $ wget "{url}" --start-pos {start} -O - | {wget_post} $?
else:
fail 1
}
}
/// Drop-in replacement for Amber's `file_download()` stdlib builtin.
///
/// It downloads a file (or portion of a file) from a given URL to a specified
/// path.
///
/// ### Arguments
/// - `url`: The URL to download from
/// - `path`: The local path to write to
/// - `start`: The start offset within the file
/// - `length`: The length of data to download
pub fun file_download(url: Text, path: Text, start: Int = 0, length: Int = -1): Null?
{
let end = ""
let wget_post = "cat"
if length >= 0 {
end = "{length}"
wget_post = "head -c {length}"
}
if {
is_command("curl"):
$ curl -L -o "{path}" -r {start}-{end} "{url}" $?
is_command("wget"):
$ wget "{url}" --start-pos {start} -O - | {wget_post} > {path} $?
else:
fail 1
}
return
}