|
| 1 | +use colored::Colorize; |
| 2 | +use spinners::Spinner; |
| 3 | +use std::io::prelude::*; |
| 4 | +use std::io::{Seek, Write}; |
| 5 | +use std::iter::Iterator; |
| 6 | +use zip::result::ZipError; |
| 7 | +use zip::write::FileOptions; |
| 8 | + |
| 9 | +use std::fs::File; |
| 10 | +use std::path::{Path, PathBuf}; |
| 11 | +use walkdir::{DirEntry, WalkDir}; |
| 12 | +fn get_zip_file_path() -> PathBuf { |
| 13 | + let mut dst_file = std::env::temp_dir(); |
| 14 | + dst_file.push("discloud.zip"); |
| 15 | + dst_file |
| 16 | +} |
| 17 | +pub fn commit() { |
| 18 | + let token = super::expect_token(); |
| 19 | + let app_id = match super::ask_for_app(token.clone()) { |
| 20 | + Ok(app_id) => app_id, |
| 21 | + Err(error) => { |
| 22 | + super::err(&format!("Couldn't fetch apps: {}", error)); |
| 23 | + std::process::exit(1); |
| 24 | + } |
| 25 | + }; |
| 26 | + |
| 27 | + let src_dir = "."; |
| 28 | + let dst_file = get_zip_file_path(); |
| 29 | + match zip_dir_to_file(src_dir, dst_file.to_str().unwrap(), METHOD_DEFLATED) { |
| 30 | + Ok(_) => {} |
| 31 | + Err(e) => super::err(&format!("Failed to zip: {:?}", e)), |
| 32 | + } |
| 33 | + let mut spinner = Spinner::new(spinners::Spinners::Earth, "Committing app...".to_string()); |
| 34 | + let msg = match upload_zip(token, app_id) { |
| 35 | + Ok(()) => super::format_log("Your app was successfully commited!"), |
| 36 | + Err(err) => super::format_err(&err), |
| 37 | + }; |
| 38 | + spinner.stop_with_message(msg); |
| 39 | +} |
| 40 | + |
| 41 | +const METHOD_DEFLATED: zip::CompressionMethod = zip::CompressionMethod::Deflated; |
| 42 | + |
| 43 | +fn zip_dir<T>( |
| 44 | + it: &mut dyn Iterator<Item = DirEntry>, |
| 45 | + prefix: &str, |
| 46 | + writer: T, |
| 47 | + method: zip::CompressionMethod, |
| 48 | +) -> zip::result::ZipResult<()> |
| 49 | +where |
| 50 | + T: Write + Seek, |
| 51 | +{ |
| 52 | + let mut zip = zip::ZipWriter::new(writer); |
| 53 | + let options = FileOptions::default() |
| 54 | + .compression_method(method) |
| 55 | + .unix_permissions(0o755); |
| 56 | + |
| 57 | + let mut buffer = Vec::new(); |
| 58 | + for entry in it { |
| 59 | + let path = entry.path(); |
| 60 | + let name = path.strip_prefix(Path::new(prefix)).unwrap(); |
| 61 | + if path.is_file() { |
| 62 | + print!("⌛ Zipping file: {}\r", name.to_str().unwrap()); |
| 63 | + zip.start_file(name.to_str().unwrap(), options)?; |
| 64 | + let mut f = File::open(path)?; |
| 65 | + |
| 66 | + f.read_to_end(&mut buffer)?; |
| 67 | + zip.write_all(&*buffer)?; |
| 68 | + buffer.clear(); |
| 69 | + println!("{}", "✔".green().bold()); |
| 70 | + } else if name.as_os_str().len() != 0 { |
| 71 | + zip.add_directory(name.to_str().unwrap(), options)?; |
| 72 | + } |
| 73 | + } |
| 74 | + zip.finish()?; |
| 75 | + Result::Ok(()) |
| 76 | +} |
| 77 | + |
| 78 | +fn zip_dir_to_file( |
| 79 | + src_dir: &str, |
| 80 | + dst_file: &str, |
| 81 | + method: zip::CompressionMethod, |
| 82 | +) -> zip::result::ZipResult<()> { |
| 83 | + if !Path::new(src_dir).is_dir() { |
| 84 | + return Err(ZipError::FileNotFound); |
| 85 | + } |
| 86 | + let writer = File::create(dst_file).unwrap(); |
| 87 | + |
| 88 | + let walkdir = WalkDir::new(src_dir.to_string()); |
| 89 | + let it = walkdir.into_iter(); |
| 90 | + |
| 91 | + zip_dir( |
| 92 | + &mut it.filter_map(|e| { |
| 93 | + if let Ok(e) = e { |
| 94 | + let components = e.path().components().collect::<Vec<_>>(); |
| 95 | + if components.len() < 2 { |
| 96 | + Some(e) |
| 97 | + } else { |
| 98 | + match components[1].as_os_str().to_str().unwrap() { |
| 99 | + "target" | ".git" | "build" | "out" | "node_modules" | ".gitignore" => None, |
| 100 | + _ => Some(e), |
| 101 | + } |
| 102 | + } |
| 103 | + } else { |
| 104 | + None |
| 105 | + } |
| 106 | + }), |
| 107 | + src_dir, |
| 108 | + writer, |
| 109 | + method, |
| 110 | + )?; |
| 111 | + |
| 112 | + Ok(()) |
| 113 | +} |
| 114 | +fn upload_zip(token: String, app_id: u128) -> Result<(), String> { |
| 115 | + let file_path = get_zip_file_path(); |
| 116 | + let file_path = file_path.to_str().unwrap(); |
| 117 | + let client = reqwest::blocking::Client::new(); |
| 118 | + let form = reqwest::blocking::multipart::Form::new().file("file", file_path); |
| 119 | + match form { |
| 120 | + Err(err) => Err(format!("Couldn't open zip file: {}", err)), |
| 121 | + Ok(form) => { |
| 122 | + let req = client |
| 123 | + .put(crate::api_url!(format!("/app/{}/commit", app_id))) |
| 124 | + .multipart(form) |
| 125 | + .header("api-token", token); |
| 126 | + let res = req.send(); |
| 127 | + match res { |
| 128 | + Err(err) => Err(err.to_string()), |
| 129 | + Ok(res) => { |
| 130 | + if res.status().is_success() { |
| 131 | + Ok(()) |
| 132 | + } else { |
| 133 | + Err(format!( |
| 134 | + "Discloud API returned {} http code: {}", |
| 135 | + res.status().as_u16(), |
| 136 | + res.text().unwrap() |
| 137 | + )) |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments