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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
use crate::myip;
use crate::webserver;
pub use iron::error::HttpError;
pub use iron::Listening;
use percent_encoding::{utf8_percent_encode, AsciiSet, NON_ALPHANUMERIC};
use std::fmt;
use std::path::{Path, PathBuf};
pub struct Qair {
port: u16,
is_receiving_enabled: bool,
file_to_send: Option<PathBuf>,
always_index: bool,
ip_autodetection_config: IpAutodetectionConfig,
}
pub enum IpAutodetectionConfig {
OnlyAutodetect,
#[allow(dead_code)]
AutoDetectWithFallback(String),
AlwaysCustom(String),
}
#[derive(Debug)]
pub enum FormatUrlError {
NotUtf8(PathBuf),
CannotBeAFile(PathBuf),
CannotAutodetectIp,
}
impl fmt::Display for FormatUrlError {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
let text = match self {
FormatUrlError::NotUtf8(_) => "Filename is not valid UTF-8",
FormatUrlError::CannotBeAFile(_) => "Filename cannot be a file",
FormatUrlError::CannotAutodetectIp => "IP address autodetection failed",
};
formatter.write_str(text)
}
}
const PERCENT_ENCODE_CHARS: &AsciiSet = &NON_ALPHANUMERIC
.remove(b'-')
.remove(b'.')
.remove(b'/')
.remove(b'_');
const DEFAULT_PORT: u16 = 1234;
impl Qair {
pub fn new() -> Self {
Qair {
port: DEFAULT_PORT,
is_receiving_enabled: false,
file_to_send: None,
always_index: false,
ip_autodetection_config: IpAutodetectionConfig::OnlyAutodetect,
}
}
pub fn start(&self) -> Result<iron::Listening, HttpError> {
webserver::start(self.port, self.is_receiving_enabled, &self.file_to_send)
}
pub fn url(&self) -> Result<String, FormatUrlError> {
let index = self.always_index || self.is_receiving_enabled;
let host = self.ip().ok_or(FormatUrlError::CannotAutodetectIp)?;
match &self.file_to_send {
Some(file) if !index => {
let basename_non_converted = Path::new(&file)
.file_name()
.ok_or(FormatUrlError::CannotBeAFile(file.clone()))?;
let basename = basename_non_converted
.to_str()
.ok_or(FormatUrlError::NotUtf8(file.clone()))?;
let basename_encoded =
utf8_percent_encode(basename, PERCENT_ENCODE_CHARS).to_string();
Ok(format!(
"http://{}:{}/{}",
host, self.port, basename_encoded
))
}
_ => Ok(format!("http://{}:{}/", host, self.port)),
}
}
pub fn set_port(&mut self, port: u16) {
self.port = port;
}
pub fn set_is_receiving_enabled(&mut self, is_receiving_enabled: bool) {
self.is_receiving_enabled = is_receiving_enabled;
}
pub fn set_file_to_send(&mut self, file_to_send: &Option<PathBuf>) {
self.file_to_send = file_to_send.clone();
}
pub fn set_ip_autodetection_config(&mut self, config: IpAutodetectionConfig) {
self.ip_autodetection_config = config;
}
pub fn set_always_index(&mut self, always_index: bool) {
self.always_index = always_index;
}
fn ip(&self) -> Option<String> {
match &self.ip_autodetection_config {
IpAutodetectionConfig::OnlyAutodetect => myip::autodetect_ip_address().ok(),
IpAutodetectionConfig::AutoDetectWithFallback(fallback) => {
Some(myip::autodetect_ip_address().unwrap_or(fallback.to_string()))
}
IpAutodetectionConfig::AlwaysCustom(custom) => Some(custom.to_string()),
}
}
}