leaf-markdown-viewer 1.26.2

Terminal Markdown previewer with a GUI-like experience
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
use anyhow::{bail, Context, Result};
use reqwest::blocking::Client;
use semver::Version;
use serde::Deserialize;
use sha2::{Digest, Sha256};
use std::{
    fs,
    path::{Path, PathBuf},
    time::Duration,
};

const REPOS: &[&str] = &["RivoLink/leaf", "leaf-mg/leaf"];
const CHECKSUMS_ASSET_NAME: &str = "checksums.txt";
const HTTP_TIMEOUT: Duration = Duration::from_secs(20);

#[cfg(windows)]
const INSTALLER_NAME: &str = "install.ps1";

#[cfg(not(windows))]
const INSTALLER_NAME: &str = "install.sh";

#[derive(Debug, Deserialize)]
struct GithubRelease {
    tag_name: String,
    assets: Vec<GithubAsset>,
}

#[derive(Debug, Deserialize)]
struct GithubAsset {
    name: String,
    browser_download_url: String,
}

pub(crate) fn run_update() -> Result<()> {
    println!("Updating leaf...");

    let current_version = env!("CARGO_PKG_VERSION");
    let asset_name = current_asset_name()?;
    let release = fetch_latest_release(asset_name)?;
    let latest_version = normalize_version_tag(&release.tag_name);

    if !is_newer_version(current_version, latest_version)? {
        println!("leaf {current_version} is already up to date");
        return Ok(());
    }

    let download_url = expected_asset_download_url(&release.tag_name, &release.assets, asset_name)?;
    let checksums_url =
        expected_asset_download_url(&release.tag_name, &release.assets, CHECKSUMS_ASSET_NAME)?;
    let checksums = download_text_asset(checksums_url)?;
    let expected_checksum = find_expected_checksum(&checksums, asset_name)?;
    let current_exe = match std::env::var("LEAF_CURRENT_EXE") {
        Ok(path) => PathBuf::from(path),
        Err(_) => std::env::current_exe().context("Cannot locate current executable")?,
    };
    let temp_path = temp_download_path(&current_exe);

    download_asset(download_url, &temp_path)?;
    verify_download_checksum(&temp_path, expected_checksum)?;

    match replace_binary(&current_exe, &temp_path) {
        Ok(()) => {
            println!("leaf updated from {current_version} to {latest_version}");
            Ok(())
        }
        Err(err) => {
            cleanup_file_if_exists(&temp_path);
            Err(err)
        }
    }
}

fn current_asset_name() -> Result<&'static str> {
    asset_name_for_target(std::env::consts::OS, std::env::consts::ARCH).ok_or_else(|| {
        anyhow::anyhow!(
            "Unsupported platform: {} {}",
            std::env::consts::OS,
            std::env::consts::ARCH
        )
    })
}

pub(crate) fn asset_name_for_target(os: &str, arch: &str) -> Option<&'static str> {
    match (os, arch) {
        ("macos", "x86_64") => Some("leaf-macos-x86_64"),
        ("macos", "aarch64") => Some("leaf-macos-arm64"),
        ("linux", "x86_64") => Some("leaf-linux-x86_64"),
        ("linux", "aarch64") => Some("leaf-linux-arm64"),
        ("android", "aarch64") => Some("leaf-android-arm64"),
        ("windows", "x86_64") => Some("leaf-windows-x86_64.exe"),
        _ => None,
    }
}

fn fetch_latest_release(asset_name: &str) -> Result<GithubRelease> {
    let mut last_err: Option<anyhow::Error> = None;

    for repo in REPOS {
        match fetch_release_from_api(repo) {
            Ok(release) => return Ok(release),
            Err(err) => last_err = Some(err),
        }
    }
    for repo in REPOS {
        match fetch_release_from_redirect(repo, asset_name) {
            Ok(release) => return Ok(release),
            Err(err) => last_err = Some(err),
        }
    }

    let last = last_err.unwrap_or_else(|| anyhow::anyhow!("Unable to fetch latest leaf release"));
    Err(last.context(format!(
        "All GitHub sources failed. Try reinstalling via {INSTALLER_NAME}."
    )))
}

fn fetch_release_from_api(repo: &str) -> Result<GithubRelease> {
    let client = http_client()?;

    let response = client
        .get(format!(
            "https://api.github.com/repos/{repo}/releases/latest"
        ))
        .header(reqwest::header::USER_AGENT, "leaf-updater")
        .send()
        .context("Cannot reach GitHub releases API")?;

    let status = response.status();
    if status == reqwest::StatusCode::FORBIDDEN {
        bail!("GitHub API request was forbidden or rate-limited");
    }
    if status == reqwest::StatusCode::NOT_FOUND {
        bail!("Latest GitHub release was not found");
    }
    if !status.is_success() {
        bail!("GitHub releases API returned HTTP {status}");
    }

    response
        .json::<GithubRelease>()
        .context("Cannot parse GitHub release metadata")
}

