Compare commits
No commits in common. "ba4c7bfcbe3582c9928e6a4af443383ef698bf2d" and "fa1917ac1752239a3d270e747d4a300fc0c301a5" have entirely different histories.
ba4c7bfcbe
...
fa1917ac17
3
resources/.gitignore
vendored
3
resources/.gitignore
vendored
|
@ -1,3 +0,0 @@
|
||||||
*.png
|
|
||||||
*.ico
|
|
||||||
*.svg
|
|
|
@ -1,22 +0,0 @@
|
||||||
density := 600
|
|
||||||
|
|
||||||
icons := transbeam.svg favicon.ico transbeam-192.png transbeam-512.png transbeam-apple.png
|
|
||||||
|
|
||||||
icons: $(icons)
|
|
||||||
|
|
||||||
%.svg: %.asy transbeam-common.asy
|
|
||||||
asy -f svg $<
|
|
||||||
|
|
||||||
transbeam-apple.png: transbeam.svg
|
|
||||||
convert -density $(density) -background white $< -resize 192x192 -extent 192x192 $@
|
|
||||||
|
|
||||||
transbeam-%.png: transbeam-%.svg
|
|
||||||
convert -density $(density) -background transparent $< -resize $*x$* -extent $*x$* $@
|
|
||||||
|
|
||||||
favicon.ico: transbeam-16.png transbeam-32.png transbeam-48.png transbeam-64.png
|
|
||||||
convert -background transparent $^ $@
|
|
||||||
|
|
||||||
install: icons
|
|
||||||
cp $(icons) ../static/
|
|
||||||
clean:
|
|
||||||
rm *.svg *.png *.ico
|
|
|
@ -1,16 +0,0 @@
|
||||||
import "transbeam-common.asy" as transbeam;
|
|
||||||
|
|
||||||
transbeamPrefs prefs;
|
|
||||||
prefs.triangleRadius = 100;
|
|
||||||
prefs.borderInset = 25;
|
|
||||||
prefs.circleRadius = 20;
|
|
||||||
prefs.beamCount = 0;
|
|
||||||
prefs.starterBeamAngle = 180;
|
|
||||||
// Not relevant with beamCount 0
|
|
||||||
prefs.bigBeamRadius = 36;
|
|
||||||
prefs.smallBeamRadius = 36;
|
|
||||||
prefs.beamAngle = 40;
|
|
||||||
|
|
||||||
drawTransbeam(prefs);
|
|
||||||
|
|
||||||
fill(circle((0,0), prefs.circleRadius), transPink);
|
|
|
@ -1 +0,0 @@
|
||||||
transbeam.asy
|
|
|
@ -1,13 +0,0 @@
|
||||||
import "transbeam-common.asy" as transbeam;
|
|
||||||
|
|
||||||
transbeamPrefs prefs;
|
|
||||||
prefs.triangleRadius = 100;
|
|
||||||
prefs.borderInset = 12;
|
|
||||||
prefs.circleRadius = 12;
|
|
||||||
prefs.bigBeamRadius = 36;
|
|
||||||
prefs.smallBeamRadius = 30;
|
|
||||||
prefs.beamCount = 7;
|
|
||||||
prefs.beamAngle = 15;
|
|
||||||
prefs.starterBeamAngle = 36;
|
|
||||||
|
|
||||||
drawTransbeam(prefs);
|
|
|
@ -1 +0,0 @@
|
||||||
transbeam.asy
|
|
|
@ -1,13 +0,0 @@
|
||||||
import "transbeam-common.asy" as transbeam;
|
|
||||||
|
|
||||||
transbeamPrefs prefs;
|
|
||||||
prefs.triangleRadius = 100;
|
|
||||||
prefs.borderInset = 9;
|
|
||||||
prefs.circleRadius = 12;
|
|
||||||
prefs.bigBeamRadius = 33;
|
|
||||||
prefs.smallBeamRadius = 25;
|
|
||||||
prefs.beamCount = 9;
|
|
||||||
prefs.beamAngle = 10;
|
|
||||||
prefs.starterBeamAngle = 24;
|
|
||||||
|
|
||||||
drawTransbeam(prefs);
|
|
|
@ -4,22 +4,21 @@ struct transbeamPrefs {
|
||||||
real circleRadius;
|
real circleRadius;
|
||||||
real bigBeamRadius;
|
real bigBeamRadius;
|
||||||
real smallBeamRadius;
|
real smallBeamRadius;
|
||||||
int beamCount;
|
real beamCount;
|
||||||
real beamAngle;
|
real beamAngle;
|
||||||
real starterBeamAngle;
|
real starterBeamAngle;
|
||||||
}
|
}
|
||||||
|
|
||||||
pen transBlue = rgb("55cdfc");
|
|
||||||
pen transPink = rgb("f7a8b8");
|
|
||||||
|
|
||||||
pen bigBeamColor = transPink;
|
|
||||||
pen smallBeamColor = transBlue;
|
|
||||||
|
|
||||||
path borderTriangle = dir(90)--dir(210)--dir(330)--cycle;
|
|
||||||
|
|
||||||
void drawTransbeam(transbeamPrefs prefs) {
|
void drawTransbeam(transbeamPrefs prefs) {
|
||||||
unravel prefs;
|
unravel prefs;
|
||||||
|
|
||||||
|
pen transBlue = rgb("55cdfc");
|
||||||
|
pen transPink = rgb("f7a8b8");
|
||||||
|
|
||||||
|
pen bigBeamColor = transPink;
|
||||||
|
pen smallBeamColor = transBlue;
|
||||||
|
|
||||||
|
path borderTriangle = dir(90)--dir(210)--dir(330)--cycle;
|
||||||
path outerBorder = scale(triangleRadius)*borderTriangle;
|
path outerBorder = scale(triangleRadius)*borderTriangle;
|
||||||
fill(outerBorder, white);
|
fill(outerBorder, white);
|
||||||
|
|
||||||
|
|
|
@ -5,9 +5,9 @@ prefs.triangleRadius = 100;
|
||||||
prefs.borderInset = 14;
|
prefs.borderInset = 14;
|
||||||
prefs.circleRadius = 12;
|
prefs.circleRadius = 12;
|
||||||
prefs.bigBeamRadius = 36;
|
prefs.bigBeamRadius = 36;
|
||||||
prefs.smallBeamRadius = 30;
|
prefs.smallBeamRadius = 36;
|
||||||
prefs.beamCount = 3;
|
prefs.beamCount = 3;
|
||||||
prefs.beamAngle = 32;
|
prefs.beamAngle = 40;
|
||||||
prefs.starterBeamAngle = 80;
|
prefs.starterBeamAngle = 80;
|
||||||
|
|
||||||
drawTransbeam(prefs);
|
drawTransbeam(prefs);
|
69
src/state.rs
69
src/state.rs
|
@ -1,6 +1,6 @@
|
||||||
use std::{collections::HashMap, io::ErrorKind};
|
use std::{collections::HashMap, io::ErrorKind};
|
||||||
|
|
||||||
use log::{error, info, warn, debug};
|
use log::{error, info};
|
||||||
use tokio::{
|
use tokio::{
|
||||||
fs::File,
|
fs::File,
|
||||||
io::{AsyncReadExt, AsyncWriteExt},
|
io::{AsyncReadExt, AsyncWriteExt},
|
||||||
|
@ -51,36 +51,6 @@ pub(crate) mod timestamp {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn is_valid_entry(key: &str, info: &DownloadableFile) -> bool {
|
|
||||||
if !crate::util::is_ascii_alphanumeric(&key) {
|
|
||||||
error!("Invalid key in persistent storage: {}", key);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
let file = if let Ok(f) = File::open(storage_dir().join(&key)).await {
|
|
||||||
f
|
|
||||||
} else {
|
|
||||||
error!(
|
|
||||||
"Unable to open file {} referenced in persistent storage",
|
|
||||||
key
|
|
||||||
);
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
let metadata = if let Ok(md) = file.metadata().await {
|
|
||||||
md
|
|
||||||
} else {
|
|
||||||
error!(
|
|
||||||
"Unable to get metadata for file {} referenced in persistent storage",
|
|
||||||
key
|
|
||||||
);
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
if metadata.len() != info.size {
|
|
||||||
error!("Mismatched file size for file {} referenced in persistent storage: expected {}, found {}", key, info.size, metadata.len());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
true
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) struct PersistentState(HashMap<String, DownloadableFile>);
|
pub(crate) struct PersistentState(HashMap<String, DownloadableFile>);
|
||||||
impl PersistentState {
|
impl PersistentState {
|
||||||
pub(crate) async fn load() -> std::io::Result<Self> {
|
pub(crate) async fn load() -> std::io::Result<Self> {
|
||||||
|
@ -93,18 +63,35 @@ impl PersistentState {
|
||||||
info!("Loaded {} file entries from persistent storage", map.len());
|
info!("Loaded {} file entries from persistent storage", map.len());
|
||||||
let mut filtered: HashMap<String, DownloadableFile> = HashMap::new();
|
let mut filtered: HashMap<String, DownloadableFile> = HashMap::new();
|
||||||
for (key, info) in map.into_iter() {
|
for (key, info) in map.into_iter() {
|
||||||
if is_valid_entry(&key, &info).await {
|
if !crate::util::is_ascii_alphanumeric(&key) {
|
||||||
filtered.insert(key, info);
|
error!("Invalid key in persistent storage: {}", key);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let file = if let Ok(f) = File::open(storage_dir().join(&key)).await {
|
||||||
|
f
|
||||||
} else {
|
} else {
|
||||||
info!("Deleting invalid file {}", key);
|
error!(
|
||||||
if let Err(e) = tokio::fs::remove_file(storage_dir().join(&key)).await {
|
"Unable to open file {} referenced in persistent storage",
|
||||||
warn!("Failed to delete invalid file {}: {}", key, e);
|
key
|
||||||
|
);
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
let metadata = if let Ok(md) = file.metadata().await {
|
||||||
|
md
|
||||||
|
} else {
|
||||||
|
error!(
|
||||||
|
"Unable to get metadata for file {} referenced in persistent storage",
|
||||||
|
key
|
||||||
|
);
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
if metadata.len() != info.size {
|
||||||
|
error!("Mismatched file size for file {} referenced in persistent storage: expected {}, found {}", key, info.size, metadata.len());
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
filtered.insert(key, info);
|
||||||
}
|
}
|
||||||
}
|
Ok(Self(filtered))
|
||||||
let mut loaded = Self(filtered);
|
|
||||||
loaded.save().await?;
|
|
||||||
Ok(loaded)
|
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
if let ErrorKind::NotFound = e.kind() {
|
if let ErrorKind::NotFound = e.kind() {
|
||||||
|
@ -117,7 +104,6 @@ impl PersistentState {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn save(&mut self) -> std::io::Result<()> {
|
async fn save(&mut self) -> std::io::Result<()> {
|
||||||
info!("saving updated state: {} entries", self.0.len());
|
|
||||||
File::create(storage_dir().join(STATE_FILE_NAME))
|
File::create(storage_dir().join(STATE_FILE_NAME))
|
||||||
.await?
|
.await?
|
||||||
.write_all(&serde_json::to_vec_pretty(&self.0)?)
|
.write_all(&serde_json::to_vec_pretty(&self.0)?)
|
||||||
|
@ -138,7 +124,6 @@ impl PersistentState {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn remove_file(&mut self, key: &str) -> std::io::Result<()> {
|
pub(crate) async fn remove_file(&mut self, key: &str) -> std::io::Result<()> {
|
||||||
debug!("removing entry {} from state", key);
|
|
||||||
self.0.remove(key);
|
self.0.remove(key);
|
||||||
self.save().await
|
self.save().await
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use std::{collections::HashSet, io::Write, task::Waker};
|
use std::{collections::HashSet, io::Write, task::Waker};
|
||||||
|
|
||||||
use actix::{fut::future::ActorFutureExt, Actor, ActorContext, AsyncContext, Handler, Message, StreamHandler};
|
use actix::{Actor, ActorContext, AsyncContext, Handler, Message, StreamHandler};
|
||||||
use actix_http::ws::{CloseReason, Item};
|
use actix_http::ws::{CloseReason, Item};
|
||||||
use actix_web_actors::ws::{self, CloseCode};
|
use actix_web_actors::ws::{self, CloseCode};
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
|
@ -9,7 +9,7 @@ use rand::distributions::{Alphanumeric, DistString};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use time::OffsetDateTime;
|
use time::OffsetDateTime;
|
||||||
|
|
||||||
use crate::{file::LiveWriter, DownloadableFile, UploadedFile, storage_dir};
|
use crate::{file::LiveWriter, DownloadableFile, UploadedFile};
|
||||||
|
|
||||||
const MAX_FILES: usize = 256;
|
const MAX_FILES: usize = 256;
|
||||||
const FILENAME_DATE_FORMAT: &[time::format_description::FormatItem] =
|
const FILENAME_DATE_FORMAT: &[time::format_description::FormatItem] =
|
||||||
|
@ -115,6 +115,7 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for Uploader {
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("Websocket error: {}", e);
|
error!("Websocket error: {}", e);
|
||||||
self.cleanup_after_error(ctx);
|
self.cleanup_after_error(ctx);
|
||||||
|
ctx.stop();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -127,6 +128,7 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for Uploader {
|
||||||
description: Some(e.to_string()),
|
description: Some(e.to_string()),
|
||||||
}));
|
}));
|
||||||
self.cleanup_after_error(ctx);
|
self.cleanup_after_error(ctx);
|
||||||
|
ctx.stop();
|
||||||
}
|
}
|
||||||
Ok(true) => {
|
Ok(true) => {
|
||||||
info!("Finished uploading data");
|
info!("Finished uploading data");
|
||||||
|
@ -138,9 +140,9 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for Uploader {
|
||||||
let data = self.app_data.clone();
|
let data = self.app_data.clone();
|
||||||
let filename = self.storage_filename.clone();
|
let filename = self.storage_filename.clone();
|
||||||
ctx.wait(actix::fut::wrap_future(async move {
|
ctx.wait(actix::fut::wrap_future(async move {
|
||||||
debug!("Spawned future to remove uploader from entry {} before stopping", filename);
|
|
||||||
data.write().await.remove_uploader(&filename);
|
data.write().await.remove_uploader(&filename);
|
||||||
}).map(|_, _, ctx: &mut Self::Context| ctx.stop()));
|
}));
|
||||||
|
ctx.stop();
|
||||||
}
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
|
@ -189,7 +191,7 @@ impl Uploader {
|
||||||
}
|
}
|
||||||
let storage_filename = Alphanumeric.sample_string(&mut rand::thread_rng(), 8);
|
let storage_filename = Alphanumeric.sample_string(&mut rand::thread_rng(), 8);
|
||||||
self.storage_filename = storage_filename.clone();
|
self.storage_filename = storage_filename.clone();
|
||||||
let storage_path = storage_dir().join(storage_filename.clone());
|
let storage_path = super::storage_dir().join(storage_filename.clone());
|
||||||
info!("storing to: {:?}", storage_path);
|
info!("storing to: {:?}", storage_path);
|
||||||
let writer = super::file::LiveFileWriter::new(&storage_path)?;
|
let writer = super::file::LiveFileWriter::new(&storage_path)?;
|
||||||
let addr = Some(ctx.address());
|
let addr = Some(ctx.address());
|
||||||
|
@ -223,7 +225,6 @@ impl Uploader {
|
||||||
self.writer = Some(writer);
|
self.writer = Some(writer);
|
||||||
let data = self.app_data.clone();
|
let data = self.app_data.clone();
|
||||||
ctx.spawn(actix::fut::wrap_future(async move {
|
ctx.spawn(actix::fut::wrap_future(async move {
|
||||||
debug!("Spawned future to add entry {} to state", storage_filename);
|
|
||||||
data.write()
|
data.write()
|
||||||
.await
|
.await
|
||||||
.add_file(storage_filename, downloadable_file)
|
.add_file(storage_filename, downloadable_file)
|
||||||
|
@ -274,15 +275,10 @@ impl Uploader {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn cleanup_after_error(&mut self, ctx: &mut <Self as Actor>::Context) {
|
fn cleanup_after_error(&mut self, ctx: &mut <Self as Actor>::Context) {
|
||||||
info!("Cleaning up after failed upload of {}", self.storage_filename);
|
|
||||||
let data = self.app_data.clone();
|
let data = self.app_data.clone();
|
||||||
let filename = self.storage_filename.clone();
|
let filename = self.storage_filename.clone();
|
||||||
ctx.wait(actix::fut::wrap_future(async move {
|
ctx.spawn(actix::fut::wrap_future(async move {
|
||||||
debug!("Spawned future to remove entry {} from state", filename);
|
|
||||||
data.write().await.remove_file(&filename).await.unwrap();
|
data.write().await.remove_file(&filename).await.unwrap();
|
||||||
}).map(|_, _, ctx: &mut <Self as Actor>::Context| ctx.stop()));
|
}));
|
||||||
if let Err(e) = std::fs::remove_file(storage_dir().join(&self.storage_filename)) {
|
|
||||||
error!("Failed to remove file {}: {}", self.storage_filename, e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 31 KiB After Width: | Height: | Size: 31 KiB |
|
@ -4,8 +4,6 @@
|
||||||
<meta charset="utf-8"/>
|
<meta charset="utf-8"/>
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
||||||
<link rel="stylesheet" type="text/css" href="transbeam.css"/>
|
<link rel="stylesheet" type="text/css" href="transbeam.css"/>
|
||||||
<link rel="apple-touch-icon" href="transbeam-apple.png"/>
|
|
||||||
<link rel="manifest" href="manifest.json"/>
|
|
||||||
<script src="util.js"></script>
|
<script src="util.js"></script>
|
||||||
<title>transbeam</title>
|
<title>transbeam</title>
|
||||||
</head>
|
</head>
|
||||||
|
|
|
@ -1,22 +0,0 @@
|
||||||
{
|
|
||||||
"name": "transbeam",
|
|
||||||
"icons": [
|
|
||||||
{
|
|
||||||
"src": "transbeam.svg",
|
|
||||||
"type": "image/svg+xml",
|
|
||||||
"sizes": "512x512"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"src": "transbeam-192.png",
|
|
||||||
"type": "image/png",
|
|
||||||
"sizes": "192x192"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"src": "transbeam-512.png",
|
|
||||||
"type": "image/png",
|
|
||||||
"sizes": "512x512"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"start_url": "./",
|
|
||||||
"background_color": "white"
|
|
||||||
}
|
|
Binary file not shown.
Before Width: | Height: | Size: 44 KiB |
Binary file not shown.
Before Width: | Height: | Size: 139 KiB |
Binary file not shown.
Before Width: | Height: | Size: 43 KiB |
Loading…
Reference in a new issue