|
1 | 1 | use std::path::{Path, PathBuf}; |
2 | 2 |
|
| 3 | +use zsync_rs::ZsyncAssembly; |
| 4 | + |
3 | 5 | use crate::appimage::AppImage; |
4 | 6 | use crate::error::{Error, Result}; |
5 | 7 | use crate::update_info::UpdateInfo; |
6 | 8 |
|
7 | 9 | pub struct Updater { |
8 | | - #[allow(dead_code)] |
9 | 10 | appimage: AppImage, |
10 | | - #[allow(dead_code)] |
11 | 11 | update_info: UpdateInfo, |
12 | 12 | output_dir: PathBuf, |
13 | 13 | overwrite: bool, |
@@ -61,10 +61,63 @@ impl Updater { |
61 | 61 | } |
62 | 62 |
|
63 | 63 | pub fn check_for_update(&self) -> Result<bool> { |
64 | | - todo!("Implement update check") |
| 64 | + let zsync_url = self.update_info.zsync_url()?; |
| 65 | + let http = zsync_rs::HttpClient::new(); |
| 66 | + let _control = http |
| 67 | + .fetch_control_file(&zsync_url) |
| 68 | + .map_err(|e| Error::Zsync(format!("Failed to fetch control file: {}", e)))?; |
| 69 | + |
| 70 | + let output_path = self.output_path(); |
| 71 | + if output_path.exists() && !self.overwrite { |
| 72 | + return Err(Error::AppImage(format!( |
| 73 | + "Output file already exists: {}", |
| 74 | + output_path.display() |
| 75 | + ))); |
| 76 | + } |
| 77 | + |
| 78 | + Ok(true) |
65 | 79 | } |
66 | 80 |
|
67 | 81 | pub fn perform_update(&self) -> Result<PathBuf> { |
68 | | - todo!("Implement update") |
| 82 | + let zsync_url = self.update_info.zsync_url()?; |
| 83 | + let output_path = self.output_path(); |
| 84 | + |
| 85 | + if output_path.exists() && !self.overwrite { |
| 86 | + return Err(Error::AppImage(format!( |
| 87 | + "Output file already exists: {}", |
| 88 | + output_path.display() |
| 89 | + ))); |
| 90 | + } |
| 91 | + |
| 92 | + let assembly = ZsyncAssembly::from_url(&zsync_url, &output_path) |
| 93 | + .map_err(|e| Error::AppImage(format!("Failed to initialize zsync: {}", e)))?; |
| 94 | + |
| 95 | + let mut assembly = assembly; |
| 96 | + |
| 97 | + assembly |
| 98 | + .submit_source_file(self.appimage.path()) |
| 99 | + .map_err(|e| Error::AppImage(format!("Failed to submit source file: {}", e)))?; |
| 100 | + |
| 101 | + assembly |
| 102 | + .submit_self_referential() |
| 103 | + .map_err(|e| Error::AppImage(format!("Self-referential scan failed: {}", e)))?; |
| 104 | + |
| 105 | + assembly |
| 106 | + .download_missing_blocks() |
| 107 | + .map_err(|e| Error::AppImage(format!("Failed to download blocks: {}", e)))?; |
| 108 | + |
| 109 | + assembly |
| 110 | + .complete() |
| 111 | + .map_err(|e| Error::AppImage(format!("Failed to complete assembly: {}", e)))?; |
| 112 | + |
| 113 | + Ok(output_path) |
| 114 | + } |
| 115 | + |
| 116 | + pub fn progress(&self) -> Option<(u64, u64)> { |
| 117 | + None |
| 118 | + } |
| 119 | + |
| 120 | + fn output_path(&self) -> PathBuf { |
| 121 | + self.appimage.path().to_path_buf() |
69 | 122 | } |
70 | 123 | } |
0 commit comments