Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
222198fcb6 | |||
be52a49c02 | |||
f06ca98460 |
718
Cargo.lock
generated
718
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
43
Cargo.toml
43
Cargo.toml
@ -3,44 +3,26 @@ name = "route96"
|
||||
version = "0.4.0"
|
||||
edition = "2021"
|
||||
|
||||
[[bin]]
|
||||
name = "void_cat_migrate"
|
||||
required-features = ["bin-void-cat-migrate"]
|
||||
|
||||
[[bin]]
|
||||
name = "void_cat_forced_migrate"
|
||||
required-features = ["bin-void-cat-force-migrate"]
|
||||
|
||||
[[bin]]
|
||||
name = "route96"
|
||||
path = "src/bin/main.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "r96util"
|
||||
path = "src/bin/r96util.rs"
|
||||
required-features = ["r96util"]
|
||||
|
||||
[lib]
|
||||
name = "route96"
|
||||
|
||||
[features]
|
||||
default = ["nip96", "blossom", "analytics", "ranges", "react-ui", "r96util"]
|
||||
default = ["nip96", "blossom", "analytics", "react-ui", "payments"]
|
||||
media-compression = ["dep:ffmpeg-rs-raw", "dep:libc"]
|
||||
labels = ["nip96", "dep:candle-core", "dep:candle-nn", "dep:candle-transformers"]
|
||||
labels = ["media-compression", "dep:candle-core", "dep:candle-nn", "dep:candle-transformers"]
|
||||
nip96 = ["media-compression"]
|
||||
blossom = []
|
||||
bin-void-cat-migrate = ["dep:sqlx-postgres"]
|
||||
bin-void-cat-force-migrate = ["dep:regex", "dep:nostr-cursor"]
|
||||
torrent-v2 = []
|
||||
analytics = []
|
||||
void-cat-redirects = ["dep:sqlx-postgres"]
|
||||
ranges = ["dep:http-range-header"]
|
||||
react-ui = []
|
||||
r96util = ["dep:walkdir", "dep:indicatif"]
|
||||
payments = ["dep:fedimint-tonic-lnd"]
|
||||
|
||||
[dependencies]
|
||||
log = "0.4.21"
|
||||
nostr = "0.37.0"
|
||||
nostr = "0.39.0"
|
||||
pretty_env_logger = "0.5.0"
|
||||
rocket = { version = "0.5.1", features = ["json"] }
|
||||
tokio = { version = "1.37.0", features = ["rt", "rt-multi-thread", "macros"] }
|
||||
@ -51,24 +33,19 @@ uuid = { version = "1.8.0", features = ["v4", "serde"] }
|
||||
anyhow = "^1.0.82"
|
||||
sha2 = "0.10.8"
|
||||
sqlx = { version = "0.8.1", features = ["mysql", "runtime-tokio", "chrono", "uuid"] }
|
||||
config = { version = "0.14.0", features = ["yaml"] }
|
||||
config = { version = "0.15.7", features = ["yaml"] }
|
||||
chrono = { version = "0.4.38", features = ["serde"] }
|
||||
serde_with = { version = "3.8.1", features = ["hex"] }
|
||||
reqwest = { version = "0.12.8", features = ["stream"] }
|
||||
reqwest = { version = "0.12.8", features = ["stream", "http2"] }
|
||||
clap = { version = "4.5.18", features = ["derive"] }
|
||||
mime2ext = "0.1.53"
|
||||
infer = "0.16.0"
|
||||
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"
|
||||
|
||||
libc = { version = "0.2.153", optional = true }
|
||||
ffmpeg-rs-raw = { git = "https://git.v0l.io/Kieran/ffmpeg-rs-raw.git", rev = "a63b88ef3c8f58c7c0ac57d361d06ff0bb3ed385", 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 }
|
||||
sqlx-postgres = { version = "0.8.2", optional = true, features = ["chrono", "uuid"] }
|
||||
http-range-header = { version = "0.4.2", optional = true }
|
||||
nostr-cursor = { git = "https://git.v0l.io/Kieran/nostr_backup_proc.git", branch = "main", optional = true }
|
||||
regex = { version = "1.11.1", optional = true }
|
||||
walkdir = { version = "2.5.0", optional = true }
|
||||
indicatif = { version = "0.17.11", optional = true }
|
||||
fedimint-tonic-lnd = { version = "0.2.0", optional = true, default-features = false, features = ["invoicesrpc", "lightningrpc"] }
|
@ -12,6 +12,7 @@ Image hosting service
|
||||
- [BUD-05](https://github.com/hzrd149/blossom/blob/master/buds/05.md)
|
||||
- [BUD-06](https://github.com/hzrd149/blossom/blob/master/buds/06.md)
|
||||
- [BUD-08](https://github.com/hzrd149/blossom/blob/master/buds/08.md)
|
||||
- [BUD-10](https://github.com/hzrd149/blossom/pull/57)
|
||||
- Image compression to WebP
|
||||
- Blurhash calculation
|
||||
- AI image labeling ([ViT224](https://huggingface.co/google/vit-base-patch16-224))
|
||||
@ -45,4 +46,10 @@ docker run --rm -it \
|
||||
```
|
||||
|
||||
### Manual
|
||||
See [install.md](docs/debian.md)
|
||||
See [install.md](docs/debian.md)
|
||||
|
||||
## Blossom only mode (No FFMPEG)
|
||||
If you don't want to support NIP-96 or any media compression you can build like so:
|
||||
```bash
|
||||
cargo build --release --no-default-features --features blossom
|
||||
```
|
@ -1,17 +1,5 @@
|
||||
# Listen address for webserver
|
||||
listen: "0.0.0.0:8000"
|
||||
|
||||
# Database connection string (MYSQL)
|
||||
database: "mysql://root:root@db:3306/route96"
|
||||
|
||||
# Directory to store uploads
|
||||
storage_dir: "/app/data"
|
||||
|
||||
# Maximum support filesize for uploading
|
||||
max_upload_bytes: 104857600
|
||||
|
||||
# Public facing url
|
||||
public_url: "http://localhost:8000"
|
||||
|
||||
# Whitelisted pubkeys, leave out to disable
|
||||
# whitelist: ["63fe6318dc58583cfe16810f86dd09e18bfd76aabc24a0081ce2856f330504ed"]
|
38
config.yaml
38
config.yaml
@ -13,19 +13,37 @@ max_upload_bytes: 5e+9
|
||||
# Public facing url
|
||||
public_url: "http://localhost:8000"
|
||||
|
||||
# Whitelisted pubkeys, leave out to disable
|
||||
# (Optional) Whitelisted pubkeys, leave out to disable
|
||||
# whitelist: ["63fe6318dc58583cfe16810f86dd09e18bfd76aabc24a0081ce2856f330504ed"]
|
||||
|
||||
# Path for ViT(224) image model (https://huggingface.co/google/vit-base-patch16-224)
|
||||
vit_model:
|
||||
model: "/home/kieran/Downloads/falcon_nsfw.safetensors"
|
||||
config: "/home/kieran/Downloads/falcon_nsfw.json"
|
||||
# (Optional) Path for ViT(224) image model (https://huggingface.co/google/vit-base-patch16-224)
|
||||
# vit_model:
|
||||
# model: "falcon_nsfw.safetensors"
|
||||
# config: "falcon_nsfw.json"
|
||||
|
||||
# Analytics support
|
||||
# (Optional) Analytics support
|
||||
# plausible_url: "https://plausible.com/"
|
||||
|
||||
# Support legacy void
|
||||
# void_cat_database: "postgres://postgres:postgres@localhost:41911/void"
|
||||
# (Optional) Legacy file path for void.cat uploads
|
||||
# void_cat_files: "/my/void.cat/data"
|
||||
|
||||
# Legacy file path for void.cat uploads
|
||||
# void_cat_files: "/my/void.cat/data"
|
||||
# (Optional) Payment system config
|
||||
payments:
|
||||
# (Optional) Fiat currency used to track exchange rate along with invoices
|
||||
# If [cost] is using a fiat currency, exchange rates will always be stored
|
||||
# in that currency, so this config is not needed
|
||||
fiat: "USD"
|
||||
# LND node config
|
||||
lnd:
|
||||
endpoint: "https://127.0.0.1:10001"
|
||||
tls: "/home/kieran/.polar/networks/3/volumes/lnd/alice/tls.cert"
|
||||
macaroon: "/home/kieran/.polar/networks/3/volumes/lnd/alice/data/chain/bitcoin/regtest/admin.macaroon"
|
||||
# Cost per unit (BTC/USD/EUR/AUD/CAD/JPY/GBP)
|
||||
cost:
|
||||
currency: "BTC"
|
||||
amount: 0.00000100
|
||||
# Unit metric used to calculate quote (GBSpace, GBEgress)
|
||||
unit: "GBSpace"
|
||||
# Billing interval (day / month / year)
|
||||
interval:
|
||||
month: 1
|
22
migrations/20250202135844_payments.sql
Normal file
22
migrations/20250202135844_payments.sql
Normal file
@ -0,0 +1,22 @@
|
||||
-- Add migration script here
|
||||
alter table users
|
||||
add column paid_until timestamp,
|
||||
add column paid_size integer unsigned not null;
|
||||
|
||||
create table payments
|
||||
(
|
||||
payment_hash binary(32) not null primary key,
|
||||
user_id integer unsigned not null,
|
||||
created timestamp default current_timestamp,
|
||||
amount integer unsigned not null,
|
||||
is_paid bit(1) not null default 0,
|
||||
days_value integer unsigned not null,
|
||||
size_value integer unsigned not null,
|
||||
settle_index integer unsigned,
|
||||
rate float,
|
||||
|
||||
constraint fk_payments_user_id
|
||||
foreign key (user_id) references users (id)
|
||||
on delete cascade
|
||||
on update restrict
|
||||
);
|
@ -3,6 +3,7 @@ use crate::filesystem::FileStore;
|
||||
use crate::processing::probe_file;
|
||||
use anyhow::Result;
|
||||
use log::{error, info, warn};
|
||||
use tokio::sync::broadcast;
|
||||
|
||||
pub struct MediaMetadata {
|
||||
db: Database,
|
||||
@ -14,12 +15,16 @@ impl MediaMetadata {
|
||||
Self { db, fs }
|
||||
}
|
||||
|
||||
pub async fn process(&mut self) -> Result<()> {
|
||||
pub async fn process(&mut self, mut rx: broadcast::Receiver<()>) -> Result<()> {
|
||||
let to_migrate = self.db.get_missing_media_metadata().await?;
|
||||
|
||||
info!("{} files are missing metadata", to_migrate.len());
|
||||
|
||||
for file in to_migrate {
|
||||
if rx.try_recv().is_ok() {
|
||||
info!("Shutting down MediaMetadata process");
|
||||
break;
|
||||
}
|
||||
// probe file and update metadata
|
||||
let path = self.fs.get(&file.id);
|
||||
match probe_file(&path) {
|
||||
@ -68,9 +73,7 @@ impl MediaMetadata {
|
||||
|
||||
impl Database {
|
||||
pub async fn get_missing_media_metadata(&mut self) -> Result<Vec<FileUpload>> {
|
||||
let results: Vec<FileUpload> = sqlx::query_as("select * from uploads where \
|
||||
(mime_type like 'image/%' and (width is null or height is null)) or \
|
||||
(mime_type like 'video/%' and (width is null or height is null or bitrate is null or duration is null))")
|
||||
let results: Vec<FileUpload> = sqlx::query_as("select * from uploads where (width is null or height is null or bitrate is null or duration is null) and (mime_type like 'image/%' or mime_type like 'video/%')")
|
||||
.fetch_all(&self.pool)
|
||||
.await?;
|
||||
|
||||
|
@ -1,24 +1,54 @@
|
||||
use crate::db::Database;
|
||||
use crate::filesystem::FileStore;
|
||||
use anyhow::Result;
|
||||
use log::info;
|
||||
use log::{error, info, warn};
|
||||
use tokio::sync::broadcast;
|
||||
use tokio::task::JoinHandle;
|
||||
|
||||
#[cfg(feature = "media-compression")]
|
||||
mod media_metadata;
|
||||
|
||||
pub fn start_background_tasks(db: Database, file_store: FileStore) -> Vec<JoinHandle<Result<()>>> {
|
||||
#[cfg(feature = "payments")]
|
||||
mod payments;
|
||||
|
||||
pub fn start_background_tasks(
|
||||
db: Database,
|
||||
file_store: FileStore,
|
||||
shutdown_rx: broadcast::Receiver<()>,
|
||||
#[cfg(feature = "payments")] client: Option<fedimint_tonic_lnd::Client>,
|
||||
) -> Vec<JoinHandle<()>> {
|
||||
let mut ret = vec![];
|
||||
|
||||
#[cfg(feature = "media-compression")]
|
||||
{
|
||||
let db = db.clone();
|
||||
let rx = shutdown_rx.resubscribe();
|
||||
ret.push(tokio::spawn(async move {
|
||||
info!("Starting MediaMetadata background task");
|
||||
let mut m = media_metadata::MediaMetadata::new(db.clone(), file_store.clone());
|
||||
m.process().await?;
|
||||
info!("MediaMetadata background task completed");
|
||||
Ok(())
|
||||
let mut m = media_metadata::MediaMetadata::new(db, file_store.clone());
|
||||
if let Err(e) = m.process(rx).await {
|
||||
error!("MediaMetadata failed: {}", e);
|
||||
} else {
|
||||
info!("MediaMetadata background task completed");
|
||||
}
|
||||
}));
|
||||
}
|
||||
#[cfg(feature = "payments")]
|
||||
{
|
||||
if let Some(client) = client {
|
||||
let db = db.clone();
|
||||
let rx = shutdown_rx.resubscribe();
|
||||
ret.push(tokio::spawn(async move {
|
||||
info!("Starting PaymentsHandler background task");
|
||||
let mut m = payments::PaymentsHandler::new(client, db);
|
||||
if let Err(e) = m.process(rx).await {
|
||||
error!("PaymentsHandler failed: {}", e);
|
||||
} else {
|
||||
info!("PaymentsHandler background task completed");
|
||||
}
|
||||
}));
|
||||
} else {
|
||||
warn!("Not starting PaymentsHandler, configuration missing")
|
||||
}
|
||||
}
|
||||
ret
|
||||
}
|
||||
|
71
src/background/payments.rs
Normal file
71
src/background/payments.rs
Normal file
@ -0,0 +1,71 @@
|
||||
use crate::db::Database;
|
||||
use anyhow::Result;
|
||||
use fedimint_tonic_lnd::lnrpc::invoice::InvoiceState;
|
||||
use fedimint_tonic_lnd::lnrpc::InvoiceSubscription;
|
||||
use fedimint_tonic_lnd::Client;
|
||||
use log::{error, info};
|
||||
use rocket::futures::StreamExt;
|
||||
use sqlx::Row;
|
||||
use tokio::sync::broadcast;
|
||||
|
||||
pub struct PaymentsHandler {
|
||||
client: Client,
|
||||
database: Database,
|
||||
}
|
||||
|
||||
impl PaymentsHandler {
|
||||
pub fn new(client: Client, database: Database) -> Self {
|
||||
PaymentsHandler { client, database }
|
||||
}
|
||||
|
||||
pub async fn process(&mut self, mut rx: broadcast::Receiver<()>) -> Result<()> {
|
||||
let start_idx = self.database.get_last_settle_index().await?;
|
||||
let mut invoices = self
|
||||
.client
|
||||
.lightning()
|
||||
.subscribe_invoices(InvoiceSubscription {
|
||||
add_index: 0,
|
||||
settle_index: start_idx,
|
||||
})
|
||||
.await?;
|
||||
info!("Starting invoice subscription from {}", start_idx);
|
||||
|
||||
let invoices = invoices.get_mut();
|
||||
loop {
|
||||
tokio::select! {
|
||||
Ok(_) = rx.recv() => {
|
||||
break;
|
||||
}
|
||||
Some(Ok(msg)) = invoices.next() => {
|
||||
if msg.state == InvoiceState::Settled as i32 {
|
||||
if let Ok(Some(mut p)) = self.database.get_payment(&msg.r_hash).await {
|
||||
p.settle_index = Some(msg.settle_index);
|
||||
p.is_paid = true;
|
||||
match self.database.complete_payment(&p).await {
|
||||
Ok(()) => info!(
|
||||
"Successfully completed payment: {}",
|
||||
hex::encode(&msg.r_hash)
|
||||
),
|
||||
Err(e) => error!("Failed to complete payment: {}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Database {
|
||||
async fn get_last_settle_index(&self) -> Result<u64> {
|
||||
Ok(
|
||||
sqlx::query("select max(settle_index) from payments where is_paid = true")
|
||||
.fetch_one(&self.pool)
|
||||
.await?
|
||||
.try_get(0)
|
||||
.unwrap_or(0),
|
||||
)
|
||||
}
|
||||
}
|
@ -3,6 +3,8 @@ use std::net::{IpAddr, SocketAddr};
|
||||
use anyhow::Error;
|
||||
use clap::Parser;
|
||||
use config::Config;
|
||||
#[cfg(feature = "payments")]
|
||||
use fedimint_tonic_lnd::lnrpc::GetInfoRequest;
|
||||
use log::{error, info};
|
||||
use rocket::config::Ident;
|
||||
use rocket::data::{ByteUnit, Limits};
|
||||
@ -19,6 +21,7 @@ use route96::filesystem::FileStore;
|
||||
use route96::routes;
|
||||
use route96::routes::{get_blob, head_blob, root};
|
||||
use route96::settings::Settings;
|
||||
use tokio::sync::broadcast;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(version, about)]
|
||||
@ -101,19 +104,47 @@ async fn main() -> Result<(), Error> {
|
||||
{
|
||||
rocket = rocket.mount("/", routes![routes::get_blob_thumb]);
|
||||
}
|
||||
#[cfg(feature = "payments")]
|
||||
let lnd = {
|
||||
if let Some(lnd) = settings.payments.as_ref().map(|p| &p.lnd) {
|
||||
let lnd = fedimint_tonic_lnd::connect(
|
||||
lnd.endpoint.clone(),
|
||||
lnd.tls.clone(),
|
||||
lnd.macaroon.clone(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
let jh = start_background_tasks(db, fs);
|
||||
let info = {
|
||||
let mut lnd = lnd.clone();
|
||||
lnd.lightning().get_info(GetInfoRequest::default()).await?
|
||||
};
|
||||
|
||||
info!(
|
||||
"LND connected: {} v{}",
|
||||
info.get_ref().alias,
|
||||
info.get_ref().version
|
||||
);
|
||||
rocket = rocket
|
||||
.manage(lnd.clone())
|
||||
.mount("/", routes::payment::routes());
|
||||
Some(lnd)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
};
|
||||
|
||||
let (shutdown_tx, shutdown_rx) = broadcast::channel(1);
|
||||
let jh = start_background_tasks(db, fs, shutdown_rx, lnd);
|
||||
|
||||
if let Err(e) = rocket.launch().await {
|
||||
error!("Rocker error {}", e);
|
||||
for j in jh {
|
||||
let _ = j.await?;
|
||||
}
|
||||
Err(Error::from(e))
|
||||
} else {
|
||||
for j in jh {
|
||||
let _ = j.await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
shutdown_tx
|
||||
.send(())
|
||||
.expect("Failed to send shutdown signal");
|
||||
|
||||
for j in jh {
|
||||
j.await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
@ -1,228 +0,0 @@
|
||||
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 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() {
|
||||
std::env::set_var("RUST_LOG", "info");
|
||||
}
|
||||
pretty_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(())
|
||||
}
|
@ -1,70 +0,0 @@
|
||||
use clap::Parser;
|
||||
use log::{info, warn};
|
||||
use nostr::serde_json;
|
||||
use nostr_cursor::cursor::NostrCursor;
|
||||
use regex::Regex;
|
||||
use rocket::futures::StreamExt;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::path::PathBuf;
|
||||
use tokio::fs::File;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(version, about)]
|
||||
struct ProgramArgs {
|
||||
/// Directory pointing to archives to scan
|
||||
#[arg(short, long)]
|
||||
pub archive: PathBuf,
|
||||
|
||||
/// Output path .csv
|
||||
#[arg(short, long)]
|
||||
pub output: PathBuf,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), anyhow::Error> {
|
||||
pretty_env_logger::init();
|
||||
|
||||
let args: ProgramArgs = ProgramArgs::parse();
|
||||
|
||||
let mut report: HashMap<String, HashSet<String>> = HashMap::new();
|
||||
|
||||
let mut binding = NostrCursor::new(args.archive);
|
||||
let mut cursor = Box::pin(binding.walk());
|
||||
let matcher = Regex::new(r"void\.cat/d/(\w+)")?;
|
||||
while let Some(Ok(e)) = cursor.next().await {
|
||||
if e.content.contains("void.cat") {
|
||||
let links = matcher.captures_iter(&e.content).collect::<Vec<_>>();
|
||||
for link in links {
|
||||
let g = link.get(1).unwrap().as_str();
|
||||
let base58 = if let Ok(b) = nostr::bitcoin::base58::decode(g) {
|
||||
b
|
||||
} else {
|
||||
warn!("Invalid base58 id {}", g);
|
||||
continue;
|
||||
};
|
||||
let _uuid = if let Ok(u) = Uuid::from_slice_le(base58.as_slice()) {
|
||||
u
|
||||
} else {
|
||||
warn!("Invalid uuid {}", g);
|
||||
continue;
|
||||
};
|
||||
info!("Got link: {} => {}", g, e.pubkey);
|
||||
if let Some(ur) = report.get_mut(&e.pubkey) {
|
||||
ur.insert(g.to_string());
|
||||
} else {
|
||||
report.insert(e.pubkey.clone(), HashSet::from([g.to_string()]));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let json = serde_json::to_string(&report)?;
|
||||
File::create(args.output)
|
||||
.await?
|
||||
.write_all(json.as_bytes())
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
@ -1,147 +0,0 @@
|
||||
use anyhow::Error;
|
||||
use clap::Parser;
|
||||
use config::Config;
|
||||
use log::{info, warn};
|
||||
use nostr::bitcoin::base58;
|
||||
use route96::db::{Database, FileUpload};
|
||||
use route96::filesystem::FileStore;
|
||||
use route96::settings::Settings;
|
||||
use route96::void_db::VoidCatDb;
|
||||
use route96::void_file::VoidFile;
|
||||
use std::path::PathBuf;
|
||||
use tokio::io::{AsyncWriteExt, BufWriter};
|
||||
|
||||
#[derive(Debug, Clone, clap::ValueEnum)]
|
||||
enum ArgOperation {
|
||||
Migrate,
|
||||
ExportNginxRedirects,
|
||||
}
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(version, about)]
|
||||
struct Args {
|
||||
/// Database connection string for void.cat DB
|
||||
#[arg(long)]
|
||||
pub database: String,
|
||||
|
||||
/// Path to filestore on void.cat
|
||||
#[arg(long)]
|
||||
pub data_path: String,
|
||||
|
||||
#[arg(long)]
|
||||
pub operation: ArgOperation,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Error> {
|
||||
pretty_env_logger::init();
|
||||
|
||||
let builder = Config::builder()
|
||||
.add_source(config::File::with_name("config.yaml"))
|
||||
.add_source(config::Environment::with_prefix("APP"))
|
||||
.build()?;
|
||||
|
||||
let settings: Settings = builder.try_deserialize()?;
|
||||
|
||||
let db = Database::new(&settings.database).await?;
|
||||
let fs = FileStore::new(settings.clone());
|
||||
|
||||
let args: Args = Args::parse();
|
||||
|
||||
let db_void = VoidCatDb::connect(&args.database).await?;
|
||||
|
||||
match args.operation {
|
||||
ArgOperation::Migrate => {
|
||||
let mut page = 0;
|
||||
loop {
|
||||
let files = db_void.list_files(page).await?;
|
||||
if files.is_empty() {
|
||||
break;
|
||||
}
|
||||
for f in files {
|
||||
if let Err(e) = migrate_file(&f, &db, &fs, &args).await {
|
||||
warn!("Failed to migrate file: {}, {}", &f.id, e);
|
||||
}
|
||||
}
|
||||
page += 1;
|
||||
}
|
||||
}
|
||||
ArgOperation::ExportNginxRedirects => {
|
||||
let path: PathBuf = args.data_path.parse()?;
|
||||
let conf_path = &path.join("nginx.conf");
|
||||
info!("Writing redirects to {}", conf_path.to_str().unwrap());
|
||||
let mut fout = BufWriter::new(tokio::fs::File::create(conf_path).await?);
|
||||
let mut page = 0;
|
||||
loop {
|
||||
let files = db_void.list_files(page).await?;
|
||||
if files.is_empty() {
|
||||
break;
|
||||
}
|
||||
for f in files {
|
||||
let legacy_id = base58::encode(f.id.to_bytes_le().as_slice());
|
||||
let redirect = format!("location ^\\/d\\/{}(?:\\.\\w+)?$ {{\n\treturn 301 https://nostr.download/{};\n}}\n", &legacy_id, &f.digest);
|
||||
fout.write_all(redirect.as_bytes()).await?;
|
||||
}
|
||||
page += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn migrate_file(
|
||||
f: &VoidFile,
|
||||
db: &Database,
|
||||
fs: &FileStore,
|
||||
args: &Args,
|
||||
) -> Result<(), Error> {
|
||||
let pubkey_vec = hex::decode(&f.email)?;
|
||||
let id_vec = hex::decode(&f.digest)?;
|
||||
|
||||
// copy file
|
||||
let src_path = PathBuf::new()
|
||||
.join(&args.data_path)
|
||||
.join(VoidFile::map_to_path(&f.id));
|
||||
let dst_path = fs.get(&id_vec);
|
||||
if src_path.exists() && !dst_path.exists() {
|
||||
info!(
|
||||
"Copying file: {} from {} => {}",
|
||||
&f.id,
|
||||
src_path.to_str().unwrap(),
|
||||
dst_path.to_str().unwrap()
|
||||
);
|
||||
|
||||
tokio::fs::create_dir_all(dst_path.parent().unwrap()).await?;
|
||||
tokio::fs::copy(src_path, dst_path).await?;
|
||||
} else if dst_path.exists() {
|
||||
info!("File already exists {}, continuing...", &f.id);
|
||||
} else {
|
||||
anyhow::bail!("Source file not found {}", src_path.to_str().unwrap());
|
||||
}
|
||||
let uid = db.upsert_user(&pubkey_vec).await?;
|
||||
info!("Mapped user {} => {}", &f.email, uid);
|
||||
|
||||
let md: Option<Vec<&str>> = f.media_dimensions.as_ref().map(|s| s.split("x").collect());
|
||||
let fu = FileUpload {
|
||||
id: id_vec,
|
||||
name: f.name.clone(),
|
||||
size: f.size as u64,
|
||||
mime_type: f.mime_type.clone(),
|
||||
created: f.uploaded,
|
||||
width: match &md {
|
||||
Some(s) => Some(s[0].parse::<u32>()?),
|
||||
None => None,
|
||||
},
|
||||
height: match &md {
|
||||
Some(s) => Some(s[1].parse::<u32>()?),
|
||||
None => None,
|
||||
},
|
||||
blur_hash: None,
|
||||
alt: f.description.clone(),
|
||||
duration: None,
|
||||
bitrate: None,
|
||||
};
|
||||
db.add_file(&fu, Some(uid)).await?;
|
||||
Ok(())
|
||||
}
|
84
src/db.rs
84
src/db.rs
@ -61,6 +61,10 @@ pub struct User {
|
||||
pub pubkey: Vec<u8>,
|
||||
pub created: DateTime<Utc>,
|
||||
pub is_admin: bool,
|
||||
#[cfg(feature = "payments")]
|
||||
pub paid_until: Option<DateTime<Utc>>,
|
||||
#[cfg(feature = "payments")]
|
||||
pub paid_size: u64,
|
||||
}
|
||||
|
||||
#[cfg(feature = "labels")]
|
||||
@ -90,6 +94,20 @@ pub struct UserStats {
|
||||
pub total_size: u64,
|
||||
}
|
||||
|
||||
#[cfg(feature = "payments")]
|
||||
#[derive(Clone, FromRow, Serialize)]
|
||||
pub struct Payment {
|
||||
pub payment_hash: Vec<u8>,
|
||||
pub user_id: u64,
|
||||
pub created: DateTime<Utc>,
|
||||
pub amount: u64,
|
||||
pub is_paid: bool,
|
||||
pub days_value: u64,
|
||||
pub size_value: u64,
|
||||
pub settle_index: Option<u64>,
|
||||
pub rate: Option<f32>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Database {
|
||||
pub(crate) pool: sqlx::pool::Pool<sqlx::mysql::MySql>,
|
||||
@ -148,7 +166,7 @@ impl Database {
|
||||
.try_get(0)
|
||||
}
|
||||
|
||||
pub async fn add_file(&self, file: &FileUpload, user_id: Option<u64>) -> Result<(), Error> {
|
||||
pub async fn add_file(&self, file: &FileUpload, user_id: 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(?,?,?,?,?,?,?,?,?,?,?)")
|
||||
@ -165,13 +183,10 @@ impl Database {
|
||||
.bind(file.bitrate);
|
||||
tx.execute(q).await?;
|
||||
|
||||
if let Some(user_id) = user_id {
|
||||
let q2 = sqlx::query("insert ignore into user_uploads(file,user_id) values(?,?)")
|
||||
.bind(&file.id)
|
||||
.bind(user_id);
|
||||
|
||||
tx.execute(q2).await?;
|
||||
}
|
||||
let q2 = sqlx::query("insert ignore into user_uploads(file,user_id) values(?,?)")
|
||||
.bind(&file.id)
|
||||
.bind(user_id);
|
||||
tx.execute(q2).await?;
|
||||
|
||||
#[cfg(feature = "labels")]
|
||||
for lbl in &file.labels {
|
||||
@ -273,3 +288,56 @@ impl Database {
|
||||
Ok((results, count))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "payments")]
|
||||
impl Database {
|
||||
pub async fn insert_payment(&self, payment: &Payment) -> Result<(), Error> {
|
||||
sqlx::query("insert into payments(payment_hash,user_id,amount,days_value,size_value,rate) values(?,?,?,?,?,?)")
|
||||
.bind(&payment.payment_hash)
|
||||
.bind(payment.user_id)
|
||||
.bind(payment.amount)
|
||||
.bind(payment.days_value)
|
||||
.bind(payment.size_value)
|
||||
.bind(payment.rate)
|
||||
.execute(&self.pool)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn get_payment(&self, payment_hash: &Vec<u8>) -> Result<Option<Payment>, Error> {
|
||||
sqlx::query_as("select * from payments where payment_hash = ?")
|
||||
.bind(payment_hash)
|
||||
.fetch_optional(&self.pool)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn get_user_payments(&self, uid: u64) -> Result<Vec<Payment>, Error> {
|
||||
sqlx::query_as("select * from payments where user_id = ?")
|
||||
.bind(uid)
|
||||
.fetch_all(&self.pool)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn complete_payment(&self, payment: &Payment) -> Result<(), Error> {
|
||||
let mut tx = self.pool.begin().await?;
|
||||
|
||||
sqlx::query("update payments set is_paid = true, settle_index = ? where payment_hash = ?")
|
||||
.bind(payment.settle_index)
|
||||
.bind(&payment.payment_hash)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
|
||||
// TODO: check space is not downgraded
|
||||
|
||||
sqlx::query("update users set paid_until = TIMESTAMPADD(DAY, ?, IFNULL(paid_until, current_timestamp)), paid_size = ? where id = ?")
|
||||
.bind(payment.days_value)
|
||||
.bind(payment.size_value)
|
||||
.bind(payment.user_id)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
|
||||
tx.commit().await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
#[cfg(feature = "labels")]
|
||||
use crate::db::FileLabel;
|
||||
use crate::processing::can_compress;
|
||||
#[cfg(feature = "labels")]
|
||||
use crate::processing::labeling::label_frame;
|
||||
#[cfg(feature = "media-compression")]
|
||||
@ -14,10 +13,11 @@ use ffmpeg_rs_raw::DemuxerInfo;
|
||||
use rocket::form::validate::Contains;
|
||||
use serde::Serialize;
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::path::PathBuf;
|
||||
use tokio::fs::File;
|
||||
use tokio::io::{AsyncRead, AsyncReadExt};
|
||||
use uuid::Uuid;
|
||||
use crate::can_compress;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum FileSystemResult {
|
||||
@ -61,7 +61,7 @@ impl FileStore {
|
||||
/// Store a new file
|
||||
pub async fn put<'r, S>(
|
||||
&self,
|
||||
path: S,
|
||||
stream: S,
|
||||
mime_type: &str,
|
||||
compress: bool,
|
||||
) -> Result<FileSystemResult>
|
||||
@ -69,7 +69,7 @@ impl FileStore {
|
||||
S: AsyncRead + Unpin + 'r,
|
||||
{
|
||||
// store file in temp path and hash the file
|
||||
let (temp_file, size, hash) = self.store_hash_temp_file(path).await?;
|
||||
let (temp_file, size, hash) = self.store_hash_temp_file(stream).await?;
|
||||
let dst_path = self.map_path(&hash);
|
||||
|
||||
// check if file hash already exists
|
||||
@ -180,6 +180,7 @@ impl FileStore {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "media-compression")]
|
||||
async fn compress_file(&self, input: &PathBuf, mime_type: &str) -> Result<NewFileResult> {
|
||||
let compressed_result = compress_file(input, mime_type, &self.temp_dir())?;
|
||||
#[cfg(feature = "labels")]
|
||||
@ -232,7 +233,7 @@ impl FileStore {
|
||||
Ok((out_path, n, hash))
|
||||
}
|
||||
|
||||
pub async fn hash_file(p: &Path) -> Result<Vec<u8>, Error> {
|
||||
async fn hash_file(p: &PathBuf) -> Result<Vec<u8>, Error> {
|
||||
let mut file = File::open(p).await?;
|
||||
let mut hasher = Sha256::new();
|
||||
let mut buf = [0; 4096];
|
||||
@ -247,7 +248,7 @@ impl FileStore {
|
||||
Ok(res.to_vec())
|
||||
}
|
||||
|
||||
fn map_path(&self, id: &Vec<u8>) -> PathBuf {
|
||||
pub fn map_path(&self, id: &Vec<u8>) -> PathBuf {
|
||||
let id = hex::encode(id);
|
||||
self.storage_dir().join(&id[0..2]).join(&id[2..4]).join(id)
|
||||
}
|
||||
|
@ -5,10 +5,14 @@ pub mod background;
|
||||
pub mod cors;
|
||||
pub mod db;
|
||||
pub mod filesystem;
|
||||
#[cfg(feature = "payments")]
|
||||
pub mod payments;
|
||||
#[cfg(feature = "media-compression")]
|
||||
pub mod processing;
|
||||
pub mod routes;
|
||||
pub mod settings;
|
||||
#[cfg(any(feature = "void-cat-redirects", feature = "bin-void-cat-migrate"))]
|
||||
pub mod void_db;
|
||||
pub mod void_file;
|
||||
|
||||
pub fn can_compress(mime_type: &str) -> bool {
|
||||
mime_type.starts_with("image/")
|
||||
}
|
||||
|
53
src/payments.rs
Normal file
53
src/payments.rs
Normal file
@ -0,0 +1,53 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt::{Display, Formatter};
|
||||
|
||||
#[cfg(feature = "payments")]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct PaymentAmount {
|
||||
pub currency: Currency,
|
||||
pub amount: f32,
|
||||
}
|
||||
|
||||
#[cfg(feature = "payments")]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub enum Currency {
|
||||
BTC,
|
||||
USD,
|
||||
EUR,
|
||||
GBP,
|
||||
JPY,
|
||||
CAD,
|
||||
AUD,
|
||||
}
|
||||
|
||||
#[cfg(feature = "payments")]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub enum PaymentUnit {
|
||||
GBSpace,
|
||||
GBEgress,
|
||||
}
|
||||
|
||||
impl PaymentUnit {
|
||||
/// Get the total size from a number of units
|
||||
pub fn to_size(&self, units: f32) -> u64 {
|
||||
(1000f32 * 1000f32 * 1000f32 * units) as u64
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for PaymentUnit {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
PaymentUnit::GBSpace => write!(f, "GB Space"),
|
||||
PaymentUnit::GBEgress => write!(f, "GB Egress"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "payments")]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum PaymentInterval {
|
||||
Day(u16),
|
||||
Month(u16),
|
||||
Year(u16),
|
||||
}
|
@ -5,6 +5,7 @@ use ffmpeg_rs_raw::{Decoder, Demuxer, DemuxerInfo, Encoder, Scaler, StreamType,
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::ptr;
|
||||
use uuid::Uuid;
|
||||
use crate::can_compress;
|
||||
|
||||
#[cfg(feature = "labels")]
|
||||
pub mod labeling;
|
||||
@ -136,10 +137,6 @@ pub struct NewFileProcessorResult {
|
||||
pub bitrate: u32,
|
||||
}
|
||||
|
||||
pub fn can_compress(mime_type: &str) -> bool {
|
||||
mime_type.starts_with("image/")
|
||||
}
|
||||
|
||||
pub fn compress_file(
|
||||
path: &Path,
|
||||
mime_type: &str,
|
||||
|
@ -53,6 +53,10 @@ pub struct SelfUser {
|
||||
pub is_admin: bool,
|
||||
pub file_count: u64,
|
||||
pub total_size: u64,
|
||||
#[cfg(feature = "payments")]
|
||||
pub paid_until: u64,
|
||||
#[cfg(feature = "payments")]
|
||||
pub quota: u64,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
@ -77,6 +81,12 @@ async fn admin_get_self(auth: Nip98Auth, db: &State<Database>) -> AdminResponse<
|
||||
is_admin: user.is_admin,
|
||||
file_count: s.file_count,
|
||||
total_size: s.total_size,
|
||||
paid_until: if let Some(u) = &user.paid_until {
|
||||
u.timestamp() as u64
|
||||
} else {
|
||||
0
|
||||
},
|
||||
quota: user.paid_size,
|
||||
})
|
||||
}
|
||||
Err(_) => AdminResponse::error("User not found"),
|
||||
|
@ -25,7 +25,7 @@ pub struct BlobDescriptor {
|
||||
pub size: u64,
|
||||
#[serde(rename = "type", skip_serializing_if = "Option::is_none")]
|
||||
pub mime_type: Option<String>,
|
||||
pub uploaded: u64,
|
||||
pub created: u64,
|
||||
#[serde(rename = "nip94", skip_serializing_if = "Option::is_none")]
|
||||
pub nip94: Option<HashMap<String, String>>,
|
||||
}
|
||||
@ -45,7 +45,7 @@ impl BlobDescriptor {
|
||||
sha256: id_hex,
|
||||
size: value.size,
|
||||
mime_type: Some(value.mime_type.clone()),
|
||||
uploaded: value.created.timestamp() as u64,
|
||||
created: value.created.timestamp() as u64,
|
||||
nip94: Some(
|
||||
Nip94Event::from_upload(settings, value)
|
||||
.tags
|
||||
@ -415,7 +415,7 @@ where
|
||||
return BlossomResponse::error(format!("Failed to save file (db): {}", e));
|
||||
}
|
||||
};
|
||||
if let Err(e) = db.add_file(&upload, Some(user_id)).await {
|
||||
if let Err(e) = db.add_file(&upload, user_id).await {
|
||||
error!("{}", e.to_string());
|
||||
BlossomResponse::error(format!("Error saving file (db): {}", e))
|
||||
} else {
|
||||
|
@ -10,6 +10,7 @@ pub use crate::routes::nip96::nip96_routes;
|
||||
use crate::settings::Settings;
|
||||
use crate::void_file::VoidFile;
|
||||
use anyhow::Error;
|
||||
use base58::FromBase58;
|
||||
use http_range_header::{parse_range_header, EndPosition, StartPosition};
|
||||
use log::{debug, warn};
|
||||
use nostr::Event;
|
||||
@ -33,6 +34,8 @@ mod blossom;
|
||||
mod nip96;
|
||||
|
||||
mod admin;
|
||||
#[cfg(feature = "payments")]
|
||||
pub mod payment;
|
||||
|
||||
pub struct FilePayload {
|
||||
pub file: File,
|
||||
@ -59,27 +62,22 @@ struct PagedResult<T> {
|
||||
impl Nip94Event {
|
||||
pub fn from_upload(settings: &Settings, upload: &FileUpload) -> Self {
|
||||
let hex_id = hex::encode(&upload.id);
|
||||
let ext = if upload.mime_type != "application/octet-stream" {
|
||||
mime2ext::mime2ext(&upload.mime_type)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let mut tags = vec![
|
||||
vec![
|
||||
"url".to_string(),
|
||||
format!("{}/{}.{}", &settings.public_url, &hex_id, ext.unwrap_or("")),
|
||||
format!(
|
||||
"{}/{}{}",
|
||||
&settings.public_url,
|
||||
&hex_id,
|
||||
mime2ext::mime2ext(&upload.mime_type)
|
||||
.map(|m| format!(".{m}"))
|
||||
.unwrap_or("".to_string())
|
||||
),
|
||||
],
|
||||
vec!["x".to_string(), hex_id.clone()],
|
||||
vec!["x".to_string(), hex_id],
|
||||
vec!["m".to_string(), upload.mime_type.clone()],
|
||||
vec!["size".to_string(), upload.size.to_string()],
|
||||
];
|
||||
if upload.mime_type.starts_with("image/") || upload.mime_type.starts_with("video/") {
|
||||
tags.push(vec![
|
||||
"thumb".to_string(),
|
||||
format!("{}/thumb/{}.webp", &settings.public_url, &hex_id),
|
||||
]);
|
||||
}
|
||||
|
||||
if let Some(bh) = &upload.blur_hash {
|
||||
tags.push(vec!["blurhash".to_string(), bh.clone()]);
|
||||
}
|
||||
@ -188,58 +186,44 @@ impl<'r> Responder<'r, 'static> for FilePayload {
|
||||
response.set_header(Header::new("cache-control", "max-age=31536000, immutable"));
|
||||
|
||||
// handle ranges
|
||||
#[cfg(feature = "ranges")]
|
||||
{
|
||||
const MAX_UNBOUNDED_RANGE: u64 = 1024 * 1024;
|
||||
// only use range response for files > 1MiB
|
||||
if self.info.size < MAX_UNBOUNDED_RANGE {
|
||||
response.set_sized_body(None, self.file);
|
||||
} else {
|
||||
response.set_header(Header::new("accept-ranges", "bytes"));
|
||||
if let Some(r) = request.headers().get("range").next() {
|
||||
if let Ok(ranges) = parse_range_header(r) {
|
||||
if ranges.ranges.len() > 1 {
|
||||
warn!(
|
||||
"Multipart ranges are not supported, fallback to non-range request"
|
||||
);
|
||||
response.set_streamed_body(self.file);
|
||||
} else {
|
||||
let single_range = ranges.ranges.first().unwrap();
|
||||
let range_start = match single_range.start {
|
||||
StartPosition::Index(i) => i,
|
||||
StartPosition::FromLast(i) => self.info.size - i,
|
||||
};
|
||||
let range_end = match single_range.end {
|
||||
EndPosition::Index(i) => i,
|
||||
EndPosition::LastByte => {
|
||||
(range_start + MAX_UNBOUNDED_RANGE).min(self.info.size)
|
||||
}
|
||||
};
|
||||
let r_len = range_end - range_start;
|
||||
let r_body = RangeBody::new(self.file, range_start..range_end);
|
||||
|
||||
response.set_status(Status::PartialContent);
|
||||
response.set_header(Header::new("content-length", r_len.to_string()));
|
||||
response.set_header(Header::new(
|
||||
"content-range",
|
||||
format!(
|
||||
"bytes {}-{}/{}",
|
||||
range_start,
|
||||
range_end - 1,
|
||||
self.info.size
|
||||
),
|
||||
));
|
||||
response.set_streamed_body(Box::pin(r_body));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
response.set_sized_body(None, self.file);
|
||||
}
|
||||
}
|
||||
}
|
||||
#[cfg(not(feature = "ranges"))]
|
||||
{
|
||||
const MAX_UNBOUNDED_RANGE: u64 = 1024 * 1024;
|
||||
// only use range response for files > 1MiB
|
||||
if self.info.size < MAX_UNBOUNDED_RANGE {
|
||||
response.set_sized_body(None, self.file);
|
||||
} else {
|
||||
response.set_header(Header::new("accept-ranges", "bytes"));
|
||||
if let Some(r) = request.headers().get("range").next() {
|
||||
if let Ok(ranges) = parse_range_header(r) {
|
||||
if ranges.ranges.len() > 1 {
|
||||
warn!("Multipart ranges are not supported, fallback to non-range request");
|
||||
response.set_streamed_body(self.file);
|
||||
} else {
|
||||
let single_range = ranges.ranges.first().unwrap();
|
||||
let range_start = match single_range.start {
|
||||
StartPosition::Index(i) => i,
|
||||
StartPosition::FromLast(i) => self.info.size - i,
|
||||
};
|
||||
let range_end = match single_range.end {
|
||||
EndPosition::Index(i) => i,
|
||||
EndPosition::LastByte => {
|
||||
(range_start + MAX_UNBOUNDED_RANGE).min(self.info.size)
|
||||
}
|
||||
};
|
||||
let r_len = range_end - range_start;
|
||||
let r_body = RangeBody::new(self.file, range_start..range_end);
|
||||
|
||||
response.set_status(Status::PartialContent);
|
||||
response.set_header(Header::new("content-length", r_len.to_string()));
|
||||
response.set_header(Header::new(
|
||||
"content-range",
|
||||
format!("bytes {}-{}/{}", range_start, range_end - 1, self.info.size),
|
||||
));
|
||||
response.set_streamed_body(Box::pin(r_body));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
response.set_sized_body(None, self.file);
|
||||
}
|
||||
}
|
||||
|
||||
if let Ok(ct) = ContentType::from_str(&self.info.mime_type) {
|
||||
@ -407,10 +391,6 @@ pub async fn get_blob_thumb(
|
||||
return Err(Status::NotFound);
|
||||
};
|
||||
|
||||
if !(info.mime_type.starts_with("image/") || info.mime_type.starts_with("video/")) {
|
||||
return Err(Status::NotFound);
|
||||
}
|
||||
|
||||
let file_path = fs.get(&id);
|
||||
|
||||
let mut thumb_file = temp_dir().join(format!("thumb_{}", sha256));
|
||||
@ -446,15 +426,8 @@ pub async fn void_cat_redirect(id: &str, settings: &State<Settings>) -> Option<N
|
||||
id
|
||||
};
|
||||
if let Some(base) = &settings.void_cat_files {
|
||||
let uuid = if let Ok(b58) = nostr::bitcoin::base58::decode(id) {
|
||||
uuid::Uuid::from_slice_le(b58.as_slice())
|
||||
} else {
|
||||
uuid::Uuid::parse_str(id)
|
||||
};
|
||||
if uuid.is_err() {
|
||||
return None;
|
||||
}
|
||||
let f = base.join(VoidFile::map_to_path(&uuid.unwrap()));
|
||||
let uuid = uuid::Uuid::from_slice_le(id.from_base58().unwrap().as_slice()).unwrap();
|
||||
let f = base.join(VoidFile::map_to_path(&uuid));
|
||||
debug!("Legacy file map: {} => {}", id, f.display());
|
||||
if let Ok(f) = NamedFile::open(f).await {
|
||||
Some(f)
|
||||
@ -473,8 +446,7 @@ pub async fn void_cat_redirect_head(id: &str) -> VoidCatFile {
|
||||
} else {
|
||||
id
|
||||
};
|
||||
let uuid =
|
||||
uuid::Uuid::from_slice_le(nostr::bitcoin::base58::decode(id).unwrap().as_slice()).unwrap();
|
||||
let uuid = uuid::Uuid::from_slice_le(id.from_base58().unwrap().as_slice()).unwrap();
|
||||
VoidCatFile {
|
||||
status: Status::Ok,
|
||||
uuid: Header::new("X-UUID", uuid.to_string()),
|
||||
|
@ -232,7 +232,7 @@ async fn upload(
|
||||
Err(e) => return Nip96Response::error(&format!("Could not save user: {}", e)),
|
||||
};
|
||||
|
||||
if let Err(e) = db.add_file(&upload, Some(user_id)).await {
|
||||
if let Err(e) = db.add_file(&upload, user_id).await {
|
||||
error!("{}", e.to_string());
|
||||
return Nip96Response::error(&format!("Could not save file (db): {}", e));
|
||||
}
|
||||
|
131
src/routes/payment.rs
Normal file
131
src/routes/payment.rs
Normal file
@ -0,0 +1,131 @@
|
||||
use crate::auth::nip98::Nip98Auth;
|
||||
use crate::db::{Database, Payment};
|
||||
use crate::payments::{Currency, PaymentAmount, PaymentInterval, PaymentUnit};
|
||||
use crate::settings::Settings;
|
||||
use chrono::{Months, Utc};
|
||||
use fedimint_tonic_lnd::lnrpc::Invoice;
|
||||
use fedimint_tonic_lnd::Client;
|
||||
use log::{error, info};
|
||||
use rocket::serde::json::Json;
|
||||
use rocket::{routes, Route, State};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::ops::{Add, Deref};
|
||||
|
||||
pub fn routes() -> Vec<Route> {
|
||||
routes![get_payment, req_payment]
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
struct PaymentInfo {
|
||||
/// Billing quota metric
|
||||
pub unit: PaymentUnit,
|
||||
|
||||
/// Amount of time to bill units (GB/mo, Gb Egress/day etc.)
|
||||
pub interval: PaymentInterval,
|
||||
|
||||
/// Value amount of payment
|
||||
pub cost: PaymentAmount,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
struct PaymentRequest {
|
||||
/// Number of units requested to make payment
|
||||
pub units: f32,
|
||||
|
||||
/// Quantity of orders to make
|
||||
pub quantity: u16,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
struct PaymentResponse {
|
||||
pub pr: String,
|
||||
}
|
||||
|
||||
#[rocket::get("/payment")]
|
||||
async fn get_payment(settings: &State<Settings>) -> Option<Json<PaymentInfo>> {
|
||||
settings.payments.as_ref().map(|p| {
|
||||
Json::from(PaymentInfo {
|
||||
unit: p.unit.clone(),
|
||||
interval: p.interval.clone(),
|
||||
cost: p.cost.clone(),
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
#[rocket::post("/payment", data = "<req>", format = "json")]
|
||||
async fn req_payment(
|
||||
auth: Nip98Auth,
|
||||
db: &State<Database>,
|
||||
settings: &State<Settings>,
|
||||
lnd: &State<Client>,
|
||||
req: Json<PaymentRequest>,
|
||||
) -> Result<Json<PaymentResponse>, String> {
|
||||
let cfg = if let Some(p) = &settings.payments {
|
||||
p
|
||||
} else {
|
||||
return Err("Payment not enabled, missing configuration option(s)".to_string());
|
||||
};
|
||||
|
||||
let btc_amount = match cfg.cost.currency {
|
||||
Currency::BTC => cfg.cost.amount,
|
||||
_ => return Err("Currency not supported".to_string()),
|
||||
};
|
||||
|
||||
let amount = btc_amount * req.units * req.quantity as f32;
|
||||
|
||||
let pubkey_vec = auth.event.pubkey.to_bytes().to_vec();
|
||||
let uid = db
|
||||
.upsert_user(&pubkey_vec)
|
||||
.await
|
||||
.map_err(|_| "Failed to get user account".to_string())?;
|
||||
|
||||
let mut lnd = lnd.deref().clone();
|
||||
let c = lnd.lightning();
|
||||
let msat = (amount * 1e11f32) as u64;
|
||||
let memo = format!(
|
||||
"{}x {} {} for {}",
|
||||
req.quantity, req.units, cfg.unit, auth.event.pubkey
|
||||
);
|
||||
info!("Requesting {} msats: {}", msat, memo);
|
||||
let invoice = c
|
||||
.add_invoice(Invoice {
|
||||
value_msat: msat as i64,
|
||||
memo,
|
||||
..Default::default()
|
||||
})
|
||||
.await
|
||||
.map_err(|e| e.message().to_string())?;
|
||||
|
||||
let days_value = match cfg.interval {
|
||||
PaymentInterval::Day(d) => d as u64,
|
||||
PaymentInterval::Month(m) => {
|
||||
let now = Utc::now();
|
||||
(now.add(Months::new(m as u32)) - now).num_days() as u64
|
||||
}
|
||||
PaymentInterval::Year(y) => {
|
||||
let now = Utc::now();
|
||||
(now.add(Months::new(12 * y as u32)) - now).num_days() as u64
|
||||
}
|
||||
};
|
||||
|
||||
let record = Payment {
|
||||
payment_hash: invoice.get_ref().r_hash.clone(),
|
||||
user_id: uid,
|
||||
created: Default::default(),
|
||||
amount: msat,
|
||||
is_paid: false,
|
||||
days_value,
|
||||
size_value: cfg.unit.to_size(req.units),
|
||||
settle_index: None,
|
||||
rate: None,
|
||||
};
|
||||
|
||||
if let Err(e) = db.insert_payment(&record).await {
|
||||
error!("Failed to insert payment: {}", e);
|
||||
return Err("Failed to insert payment".to_string());
|
||||
}
|
||||
|
||||
Ok(Json(PaymentResponse {
|
||||
pr: invoice.get_ref().payment_request.clone(),
|
||||
}))
|
||||
}
|
@ -1,3 +1,5 @@
|
||||
#[cfg(feature = "payments")]
|
||||
use crate::payments::{Currency, PaymentAmount, PaymentInterval, PaymentUnit};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::path::PathBuf;
|
||||
|
||||
@ -30,11 +32,12 @@ pub struct Settings {
|
||||
/// Analytics tracking
|
||||
pub plausible_url: Option<String>,
|
||||
|
||||
#[cfg(feature = "void-cat-redirects")]
|
||||
pub void_cat_database: Option<String>,
|
||||
|
||||
/// Path to void.cat uploads (files-v2)
|
||||
pub void_cat_files: Option<PathBuf>,
|
||||
|
||||
#[cfg(feature = "payments")]
|
||||
/// Payment options for paid storage
|
||||
pub payments: Option<PaymentConfig>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
@ -42,3 +45,30 @@ pub struct VitModelConfig {
|
||||
pub model: PathBuf,
|
||||
pub config: PathBuf,
|
||||
}
|
||||
|
||||
#[cfg(feature = "payments")]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct PaymentConfig {
|
||||
/// LND connection details
|
||||
pub lnd: LndConfig,
|
||||
|
||||
/// Pricing per unit
|
||||
pub cost: PaymentAmount,
|
||||
|
||||
/// What metric to bill payments on
|
||||
pub unit: PaymentUnit,
|
||||
|
||||
/// Billing interval time per unit
|
||||
pub interval: PaymentInterval,
|
||||
|
||||
/// Fiat base currency to store exchange rates along with invoice
|
||||
pub fiat: Option<Currency>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "payments")]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct LndConfig {
|
||||
pub endpoint: String,
|
||||
pub tls: PathBuf,
|
||||
pub macaroon: PathBuf,
|
||||
}
|
||||
|
@ -22,9 +22,9 @@ export class Route96 {
|
||||
return data;
|
||||
}
|
||||
|
||||
async listFiles(page = 0, count = 10, mime: string | undefined) {
|
||||
async listFiles(page = 0, count = 10) {
|
||||
const rsp = await this.#req(
|
||||
`admin/files?page=${page}&count=${count}${mime ? `&mime_type=${mime}` : ""}`,
|
||||
`admin/files?page=${page}&count=${count}`,
|
||||
"GET",
|
||||
);
|
||||
const data = await this.#handleResponse<AdminResponseFileList>(rsp);
|
||||
|
@ -23,7 +23,6 @@ export default function Upload() {
|
||||
const [adminListedFiles, setAdminListedFiles] = useState<Nip96FileList>();
|
||||
const [listedPage, setListedPage] = useState(0);
|
||||
const [adminListedPage, setAdminListedPage] = useState(0);
|
||||
const [mimeFilter, setMimeFilter] = useState<string>();
|
||||
|
||||
const login = useLogin();
|
||||
const pub = usePublisher();
|
||||
@ -86,7 +85,7 @@ export default function Upload() {
|
||||
try {
|
||||
setError(undefined);
|
||||
const uploader = new Route96(url, pub);
|
||||
const result = await uploader.listFiles(n, 50, mimeFilter);
|
||||
const result = await uploader.listFiles(n, 50);
|
||||
setAdminListedFiles(result);
|
||||
} catch (e) {
|
||||
if (e instanceof Error) {
|
||||
@ -136,7 +135,7 @@ export default function Upload() {
|
||||
|
||||
useEffect(() => {
|
||||
listAllUploads(adminListedPage);
|
||||
}, [adminListedPage, mimeFilter]);
|
||||
}, [adminListedPage]);
|
||||
|
||||
useEffect(() => {
|
||||
if (pub && !self) {
|
||||
@ -250,18 +249,6 @@ export default function Upload() {
|
||||
<hr />
|
||||
<h3>Admin File List:</h3>
|
||||
<Button onClick={() => listAllUploads(0)}>List All Uploads</Button>
|
||||
<div>
|
||||
<select value={mimeFilter} onChange={e => setMimeFilter(e.target.value)}>
|
||||
<option value={""}>All</option>
|
||||
<option>image/webp</option>
|
||||
<option>image/jpeg</option>
|
||||
<option>image/jpg</option>
|
||||
<option>image/png</option>
|
||||
<option>image/gif</option>
|
||||
<option>video/mp4</option>
|
||||
<option>video/mov</option>
|
||||
</select>
|
||||
</div>
|
||||
{adminListedFiles && (
|
||||
<FileList
|
||||
files={adminListedFiles.files}
|
||||
|
Loading…
x
Reference in New Issue
Block a user