mirror of
https://github.com/v0l/route96.git
synced 2025-06-18 14:55:20 +00:00
Compare commits
13 Commits
470af79a24
...
main
Author | SHA1 | Date | |
---|---|---|---|
5026134eef
|
|||
0538eaeba3
|
|||
84be9c16bb
|
|||
84b475d11a
|
|||
9290443a20 | |||
b2fb86021b | |||
5bd47af70b | |||
01c5281425
|
|||
510ba00368 | |||
dccc2d23eb | |||
664499d22c
|
|||
455970b9fe
|
|||
b6c12de685
|
4
.gitignore
vendored
4
.gitignore
vendored
@ -1,3 +1,7 @@
|
||||
target/
|
||||
data/
|
||||
.idea/
|
||||
ui_src/dist/
|
||||
ui_src/node_modules/
|
||||
ui_src/package-lock.json
|
||||
ui_src/*.tsbuildinfo
|
65
Cargo.lock
generated
65
Cargo.lock
generated
@ -691,6 +691,19 @@ dependencies = [
|
||||
"yaml-rust2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "console"
|
||||
version = "0.15.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "054ccb5b10f9f2cbf51eb355ca1d05c2d279ce1804688d0db74b4733a5aeafd8"
|
||||
dependencies = [
|
||||
"encode_unicode",
|
||||
"libc",
|
||||
"once_cell",
|
||||
"unicode-width",
|
||||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "const-oid"
|
||||
version = "0.9.6"
|
||||
@ -958,6 +971,12 @@ dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "encode_unicode"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "34aa73646ffb006b8f5147f3dc182bd4bcb190227ce861fc4a4844bf8e3cb2c0"
|
||||
|
||||
[[package]]
|
||||
name = "encoding_rs"
|
||||
version = "0.8.35"
|
||||
@ -1070,7 +1089,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "ffmpeg-rs-raw"
|
||||
version = "0.1.0"
|
||||
source = "git+https://git.v0l.io/Kieran/ffmpeg-rs-raw.git?rev=928ab9664ff47c1b0bd8313ebc73d13b1ab43fc5#928ab9664ff47c1b0bd8313ebc73d13b1ab43fc5"
|
||||
source = "git+https://git.v0l.io/Kieran/ffmpeg-rs-raw.git?rev=aa1ce3edcad0fcd286d39b3e0c2fdc610c3988e7#aa1ce3edcad0fcd286d39b3e0c2fdc610c3988e7"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"ffmpeg-sys-the-third",
|
||||
@ -1975,6 +1994,19 @@ dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "indicatif"
|
||||
version = "0.17.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "183b3088984b400f4cfac3620d5e076c84da5364016b4f49473de574b2586235"
|
||||
dependencies = [
|
||||
"console",
|
||||
"number_prefix",
|
||||
"portable-atomic",
|
||||
"unicode-width",
|
||||
"web-time",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "infer"
|
||||
version = "0.19.0"
|
||||
@ -2454,6 +2486,12 @@ dependencies = [
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "number_prefix"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3"
|
||||
|
||||
[[package]]
|
||||
name = "object"
|
||||
version = "0.36.7"
|
||||
@ -2760,6 +2798,12 @@ dependencies = [
|
||||
"universal-hash",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "portable-atomic"
|
||||
version = "1.11.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f84267b20a16ea918e43c6a88433c2d54fa145c92a811b5b047ccbe153674483"
|
||||
|
||||
[[package]]
|
||||
name = "powerfmt"
|
||||
version = "0.2.0"
|
||||
@ -3265,6 +3309,7 @@ dependencies = [
|
||||
"ffmpeg-rs-raw",
|
||||
"hex",
|
||||
"http-range-header",
|
||||
"indicatif",
|
||||
"infer",
|
||||
"libc",
|
||||
"log",
|
||||
@ -3278,7 +3323,9 @@ dependencies = [
|
||||
"sqlx",
|
||||
"tokio",
|
||||
"tokio-util",
|
||||
"url",
|
||||
"uuid",
|
||||
"walkdir",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -4528,6 +4575,12 @@ version = "1.12.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-width"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4a1a07cc7db3810833284e8d372ccdc6da29741639ecc70c9ec107df0fa6154c"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.2.6"
|
||||
@ -4742,6 +4795,16 @@ dependencies = [
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "web-time"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5a6580f308b1fad9207618087a65c04e7a10bc77e02c8e84e9b00dd4b12fa0bb"
|
||||
dependencies = [
|
||||
"js-sys",
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "whoami"
|
||||
version = "1.6.0"
|
||||
|
10
Cargo.toml
10
Cargo.toml
@ -11,7 +11,7 @@ path = "src/bin/main.rs"
|
||||
name = "route96"
|
||||
|
||||
[features]
|
||||
default = ["nip96", "blossom", "analytics", "react-ui", "payments"]
|
||||
default = ["nip96", "blossom", "analytics", "react-ui", "payments", "r96util"]
|
||||
media-compression = ["dep:ffmpeg-rs-raw", "dep:libc"]
|
||||
labels = ["media-compression", "dep:candle-core", "dep:candle-nn", "dep:candle-transformers"]
|
||||
nip96 = ["media-compression"]
|
||||
@ -19,6 +19,7 @@ blossom = []
|
||||
analytics = []
|
||||
react-ui = []
|
||||
payments = ["dep:fedimint-tonic-lnd"]
|
||||
r96util = ["dep:walkdir", "dep:indicatif"]
|
||||
|
||||
[dependencies]
|
||||
log = "0.4.21"
|
||||
@ -35,17 +36,20 @@ sha2 = "0.10.8"
|
||||
sqlx = { version = "0.8.1", features = ["mysql", "runtime-tokio", "chrono", "uuid"] }
|
||||
config = { version = "0.15.7", features = ["yaml"] }
|
||||
chrono = { version = "0.4.38", features = ["serde"] }
|
||||
reqwest = { version = "0.12.8", features = ["stream", "http2"] }
|
||||
reqwest = { version = "0.12.8", features = ["stream", "http2", "json"] }
|
||||
clap = { version = "4.5.18", features = ["derive"] }
|
||||
mime2ext = "0.1.53"
|
||||
infer = "0.19.0"
|
||||
tokio-util = { version = "0.7.13", features = ["io", "io-util"] }
|
||||
http-range-header = { version = "0.4.2" }
|
||||
base58 = "0.2.0"
|
||||
url = "2.5.0"
|
||||
|
||||
libc = { version = "0.2.153", optional = true }
|
||||
ffmpeg-rs-raw = { git = "https://git.v0l.io/Kieran/ffmpeg-rs-raw.git", rev = "928ab9664ff47c1b0bd8313ebc73d13b1ab43fc5", optional = true }
|
||||
ffmpeg-rs-raw = { git = "https://git.v0l.io/Kieran/ffmpeg-rs-raw.git", rev = "aa1ce3edcad0fcd286d39b3e0c2fdc610c3988e7", optional = true }
|
||||
candle-core = { git = "https://git.v0l.io/huggingface/candle.git", tag = "0.8.1", optional = true }
|
||||
candle-nn = { git = "https://git.v0l.io/huggingface/candle.git", tag = "0.8.1", optional = true }
|
||||
candle-transformers = { git = "https://git.v0l.io/huggingface/candle.git", tag = "0.8.1", optional = true }
|
||||
fedimint-tonic-lnd = { version = "0.2.0", optional = true, default-features = false, features = ["invoicesrpc", "lightningrpc"] }
|
||||
walkdir = { version = "2.5.0", optional = true }
|
||||
indicatif = { version = "0.17.11", optional = true }
|
19
index.html
19
index.html
@ -66,13 +66,7 @@
|
||||
const file = input.files[0];
|
||||
console.debug(file);
|
||||
|
||||
const r_nip96 = document.querySelector("#method-nip96").checked;
|
||||
const r_blossom = document.querySelector("#method-blossom").checked;
|
||||
if (r_nip96) {
|
||||
await uploadFilesNip96(file)
|
||||
} else if (r_blossom) {
|
||||
await uploadBlossom(file);
|
||||
}
|
||||
} catch (ex) {
|
||||
if (ex instanceof Error) {
|
||||
alert(ex.message);
|
||||
@ -147,16 +141,7 @@
|
||||
Welcome to route96
|
||||
</h1>
|
||||
<div class="flex flex-col gap-2">
|
||||
<div>
|
||||
<label>
|
||||
NIP-96
|
||||
<input type="radio" name="method" id="method-nip96"/>
|
||||
</label>
|
||||
<label>
|
||||
Blossom
|
||||
<input type="radio" name="method" id="method-blossom"/>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<div style="color: #ff8383;">
|
||||
You must have a nostr extension for this to work
|
||||
</div>
|
||||
@ -164,7 +149,7 @@
|
||||
<div>
|
||||
<input type="checkbox" id="no_transform">
|
||||
<label for="no_transform">
|
||||
Disable compression (images)
|
||||
Disable compression (videos and images)
|
||||
</label>
|
||||
</div>
|
||||
<div>
|
||||
|
229
src/bin/r96util.rs
Normal file
229
src/bin/r96util.rs
Normal file
@ -0,0 +1,229 @@
|
||||
use anyhow::{Context, Error, Result};
|
||||
use clap::{Parser, Subcommand};
|
||||
use config::Config;
|
||||
use indicatif::{ProgressBar, ProgressStyle};
|
||||
use log::{error, info};
|
||||
use route96::db::{Database, FileUpload};
|
||||
use route96::filesystem::{FileStore, FileSystemResult};
|
||||
use route96::processing::probe_file;
|
||||
use route96::settings::Settings;
|
||||
use std::future::Future;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::pin::Pin;
|
||||
use std::sync::Arc;
|
||||
use std::time::SystemTime;
|
||||
use pretty_env_logger::env_logger;
|
||||
use tokio::sync::Semaphore;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(version, about)]
|
||||
struct Args {
|
||||
#[arg(long)]
|
||||
pub config: Option<String>,
|
||||
|
||||
#[clap(subcommand)]
|
||||
pub command: Commands,
|
||||
}
|
||||
|
||||
#[derive(Debug, Subcommand)]
|
||||
enum Commands {
|
||||
/// Check file hash matches filename / path
|
||||
Check {
|
||||
#[arg(long)]
|
||||
delete: Option<bool>,
|
||||
},
|
||||
|
||||
/// Import a directory into the filesystem
|
||||
/// (does NOT import files into the database, use database-import command for that)
|
||||
Import {
|
||||
#[arg(long)]
|
||||
from: PathBuf,
|
||||
#[arg(long, default_missing_value = "true", num_args = 0..=1)]
|
||||
probe_media: Option<bool>,
|
||||
},
|
||||
|
||||
/// Import files from filesystem into database
|
||||
DatabaseImport {
|
||||
/// Don't actually import data and just print which files WOULD be imported
|
||||
#[arg(long, default_missing_value = "true", num_args = 0..=1)]
|
||||
dry_run: Option<bool>,
|
||||
},
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Error> {
|
||||
if std::env::var("RUST_LOG").is_err() {
|
||||
unsafe { std::env::set_var("RUST_LOG", "info"); }
|
||||
}
|
||||
env_logger::init();
|
||||
|
||||
let args: Args = Args::parse();
|
||||
|
||||
let builder = Config::builder()
|
||||
.add_source(config::File::with_name(if let Some(ref c) = args.config {
|
||||
c.as_str()
|
||||
} else {
|
||||
"config.yaml"
|
||||
}))
|
||||
.add_source(config::Environment::with_prefix("APP"))
|
||||
.build()?;
|
||||
|
||||
let settings: Settings = builder.try_deserialize()?;
|
||||
|
||||
match args.command {
|
||||
Commands::Check { delete } => {
|
||||
info!("Checking files in: {}", settings.storage_dir);
|
||||
let fs = FileStore::new(settings.clone());
|
||||
iter_files(&fs.storage_dir(), 4, |entry, p| {
|
||||
let p = p.clone();
|
||||
Box::pin(async move {
|
||||
let id = if let Some(i) = id_from_path(&entry) {
|
||||
i
|
||||
} else {
|
||||
p.set_message(format!("Skipping invalid file: {}", &entry.display()));
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
let hash = FileStore::hash_file(&entry).await?;
|
||||
if hash != id {
|
||||
if delete.unwrap_or(false) {
|
||||
p.set_message(format!("Deleting corrupt file: {}", &entry.display()));
|
||||
tokio::fs::remove_file(&entry).await?;
|
||||
} else {
|
||||
p.set_message(format!("File is corrupted: {}", &entry.display()));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
})
|
||||
.await?;
|
||||
}
|
||||
Commands::Import { from, probe_media } => {
|
||||
let fs = FileStore::new(settings.clone());
|
||||
let db = Database::new(&settings.database).await?;
|
||||
db.migrate().await?;
|
||||
info!("Importing from: {}", fs.storage_dir().display());
|
||||
iter_files(&from, 4, |entry, p| {
|
||||
let fs = fs.clone();
|
||||
let p = p.clone();
|
||||
Box::pin(async move {
|
||||
let mime = infer::get_from_path(&entry)?
|
||||
.map(|m| m.mime_type())
|
||||
.unwrap_or("application/octet-stream");
|
||||
|
||||
// test media is not corrupt
|
||||
if probe_media.unwrap_or(true)
|
||||
&& (mime.starts_with("image/") || mime.starts_with("video/"))
|
||||
&& probe_file(&entry).is_err()
|
||||
{
|
||||
p.set_message(format!("Skipping media invalid file: {}", &entry.display()));
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let file = tokio::fs::File::open(&entry).await?;
|
||||
let dst = fs.put(file, mime, false).await?;
|
||||
match dst {
|
||||
FileSystemResult::AlreadyExists(_) => {
|
||||
p.set_message(format!("Duplicate file: {}", &entry.display()));
|
||||
}
|
||||
FileSystemResult::NewFile(_) => {
|
||||
p.set_message(format!("Imported: {}", &entry.display()));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
})
|
||||
.await?;
|
||||
}
|
||||
Commands::DatabaseImport { dry_run } => {
|
||||
let fs = FileStore::new(settings.clone());
|
||||
let db = Database::new(&settings.database).await?;
|
||||
db.migrate().await?;
|
||||
info!("Importing to DB from: {}", fs.storage_dir().display());
|
||||
iter_files(&fs.storage_dir(), 4, |entry, p| {
|
||||
let db = db.clone();
|
||||
let p = p.clone();
|
||||
Box::pin(async move {
|
||||
let id = if let Some(i) = id_from_path(&entry) {
|
||||
i
|
||||
} else {
|
||||
p.set_message(format!("Skipping invalid file: {}", &entry.display()));
|
||||
return Ok(());
|
||||
};
|
||||
let u = db.get_file(&id).await.context("db get_file")?;
|
||||
if u.is_none() {
|
||||
if !dry_run.unwrap_or(false) {
|
||||
p.set_message(format!("Importing file: {}", &entry.display()));
|
||||
let mime = infer::get_from_path(&entry)
|
||||
.context("infer")?
|
||||
.map(|m| m.mime_type())
|
||||
.unwrap_or("application/octet-stream")
|
||||
.to_string();
|
||||
let meta = entry.metadata().context("file metadata")?;
|
||||
let entry = FileUpload {
|
||||
id,
|
||||
name: None,
|
||||
size: meta.len(),
|
||||
mime_type: mime,
|
||||
created: meta.created().unwrap_or(SystemTime::now()).into(),
|
||||
width: None,
|
||||
height: None,
|
||||
blur_hash: None,
|
||||
alt: None,
|
||||
duration: None,
|
||||
bitrate: None,
|
||||
};
|
||||
db.add_file(&entry, None).await.context("db add_file")?;
|
||||
} else {
|
||||
p.set_message(format!(
|
||||
"[DRY-RUN] Importing file: {}",
|
||||
&entry.display()
|
||||
));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
})
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn id_from_path(path: &Path) -> Option<Vec<u8>> {
|
||||
hex::decode(path.file_name()?.to_str()?).ok()
|
||||
}
|
||||
|
||||
async fn iter_files<F>(p: &Path, threads: usize, mut op: F) -> Result<()>
|
||||
where
|
||||
F: FnMut(PathBuf, ProgressBar) -> Pin<Box<dyn Future<Output = Result<()>> + Send>>,
|
||||
{
|
||||
let semaphore = Arc::new(Semaphore::new(threads));
|
||||
info!("Scanning files: {}", p.display());
|
||||
let entries = walkdir::WalkDir::new(p);
|
||||
let dir = entries
|
||||
.into_iter()
|
||||
.filter_map(Result::ok)
|
||||
.filter(|e| e.file_type().is_file())
|
||||
.collect::<Vec<_>>();
|
||||
let p = ProgressBar::new(dir.len() as u64).with_style(ProgressStyle::with_template(
|
||||
"{spinner} [{pos}/{len}] {msg}",
|
||||
)?);
|
||||
let mut all_tasks = vec![];
|
||||
for entry in dir {
|
||||
let _lock = semaphore.clone().acquire_owned().await?;
|
||||
p.inc(1);
|
||||
let fut = op(entry.path().to_path_buf(), p.clone());
|
||||
all_tasks.push(tokio::spawn(async move {
|
||||
if let Err(e) = fut.await {
|
||||
error!("Error processing file: {} {}", entry.path().display(), e);
|
||||
}
|
||||
drop(_lock);
|
||||
}));
|
||||
}
|
||||
for task in all_tasks {
|
||||
task.await?;
|
||||
}
|
||||
p.finish_with_message("Done!");
|
||||
Ok(())
|
||||
}
|
@ -193,7 +193,7 @@ impl Database {
|
||||
.try_get(0)
|
||||
}
|
||||
|
||||
pub async fn add_file(&self, file: &FileUpload, user_id: u64) -> Result<(), Error> {
|
||||
pub async fn add_file(&self, file: &FileUpload, user_id: Option<u64>) -> Result<(), Error> {
|
||||
let mut tx = self.pool.begin().await?;
|
||||
let q = sqlx::query("insert ignore into \
|
||||
uploads(id,name,size,mime_type,blur_hash,width,height,alt,created,duration,bitrate) values(?,?,?,?,?,?,?,?,?,?,?)")
|
||||
@ -210,10 +210,12 @@ impl Database {
|
||||
.bind(file.bitrate);
|
||||
tx.execute(q).await?;
|
||||
|
||||
if let Some(uid) = user_id {
|
||||
let q2 = sqlx::query("insert ignore into user_uploads(file,user_id) values(?,?)")
|
||||
.bind(&file.id)
|
||||
.bind(user_id);
|
||||
.bind(uid);
|
||||
tx.execute(q2).await?;
|
||||
}
|
||||
|
||||
#[cfg(feature = "labels")]
|
||||
for lbl in &file.labels {
|
||||
|
@ -10,6 +10,7 @@ use anyhow::Error;
|
||||
use anyhow::Result;
|
||||
#[cfg(feature = "media-compression")]
|
||||
use ffmpeg_rs_raw::DemuxerInfo;
|
||||
#[cfg(feature = "media-compression")]
|
||||
use ffmpeg_rs_raw::StreamInfo;
|
||||
#[cfg(feature = "media-compression")]
|
||||
use rocket::form::validate::Contains;
|
||||
|
@ -105,7 +105,12 @@ impl WebpProcessor {
|
||||
let mut decoder = Decoder::new();
|
||||
decoder.setup_decoder(image_stream, None)?;
|
||||
|
||||
while let Ok((mut pkt, _stream)) = input.get_packet() {
|
||||
while let Ok((mut pkt, _)) = input.get_packet() {
|
||||
// skip packets not in the image stream
|
||||
if (*pkt).stream_index != image_stream.index as i32 {
|
||||
av_packet_free(&mut pkt);
|
||||
continue;
|
||||
}
|
||||
let mut frame_save: *mut AVFrame = ptr::null_mut();
|
||||
for (mut frame, _stream) in decoder.decode_pkt(pkt)? {
|
||||
if frame_save.is_null() {
|
||||
|
@ -1,5 +1,5 @@
|
||||
use crate::auth::nip98::Nip98Auth;
|
||||
use crate::db::{Database, FileUpload, User, Report};
|
||||
use crate::db::{Database, FileUpload, Report, User};
|
||||
use crate::routes::{Nip94Event, PagedResult};
|
||||
use crate::settings::Settings;
|
||||
use rocket::serde::json::Json;
|
||||
@ -8,7 +8,12 @@ use rocket::{routes, Responder, Route, State};
|
||||
use sqlx::{Error, QueryBuilder, Row};
|
||||
|
||||
pub fn admin_routes() -> Vec<Route> {
|
||||
routes![admin_list_files, admin_get_self, admin_list_reports, admin_acknowledge_report]
|
||||
routes![
|
||||
admin_list_files,
|
||||
admin_get_self,
|
||||
admin_list_reports,
|
||||
admin_acknowledge_report,
|
||||
]
|
||||
}
|
||||
|
||||
#[derive(Serialize, Default)]
|
||||
@ -71,7 +76,11 @@ pub struct AdminNip94File {
|
||||
}
|
||||
|
||||
#[rocket::get("/self")]
|
||||
async fn admin_get_self(auth: Nip98Auth, db: &State<Database>, settings: &State<Settings>) -> AdminResponse<SelfUser> {
|
||||
async fn admin_get_self(
|
||||
auth: Nip98Auth,
|
||||
db: &State<Database>,
|
||||
settings: &State<Settings>,
|
||||
) -> AdminResponse<SelfUser> {
|
||||
let pubkey_vec = auth.event.pubkey.to_bytes().to_vec();
|
||||
match db.get_user(&pubkey_vec).await {
|
||||
Ok(user) => {
|
||||
@ -84,7 +93,9 @@ async fn admin_get_self(auth: Nip98Auth, db: &State<Database>, settings: &State<
|
||||
|
||||
#[cfg(feature = "payments")]
|
||||
let (free_quota, total_available_quota) = {
|
||||
let free_quota = settings.payments.as_ref()
|
||||
let free_quota = settings
|
||||
.payments
|
||||
.as_ref()
|
||||
.and_then(|p| p.free_quota_bytes)
|
||||
.unwrap_or(104857600);
|
||||
let mut total_available = free_quota;
|
||||
@ -223,8 +234,9 @@ impl Database {
|
||||
) -> Result<(Vec<(FileUpload, Vec<User>)>, i64), Error> {
|
||||
let mut q = QueryBuilder::new("select u.* from uploads u ");
|
||||
if let Some(m) = mime_type {
|
||||
q.push("where u.mime_type = ");
|
||||
q.push("where INSTR(u.mime_type,");
|
||||
q.push_bind(m);
|
||||
q.push(") > 0");
|
||||
}
|
||||
q.push(" order by u.created desc limit ");
|
||||
q.push_bind(limit);
|
||||
|
@ -15,8 +15,9 @@ use rocket::{routes, Data, Request, Response, Route, State};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tokio::io::AsyncRead;
|
||||
use tokio_util::io::StreamReader;
|
||||
use url::Url;
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(crate = "rocket::serde")]
|
||||
pub struct BlobDescriptor {
|
||||
pub url: String,
|
||||
@ -225,15 +226,25 @@ async fn mirror(
|
||||
settings: &State<Settings>,
|
||||
req: Json<MirrorRequest>,
|
||||
) -> BlossomResponse {
|
||||
if !check_method(&auth.event, "mirror") {
|
||||
if !check_method(&auth.event, "upload") {
|
||||
return BlossomResponse::error("Invalid request method tag");
|
||||
}
|
||||
if let Some(e) = check_whitelist(&auth, settings) {
|
||||
return e;
|
||||
}
|
||||
|
||||
let url = match Url::parse(&req.url) {
|
||||
Ok(u) => u,
|
||||
Err(e) => return BlossomResponse::error(format!("Invalid URL: {}", e)),
|
||||
};
|
||||
|
||||
let hash = url
|
||||
.path_segments()
|
||||
.and_then(|mut c| c.next_back())
|
||||
.and_then(|s| s.split(".").next());
|
||||
|
||||
// download file
|
||||
let rsp = match reqwest::get(&req.url).await {
|
||||
let rsp = match reqwest::get(url.clone()).await {
|
||||
Err(e) => {
|
||||
error!("Error downloading file: {}", e);
|
||||
return BlossomResponse::error("Failed to mirror file");
|
||||
@ -250,9 +261,10 @@ async fn mirror(
|
||||
let pubkey = auth.event.pubkey.to_bytes().to_vec();
|
||||
|
||||
process_stream(
|
||||
StreamReader::new(rsp.bytes_stream().map(|result| {
|
||||
result.map_err(std::io::Error::other)
|
||||
})),
|
||||
StreamReader::new(
|
||||
rsp.bytes_stream()
|
||||
.map(|result| result.map_err(std::io::Error::other)),
|
||||
),
|
||||
&mime_type,
|
||||
&None,
|
||||
&pubkey,
|
||||
@ -261,6 +273,7 @@ async fn mirror(
|
||||
fs,
|
||||
db,
|
||||
settings,
|
||||
hash.and_then(|h| hex::decode(h).ok()),
|
||||
)
|
||||
.await
|
||||
}
|
||||
@ -367,15 +380,11 @@ async fn process_upload(
|
||||
// check quota (only if payments are configured)
|
||||
#[cfg(feature = "payments")]
|
||||
if let Some(payment_config) = &settings.payments {
|
||||
let free_quota = payment_config.free_quota_bytes
|
||||
.unwrap_or(104857600); // Default to 100MB
|
||||
let free_quota = payment_config.free_quota_bytes.unwrap_or(104857600); // Default to 100MB
|
||||
let pubkey_vec = auth.event.pubkey.to_bytes().to_vec();
|
||||
|
||||
if size > 0 {
|
||||
match db
|
||||
.check_user_quota(&pubkey_vec, size, free_quota)
|
||||
.await
|
||||
{
|
||||
match db.check_user_quota(&pubkey_vec, size, free_quota).await {
|
||||
Ok(false) => return BlossomResponse::error("Upload would exceed quota"),
|
||||
Err(_) => return BlossomResponse::error("Failed to check quota"),
|
||||
Ok(true) => {} // Quota check passed
|
||||
@ -395,6 +404,7 @@ async fn process_upload(
|
||||
fs,
|
||||
db,
|
||||
settings,
|
||||
None,
|
||||
)
|
||||
.await
|
||||
}
|
||||
@ -409,6 +419,7 @@ async fn process_stream<'p, S>(
|
||||
fs: &State<FileStore>,
|
||||
db: &State<Database>,
|
||||
settings: &State<Settings>,
|
||||
expect_hash: Option<Vec<u8>>,
|
||||
) -> BlossomResponse
|
||||
where
|
||||
S: AsyncRead + Unpin + 'p,
|
||||
@ -417,6 +428,15 @@ where
|
||||
Ok(FileSystemResult::NewFile(blob)) => {
|
||||
let mut ret: FileUpload = (&blob).into();
|
||||
|
||||
// check expected hash (mirroring)
|
||||
if let Some(h) = expect_hash {
|
||||
if h != ret.id {
|
||||
if let Err(e) = tokio::fs::remove_file(fs.get(&ret.id)).await {
|
||||
log::warn!("Failed to cleanup file: {}", e);
|
||||
}
|
||||
return BlossomResponse::error("Mirror request failed, server responses with invalid file content (hash mismatch)");
|
||||
}
|
||||
}
|
||||
// update file data before inserting
|
||||
ret.name = name.map(|s| s.to_string());
|
||||
|
||||
@ -443,8 +463,7 @@ where
|
||||
#[cfg(feature = "payments")]
|
||||
if size == 0 {
|
||||
if let Some(payment_config) = &settings.payments {
|
||||
let free_quota = payment_config.free_quota_bytes
|
||||
.unwrap_or(104857600); // Default to 100MB
|
||||
let free_quota = payment_config.free_quota_bytes.unwrap_or(104857600); // Default to 100MB
|
||||
|
||||
match db.check_user_quota(pubkey, upload.size, free_quota).await {
|
||||
Ok(false) => {
|
||||
@ -465,7 +484,7 @@ where
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Err(e) = db.add_file(&upload, user_id).await {
|
||||
if let Err(e) = db.add_file(&upload, Some(user_id)).await {
|
||||
error!("{}", e);
|
||||
BlossomResponse::error(format!("Error saving file (db): {}", e))
|
||||
} else {
|
||||
|
@ -423,7 +423,8 @@ pub async fn get_blob_thumb(
|
||||
|
||||
if !thumb_file.exists() {
|
||||
let mut p = WebpProcessor::new();
|
||||
if p.thumbnail(&file_path, &thumb_file).is_err() {
|
||||
if let Err(e) = p.thumbnail(&file_path, &thumb_file) {
|
||||
warn!("Failed to generate thumbnail: {}", e);
|
||||
return Err(Status::InternalServerError);
|
||||
}
|
||||
};
|
||||
|
@ -281,7 +281,7 @@ async fn upload(
|
||||
}
|
||||
}
|
||||
|
||||
if let Err(e) = db.add_file(&upload, user_id).await {
|
||||
if let Err(e) = db.add_file(&upload, Some(user_id)).await {
|
||||
error!("{}", e);
|
||||
return Nip96Response::error(&format!("Could not save file (db): {}", e));
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ function App() {
|
||||
return (
|
||||
<Router>
|
||||
<div className="min-h-screen bg-gray-900">
|
||||
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
|
||||
<div className="max-lg:px-6">
|
||||
<Header />
|
||||
<main className="py-8">
|
||||
<Routes>
|
||||
|
370
ui_src/src/components/mirror-suggestions.tsx
Normal file
370
ui_src/src/components/mirror-suggestions.tsx
Normal file
@ -0,0 +1,370 @@
|
||||
import { useState, useEffect } from "react";
|
||||
import { Blossom } from "../upload/blossom";
|
||||
import { FormatBytes } from "../const";
|
||||
import Button from "./button";
|
||||
import usePublisher from "../hooks/publisher";
|
||||
import useLogin from "../hooks/login";
|
||||
|
||||
interface FileMirrorSuggestion {
|
||||
sha256: string;
|
||||
url: string;
|
||||
size: number;
|
||||
mime_type?: string;
|
||||
available_on: string[];
|
||||
missing_from: string[];
|
||||
}
|
||||
|
||||
interface MirrorSuggestionsProps {
|
||||
servers: string[];
|
||||
}
|
||||
|
||||
interface MirrorProgress {
|
||||
total: number;
|
||||
completed: number;
|
||||
failed: number;
|
||||
errors: string[];
|
||||
}
|
||||
|
||||
export default function MirrorSuggestions({ servers }: MirrorSuggestionsProps) {
|
||||
const [suggestions, setSuggestions] = useState<FileMirrorSuggestion[]>([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState<string>();
|
||||
const [mirrorAllProgress, setMirrorAllProgress] = useState<MirrorProgress | null>(null);
|
||||
|
||||
const pub = usePublisher();
|
||||
const login = useLogin();
|
||||
|
||||
useEffect(() => {
|
||||
if (servers.length > 1 && pub && login?.pubkey) {
|
||||
fetchSuggestions();
|
||||
}
|
||||
}, [servers, pub, login?.pubkey]);
|
||||
|
||||
async function fetchSuggestions() {
|
||||
if (!pub || !login?.pubkey) return;
|
||||
|
||||
try {
|
||||
setLoading(true);
|
||||
setError(undefined);
|
||||
|
||||
// Capture the servers list at the start to avoid race conditions
|
||||
const serverList = [...servers];
|
||||
|
||||
if (serverList.length <= 1) {
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const fileMap: Map<string, FileMirrorSuggestion> = new Map();
|
||||
|
||||
// Fetch files from each server
|
||||
for (const serverUrl of serverList) {
|
||||
try {
|
||||
const blossom = new Blossom(serverUrl, pub);
|
||||
const files = await blossom.list(login.pubkey);
|
||||
|
||||
for (const file of files) {
|
||||
const suggestion = fileMap.get(file.sha256);
|
||||
if (suggestion) {
|
||||
suggestion.available_on.push(serverUrl);
|
||||
} else {
|
||||
fileMap.set(file.sha256, {
|
||||
sha256: file.sha256,
|
||||
url: file.url || "",
|
||||
size: file.size,
|
||||
mime_type: file.type,
|
||||
available_on: [serverUrl],
|
||||
missing_from: [],
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(`Failed to fetch files from ${serverUrl}:`, e);
|
||||
// Continue with other servers instead of failing completely
|
||||
}
|
||||
}
|
||||
|
||||
// Determine missing servers for each file using the captured server list
|
||||
for (const suggestion of fileMap.values()) {
|
||||
for (const serverUrl of serverList) {
|
||||
if (!suggestion.available_on.includes(serverUrl)) {
|
||||
suggestion.missing_from.push(serverUrl);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Filter to only files that are missing from at least one server and available on at least one
|
||||
const filteredSuggestions = Array.from(fileMap.values()).filter(
|
||||
s => s.missing_from.length > 0 && s.available_on.length > 0
|
||||
);
|
||||
|
||||
setSuggestions(filteredSuggestions);
|
||||
} catch (e) {
|
||||
if (e instanceof Error) {
|
||||
setError(e.message);
|
||||
} else {
|
||||
setError("Failed to fetch mirror suggestions");
|
||||
}
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function mirrorAll() {
|
||||
if (!pub || suggestions.length === 0) return;
|
||||
|
||||
// Calculate total operations needed
|
||||
const totalOperations = suggestions.reduce((total, suggestion) =>
|
||||
total + suggestion.missing_from.length, 0
|
||||
);
|
||||
|
||||
setMirrorAllProgress({
|
||||
total: totalOperations,
|
||||
completed: 0,
|
||||
failed: 0,
|
||||
errors: []
|
||||
});
|
||||
|
||||
let completed = 0;
|
||||
let failed = 0;
|
||||
const errors: string[] = [];
|
||||
|
||||
// Mirror all files to all missing servers
|
||||
for (const suggestion of suggestions) {
|
||||
for (const targetServer of suggestion.missing_from) {
|
||||
try {
|
||||
const blossom = new Blossom(targetServer, pub);
|
||||
await blossom.mirror(suggestion.url);
|
||||
completed++;
|
||||
|
||||
setMirrorAllProgress(prev => prev ? {
|
||||
...prev,
|
||||
completed: completed,
|
||||
failed: failed
|
||||
} : null);
|
||||
|
||||
// Update suggestions in real-time
|
||||
setSuggestions(prev =>
|
||||
prev.map(s =>
|
||||
s.sha256 === suggestion.sha256
|
||||
? {
|
||||
...s,
|
||||
available_on: [...s.available_on, targetServer],
|
||||
missing_from: s.missing_from.filter(server => server !== targetServer)
|
||||
}
|
||||
: s
|
||||
).filter(s => s.missing_from.length > 0)
|
||||
);
|
||||
} catch (e) {
|
||||
failed++;
|
||||
const errorMessage = e instanceof Error ? e.message : "Unknown error";
|
||||
const serverHost = new URL(targetServer).hostname;
|
||||
errors.push(`${serverHost}: ${errorMessage}`);
|
||||
|
||||
setMirrorAllProgress(prev => prev ? {
|
||||
...prev,
|
||||
completed: completed,
|
||||
failed: failed,
|
||||
errors: [...errors]
|
||||
} : null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Keep progress visible for a moment before clearing
|
||||
setTimeout(() => {
|
||||
setMirrorAllProgress(null);
|
||||
}, 3000);
|
||||
}
|
||||
|
||||
// Calculate coverage statistics
|
||||
const totalFiles = suggestions.length;
|
||||
const totalMirrorOperations = suggestions.reduce((total, suggestion) =>
|
||||
total + suggestion.missing_from.length, 0
|
||||
);
|
||||
const totalSize = suggestions.reduce((total, suggestion) => total + suggestion.size, 0);
|
||||
|
||||
// Calculate coverage per server
|
||||
const serverCoverage = servers.map(serverUrl => {
|
||||
const filesOnServer = suggestions.filter(s => s.available_on.includes(serverUrl)).length;
|
||||
const totalFilesAcrossAllServers = new Set(suggestions.map(s => s.sha256)).size;
|
||||
const coveragePercentage = totalFilesAcrossAllServers > 0 ?
|
||||
Math.round((filesOnServer / totalFilesAcrossAllServers) * 100) : 100;
|
||||
|
||||
return {
|
||||
url: serverUrl,
|
||||
hostname: new URL(serverUrl).hostname,
|
||||
filesCount: filesOnServer,
|
||||
totalFiles: totalFilesAcrossAllServers,
|
||||
coveragePercentage
|
||||
};
|
||||
});
|
||||
|
||||
if (servers.length <= 1) {
|
||||
return null; // No suggestions needed for single server
|
||||
}
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<div className="card">
|
||||
<h3 className="text-lg font-semibold mb-4">Mirror Suggestions</h3>
|
||||
<p className="text-gray-400">Loading mirror suggestions...</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (error) {
|
||||
return (
|
||||
<div className="card">
|
||||
<h3 className="text-lg font-semibold mb-4">Mirror Suggestions</h3>
|
||||
<div className="bg-red-900/20 border border-red-800 text-red-400 px-4 py-3 rounded-lg mb-4">
|
||||
{error}
|
||||
</div>
|
||||
<Button onClick={fetchSuggestions} className="btn-secondary">
|
||||
Retry
|
||||
</Button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (suggestions.length === 0) {
|
||||
return (
|
||||
<div className="card">
|
||||
<h3 className="text-lg font-semibold mb-4">Mirror Suggestions</h3>
|
||||
<p className="text-gray-400">All your files are synchronized across all servers.</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="card">
|
||||
<h3 className="text-lg font-semibold mb-4">Mirror Coverage</h3>
|
||||
|
||||
{/* Coverage Summary */}
|
||||
<div className="bg-gray-800 border border-gray-700 rounded-lg p-4 mb-6">
|
||||
<div className="grid grid-cols-3 gap-4 text-center">
|
||||
<div>
|
||||
<div className="text-2xl font-bold text-blue-400">{totalFiles}</div>
|
||||
<div className="text-xs text-gray-400">Files to Mirror</div>
|
||||
</div>
|
||||
<div>
|
||||
<div className="text-2xl font-bold text-orange-400">{totalMirrorOperations}</div>
|
||||
<div className="text-xs text-gray-400">Operations Needed</div>
|
||||
</div>
|
||||
<div>
|
||||
<div className="text-2xl font-bold text-green-400">{FormatBytes(totalSize)}</div>
|
||||
<div className="text-xs text-gray-400">Total Size</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Server Coverage */}
|
||||
<div className="bg-gray-800 border border-gray-700 rounded-lg p-4 mb-6">
|
||||
<h4 className="text-sm font-semibold text-gray-300 mb-3">Coverage by Server</h4>
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-4">
|
||||
{serverCoverage.map((server) => (
|
||||
<div key={server.url} className="bg-gray-750 border border-gray-600 rounded-lg p-3">
|
||||
<div className="flex items-center justify-between mb-2">
|
||||
<span className="text-sm font-medium text-gray-300 truncate">
|
||||
{server.hostname}
|
||||
</span>
|
||||
<span
|
||||
className={`text-sm font-semibold ${server.coveragePercentage === 100
|
||||
? "text-green-400"
|
||||
: server.coveragePercentage >= 80
|
||||
? "text-yellow-400"
|
||||
: "text-red-400"
|
||||
}`}
|
||||
>
|
||||
{server.coveragePercentage}%
|
||||
</span>
|
||||
</div>
|
||||
<div className="w-full bg-gray-700 rounded-full h-2 mb-2">
|
||||
<div
|
||||
className={`h-2 rounded-full transition-all duration-300 ${server.coveragePercentage === 100
|
||||
? "bg-green-500"
|
||||
: server.coveragePercentage >= 80
|
||||
? "bg-yellow-500"
|
||||
: "bg-red-500"
|
||||
}`}
|
||||
style={{
|
||||
width: `${server.coveragePercentage}%`,
|
||||
}}
|
||||
></div>
|
||||
</div>
|
||||
<div className="text-xs text-gray-400 text-center">
|
||||
{server.filesCount} / {server.totalFiles} files
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Mirror All Section */}
|
||||
{!mirrorAllProgress ? (
|
||||
<div className="text-center">
|
||||
<p className="text-gray-400 mb-4">
|
||||
{totalFiles} files need to be synchronized across your servers
|
||||
</p>
|
||||
<Button
|
||||
onClick={mirrorAll}
|
||||
className="btn-primary"
|
||||
disabled={totalMirrorOperations === 0}
|
||||
>
|
||||
Mirror Everything
|
||||
</Button>
|
||||
</div>
|
||||
) : (
|
||||
<div className="space-y-4">
|
||||
{/* Progress Bar */}
|
||||
<div>
|
||||
<div className="flex justify-between text-sm mb-2">
|
||||
<span className="text-gray-400">Progress</span>
|
||||
<span className="text-gray-400">
|
||||
{mirrorAllProgress.completed + mirrorAllProgress.failed} / {mirrorAllProgress.total}
|
||||
</span>
|
||||
</div>
|
||||
<div className="w-full bg-gray-700 rounded-full h-2">
|
||||
<div
|
||||
className="bg-blue-500 h-2 rounded-full transition-all duration-300"
|
||||
style={{
|
||||
width: `${((mirrorAllProgress.completed + mirrorAllProgress.failed) / mirrorAllProgress.total) * 100}%`
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Status Summary */}
|
||||
<div className="grid grid-cols-3 gap-4 text-center text-sm">
|
||||
<div>
|
||||
<div className="text-green-400 font-semibold">{mirrorAllProgress.completed}</div>
|
||||
<div className="text-gray-400">Completed</div>
|
||||
</div>
|
||||
<div>
|
||||
<div className="text-red-400 font-semibold">{mirrorAllProgress.failed}</div>
|
||||
<div className="text-gray-400">Failed</div>
|
||||
</div>
|
||||
<div>
|
||||
<div className="text-gray-400 font-semibold">
|
||||
{mirrorAllProgress.total - mirrorAllProgress.completed - mirrorAllProgress.failed}
|
||||
</div>
|
||||
<div className="text-gray-400">Remaining</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Errors */}
|
||||
{mirrorAllProgress.errors.length > 0 && (
|
||||
<div className="bg-red-900/20 border border-red-800 rounded-lg p-3">
|
||||
<h4 className="text-red-400 font-semibold mb-2">Errors ({mirrorAllProgress.errors.length})</h4>
|
||||
<div className="space-y-1 max-h-32 overflow-y-auto">
|
||||
{mirrorAllProgress.errors.map((error, index) => (
|
||||
<div key={index} className="text-red-300 text-xs">{error}</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
62
ui_src/src/components/progress-bar.tsx
Normal file
62
ui_src/src/components/progress-bar.tsx
Normal file
@ -0,0 +1,62 @@
|
||||
import { UploadProgress, formatSpeed, formatTime } from "../upload/progress";
|
||||
import { FormatBytes } from "../const";
|
||||
|
||||
interface ProgressBarProps {
|
||||
progress: UploadProgress;
|
||||
fileName?: string;
|
||||
}
|
||||
|
||||
export default function ProgressBar({ progress, fileName }: ProgressBarProps) {
|
||||
const {
|
||||
percentage,
|
||||
bytesUploaded,
|
||||
totalBytes,
|
||||
averageSpeed,
|
||||
estimatedTimeRemaining,
|
||||
} = progress;
|
||||
|
||||
return (
|
||||
<div className="bg-gray-800 border border-gray-700 rounded-lg p-4">
|
||||
<div className="flex items-center justify-between mb-2">
|
||||
<h4 className="font-medium text-blue-400">
|
||||
{fileName ? `Uploading ${fileName}` : "Uploading..."}
|
||||
</h4>
|
||||
<span className="text-sm text-gray-400">
|
||||
{percentage.toFixed(1)}%
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{/* Progress Bar */}
|
||||
<div className="w-full bg-gray-700 rounded-full h-2.5 mb-3">
|
||||
<div
|
||||
className="bg-blue-500 h-2.5 rounded-full transition-all duration-300"
|
||||
style={{ width: `${Math.min(100, percentage)}%` }}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Upload Stats */}
|
||||
<div className="grid grid-cols-1 md:grid-cols-3 gap-3 text-sm">
|
||||
<div>
|
||||
<span className="text-gray-400">Progress:</span>
|
||||
<span className="ml-2 font-medium">
|
||||
{FormatBytes(bytesUploaded)} / {FormatBytes(totalBytes)}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<span className="text-gray-400">Speed:</span>
|
||||
<span className="ml-2 font-medium text-green-400">
|
||||
{formatSpeed(averageSpeed)}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<span className="text-gray-400">ETA:</span>
|
||||
<span className="ml-2 font-medium">
|
||||
{formatTime(estimatedTimeRemaining)}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
@ -43,3 +43,6 @@ export function FormatBytes(b: number, f?: number) {
|
||||
if (b >= kiB) return (b / kiB).toFixed(f) + " KiB";
|
||||
return b.toFixed(f) + " B";
|
||||
}
|
||||
|
||||
export const ServerUrl =
|
||||
import.meta.env.VITE_API_URL || `${location.protocol}//${location.host}`;
|
25
ui_src/src/hooks/use-blossom-servers.ts
Normal file
25
ui_src/src/hooks/use-blossom-servers.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import useLogin from "./login";
|
||||
import { useRequestBuilder } from "@snort/system-react";
|
||||
import { EventKind, RequestBuilder } from "@snort/system";
|
||||
import { dedupe, removeUndefined, sanitizeRelayUrl } from "@snort/shared";
|
||||
import { ServerUrl } from "../const";
|
||||
|
||||
const DefaultMediaServers = ["https://blossom.band/", "https://blossom.primal.net", ServerUrl]
|
||||
|
||||
export function useBlossomServers() {
|
||||
const login = useLogin();
|
||||
|
||||
const rb = new RequestBuilder("media-servers");
|
||||
if (login?.pubkey) {
|
||||
rb.withFilter()
|
||||
.kinds([10_063 as EventKind])
|
||||
.authors([login.pubkey]);
|
||||
}
|
||||
const req = useRequestBuilder(rb);
|
||||
|
||||
const servers = req === undefined ? undefined :
|
||||
req
|
||||
.flatMap((e) => e.tags.filter(t => t[0] === "server")
|
||||
.map((t) => t[1]));
|
||||
return dedupe(removeUndefined([...DefaultMediaServers, ...(servers ?? [])].map(sanitizeRelayUrl)));
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
import { base64, bytesToString } from "@scure/base";
|
||||
import { throwIfOffline, unixNow } from "@snort/shared";
|
||||
import { EventKind, EventPublisher } from "@snort/system";
|
||||
import { UploadProgressCallback, uploadWithProgress } from "./progress";
|
||||
|
||||
export interface BlobDescriptor {
|
||||
url?: string;
|
||||
@ -28,14 +29,14 @@ export class Blossom {
|
||||
}
|
||||
}
|
||||
|
||||
async upload(file: File): Promise<BlobDescriptor> {
|
||||
async upload(file: File, onProgress?: UploadProgressCallback): Promise<BlobDescriptor> {
|
||||
const hash = await window.crypto.subtle.digest(
|
||||
"SHA-256",
|
||||
await file.arrayBuffer(),
|
||||
);
|
||||
const tags = [["x", bytesToString("hex", new Uint8Array(hash))]];
|
||||
|
||||
const rsp = await this.#req("upload", "PUT", "upload", file, tags);
|
||||
const rsp = await this.#req("upload", "PUT", "upload", file, tags, undefined, onProgress);
|
||||
if (rsp.ok) {
|
||||
return (await rsp.json()) as BlobDescriptor;
|
||||
} else {
|
||||
@ -44,14 +45,14 @@ export class Blossom {
|
||||
}
|
||||
}
|
||||
|
||||
async media(file: File): Promise<BlobDescriptor> {
|
||||
async media(file: File, onProgress?: UploadProgressCallback): Promise<BlobDescriptor> {
|
||||
const hash = await window.crypto.subtle.digest(
|
||||
"SHA-256",
|
||||
await file.arrayBuffer(),
|
||||
);
|
||||
const tags = [["x", bytesToString("hex", new Uint8Array(hash))]];
|
||||
|
||||
const rsp = await this.#req("media", "PUT", "media", file, tags);
|
||||
const rsp = await this.#req("media", "PUT", "media", file, tags, undefined, onProgress);
|
||||
if (rsp.ok) {
|
||||
return (await rsp.json()) as BlobDescriptor;
|
||||
} else {
|
||||
@ -64,7 +65,7 @@ export class Blossom {
|
||||
const rsp = await this.#req(
|
||||
"mirror",
|
||||
"PUT",
|
||||
"mirror",
|
||||
"upload",
|
||||
JSON.stringify({ url }),
|
||||
undefined,
|
||||
{
|
||||
@ -106,6 +107,7 @@ export class Blossom {
|
||||
body?: BodyInit,
|
||||
tags?: Array<Array<string>>,
|
||||
headers?: Record<string, string>,
|
||||
onProgress?: UploadProgressCallback,
|
||||
) {
|
||||
throwIfOffline();
|
||||
|
||||
@ -126,14 +128,22 @@ export class Blossom {
|
||||
)}`;
|
||||
};
|
||||
|
||||
return await fetch(url, {
|
||||
method,
|
||||
body,
|
||||
headers: {
|
||||
const requestHeaders = {
|
||||
...headers,
|
||||
accept: "application/json",
|
||||
authorization: await auth(url, method),
|
||||
},
|
||||
};
|
||||
|
||||
// Use progress-enabled upload for PUT requests with body
|
||||
if (method === "PUT" && body && onProgress) {
|
||||
return await uploadWithProgress(url, method, body, requestHeaders, onProgress);
|
||||
}
|
||||
|
||||
// Fall back to regular fetch for other requests
|
||||
return await fetch(url, {
|
||||
method,
|
||||
body,
|
||||
headers: requestHeaders,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ export async function openFile(): Promise<File | undefined> {
|
||||
const elm = document.createElement("input");
|
||||
let lock = false;
|
||||
elm.type = "file";
|
||||
elm.multiple = true; // Allow multiple file selection
|
||||
const handleInput = (e: Event) => {
|
||||
lock = true;
|
||||
const elm = e.target as HTMLInputElement;
|
||||
@ -28,3 +29,35 @@ export async function openFile(): Promise<File | undefined> {
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
export async function openFiles(): Promise<FileList | undefined> {
|
||||
return new Promise((resolve) => {
|
||||
const elm = document.createElement("input");
|
||||
let lock = false;
|
||||
elm.type = "file";
|
||||
elm.multiple = true;
|
||||
const handleInput = (e: Event) => {
|
||||
lock = true;
|
||||
const elm = e.target as HTMLInputElement;
|
||||
if ((elm.files?.length ?? 0) > 0) {
|
||||
resolve(elm.files!);
|
||||
} else {
|
||||
resolve(undefined);
|
||||
}
|
||||
};
|
||||
|
||||
elm.onchange = (e) => handleInput(e);
|
||||
elm.click();
|
||||
window.addEventListener(
|
||||
"focus",
|
||||
() => {
|
||||
setTimeout(() => {
|
||||
if (!lock) {
|
||||
resolve(undefined);
|
||||
}
|
||||
}, 300);
|
||||
},
|
||||
{ once: true },
|
||||
);
|
||||
});
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
import { base64 } from "@scure/base";
|
||||
import { throwIfOffline } from "@snort/shared";
|
||||
import { EventKind, EventPublisher, NostrEvent } from "@snort/system";
|
||||
import { UploadProgressCallback, uploadWithProgress } from "./progress";
|
||||
|
||||
export class Nip96 {
|
||||
#info?: Nip96Info;
|
||||
@ -28,14 +29,14 @@ export class Nip96 {
|
||||
return data;
|
||||
}
|
||||
|
||||
async upload(file: File) {
|
||||
async upload(file: File, onProgress?: UploadProgressCallback) {
|
||||
const fd = new FormData();
|
||||
fd.append("size", file.size.toString());
|
||||
fd.append("caption", file.name);
|
||||
fd.append("content_type", file.type);
|
||||
fd.append("file", file);
|
||||
|
||||
const rsp = await this.#req("", "POST", fd);
|
||||
const rsp = await this.#req("", "POST", fd, onProgress);
|
||||
const data = await this.#handleResponse<Nip96Result>(rsp);
|
||||
if (data.status !== "success") {
|
||||
throw new Error(data.message);
|
||||
@ -57,7 +58,7 @@ export class Nip96 {
|
||||
}
|
||||
}
|
||||
|
||||
async #req(path: string, method: "GET" | "POST" | "DELETE", body?: BodyInit) {
|
||||
async #req(path: string, method: "GET" | "POST" | "DELETE", body?: BodyInit, onProgress?: UploadProgressCallback) {
|
||||
throwIfOffline();
|
||||
const auth = async (url: string, method: string) => {
|
||||
const auth = await this.publisher.generic((eb) => {
|
||||
@ -77,13 +78,22 @@ export class Nip96 {
|
||||
u = `${this.url}${u.slice(1)}`;
|
||||
}
|
||||
u += path;
|
||||
|
||||
const requestHeaders = {
|
||||
accept: "application/json",
|
||||
authorization: await auth(u, method),
|
||||
};
|
||||
|
||||
// Use progress-enabled upload for POST requests with FormData
|
||||
if (method === "POST" && body && onProgress) {
|
||||
return await uploadWithProgress(u, method, body, requestHeaders, onProgress);
|
||||
}
|
||||
|
||||
// Fall back to regular fetch for other requests
|
||||
return await fetch(u, {
|
||||
method,
|
||||
body,
|
||||
headers: {
|
||||
accept: "application/json",
|
||||
authorization: await auth(u, method),
|
||||
},
|
||||
headers: requestHeaders,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
184
ui_src/src/upload/progress.ts
Normal file
184
ui_src/src/upload/progress.ts
Normal file
@ -0,0 +1,184 @@
|
||||
// Upload progress tracking types and utilities
|
||||
|
||||
export interface UploadProgress {
|
||||
percentage: number;
|
||||
bytesUploaded: number;
|
||||
totalBytes: number;
|
||||
averageSpeed: number; // bytes per second
|
||||
estimatedTimeRemaining: number; // seconds
|
||||
startTime: number;
|
||||
}
|
||||
|
||||
export interface UploadProgressCallback {
|
||||
(progress: UploadProgress): void;
|
||||
}
|
||||
|
||||
export class ProgressTracker {
|
||||
private startTime: number;
|
||||
private lastUpdateTime: number;
|
||||
private bytesUploaded: number = 0;
|
||||
private totalBytes: number;
|
||||
private speedSamples: number[] = [];
|
||||
private maxSamples = 10; // Keep last 10 speed samples for averaging
|
||||
|
||||
constructor(totalBytes: number) {
|
||||
this.totalBytes = totalBytes;
|
||||
this.startTime = Date.now();
|
||||
this.lastUpdateTime = this.startTime;
|
||||
}
|
||||
|
||||
update(bytesUploaded: number): UploadProgress {
|
||||
const now = Date.now();
|
||||
const timeDiff = now - this.lastUpdateTime;
|
||||
|
||||
// Calculate instantaneous speed
|
||||
if (timeDiff > 0) {
|
||||
const bytesDiff = bytesUploaded - this.bytesUploaded;
|
||||
const instantSpeed = (bytesDiff / timeDiff) * 1000; // bytes per second
|
||||
|
||||
// Keep a rolling average of speed samples
|
||||
this.speedSamples.push(instantSpeed);
|
||||
if (this.speedSamples.length > this.maxSamples) {
|
||||
this.speedSamples.shift();
|
||||
}
|
||||
}
|
||||
|
||||
this.bytesUploaded = bytesUploaded;
|
||||
this.lastUpdateTime = now;
|
||||
|
||||
// Calculate average speed
|
||||
const averageSpeed = this.speedSamples.length > 0
|
||||
? this.speedSamples.reduce((sum, speed) => sum + speed, 0) / this.speedSamples.length
|
||||
: 0;
|
||||
|
||||
// Calculate estimated time remaining
|
||||
const remainingBytes = this.totalBytes - bytesUploaded;
|
||||
const estimatedTimeRemaining = averageSpeed > 0 ? remainingBytes / averageSpeed : 0;
|
||||
|
||||
return {
|
||||
percentage: (bytesUploaded / this.totalBytes) * 100,
|
||||
bytesUploaded,
|
||||
totalBytes: this.totalBytes,
|
||||
averageSpeed,
|
||||
estimatedTimeRemaining,
|
||||
startTime: this.startTime,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Utility function to format speed for display
|
||||
export function formatSpeed(bytesPerSecond: number): string {
|
||||
if (bytesPerSecond === 0) return "0 B/s";
|
||||
|
||||
const units = ["B/s", "KB/s", "MB/s", "GB/s"];
|
||||
let value = bytesPerSecond;
|
||||
let unitIndex = 0;
|
||||
|
||||
while (value >= 1024 && unitIndex < units.length - 1) {
|
||||
value /= 1024;
|
||||
unitIndex++;
|
||||
}
|
||||
|
||||
return `${value.toFixed(1)} ${units[unitIndex]}`;
|
||||
}
|
||||
|
||||
// Utility function to format time for display
|
||||
export function formatTime(seconds: number): string {
|
||||
if (seconds === 0 || !isFinite(seconds)) return "--";
|
||||
|
||||
const minutes = Math.floor(seconds / 60);
|
||||
const remainingSeconds = Math.floor(seconds % 60);
|
||||
|
||||
if (minutes > 0) {
|
||||
return `${minutes}m ${remainingSeconds}s`;
|
||||
} else {
|
||||
return `${remainingSeconds}s`;
|
||||
}
|
||||
}
|
||||
|
||||
// XMLHttpRequest wrapper with progress tracking
|
||||
export function uploadWithProgress(
|
||||
url: string,
|
||||
method: string,
|
||||
body: BodyInit | null,
|
||||
headers: Record<string, string>,
|
||||
onProgress?: UploadProgressCallback,
|
||||
): Promise<Response> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const xhr = new XMLHttpRequest();
|
||||
|
||||
// Determine total size
|
||||
let totalSize = 0;
|
||||
if (body instanceof File) {
|
||||
totalSize = body.size;
|
||||
} else if (body instanceof FormData) {
|
||||
// For FormData, we need to estimate size
|
||||
const formData = body as FormData;
|
||||
for (const [, value] of formData.entries()) {
|
||||
if (value instanceof File) {
|
||||
totalSize += value.size;
|
||||
} else if (typeof value === 'string') {
|
||||
totalSize += new Blob([value]).size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const tracker = new ProgressTracker(totalSize);
|
||||
|
||||
// Set up progress tracking
|
||||
if (onProgress && totalSize > 0) {
|
||||
xhr.upload.addEventListener('progress', (event) => {
|
||||
if (event.lengthComputable) {
|
||||
const progress = tracker.update(event.loaded);
|
||||
onProgress(progress);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Set up response handling
|
||||
xhr.addEventListener('load', () => {
|
||||
const response = new Response(xhr.response, {
|
||||
status: xhr.status,
|
||||
statusText: xhr.statusText,
|
||||
headers: parseHeaders(xhr.getAllResponseHeaders()),
|
||||
});
|
||||
resolve(response);
|
||||
});
|
||||
|
||||
xhr.addEventListener('error', () => {
|
||||
reject(new Error('Network error'));
|
||||
});
|
||||
|
||||
xhr.addEventListener('abort', () => {
|
||||
reject(new Error('Upload aborted'));
|
||||
});
|
||||
|
||||
// Configure request
|
||||
xhr.open(method, url);
|
||||
|
||||
// Set headers
|
||||
for (const [key, value] of Object.entries(headers)) {
|
||||
xhr.setRequestHeader(key, value);
|
||||
}
|
||||
|
||||
// Send request
|
||||
xhr.send(body as XMLHttpRequestBodyInit | Document | null);
|
||||
});
|
||||
}
|
||||
|
||||
// Helper function to parse response headers
|
||||
function parseHeaders(headerString: string): Headers {
|
||||
const headers = new Headers();
|
||||
const lines = headerString.trim().split('\r\n');
|
||||
|
||||
for (const line of lines) {
|
||||
const colonIndex = line.indexOf(':');
|
||||
if (colonIndex > 0) {
|
||||
const name = line.substring(0, colonIndex).trim();
|
||||
const value = line.substring(colonIndex + 1).trim();
|
||||
headers.append(name, value);
|
||||
}
|
||||
}
|
||||
|
||||
return headers;
|
||||
}
|
@ -122,16 +122,16 @@ export default function Admin() {
|
||||
}, [pub, self, url]);
|
||||
|
||||
useEffect(() => {
|
||||
if (pub && self?.is_admin && !adminListedFiles) {
|
||||
if (pub && self?.is_admin) {
|
||||
listAllUploads(adminListedPage);
|
||||
}
|
||||
}, [adminListedPage, pub, self?.is_admin, listAllUploads, adminListedFiles]);
|
||||
}, [adminListedPage, pub, self?.is_admin, listAllUploads]);
|
||||
|
||||
useEffect(() => {
|
||||
if (pub && self?.is_admin && !reports) {
|
||||
if (pub && self?.is_admin) {
|
||||
listReports(reportPage);
|
||||
}
|
||||
}, [reportPage, pub, self?.is_admin, listReports, reports]);
|
||||
}, [reportPage, pub, self?.is_admin, listReports]);
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
|
@ -43,7 +43,7 @@ export default function Header() {
|
||||
|
||||
return (
|
||||
<header className="border-b border-gray-700 bg-gray-800 w-full">
|
||||
<div className="px-4 sm:px-6 lg:px-8 flex justify-between items-center py-4">
|
||||
<div className="px-4 flex justify-between items-center py-4">
|
||||
<div className="flex items-center space-x-8">
|
||||
<Link to="/">
|
||||
<div className="text-2xl font-bold text-gray-100 hover:text-blue-400 transition-colors">
|
||||
|
@ -2,48 +2,60 @@ import { useEffect, useState, useCallback } from "react";
|
||||
import Button from "../components/button";
|
||||
import FileList from "./files";
|
||||
import PaymentFlow from "../components/payment";
|
||||
import { openFile } from "../upload";
|
||||
import { Blossom } from "../upload/blossom";
|
||||
import ProgressBar from "../components/progress-bar";
|
||||
import MirrorSuggestions from "../components/mirror-suggestions";
|
||||
import { useBlossomServers } from "../hooks/use-blossom-servers";
|
||||
import { openFiles } from "../upload";
|
||||
import { Blossom, BlobDescriptor } from "../upload/blossom";
|
||||
import useLogin from "../hooks/login";
|
||||
import usePublisher from "../hooks/publisher";
|
||||
import { Nip96, Nip96FileList } from "../upload/nip96";
|
||||
import { AdminSelf, Route96 } from "../upload/admin";
|
||||
import { FormatBytes } from "../const";
|
||||
import { FormatBytes, ServerUrl } from "../const";
|
||||
import { UploadProgress } from "../upload/progress";
|
||||
|
||||
export default function Upload() {
|
||||
const [type, setType] = useState<"blossom" | "nip96">("blossom");
|
||||
const [noCompress, setNoCompress] = useState(false);
|
||||
const [toUpload, setToUpload] = useState<File>();
|
||||
const [stripMetadata, setStripMetadata] = useState(true);
|
||||
const [self, setSelf] = useState<AdminSelf>();
|
||||
const [error, setError] = useState<string>();
|
||||
const [results, setResults] = useState<Array<object>>([]);
|
||||
const [results, setResults] = useState<Array<BlobDescriptor>>([]);
|
||||
const [listedFiles, setListedFiles] = useState<Nip96FileList>();
|
||||
const [listedPage, setListedPage] = useState(0);
|
||||
const [showPaymentFlow, setShowPaymentFlow] = useState(false);
|
||||
const [isUploading, setIsUploading] = useState(false);
|
||||
const [uploadProgress, setUploadProgress] = useState<UploadProgress>();
|
||||
|
||||
const blossomServers = useBlossomServers();
|
||||
|
||||
const login = useLogin();
|
||||
const pub = usePublisher();
|
||||
|
||||
const url =
|
||||
import.meta.env.VITE_API_URL || `${location.protocol}//${location.host}`;
|
||||
async function doUpload() {
|
||||
// Check if file should have compression enabled by default
|
||||
const shouldCompress = (file: File) => {
|
||||
return file.type.startsWith('video/') || file.type.startsWith('image/');
|
||||
};
|
||||
|
||||
async function doUpload(file: File) {
|
||||
if (!pub) return;
|
||||
if (!toUpload) return;
|
||||
if (!file) return;
|
||||
if (isUploading) return; // Prevent multiple uploads
|
||||
|
||||
try {
|
||||
setError(undefined);
|
||||
if (type === "blossom") {
|
||||
const uploader = new Blossom(url, pub);
|
||||
const result = noCompress
|
||||
? await uploader.upload(toUpload)
|
||||
: await uploader.media(toUpload);
|
||||
setIsUploading(true);
|
||||
setUploadProgress(undefined);
|
||||
|
||||
const onProgress = (progress: UploadProgress) => {
|
||||
setUploadProgress(progress);
|
||||
};
|
||||
|
||||
const uploader = new Blossom(ServerUrl, pub);
|
||||
// Use compression for video and image files when metadata stripping is enabled
|
||||
const useCompression = shouldCompress(file) && stripMetadata;
|
||||
const result = useCompression
|
||||
? await uploader.media(file, onProgress)
|
||||
: await uploader.upload(file, onProgress);
|
||||
setResults((s) => [...s, result]);
|
||||
}
|
||||
if (type === "nip96") {
|
||||
const uploader = new Nip96(url, pub);
|
||||
await uploader.loadInfo();
|
||||
const result = await uploader.upload(toUpload);
|
||||
setResults((s) => [...s, result]);
|
||||
}
|
||||
} catch (e) {
|
||||
if (e instanceof Error) {
|
||||
setError(e.message || "Upload failed - no error details provided");
|
||||
@ -52,6 +64,30 @@ export default function Upload() {
|
||||
} else {
|
||||
setError("Upload failed");
|
||||
}
|
||||
} finally {
|
||||
setIsUploading(false);
|
||||
setUploadProgress(undefined);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleFileSelection() {
|
||||
if (isUploading) return;
|
||||
|
||||
try {
|
||||
const files = await openFiles();
|
||||
if (!files || files.length === 0) return;
|
||||
|
||||
// Start uploading each file immediately
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
const file = files[i];
|
||||
await doUpload(file);
|
||||
}
|
||||
} catch (e) {
|
||||
if (e instanceof Error) {
|
||||
setError(e.message || "File selection failed");
|
||||
} else {
|
||||
setError("File selection failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -60,7 +96,7 @@ export default function Upload() {
|
||||
if (!pub) return;
|
||||
try {
|
||||
setError(undefined);
|
||||
const uploader = new Nip96(url, pub);
|
||||
const uploader = new Nip96(ServerUrl, pub);
|
||||
await uploader.loadInfo();
|
||||
const result = await uploader.listFiles(n, 50);
|
||||
setListedFiles(result);
|
||||
@ -76,14 +112,14 @@ export default function Upload() {
|
||||
}
|
||||
}
|
||||
},
|
||||
[pub, url],
|
||||
[pub],
|
||||
);
|
||||
|
||||
async function deleteFile(id: string) {
|
||||
if (!pub) return;
|
||||
try {
|
||||
setError(undefined);
|
||||
const uploader = new Blossom(url, pub);
|
||||
const uploader = new Blossom(ServerUrl, pub);
|
||||
await uploader.delete(id);
|
||||
} catch (e) {
|
||||
if (e instanceof Error) {
|
||||
@ -104,10 +140,10 @@ export default function Upload() {
|
||||
|
||||
useEffect(() => {
|
||||
if (pub && !self) {
|
||||
const r96 = new Route96(url, pub);
|
||||
const r96 = new Route96(ServerUrl, pub);
|
||||
r96.getSelf().then((v) => setSelf(v.data));
|
||||
}
|
||||
}, [pub, self, url]);
|
||||
}, [pub, self]);
|
||||
|
||||
if (!login) {
|
||||
return (
|
||||
@ -123,94 +159,57 @@ export default function Upload() {
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="max-w-4xl mx-auto space-y-8">
|
||||
<div className="w-full px-4">
|
||||
{error && (
|
||||
<div className="bg-red-900/20 border border-red-800 text-red-400 px-4 py-3 rounded-lg">
|
||||
<div className="bg-red-900/20 border border-red-800 text-red-400 px-4 py-3 rounded-lg mb-6">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="card">
|
||||
<h2 className="text-xl font-semibold mb-6">Upload Settings</h2>
|
||||
<div className="flex flex-wrap gap-6">
|
||||
{/* Upload Widget */}
|
||||
<div className="card flex-1 min-w-80">
|
||||
<h2 className="text-xl font-semibold mb-6">Upload Files</h2>
|
||||
|
||||
<div className="space-y-6">
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-300 mb-3">
|
||||
Upload Method
|
||||
</label>
|
||||
<div className="flex gap-6">
|
||||
<label className="flex items-center cursor-pointer">
|
||||
<input
|
||||
type="radio"
|
||||
checked={type === "blossom"}
|
||||
onChange={() => setType("blossom")}
|
||||
className="mr-2"
|
||||
/>
|
||||
<span className="text-sm font-medium text-gray-300">
|
||||
Blossom
|
||||
</span>
|
||||
</label>
|
||||
<label className="flex items-center cursor-pointer">
|
||||
<input
|
||||
type="radio"
|
||||
checked={type === "nip96"}
|
||||
onChange={() => setType("nip96")}
|
||||
className="mr-2"
|
||||
/>
|
||||
<span className="text-sm font-medium text-gray-300">
|
||||
NIP-96
|
||||
</span>
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="flex items-center cursor-pointer">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={noCompress}
|
||||
onChange={(e) => setNoCompress(e.target.checked)}
|
||||
checked={stripMetadata}
|
||||
onChange={(e) => setStripMetadata(e.target.checked)}
|
||||
className="mr-2"
|
||||
/>
|
||||
<span className="text-sm font-medium text-gray-300">
|
||||
Disable Compression
|
||||
Strip metadata (for images)
|
||||
</span>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
{toUpload && (
|
||||
<div className="border-2 border-dashed border-gray-600 rounded-lg p-4">
|
||||
<FileList files={[toUpload]} />
|
||||
</div>
|
||||
{/* Upload Progress */}
|
||||
{isUploading && uploadProgress && (
|
||||
<ProgressBar
|
||||
progress={uploadProgress}
|
||||
/>
|
||||
)}
|
||||
|
||||
<div className="flex gap-4">
|
||||
<Button
|
||||
onClick={async () => {
|
||||
const f = await openFile();
|
||||
setToUpload(f);
|
||||
}}
|
||||
className="btn-secondary flex-1"
|
||||
>
|
||||
Choose File
|
||||
</Button>
|
||||
<Button
|
||||
onClick={doUpload}
|
||||
disabled={!toUpload}
|
||||
onClick={handleFileSelection}
|
||||
className="btn-primary flex-1"
|
||||
disabled={isUploading}
|
||||
>
|
||||
Upload
|
||||
{isUploading ? "Uploading..." : "Select Files to Upload"}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Storage Usage Widget */}
|
||||
{self && (
|
||||
<div className="card max-w-2xl mx-auto">
|
||||
<h3 className="text-lg font-semibold mb-4">Storage Quota</h3>
|
||||
<div className="card flex-1 min-w-80">
|
||||
<h3 className="text-lg font-semibold mb-4">Storage Usage</h3>
|
||||
<div className="space-y-4">
|
||||
{self.total_available_quota && self.total_available_quota > 0 && (
|
||||
<>
|
||||
{/* File Count */}
|
||||
<div className="flex justify-between text-sm">
|
||||
<span>Files:</span>
|
||||
@ -219,10 +218,21 @@ export default function Upload() {
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{/* Total Usage */}
|
||||
<div className="flex justify-between text-sm">
|
||||
<span>Total Size:</span>
|
||||
<span className="font-medium">
|
||||
{FormatBytes(self.total_size)}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{/* Only show quota information if available */}
|
||||
{self.total_available_quota && self.total_available_quota > 0 && (
|
||||
<>
|
||||
{/* Progress Bar */}
|
||||
<div className="space-y-2">
|
||||
<div className="flex justify-between text-sm">
|
||||
<span>Used:</span>
|
||||
<span>Quota Used:</span>
|
||||
<span className="font-medium">
|
||||
{FormatBytes(self.total_size)} of{" "}
|
||||
{FormatBytes(self.total_available_quota)}
|
||||
@ -230,8 +240,7 @@ export default function Upload() {
|
||||
</div>
|
||||
<div className="w-full bg-gray-700 rounded-full h-2.5">
|
||||
<div
|
||||
className={`h-2.5 rounded-full transition-all duration-300 ${
|
||||
self.total_size / self.total_available_quota > 0.8
|
||||
className={`h-2.5 rounded-full transition-all duration-300 ${self.total_size / self.total_available_quota > 0.8
|
||||
? "bg-red-500"
|
||||
: self.total_size / self.total_available_quota > 0.6
|
||||
? "bg-yellow-500"
|
||||
@ -251,8 +260,7 @@ export default function Upload() {
|
||||
% used
|
||||
</span>
|
||||
<span
|
||||
className={`${
|
||||
self.total_size / self.total_available_quota > 0.8
|
||||
className={`${self.total_size / self.total_available_quota > 0.8
|
||||
? "text-red-400"
|
||||
: self.total_size / self.total_available_quota > 0.6
|
||||
? "text-yellow-400"
|
||||
@ -270,16 +278,8 @@ export default function Upload() {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Quota Breakdown */}
|
||||
{/* Quota Breakdown - excluding free quota */}
|
||||
<div className="space-y-2 pt-2 border-t border-gray-700">
|
||||
{self.free_quota && self.free_quota > 0 && (
|
||||
<div className="flex justify-between text-sm">
|
||||
<span>Free Quota:</span>
|
||||
<span className="font-medium">
|
||||
{FormatBytes(self.free_quota)}
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
{(self.quota ?? 0) > 0 && (
|
||||
<div className="flex justify-between text-sm">
|
||||
<span>Paid Quota:</span>
|
||||
@ -317,16 +317,6 @@ export default function Upload() {
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
|
||||
{(!self.total_available_quota ||
|
||||
self.total_available_quota === 0) && (
|
||||
<div className="text-center py-4 text-gray-400">
|
||||
<p>No quota information available</p>
|
||||
<p className="text-sm">
|
||||
Contact administrator for storage access
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<Button
|
||||
onClick={() => setShowPaymentFlow(!showPaymentFlow)}
|
||||
@ -337,10 +327,11 @@ export default function Upload() {
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Payment Flow Widget */}
|
||||
{showPaymentFlow && pub && (
|
||||
<div className="card">
|
||||
<div className="card flex-1 min-w-80">
|
||||
<PaymentFlow
|
||||
route96={new Route96(url, pub)}
|
||||
route96={new Route96(ServerUrl, pub)}
|
||||
onPaymentRequested={(pr) => {
|
||||
console.log("Payment requested:", pr);
|
||||
}}
|
||||
@ -349,7 +340,17 @@ export default function Upload() {
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="card">
|
||||
{/* Mirror Suggestions Widget */}
|
||||
{blossomServers && blossomServers.length > 1 && (
|
||||
<div className="w-full">
|
||||
<MirrorSuggestions
|
||||
servers={blossomServers}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Files Widget */}
|
||||
<div className="card w-full">
|
||||
<div className="flex justify-between items-center mb-6">
|
||||
<h2 className="text-xl font-semibold">Your Files</h2>
|
||||
{!listedFiles && (
|
||||
@ -373,11 +374,12 @@ export default function Upload() {
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Upload Results Widget */}
|
||||
{results.length > 0 && (
|
||||
<div className="card">
|
||||
<div className="card w-full">
|
||||
<h3 className="text-lg font-semibold mb-4">Upload Results</h3>
|
||||
<div className="space-y-4">
|
||||
{results.map((result: any, index) => (
|
||||
{results.map((result, index) => (
|
||||
<div
|
||||
key={index}
|
||||
className="bg-gray-800 border border-gray-700 rounded-lg p-4"
|
||||
@ -407,21 +409,10 @@ export default function Upload() {
|
||||
{FormatBytes(result.size || 0)}
|
||||
</p>
|
||||
</div>
|
||||
{result.nip94?.find((tag: any[]) => tag[0] === "dim") && (
|
||||
<div>
|
||||
<p className="text-sm text-gray-400">Dimensions</p>
|
||||
<p className="font-medium">
|
||||
{
|
||||
result.nip94.find(
|
||||
(tag: any[]) => tag[0] === "dim",
|
||||
)?.[1]
|
||||
}
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
{result.url && (
|
||||
<div>
|
||||
<p className="text-sm text-gray-400 mb-1">File URL</p>
|
||||
<div className="flex items-center gap-2">
|
||||
@ -430,7 +421,7 @@ export default function Upload() {
|
||||
</code>
|
||||
<button
|
||||
onClick={() =>
|
||||
navigator.clipboard.writeText(result.url)
|
||||
navigator.clipboard.writeText(result.url!)
|
||||
}
|
||||
className="text-xs bg-blue-600 hover:bg-blue-700 text-white px-2 py-1 rounded transition-colors"
|
||||
title="Copy URL"
|
||||
@ -439,35 +430,6 @@ export default function Upload() {
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{result.nip94?.find((tag: any[]) => tag[0] === "thumb") && (
|
||||
<div>
|
||||
<p className="text-sm text-gray-400 mb-1">
|
||||
Thumbnail URL
|
||||
</p>
|
||||
<div className="flex items-center gap-2">
|
||||
<code className="text-xs bg-gray-900 text-blue-400 px-2 py-1 rounded flex-1 overflow-hidden">
|
||||
{
|
||||
result.nip94.find(
|
||||
(tag: any[]) => tag[0] === "thumb",
|
||||
)?.[1]
|
||||
}
|
||||
</code>
|
||||
<button
|
||||
onClick={() =>
|
||||
navigator.clipboard.writeText(
|
||||
result.nip94.find(
|
||||
(tag: any[]) => tag[0] === "thumb",
|
||||
)?.[1],
|
||||
)
|
||||
}
|
||||
className="text-xs bg-blue-600 hover:bg-blue-700 text-white px-2 py-1 rounded transition-colors"
|
||||
title="Copy Thumbnail URL"
|
||||
>
|
||||
Copy
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div>
|
||||
@ -494,5 +456,6 @@ export default function Upload() {
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
@ -1 +1 @@
|
||||
{"root":["./src/App.tsx","./src/const.ts","./src/login.ts","./src/main.tsx","./src/vite-env.d.ts","./src/components/button.tsx","./src/components/payment.tsx","./src/components/profile.tsx","./src/hooks/login.ts","./src/hooks/publisher.ts","./src/upload/admin.ts","./src/upload/blossom.ts","./src/upload/index.ts","./src/upload/nip96.ts","./src/views/admin.tsx","./src/views/files.tsx","./src/views/header.tsx","./src/views/reports.tsx","./src/views/upload.tsx"],"version":"5.6.2"}
|
||||
{"root":["./src/App.tsx","./src/const.ts","./src/login.ts","./src/main.tsx","./src/vite-env.d.ts","./src/components/button.tsx","./src/components/mirror-suggestions.tsx","./src/components/payment.tsx","./src/components/profile.tsx","./src/components/progress-bar.tsx","./src/hooks/login.ts","./src/hooks/publisher.ts","./src/hooks/use-blossom-servers.ts","./src/upload/admin.ts","./src/upload/blossom.ts","./src/upload/index.ts","./src/upload/nip96.ts","./src/upload/progress.ts","./src/views/admin.tsx","./src/views/files.tsx","./src/views/header.tsx","./src/views/reports.tsx","./src/views/upload.tsx"],"version":"5.6.2"}
|
Reference in New Issue
Block a user