Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CDN #81

Draft
wants to merge 8 commits into
base: develop
Choose a base branch
from
Draft

CDN #81

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
736 changes: 664 additions & 72 deletions Cargo.lock

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions cdn/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "cdn"
version = "3.5.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
reqwest = { version = "0.10" }
tokio = { version = "0.2", features = ["full"] }
zip = "0.6.3"
log = "0.4"
chrono = "0.4"
151 changes: 151 additions & 0 deletions cdn/src/cdn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
use chrono::prelude::*;
use reqwest;
use std::env::current_dir;
use std::fs;
use std::fs::create_dir_all;
use std::io;
use std::io::Write;
use std::path::Path;
use zip;
//use std::sync::Arc;
use chrono::prelude::*;
use std::thread;
use std::time;
use tokio::runtime::Runtime;

pub async fn print_async(text: &str) {
println!("{text}");
}

pub fn generate_name() -> String {
let current_date = Local::now();
format!("./download_{}.zip", current_date.format("%Y%m%d%H"))
}

pub fn run_download(path: String) {
let mut rt = Runtime::new().unwrap();
let hand = rt.spawn(async {
let cdn_syncer = CDNSyncer {
cdn: CDN {},
download_path: path,
};
cdn_syncer.async_download().await;
});

rt.block_on(hand).unwrap();
}

pub struct CDNSyncer {
pub cdn: CDN,
pub download_path: String,
}

impl CDNSyncer {
pub fn run(&self) {
self.cdn.unzip();
self.cdn
.chdir("./chain_data", "/home/jualns/.epic/main/chain_data");
}

pub async fn async_download(&self) {
print_async("++ run started").await;
self.cdn.download(&self.download_path).await;
print_async("++ download finished").await;
}
}

pub struct CDN;

impl CDN {
pub fn unzip(&self) {
println!("++ UNZIP STARTED!");
let fname = std::path::Path::new("./download.zip");
let file = fs::File::open(&fname).unwrap();

let mut archive = zip::ZipArchive::new(file).unwrap();

for i in 0..archive.len() {
let mut file = archive.by_index(i).unwrap();
let outpath = match file.enclosed_name() {
Some(path) => path.to_owned(),
None => continue,
};

if (*file.name()).ends_with('/') {
fs::create_dir_all(&outpath).unwrap();
} else {
if let Some(p) = outpath.parent() {
if !p.exists() {
fs::create_dir_all(p).unwrap();
}
}
let mut outfile = fs::File::create(&outpath).unwrap();
io::copy(&mut file, &mut outfile).unwrap();
}

// Get and Set permissions
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;

if let Some(mode) = file.unix_mode() {
fs::set_permissions(&outpath, fs::Permissions::from_mode(mode)).unwrap();
}
}
}
println!("++ UNZIP FINISHED!");
}

pub async fn download(&self, download_path: &str) {
print_async("++ DOWNLOAD STARTED!").await;
let target = "https://epiccash.s3.sa-east-1.amazonaws.com/mainnet.zip";
let response = reqwest::get(target);

let resp = match response.await {
Ok(r) => r,
_ => panic!("error"),
};

let path = Path::new(download_path);

if path.exists() {
println!("Path exist");
return;
}

let mut file = match fs::File::create(&path) {
Err(why) => panic!("couldn't create {}", why),
Ok(file) => file,
};
let content = resp.bytes();
let content = match content.await {
Ok(b) => b,
Err(_) => panic!("error"),
};
print_async("++ DOWNLOAD FINISHED!").await;
match file.write_all(&content) {
Ok(_) => return,
Err(_) => panic!("error"),
};
}

pub fn chdir(&self, from: &str, to: &str) {
println!("++ chdir started!");
let path = current_dir().unwrap(); //Path::new("/home/~/.epic/main/cache_cdn/");

if Path::new(to).exists() {
let remove = fs::remove_dir_all(to);
match remove {
Ok(_) => (),
Err(e) => panic!("Can't remove the original chain_data, error: {e}"),
}
}
println!("++ Current Dir: {path:?}");
match fs::rename(from, to) {
Ok(_) => {
return;
}
Err(e) => panic!("{}", e),
};
}
}
16 changes: 16 additions & 0 deletions cdn/src/download.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use reqwest;
use std::io::File;

async fn download() {
let target = "https://epiccash.s3.sa-east-1.amazonaws.com/mainnet.zip";
let response = reqwest::get(target).await?;

let path = Path::new("./download.zip");

let mut file = match File::create(&path) {
Err(why) => panic!("couldn't create {}", why),
Ok(file) => file,
};
let content = response.bytes().await?;
file.write_all(&content)?;
}
10 changes: 10 additions & 0 deletions cdn/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![deny(non_upper_case_globals)]
#![deny(non_camel_case_types)]
#![deny(non_snake_case)]
#![deny(unused_mut)]
#![warn(missing_docs)]

#[macro_use]
extern crate log;

pub mod cdn;
40 changes: 40 additions & 0 deletions cdn/src/unzip.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::fs;
use std::io;
use zip;

fn unzip() {
let fname = std::path::Path::new("./download.zip");
let file = fs::File::open(&fname).unwrap();

let mut archive = zip::ZipArchive::new(file).unwrap();

for i in 0..archive.len() {
let mut file = archive.by_index(i).unwrap();
let outpath = match file.enclosed_name() {
Some(path) => path.to_owned(),
None => continue,
};

if (*file.name()).ends_with('/') {
fs::create_dir_all(&outpath).unwrap();
} else {
if let Some(p) = outpath.parent() {
if !p.exists() {
fs::create_dir_all(p).unwrap();
}
}
let mut outfile = fs::File::create(&outpath).unwrap();
io::copy(&mut file, &mut outfile).unwrap();
}

// Get and Set permissions
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;

if let Some(mode) = file.unix_mode() {
fs::set_permissions(&outpath, fs::Permissions::from_mode(mode)).unwrap();
}
}
}
}
8 changes: 8 additions & 0 deletions config/src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ fn comments() -> HashMap<String, String> {
.to_string(),
);

retval.insert(
"syncing_method".to_string(),
"
#method to be used when syncing with empty chain data
"
.to_string(),
);

retval.insert(
"db_root".to_string(),
"
Expand Down
3 changes: 2 additions & 1 deletion config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ const EPIC_CHAIN_DIR: &'static str = "chain_data";
/// Node API secret
pub const API_SECRET_FILE_NAME: &'static str = ".api_secret";

fn get_epic_path(chain_type: &global::ChainTypes) -> Result<PathBuf, ConfigError> {
/// This function will return the `~/.epic/chain_type` directory, and if it doesn't exist -> create and return
pub fn get_epic_path(chain_type: &global::ChainTypes) -> Result<PathBuf, ConfigError> {
// Check if epic dir exists
let mut epic_path = match dirs::home_dir() {
Some(p) => p,
Expand Down
Loading