fn fetch_release_from_redirect(repo: &str, asset_name: &str) -> Result<GithubRelease> {
    let client = http_client_no_redirect()?;

    let response = client
        .head(format!("https://github.com/{repo}/releases/latest"))
        .header(reqwest::header::USER_AGENT, "leaf-updater")
        .send()
        .context("Cannot reach GitHub releases page")?;

    let status = response.status();
    if !status.is_redirection() {
        bail!("GitHub releases page returned unexpected HTTP {status}");
    }

    let location = response
        .headers()
        .get(reqwest::header::LOCATION)
        .and_then(|v| v.to_str().ok())
        .ok_or_else(|| anyhow::anyhow!("GitHub releases redirect missing Location header"))?;

    let tag_name = extract_tag_from_release_url(location)?.to_string();
    let assets = build_synthetic_assets(repo, &tag_name, asset_name);

    Ok(GithubRelease { tag_name, assets })
}

pub(crate) fn extract_tag_from_release_url(url: &str) -> Result<&str> {
    let marker = "/releases/tag/";
    let idx = url
        .rfind(marker)
        .ok_or_else(|| anyhow::anyhow!("Release URL missing /releases/tag/ segment: {url}"))?;
    let tail = &url[idx + marker.len()..];
    let tag = tail.split(['/', '?', '#']).next().unwrap_or("");
    if tag.is_empty() {
        bail!("Release URL contains an empty tag: {url}");
    }
    Ok(tag)
}

pub(crate) fn build_download_url(repo: &str, tag: &str, asset_name: &str) -> String {
    format!("https://github.com/{repo}/releases/download/{tag}/{asset_name}")
}

fn build_synthetic_assets(repo: &str, tag: &str, asset_name: &str) -> Vec<GithubAsset> {
    [asset_name, CHECKSUMS_ASSET_NAME]
        .into_iter()
        .map(|name| GithubAsset {
            name: name.to_string(),
            browser_download_url: build_download_url(repo, tag, name),
        })
        .collect()
}

pub(crate) fn expected_asset_download_url<'a>(
    tag_name: &str,
    assets: &'a [impl AsRefAsset],
    expected_asset: &str,
) -> Result<&'a str> {
    let _ = normalize_version_tag(tag_name);
    assets
        .iter()
        .find(|asset| asset.name() == expected_asset)
        .map(|asset| asset.download_url())
        .ok_or_else(|| anyhow::anyhow!("Release does not contain asset {expected_asset}"))
}

pub(crate) trait AsRefAsset {
    fn name(&self) -> &str;
    fn download_url(&self) -> &str;
}

impl AsRefAsset for GithubAsset {
    fn name(&self) -> &str {
        &self.name
    }

    fn download_url(&self) -> &str {
        &self.browser_download_url
    }
}

pub(crate) fn is_newer_version(current: &str, remote: &str) -> Result<bool> {
    let current = Version::parse(normalize_version_tag(current))
        .with_context(|| format!("Invalid current version: {current}"))?;
    let remote = Version::parse(normalize_version_tag(remote))
        .with_context(|| format!("Invalid remote version: {remote}"))?;
    Ok(remote > current)
}

fn normalize_version_tag(version: &str) -> &str {
    version.strip_prefix('v').unwrap_or(version)
}

fn download_asset(url: &str, destination: &Path) -> Result<()> {
    cleanup_file_if_exists(destination);
    let _cleanup = TempFileGuard::new(destination.to_path_buf());
    let client = http_client()?;
    let mut response = client
        .get(url)
        .header(reqwest::header::USER_AGENT, "leaf-updater")
        .send()
        .with_context(|| format!("Cannot download release asset: {url}"))?;

    let expected_len = validate_download_response(response.status(), response.content_length())?;
    let mut file = fs::File::create(destination)
        .with_context(|| format!("Cannot create temporary file: {}", destination.display()))?;
    let copied = response
        .copy_to(&mut file)
        .with_context(|| format!("Cannot write downloaded asset: {}", destination.display()))?;
    validate_download_size(expected_len, copied)?;
    file.sync_all()
        .with_context(|| format!("Cannot flush temporary file: {}", destination.display()))?;
    _cleanup.disarm();
    Ok(())
}

fn download_text_asset(url: &str) -> Result<String> {
    let client = http_client()?;
    let response = client
        .get(url)
        .header(reqwest::header::USER_AGENT, "leaf-updater")
        .send()
        .with_context(|| format!("Cannot download release metadata asset: {url}"))?;

    validate_download_response(response.status(), response.content_length())?;
    response
        .text()
        .with_context(|| format!("Cannot read release metadata asset: {url}"))
}

fn http_client() -> Result<Client> {
    let client = Client::builder()
        .timeout(HTTP_TIMEOUT)
        .build()
        .context("Cannot initialize HTTP client")?;
    Ok(client)
}

fn http_client_no_redirect() -> Result<Client> {
    let client = Client::builder()
        .timeout(HTTP_TIMEOUT)
        .redirect(reqwest::redirect::Policy::none())
        .build()
        .context("Cannot initialize HTTP client")?;
    Ok(client)
}

fn validate_download_response(
    status: reqwest::StatusCode,
    content_length: Option<u64>,
) -> Result<Option<u64>> {
    if status == reqwest::StatusCode::FORBIDDEN {
        bail!("Release asset download was forbidden or rate-limited");
    }
    if status == reqwest::StatusCode::NOT_FOUND {
        bail!("Release asset was not found");
    }
    if !status.is_success() {
        bail!("Release asset download returned HTTP {status}");
    }
    if matches!(content_length, Some(0)) {
        bail!("Release asset download returned an empty body");
    }
    Ok(content_length)
}

pub(crate) fn validate_download_size(expected: Option<u64>, actual: u64) -> Result<()> {
    if actual == 0 {
        bail!("Downloaded release asset is empty");
    }
    if let Some(expected) = expected {
        if expected != actual {
            bail!(
                "Downloaded release asset size mismatch: expected {expected} bytes, got {actual}"
            );
        }
    }
    Ok(())
}

pub(crate) fn find_expected_checksum<'a>(checksums: &'a str, asset_name: &str) -> Result<&'a str> {
    for line in checksums.lines() {
        let trimmed = line.trim();
        if trimmed.is_empty() {
            continue;
        }
        let mut parts = trimmed.split_whitespace();
        let Some(checksum) = parts.next() else {
            continue;
        };
        let Some(filename) = parts.next() else {
            continue;
        };
        let normalized_filename = filename.trim_start_matches('*');
        if normalized_filename == asset_name {
            validate_sha256_hex(checksum)?;
            return Ok(checksum);
        }
    }

    bail!("checksums.txt does not contain {asset_name}")
}

pub(crate) fn validate_sha256_hex(value: &str) -> Result<()> {
    if value.len() != 64 || !value.bytes().all(|byte| byte.is_ascii_hexdigit()) {
        bail!("Invalid SHA256 checksum format");
    }
    Ok(())
}

fn verify_download_checksum(path: &Path, expected_checksum: &str) -> Result<()> {
    let bytes = fs::read(path).with_context(|| {
        format!(
            "Cannot read downloaded asset for checksum: {}",
            path.display()
        )
    })?;
    let actual_checksum = format!("{:x}", Sha256::digest(&bytes));

    if actual_checksum != expected_checksum {
        bail!(
            "Downloaded release asset checksum mismatch: expected {expected_checksum}, got {actual_checksum}"
        );
    }
    Ok(())
}

fn temp_download_path(current_exe: &Path) -> PathBuf {
    let extension = current_exe
        .extension()
        .map(|ext| format!("{}.download", ext.to_string_lossy()))
        .unwrap_or_else(|| "download".to_string());
    current_exe.with_extension(extension)
}

#[cfg(unix)]
fn replace_binary(current_exe: &Path, downloaded_path: &Path) -> Result<()> {
    let permissions = fs::metadata(current_exe)
        .with_context(|| {
            format!(
                "Cannot read current binary metadata: {}",
                current_exe.display()
            )
        })?
        .permissions();
    fs::set_permissions(downloaded_path, permissions).with_context(|| {
        format!(
            "Cannot apply executable permissions to {}",
            downloaded_path.display()
        )
    })?;
    fs::rename(downloaded_path, current_exe)
        .with_context(|| format!("Cannot replace current binary at {}", current_exe.display()))?;
    Ok(())
}

#[cfg(windows)]
fn replace_binary(current_exe: &Path, downloaded_path: &Path) -> Result<()> {
    let backup_path = current_exe.with_extension("old");
    cleanup_file_if_exists(&backup_path);

    fs::rename(current_exe, &backup_path).with_context(|| {
        format!(
            "Cannot replace the running Windows binary at {}. Try the PowerShell installer instead.",
            current_exe.display()
        )
    })?;

    if let Err(err) = fs::rename(downloaded_path, current_exe) {
        let _ = fs::rename(&backup_path, current_exe);
        bail!(
            "Cannot install the updated Windows binary at {}: {err}. Try the PowerShell installer instead.",
            current_exe.display()
        );
    }

    cleanup_file_if_exists(&backup_path);
    Ok(())
}

fn cleanup_file_if_exists(path: &Path) {
    if path.exists() {
        let _ = fs::remove_file(path);
    }
}

struct TempFileGuard {
    path: PathBuf,
    armed: bool,
}

impl TempFileGuard {
    fn new(path: PathBuf) -> Self {
        Self { path, armed: true }
    }

    fn disarm(mut self) {
        self.armed = false;
    }
}

impl Drop for TempFileGuard {
    fn drop(&mut self) {
        if self.armed {
            cleanup_file_if_exists(&self.path);
        }
    }
}

#[cfg(test)]
pub(crate) use test_support::TestAsset;

#[cfg(test)]
mod test_support {
    use super::AsRefAsset;

    pub(crate) struct TestAsset<'a> {
        pub(crate) name: &'a str,
        pub(crate) download_url: &'a str,
    }

    impl AsRefAsset for TestAsset<'_> {
        fn name(&self) -> &str {
            self.name
        }

        fn download_url(&self) -> &str {
            self.download_url
        }
    }
}