Compare commits
2 commits
fa1917ac17
...
ba4c7bfcbe
Author | SHA1 | Date | |
---|---|---|---|
xenofem | ba4c7bfcbe | ||
xenofem | 32738e4515 |
3
resources/.gitignore
vendored
Normal file
3
resources/.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
*.png
|
||||
*.ico
|
||||
*.svg
|
22
resources/Makefile
Normal file
22
resources/Makefile
Normal file
|
@ -0,0 +1,22 @@
|
|||
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
|
16
resources/transbeam-16.asy
Normal file
16
resources/transbeam-16.asy
Normal file
|
@ -0,0 +1,16 @@
|
|||
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
resources/transbeam-192.asy
Symbolic link
1
resources/transbeam-192.asy
Symbolic link
|
@ -0,0 +1 @@
|
|||
transbeam.asy
|
|
@ -5,9 +5,9 @@ prefs.triangleRadius = 100;
|
|||
prefs.borderInset = 14;
|
||||
prefs.circleRadius = 12;
|
||||
prefs.bigBeamRadius = 36;
|
||||
prefs.smallBeamRadius = 36;
|
||||
prefs.smallBeamRadius = 30;
|
||||
prefs.beamCount = 3;
|
||||
prefs.beamAngle = 40;
|
||||
prefs.beamAngle = 32;
|
||||
prefs.starterBeamAngle = 80;
|
||||
|
||||
drawTransbeam(prefs);
|
13
resources/transbeam-48.asy
Normal file
13
resources/transbeam-48.asy
Normal file
|
@ -0,0 +1,13 @@
|
|||
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
resources/transbeam-512.asy
Symbolic link
1
resources/transbeam-512.asy
Symbolic link
|
@ -0,0 +1 @@
|
|||
transbeam.asy
|
13
resources/transbeam-64.asy
Normal file
13
resources/transbeam-64.asy
Normal file
|
@ -0,0 +1,13 @@
|
|||
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,21 +4,22 @@ struct transbeamPrefs {
|
|||
real circleRadius;
|
||||
real bigBeamRadius;
|
||||
real smallBeamRadius;
|
||||
real beamCount;
|
||||
int beamCount;
|
||||
real beamAngle;
|
||||
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) {
|
||||
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;
|
||||
fill(outerBorder, white);
|
||||
|
||||
|
|
69
src/state.rs
69
src/state.rs
|
@ -1,6 +1,6 @@
|
|||
use std::{collections::HashMap, io::ErrorKind};
|
||||
|
||||
use log::{error, info};
|
||||
use log::{error, info, warn, debug};
|
||||
use tokio::{
|
||||
fs::File,
|
||||
io::{AsyncReadExt, AsyncWriteExt},
|
||||
|
@ -51,6 +51,36 @@ 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>);
|
||||
impl PersistentState {
|
||||
pub(crate) async fn load() -> std::io::Result<Self> {
|
||||
|
@ -63,35 +93,18 @@ impl PersistentState {
|
|||
info!("Loaded {} file entries from persistent storage", map.len());
|
||||
let mut filtered: HashMap<String, DownloadableFile> = HashMap::new();
|
||||
for (key, info) in map.into_iter() {
|
||||
if !crate::util::is_ascii_alphanumeric(&key) {
|
||||
error!("Invalid key in persistent storage: {}", key);
|
||||
continue;
|
||||
}
|
||||
let file = if let Ok(f) = File::open(storage_dir().join(&key)).await {
|
||||
f
|
||||
if is_valid_entry(&key, &info).await {
|
||||
filtered.insert(key, info);
|
||||
} else {
|
||||
error!(
|
||||
"Unable to open file {} referenced in persistent storage",
|
||||
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;
|
||||
info!("Deleting invalid file {}", key);
|
||||
if let Err(e) = tokio::fs::remove_file(storage_dir().join(&key)).await {
|
||||
warn!("Failed to delete invalid file {}: {}", key, e);
|
||||
}
|
||||
}
|
||||
filtered.insert(key, info);
|
||||
}
|
||||
Ok(Self(filtered))
|
||||
let mut loaded = Self(filtered);
|
||||
loaded.save().await?;
|
||||
Ok(loaded)
|
||||
}
|
||||
Err(e) => {
|
||||
if let ErrorKind::NotFound = e.kind() {
|
||||
|
@ -104,6 +117,7 @@ impl PersistentState {
|
|||
}
|
||||
|
||||
async fn save(&mut self) -> std::io::Result<()> {
|
||||
info!("saving updated state: {} entries", self.0.len());
|
||||
File::create(storage_dir().join(STATE_FILE_NAME))
|
||||
.await?
|
||||
.write_all(&serde_json::to_vec_pretty(&self.0)?)
|
||||
|
@ -124,6 +138,7 @@ impl PersistentState {
|
|||
}
|
||||
|
||||
pub(crate) async fn remove_file(&mut self, key: &str) -> std::io::Result<()> {
|
||||
debug!("removing entry {} from state", key);
|
||||
self.0.remove(key);
|
||||
self.save().await
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use std::{collections::HashSet, io::Write, task::Waker};
|
||||
|
||||
use actix::{Actor, ActorContext, AsyncContext, Handler, Message, StreamHandler};
|
||||
use actix::{fut::future::ActorFutureExt, Actor, ActorContext, AsyncContext, Handler, Message, StreamHandler};
|
||||
use actix_http::ws::{CloseReason, Item};
|
||||
use actix_web_actors::ws::{self, CloseCode};
|
||||
use bytes::Bytes;
|
||||
|
@ -9,7 +9,7 @@ use rand::distributions::{Alphanumeric, DistString};
|
|||
use serde::Deserialize;
|
||||
use time::OffsetDateTime;
|
||||
|
||||
use crate::{file::LiveWriter, DownloadableFile, UploadedFile};
|
||||
use crate::{file::LiveWriter, DownloadableFile, UploadedFile, storage_dir};
|
||||
|
||||
const MAX_FILES: usize = 256;
|
||||
const FILENAME_DATE_FORMAT: &[time::format_description::FormatItem] =
|
||||
|
@ -115,7 +115,6 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for Uploader {
|
|||
Err(e) => {
|
||||
error!("Websocket error: {}", e);
|
||||
self.cleanup_after_error(ctx);
|
||||
ctx.stop();
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
@ -128,7 +127,6 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for Uploader {
|
|||
description: Some(e.to_string()),
|
||||
}));
|
||||
self.cleanup_after_error(ctx);
|
||||
ctx.stop();
|
||||
}
|
||||
Ok(true) => {
|
||||
info!("Finished uploading data");
|
||||
|
@ -140,9 +138,9 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for Uploader {
|
|||
let data = self.app_data.clone();
|
||||
let filename = self.storage_filename.clone();
|
||||
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);
|
||||
}));
|
||||
ctx.stop();
|
||||
}).map(|_, _, ctx: &mut Self::Context| ctx.stop()));
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
|
@ -191,7 +189,7 @@ impl Uploader {
|
|||
}
|
||||
let storage_filename = Alphanumeric.sample_string(&mut rand::thread_rng(), 8);
|
||||
self.storage_filename = storage_filename.clone();
|
||||
let storage_path = super::storage_dir().join(storage_filename.clone());
|
||||
let storage_path = storage_dir().join(storage_filename.clone());
|
||||
info!("storing to: {:?}", storage_path);
|
||||
let writer = super::file::LiveFileWriter::new(&storage_path)?;
|
||||
let addr = Some(ctx.address());
|
||||
|
@ -225,6 +223,7 @@ impl Uploader {
|
|||
self.writer = Some(writer);
|
||||
let data = self.app_data.clone();
|
||||
ctx.spawn(actix::fut::wrap_future(async move {
|
||||
debug!("Spawned future to add entry {} to state", storage_filename);
|
||||
data.write()
|
||||
.await
|
||||
.add_file(storage_filename, downloadable_file)
|
||||
|
@ -275,10 +274,15 @@ impl Uploader {
|
|||
}
|
||||
|
||||
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 filename = self.storage_filename.clone();
|
||||
ctx.spawn(actix::fut::wrap_future(async move {
|
||||
ctx.wait(actix::fut::wrap_future(async move {
|
||||
debug!("Spawned future to remove entry {} from state", filename);
|
||||
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,6 +4,8 @@
|
|||
<meta charset="utf-8"/>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
||||
<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>
|
||||
<title>transbeam</title>
|
||||
</head>
|
||||
|
|
22
static/manifest.json
Normal file
22
static/manifest.json
Normal file
|
@ -0,0 +1,22 @@
|
|||
{
|
||||
"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"
|
||||
}
|
BIN
static/transbeam-192.png
Normal file
BIN
static/transbeam-192.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 44 KiB |
BIN
static/transbeam-512.png
Normal file
BIN
static/transbeam-512.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 139 KiB |
BIN
static/transbeam-apple.png
Normal file
BIN
static/transbeam-apple.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 43 KiB |
Loading…
Reference in a new issue