ye
This commit is contained in:
parent
ea734d7809
commit
87b9958d28
|
@ -18,8 +18,8 @@ RUN apt-get update && apt-get install libssl-dev -y
|
||||||
|
|
||||||
RUN mkdir /store
|
RUN mkdir /store
|
||||||
|
|
||||||
RUN touch /store/store.xmlfrag
|
RUN touch /store/store.xmlfrag && touch /store/tmpfile.xmlfrag
|
||||||
|
|
||||||
COPY ./templates/ /app/templates
|
COPY ./templates/ /app/templates
|
||||||
|
|
||||||
CMD [ "/app/linky", "/store/store.xmlfrag" ]
|
CMD [ "/app/linky", "/store/store.xmlfrag", "/store/tmpfile.xmlfrag" ]
|
||||||
|
|
|
@ -27,12 +27,12 @@ static SECRET_KEY: LazyLock<Key> = LazyLock::new(Key::generate);
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let args: Vec<String> = env::args().collect();
|
let args: Vec<String> = env::args().collect();
|
||||||
if args.len() < 2 {
|
if args.len() < 3 {
|
||||||
println!("Need file argument.");
|
println!("Need file argument.");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
let store = Arc::new(Mutex::new(LinkFile::new(&args[1])));
|
let store = Arc::new(Mutex::new(LinkFile::new(&args[1], &args[2])));
|
||||||
|
|
||||||
let authenticated = Router::new()
|
let authenticated = Router::new()
|
||||||
.route("/rss.xml", get(get_rss))
|
.route("/rss.xml", get(get_rss))
|
||||||
|
|
|
@ -2,7 +2,9 @@ use anyhow::{Context, Result};
|
||||||
use minijinja::{context, Environment};
|
use minijinja::{context, Environment};
|
||||||
use quick_xml::escape::escape;
|
use quick_xml::escape::escape;
|
||||||
use reqwest::Client;
|
use reqwest::Client;
|
||||||
use std::fs::{File, OpenOptions};
|
use std::fs;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
@ -17,9 +19,9 @@ const TEMPLATE_STRING: &str = "
|
||||||
";
|
";
|
||||||
|
|
||||||
pub struct LinkFile {
|
pub struct LinkFile {
|
||||||
file: File,
|
|
||||||
client: Client,
|
client: Client,
|
||||||
path: PathBuf,
|
path: PathBuf,
|
||||||
|
copy_path: PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_link_title(client: &Client, link: &str) -> Result<String> {
|
async fn get_link_title(client: &Client, link: &str) -> Result<String> {
|
||||||
|
@ -40,14 +42,31 @@ async fn get_link_title(client: &Client, link: &str) -> Result<String> {
|
||||||
Ok(node.inner_text(dom.parser()).to_string())
|
Ok(node.inner_text(dom.parser()).to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn prepend_file(
|
||||||
|
data: &[u8],
|
||||||
|
file_path: impl AsRef<Path>,
|
||||||
|
copy_path: impl AsRef<Path>,
|
||||||
|
) -> io::Result<()> {
|
||||||
|
let mut tmp = File::create(©_path)?;
|
||||||
|
let mut src = File::open(&file_path)?;
|
||||||
|
|
||||||
|
tmp.write_all(data)?;
|
||||||
|
|
||||||
|
io::copy(&mut src, &mut tmp)?;
|
||||||
|
|
||||||
|
fs::remove_file(&file_path)?;
|
||||||
|
fs::rename(©_path, &file_path)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
impl LinkFile {
|
impl LinkFile {
|
||||||
pub fn new(path: impl AsRef<Path> + Clone) -> Self {
|
pub fn new(path: impl AsRef<Path> + Clone, tempfile: impl AsRef<Path> + Clone) -> Self {
|
||||||
let file = OpenOptions::new().append(true).open(path.clone()).unwrap();
|
|
||||||
let client = Client::new();
|
let client = Client::new();
|
||||||
Self {
|
Self {
|
||||||
file,
|
|
||||||
client,
|
client,
|
||||||
path: path.as_ref().to_path_buf(),
|
path: path.as_ref().to_path_buf(),
|
||||||
|
copy_path: tempfile.as_ref().to_path_buf(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,14 +80,17 @@ impl LinkFile {
|
||||||
env.add_template("rss_fragment", TEMPLATE_STRING)?;
|
env.add_template("rss_fragment", TEMPLATE_STRING)?;
|
||||||
let template = env.get_template("rss_fragment")?;
|
let template = env.get_template("rss_fragment")?;
|
||||||
|
|
||||||
self.file.write_all(
|
prepend_file(
|
||||||
template
|
template
|
||||||
.render(context! {
|
.render(context! {
|
||||||
title => title,
|
title => title,
|
||||||
link => escape(link).to_string(),
|
link => escape(link).to_string(),
|
||||||
})?
|
})?
|
||||||
.as_bytes(),
|
.as_bytes(),
|
||||||
|
self.path.clone(),
|
||||||
|
self.copy_path.clone(),
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,7 +106,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_linkfile() {
|
fn test_linkfile() {
|
||||||
let mut linkfile = LinkFile::new("./store.xmlfrag");
|
let mut linkfile = LinkFile::new("./store.xmlfrag", "./copy_store.xmlfrag");
|
||||||
linkfile.add_link("https://evilmuff.in").unwrap();
|
linkfile.add_link("https://evilmuff.in").unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user