Skip to content

Commit

Permalink
fastn-cli/code-cleanup: publish-static code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
AbrarNitk committed Mar 1, 2023
1 parent 5a365ee commit 522f510
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 270 deletions.
166 changes: 0 additions & 166 deletions fastn-cloud/src/create.rs

This file was deleted.

51 changes: 3 additions & 48 deletions fastn-cloud/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,9 @@
extern crate self as fastn_cloud;

use std::io;

mod create;
mod http;
mod upload;
mod utils;

#[derive(thiserror::Error, Debug)]
pub enum CreateError {
#[error("BuildDirNotFound: {}", _0)]
BuildDirNotFound(String),
#[error("TejarCreateError: {}", _0)]
TejarCreateError(#[from] tejar::error::CreateError),
#[error("StdIOError: {}", _0)]
StdIOError(#[from] io::Error),
#[error("FastnCoreError: {}", _0)]
FastnCoreError(#[from] fastn_core::Error),
#[error("HttpPOSTCreateError: {}", _0)]
HttpPOSTCreateError(#[from] fastn_cloud::http::PostError),
}

#[derive(thiserror::Error, Debug)]
pub enum UpdateError {}

#[derive(thiserror::Error, Debug)]
pub enum UploadError {
#[error("BuildDirNotFound: {}", _0)]
Expand All @@ -41,36 +21,11 @@ pub enum UploadError {
#[error("HTTPPostError: {}", _0)]
HTTPPostError(#[from] fastn_cloud::http::PostError),
#[error("StdIOError: {}", _0)]
StdIOError(#[from] io::Error),
StdIOError(#[from] std::io::Error),
#[error("SidParseError: {}", _0)]
SidParseError(#[from] serde_json::Error),
}

pub async fn create() -> Result<(), fastn_cloud::CreateError> {
let build_dir = fastn_cloud::utils::build_dir();
if !build_dir.exists() {
return Err(CreateError::BuildDirNotFound(
"Run `fastn build` to create a .build directory before running this".to_string(),
));
}
fastn_cloud::create::create_package(build_dir.as_path())
.await
.unwrap();
println!("{}", build_dir);
// call fastn build
// read the content of the .build folder
// pass this content to tejar and create two files LIST and DATA
// call /api/create/ by passing the content of the LIST and META
// call /api/upload-new-package by passing the missing entries and DATA
// save package key and at home folder
// show the subdomain to user or open browser directly
println!("publish-static create called");
Ok(())
}

pub async fn update() -> Result<(), UpdateError> {
println!("publish-static update called");
Ok(())
#[error("TejarReadError: {}", _0)]
TejarRead(#[from] tejar::error::ReadError),
}

pub async fn upload() -> Result<(), UploadError> {
Expand Down
67 changes: 63 additions & 4 deletions fastn-cloud/src/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub async fn upload(
cw_id: &str,
) -> Result<(), fastn_cloud::UploadError> {
let sid: Sid = serde_json::from_str(sid)?;
let (list_file, data_file) = fastn_cloud::create::tejar_create(root).await?;
let (list_file, data_file) = tejar_create(root).await?;
let list_content = tokio::fs::read_to_string(list_file.as_path()).await?;
// TODO: missing files handle sid
println!("Getting Missing Files");
Expand All @@ -54,13 +54,12 @@ pub async fn upload(
.await?;

println!("Filtering Missing Hashes hashes");
let (missing_hashes_list, missing_hashes_content) = fastn_cloud::create::get_missing_checksums(
let (missing_hashes_list, missing_hashes_content) = get_missing_checksums(
list_content.as_str(),
missing_files_api_resp.missing_hashes.as_slice(),
data_file.as_path(),
)
.await
.unwrap();
.await?;

println!("Uploading Missing List Content");
let resp = upload_api(
Expand Down Expand Up @@ -108,3 +107,63 @@ pub async fn upload_api(
fastn_cloud::http::put("/api/upload/", body, &headers, &query).await?;
Ok(response)
}

pub async fn get_missing_checksums(
list_content: &str,
missing_hashes: &[String],
data_file: &camino::Utf8Path,
) -> Result<(String, Vec<u8>), fastn_cloud::UploadError> {
let list = tejar::read::reader(list_content)?.list;
let mut data = vec![];
let mut new_list = String::new();
let mut offset = 0;
for checksum in missing_hashes {
let record = list.iter().find(|r| r.checksum.eq(checksum)).unwrap();
let mut file_data =
read_with_offset(data_file, record.offset as u64, record.file_size as usize)
.await
.unwrap();
let list_record = tejar::create::ListRecord {
data_file_name: record.data_file_name.to_string(),
file_name: record.file_name.to_string(),
content_type: record.content_type.to_string(),
compression: "todo!".to_string(), // TODO:
start: offset,
size: record.file_size,
timestamp: record.timestamp,
checksum: record.checksum.to_string(),
};
offset += record.file_size;
data.append(&mut file_data);
new_list.push_str(list_record.to_string().as_str());
}
Ok((new_list, data))
}

pub async fn tejar_create(
root: &camino::Utf8Path,
) -> Result<(camino::Utf8PathBuf, camino::Utf8PathBuf), tejar::error::CreateError> {
let files: _ = fastn_cloud::utils::walkdir_util(root)
.into_iter()
.map(|file| tejar::create::InputFile {
path: file.path.strip_prefix(root).unwrap().to_path_buf(),
content_type: file.content_type,
gzip: file.gzip,
})
.collect::<Vec<_>>();
tejar::create::create(root, files.as_slice())
}

pub async fn read_with_offset(
path: &camino::Utf8Path,
offset: u64,
size: usize,
) -> Result<Vec<u8>, tokio::io::Error> {
use tokio::io::AsyncReadExt;
use tokio::io::AsyncSeekExt;
let mut file = tokio::fs::File::open(path).await?;
file.seek(tokio::io::SeekFrom::Start(offset)).await?;
let mut buffer = vec![0u8; size];
file.read_exact(&mut buffer).await?;
Ok(buffer)
}
21 changes: 21 additions & 0 deletions fastn/src/commands/cloud.rs
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
pub(crate) const PUBLISH_STATIC: &str = "publish-static";

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("PublishStaticError: {}", _0)]
PublishStaticError(#[from] PublishStaticError),
}

#[derive(thiserror::Error, Debug)]
pub enum PublishStaticError {
#[error("PublishStaticUploadError: {}", _0)]
Upload(#[from] fastn_cloud::UploadError),
}

pub(crate) async fn handle() -> Result<bool, Error> {
Ok(handle_publish_static().await?)
}

pub(crate) async fn handle_publish_static() -> Result<bool, PublishStaticError> {
fastn_cloud::upload().await?;
Ok(true)
}
42 changes: 0 additions & 42 deletions fastn/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1 @@
pub(crate) mod cloud;
pub(crate) const PUBLISH_STATIC: &str = "publish-static";

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("PublishStaticError: {}", _0)]
PublishStaticError(#[from] PublishStaticError),
}

#[derive(thiserror::Error, Debug)]
pub enum PublishStaticError {
#[error("PublishStaticCreateError: {}", _0)]
Create(#[from] fastn_cloud::CreateError),
#[error("PublishStaticUpdateError: {}", _0)]
Update(#[from] fastn_cloud::UpdateError),
#[error("PublishStaticUploadError: {}", _0)]
Upload(#[from] fastn_cloud::UploadError),
}

pub(crate) async fn handle(matches: &clap::ArgMatches) -> Result<bool, Error> {
// TODO: Handle Dynamic Commands
Ok(handle_publish_static(matches).await?)
}

pub(crate) async fn handle_publish_static(
matches: &clap::ArgMatches,
) -> Result<bool, PublishStaticError> {
match matches.subcommand() {
Some(("create", _matches)) => {
fastn_cloud::create().await?;
Ok(true)
}
Some(("update", _matches)) => {
fastn_cloud::update().await?;
Ok(true)
}
Some(("upload", _matches)) => {
fastn_cloud::upload().await?;
Ok(true)
}
_ => Ok(false),
}
}
Loading

0 comments on commit 522f510

Please sign in to comment.