-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #344 from KisaragiEffective/refactor/split-main
- Loading branch information
Showing
5 changed files
with
181 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
pub mod rest; | ||
pub(super) mod rest; | ||
pub mod persistence; | ||
#[cfg(feature = "unstable_activitypub")] | ||
pub mod activitypub; | ||
pub(super) mod cli; | ||
pub(super) mod import; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use std::path::PathBuf; | ||
use clap::{Parser, Subcommand}; | ||
use toy_blog_endpoint_model::ArticleId; | ||
|
||
#[derive(Parser)] | ||
pub struct Args { | ||
#[clap(subcommand)] | ||
pub subcommand: Commands | ||
} | ||
|
||
#[derive(Subcommand)] | ||
pub enum Commands { | ||
Run { | ||
#[clap(long)] | ||
http_port: u16, | ||
#[clap(long)] | ||
http_host: String, | ||
#[clap(long = "cloudflare")] | ||
cloudflare_support: bool, | ||
/// DEPRECATED, It will be removed in next major version. This switch is no-op. | ||
#[clap(long)] | ||
read_bearer_token_from_stdin: bool, | ||
}, | ||
Import { | ||
#[clap(long)] | ||
file_path: PathBuf, | ||
#[clap(long)] | ||
article_id: ArticleId, | ||
}, | ||
Version { | ||
#[clap(long)] | ||
plain: bool, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use std::fs::File; | ||
use std::io::{BufReader, Read}; | ||
use std::path::Path; | ||
use anyhow::bail; | ||
use log::{debug, info}; | ||
use toy_blog_endpoint_model::{ArticleId, Visibility}; | ||
use crate::service::rest::repository::GLOBAL_FILE; | ||
|
||
pub async fn import(file_path: &Path, article_id: &ArticleId) -> Result<(), anyhow::Error> { | ||
if !file_path.exists() { | ||
bail!("You can not import non-existent file") | ||
} | ||
|
||
if !file_path.is_file() { | ||
// TODO: /dev/stdin is not supported by this method | ||
debug!("is_dir: {}", file_path.is_dir()); | ||
debug!("is_symlink: {}", file_path.is_symlink()); | ||
debug!("metadata: {:?}", file_path.metadata()?); | ||
bail!("Non-file paths are not supported") | ||
} | ||
|
||
let content = { | ||
let mut fd = BufReader::new(File::open(file_path)?); | ||
let mut buf = vec![]; | ||
fd.read_to_end(&mut buf)?; | ||
String::from_utf8(buf) | ||
}; | ||
|
||
match content { | ||
Ok(content) => { | ||
GLOBAL_FILE.get().expect("must be fully-initialized").create_entry(article_id, content, Visibility::Private).await?; | ||
info!("Successfully imported as {article_id}."); | ||
Ok(()) | ||
} | ||
Err(err) => { | ||
bail!("The file is not UTF-8: {err}\ | ||
Please review following list:\ | ||
- The file is not binary\ | ||
- The text is encoded with UTF-8\ | ||
Especially, importing Shift-JIS texts are NOT supported.") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters