push posts to git-pages

This commit is contained in:
Songday 2022-03-02 22:00:21 +08:00
parent 34c642c555
commit 56e2e746fc
7 changed files with 59 additions and 22 deletions

View File

@ -5,7 +5,7 @@
[>>更多截图/More screenshots](manual/screenshots.md)
## 用户使用手册/User manual
[用户使用手册](manual/how-to-use-zh.md)
[用户使用手册](manual/how-to-use-zh.md)
[User manual](manual/how-to-use-en.md)
## 自带服务端的博客系统

View File

@ -22,9 +22,7 @@ pub async fn has_admin_password() -> Result<bool> {
}
pub async fn admin_login(token: &str, password: &str) -> Result<bool> {
let d = sqlx::query_as::<Sqlite, crate::db::model::Setting>("SELECT * FROM settings WHERE item='admin_password'")
.fetch_optional(&DATA_SOURCE.get().unwrap().sqlite)
.await?;
let d = get_setting("admin_password").await?;
if let Some(settings) = d {
if crypt::verify_password(password, &settings.content)? {
@ -35,7 +33,7 @@ pub async fn admin_login(token: &str, password: &str) -> Result<bool> {
return Ok(false);
}
pub async fn update_settings(settings: Setting) -> Result<()> {
pub async fn update_setting(settings: Setting) -> Result<()> {
// db::sled_save(&DATA_SOURCE.get().unwrap().management, "settings", &setting).await?;
let content = if settings.item.eq("admin_password") {
if settings.content.is_empty() {
@ -66,3 +64,11 @@ pub async fn update_settings(settings: Setting) -> Result<()> {
}
Ok(())
}
pub async fn get_setting(item: &str) -> Result<Option<Setting>> {
let r = sqlx::query_as::<Sqlite, crate::db::model::Setting>("SELECT * FROM settings WHERE item=?")
.bind(item)
.fetch_optional(super::get_sqlite())
.await?;
Ok(r)
}

View File

@ -1,5 +1,5 @@
use blog_common::{
dto::{user::UserInfo, Response as ApiResponse},
dto::{git::GitRepositoryInfo, user::UserInfo, Response as ApiResponse},
result::{Error, ErrorResponse},
val,
};
@ -8,6 +8,7 @@ use hyper::header::{self, HeaderMap, HeaderValue};
use warp::{filters::path::Tail, http::Response, Rejection, Reply};
use crate::{
db::management,
db::post,
facade::{session_id_cookie, wrap_json_data, wrap_json_err},
service::{export, status},
@ -22,6 +23,9 @@ pub async fn export_handler(tail: Tail, user: Option<UserInfo>) -> Result<Respon
if path.eq("hugo") {
return hugo().await;
}
if path.eq("git") {
return git().await;
}
if path.rfind(".zip").is_some() {
return Ok(get_file(path));
}
@ -60,3 +64,21 @@ async fn hugo() -> Result<Response<Body>, Rejection> {
.unwrap();
Ok(r)
}
async fn git() -> Result<Response<Body>, Rejection> {
let setting = management::get_setting("git-pages").await?;
let mut message = String::with_capacity(32);
if setting.is_none() {
message.push_str("Cannot find git repository setting");
} else {
let setting = setting.unwrap();
let r = serde_json::from_str::<GitRepositoryInfo>(&setting.content);
if let Ok(info) = r {
let r = export::git(&info).await;
} else {
message.push_str("Cannot deserialize git repository info");
}
}
let r = Response::builder().body(message.into()).unwrap();
Ok(r)
}

View File

@ -52,7 +52,7 @@ pub async fn update_settings(token: Option<String>, setting: Setting) -> Result<
if let Err(e) = status::check_auth(token) {
return facade::response(Err(e));
}
facade::response(management::update_settings(setting.into()).await)
facade::response(management::update_setting(setting.into()).await)
}
pub async fn forgot_password(authority: Option<warp::host::Authority>) -> Result<impl Reply, Rejection> {

View File

@ -2,6 +2,7 @@ use std::fs::{File, OpenOptions};
use std::io::Write;
use std::path::PathBuf;
use blog_common::dto::git::GitRepositoryInfo;
use lazy_static::lazy_static;
use tera::Tera;
use zip::write::FileOptions;
@ -72,7 +73,7 @@ pub async fn hugo() -> Result<String> {
let file = std::fs::File::create(output_file)?;
let mut zip = zip::ZipWriter::new(file);
let mut zip_file = |file_name: &String, post: &Post| -> Result<()> {
let zip_file = |file_name: &String, post: &Post| -> Result<()> {
zip.start_file(file_name, FileOptions::default())?;
let content = render(post, "hugo.md");
zip.write_all(content.as_bytes())?;
@ -102,18 +103,21 @@ pub async fn hugo() -> Result<String> {
Ok(filename)
}
pub async fn git(mut root_path: PathBuf, last_export_timestamp: i64) -> Result<()> {
let posts = post::all_by_since(last_export_timestamp).await?;
let mut write_file = |filename: &String, post: &Post| -> Result<()> {
root_path.set_file_name(filename);
let mut file = OpenOptions::new()
.write(true)
.truncate(true)
.open(root_path.as_path())?;
pub async fn git(git: &GitRepositoryInfo) -> Result<()> {
let mut path = std::env::current_dir().unwrap();
path.join(&git.repository_name);
let posts = post::all_by_since(git.last_export_second).await?;
let write_file = |filename: &String, post: &Post| -> Result<()> {
path.set_file_name(filename);
let mut file = OpenOptions::new().write(true).truncate(true).open(path.as_path())?;
let content = render(post, "hugo.md");
file.write_all(content.as_bytes())?;
Ok(())
};
write_posts(&posts, write_file);
let r = super::git::sync_to_remote(git);
Ok(())
}

View File

@ -7,16 +7,16 @@ use git2::{
StatusShow,
};
fn sync_to_remote(info: &GitRepositoryInfo) -> Result<(), ()> {
// export posts data to file system
// todo
pub fn sync_to_remote(info: &GitRepositoryInfo) -> Result<(), ()> {
// open git repository
let repo = match Repository::open(&info.path) {
let mut path = std::env::current_dir().unwrap();
path.join(&info.repository_name);
let repo = match Repository::open(path.as_path()) {
Ok(repo) => repo,
Err(e) => panic!("failed to open: {}", e),
};
// perform committing
// todo
let changed_files = get_changed_files(&repo);
// try pushing
// todo
Ok(())

View File

@ -1,5 +1,10 @@
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize)]
pub struct GitRepositoryInfo {
pub name: String,
pub email: String,
pub path: String,
pub remote_url: String,
pub repository_name: String,
pub last_export_second: i64,
}