Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ tikv-jemallocator = "0.5.4"

[features]
default = ["launcher", "daemon"]
embed = ["launcher", "daemon"]
launcher = []
daemon = []

Expand Down
26 changes: 6 additions & 20 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ root=$(pwd)
target_list=(x86_64-unknown-linux-musl aarch64-unknown-linux-musl)
for target in ${target_list[@]}; do

if [[ $target == *"aarch64"* ]]; then
arch=aarch64 bash +x unpack.sh
else
bash +x unpack.sh
fi

# default feature
cargo zigbuild --release --target=$target
upx --lzma target/$target/release/xunlei
Expand All @@ -24,26 +30,6 @@ for target in ${target_list[@]}; do
mv ./* $root/uploads/
cd -

# embed feature
if [[ $target == *"aarch64"* ]]; then
arch=aarch64 bash +x unpack.sh
else
bash +x unpack.sh
fi
cargo zigbuild --release --target=$target --no-default-features --features embed
upx --lzma target/$target/release/xunlei
cargo deb --target=$target --no-build --no-strip
cd target/$target/release
tar czvf xunlei-embed-$tag-$target.tar.gz xunlei
shasum -a 256 xunlei-embed-$tag-$target.tar.gz >xunlei-embed-$tag-$target.tar.gz.sha256
mv xunlei-embed-$tag-$target.tar.gz $root/uploads/
mv xunlei-embed-$tag-$target.tar.gz.sha256 $root/uploads/
cd -
cd target/$target/debian
rename 's/.*/xunlei-embed-'$tag'-'$target'.deb/' *.deb
mv ./* $root/uploads/
cd -

# launcher feature
mkdir -p xunlei-launcher-$tag-$target/bin
mv bin/* xunlei-launcher-$tag-$target/bin/
Expand Down
1 change: 0 additions & 1 deletion src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use crate::env;

use crate::util;
use crate::xunlei_asset;
use crate::xunlei_asset::XunleiAsset;

use crate::Config;
use crate::Running;
Expand Down
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use tikv_jemallocator::Jemalloc;
#[global_allocator]
static GLOBAL: Jemalloc = Jemalloc;


#[cfg(feature = "daemon")]
pub mod daemon;
pub mod env;
Expand Down
100 changes: 28 additions & 72 deletions src/xunlei_asset.rs
Original file line number Diff line number Diff line change
@@ -1,62 +1,22 @@
use core::str;
use std::{borrow::Cow, fs::File, io::Read, path::Path};
use std::{
borrow::Cow,
fs::File,
io::{Read, Write},
path::{Path, PathBuf},
};

#[cfg(not(feature = "embed"))]
use std::{io::Write, ops::Not, path::PathBuf};

pub trait XunleiAsset {
fn version(&self) -> anyhow::Result<String>;

fn get(&self, filename: &str) -> anyhow::Result<Cow<[u8]>>;

fn iter(&self) -> anyhow::Result<Vec<String>>;
}

#[cfg(feature = "embed")]
#[derive(rust_embed::RustEmbed)]
#[folder = "bin/"]
struct Asset;

use anyhow::Context;
#[cfg(feature = "embed")]
use anyhow::Context;
use tar::Archive;


#[cfg(feature = "embed")]
struct XunleiEmbedAsset;

#[cfg(feature = "embed")]
impl XunleiAsset for XunleiEmbedAsset {
fn version(&self) -> anyhow::Result<String> {
let version_bin = Asset::get("version").context("Failed to get version asset")?;
let version = std::str::from_utf8(version_bin.data.as_ref())
.context("Error getting version number!")?;
Ok(String::from(version))
}

fn get(&self, filename: &str) -> anyhow::Result<Cow<[u8]>> {
let bin = Asset::get(filename).context("Failed to get bin asset")?;
Ok(bin.data)
}

fn iter(&self) -> anyhow::Result<Vec<String>> {
Ok(Asset::iter()
.map(|v| v.into_owned())
.collect::<Vec<String>>())
}
}

#[cfg(not(feature = "embed"))]
struct XunleiLocalAsset {
pub struct Asset {
tmp_path: PathBuf,
filename: String,
}

#[cfg(not(feature = "embed"))]
impl XunleiLocalAsset {
impl Asset {
fn new() -> anyhow::Result<Self> {
let xunlei = XunleiLocalAsset {
let xunlei = Asset {
tmp_path: PathBuf::from("/tmp/xunlei_bin"),
filename: format!("nasxunlei-DSM7-{}.spk", crate::env::SUPPORT_ARCH),
};
Expand All @@ -74,7 +34,7 @@ impl XunleiLocalAsset {
pb.set_style(indicatif::ProgressStyle::with_template("{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes} ({bytes_per_sec}, {eta})")?
.progress_chars("#>-"));

if self.tmp_path.exists().not() {
if !self.tmp_path.exists() {
crate::util::create_dir_all(&self.tmp_path, 0o755)?;
}

Expand Down Expand Up @@ -155,17 +115,18 @@ impl XunleiLocalAsset {
let path = format!("{}", file.path()?.display());

if path.contains("bin/bin/version") && !path.contains("version_code")
|| path.contains("bin/bin/xunlei-pan-cli-launcher")
|| path.contains("bin/bin/xunlei-pan-cli")
{
let filename = path.trim_start_matches("bin/bin/");
let filepath = PathBuf::from(dir.as_ref()).join(filename);
let mut target = File::create(filepath)?;
Self::copy_write(file, &mut target)?;
} else if path.contains("ui/index.cgi") {
let mut target = File::create(PathBuf::from(dir.as_ref()).join("xunlei-pan-cli-web"))?;
Self::copy_write(file, &mut target)?;
}
|| path.contains("bin/bin/xunlei-pan-cli-launcher")
|| path.contains("bin/bin/xunlei-pan-cli")
{
let filename = path.trim_start_matches("bin/bin/");
let filepath = PathBuf::from(dir.as_ref()).join(filename);
let mut target = File::create(filepath)?;
Self::copy_write(file, &mut target)?;
} else if path.contains("ui/index.cgi") {
let mut target =
File::create(PathBuf::from(dir.as_ref()).join("xunlei-pan-cli-web"))?;
Self::copy_write(file, &mut target)?;
}
}

std::fs::remove_file(tar_path)?;
Expand All @@ -174,20 +135,19 @@ impl XunleiLocalAsset {
}
}

#[cfg(not(feature = "embed"))]
impl XunleiAsset for XunleiLocalAsset {
fn version(&self) -> anyhow::Result<String> {
impl Asset {
pub fn version(&self) -> anyhow::Result<String> {
Ok(std::fs::read_to_string(
PathBuf::from(&self.tmp_path).join("version"),
)?)
}

fn get(&self, filename: &str) -> anyhow::Result<Cow<[u8]>> {
pub fn get(&self, filename: &str) -> anyhow::Result<Cow<[u8]>> {
let vec = std::fs::read(PathBuf::from(&self.tmp_path).join(filename))?;
Ok(std::borrow::Cow::from(vec))
}

fn iter(&self) -> anyhow::Result<Vec<String>> {
pub fn iter(&self) -> anyhow::Result<Vec<String>> {
let entries = std::fs::read_dir(&self.tmp_path)?;
let mut file_names = Vec::new();
for entry in entries {
Expand All @@ -202,10 +162,6 @@ impl XunleiAsset for XunleiLocalAsset {
}
}

pub fn asset() -> anyhow::Result<impl XunleiAsset> {
#[cfg(not(feature = "embed"))]
let asset = XunleiLocalAsset::new()?;
#[cfg(feature = "embed")]
let asset = XunleiEmbedAsset {};
Ok(asset)
pub fn asset() -> anyhow::Result<Asset> {
Asset::new()
}