From f0717425fb6bce6bfd9eb0a575980d44cd645301 Mon Sep 17 00:00:00 2001 From: xenua Date: Thu, 5 Jan 2023 23:11:34 +0100 Subject: [PATCH] write more code (also ew frontend) --- .gitignore | 1 + README.md | 4 ++- src/main.rs | 36 ++++++++++++++++++++++-- static/main.css | 0 static/main.js | 66 ++++++++++++++++++++++++++++++++++++++++++++ templates/base.html | 18 ++++++++++-- templates/index.html | 33 ++++++++++++++++++++++ 7 files changed, 152 insertions(+), 6 deletions(-) create mode 100644 static/main.css create mode 100644 static/main.js create mode 100644 templates/index.html diff --git a/.gitignore b/.gitignore index ea8c4bf..c403c34 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +.idea/ diff --git a/README.md b/README.md index 3ff3fa1..51f8e84 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,8 @@ the dead simple - image paste - text paste +"pastebin on estrogen" ~harmonia + ## design goals - no database @@ -20,4 +22,4 @@ the dead simple - `LISTEN_ADDR` - `FILE_CACHE_MAX_SIZE` - `FILE_UPLOAD_MAX_SIZE` -- `` \ No newline at end of file +- `` diff --git a/src/main.rs b/src/main.rs index c423cb4..7ecc724 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ mod data; mod down; mod up; -use std::net::SocketAddr; +use std::{net::SocketAddr, path::PathBuf}; use axum::{routing::get, Router}; use lazy_static::lazy_static; @@ -19,13 +19,45 @@ lazy_static! { }; } +#[derive(thiserror::Error, Debug)] +pub enum Error { + #[error("DataStorage error")] + DataStorage(#[from] data::Error), + #[error("invalid env var")] + EnvVar, +} + struct AppState { alphabet: Vec, id_length: usize, cache: DataStorage, } -impl AppState {} +impl AppState { + pub fn new() -> Result { + let base_dir_string = std::env::var("V_BASE_DIR").or(Err(Error::EnvVar))?; + let id_length: usize = std::env::var("V_ID_LENGTH") + .unwrap_or("6".to_string()) + .parse() + .or(Err(Error::EnvVar))?; + let alphabet = std::env::var("V_ID_ALPHABET") + .unwrap_or("abcdefghijkmnpqrstuwxyz123467890".to_string()) + .chars() + .collect(); + + let base_dir = PathBuf::new(); + base_dir.push(base_dir_string); + + let mut cache = DataStorage::new(base_dir); + cache.init()?; + + Ok(Self { + alphabet, + id_length, + cache, + }) + } +} fn routes() -> Router { let up = up::routes(); diff --git a/static/main.css b/static/main.css new file mode 100644 index 0000000..e69de29 diff --git a/static/main.js b/static/main.js new file mode 100644 index 0000000..588fd41 --- /dev/null +++ b/static/main.js @@ -0,0 +1,66 @@ +// i dont know how to java script. if you do know how to, +// want to rewrite this into proper java script, +// and can do so without introducing any dependencies etc, +// email me at v-contrib @ xenua dot me + +document.addEventListener('load', () => { + let replaceMe = document.querySelector("#scriptReplaceTarget"); + // TODO +}) + +window.addEventListener('paste', (event) => { + handleDataTransfer(event.clipboardData); +}); + +handleDataTransfer = (data) => { + if (data.clipboardData.items) { + let item = data.clipboardData.items[0]; + if (item.kind === 'string') { + item.getAsString((thing) => { + try { + let url = new URL(thing); + promptUser("you pasted a valid link; do you want to create a short link?", () => { + createShortLink(url); + }, () => { + initTextArea(thing); + }); + } catch (e) { + initTextArea(thing); + } + }) + } else { + uploadFile(item.getAsFile()); + } + } +} + +promptUser = (promptText, yesAction, noAction) => { + +} + +initTextArea = (initialContents) => { + +} + +uploadFile = (file) => { + +} + +onDropHandler = (event) => { +} + +onDragEnterHandler = (event) => { + if (event.target.classList.contains("dropzone")) { + event.target.classList.add("dragover"); + } +} + +onDragLeaveHandler = (event) => { + if (event.target.classList.contains("dropzone")) { + event.target.classList.remove("dragover"); + } +} + +createShortLink = (url) => { + +} diff --git a/templates/base.html b/templates/base.html index 924a8ed..3a3d7f8 100644 --- a/templates/base.html +++ b/templates/base.html @@ -2,13 +2,25 @@ - ^V - the everything paste bin + {% block title %}^V - the everything paste bin{% endblock title %} + {% block head %} + {% endblock head %} - - + + {% block body %} +
+ {% block header %}{% endblock header %} +
+
+ {% block main %}{% enblock main %} +
+
+ {% block footer %}{% endblock footer %} +
+ {% endblock body %} \ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..b52fdb4 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,33 @@ +{% extends "base.html" %} + +{% block main %} +
+ + +
+ + lets you upload files and images +
+ + +
+
+ + or create short links +
+ + + +
+
+
+
+ or share some text + +
+ +
+
+ +
+{% endblock main %} \ No newline at end of file