This commit is contained in:
h 2023-10-29 02:36:20 -04:00
parent ea734d7809
commit 87b9958d28
3 changed files with 33 additions and 11 deletions

View File

@ -18,8 +18,8 @@ RUN apt-get update && apt-get install libssl-dev -y
RUN mkdir /store
RUN touch /store/store.xmlfrag
RUN touch /store/store.xmlfrag && touch /store/tmpfile.xmlfrag
COPY ./templates/ /app/templates
CMD [ "/app/linky", "/store/store.xmlfrag" ]
CMD [ "/app/linky", "/store/store.xmlfrag", "/store/tmpfile.xmlfrag" ]

View File

@ -27,12 +27,12 @@ static SECRET_KEY: LazyLock<Key> = LazyLock::new(Key::generate);
#[tokio::main]
async fn main() {
let args: Vec<String> = env::args().collect();
if args.len() < 2 {
if args.len() < 3 {
println!("Need file argument.");
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()
.route("/rss.xml", get(get_rss))

View File

@ -2,7 +2,9 @@ use anyhow::{Context, Result};
use minijinja::{context, Environment};
use quick_xml::escape::escape;
use reqwest::Client;
use std::fs::{File, OpenOptions};
use std::fs;
use std::fs::File;
use std::io;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::time::Duration;
@ -17,9 +19,9 @@ const TEMPLATE_STRING: &str = "
";
pub struct LinkFile {
file: File,
client: Client,
path: PathBuf,
copy_path: PathBuf,
}
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())
}
fn prepend_file(
data: &[u8],
file_path: impl AsRef<Path>,
copy_path: impl AsRef<Path>,
) -> io::Result<()> {
let mut tmp = File::create(&copy_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(&copy_path, &file_path)?;
Ok(())
}
impl LinkFile {
pub fn new(path: impl AsRef<Path> + Clone) -> Self {
let file = OpenOptions::new().append(true).open(path.clone()).unwrap();
pub fn new(path: impl AsRef<Path> + Clone, tempfile: impl AsRef<Path> + Clone) -> Self {
let client = Client::new();
Self {
file,
client,
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)?;
let template = env.get_template("rss_fragment")?;
self.file.write_all(
prepend_file(
template
.render(context! {
title => title,
link => escape(link).to_string(),
})?
.as_bytes(),
self.path.clone(),
self.copy_path.clone(),
)?;
Ok(())
}
@ -84,7 +106,7 @@ mod tests {
#[test]
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();
}
}