Finished BUD 1

This commit is contained in:
Your Name
2025-08-18 21:51:54 -04:00
parent e641c813eb
commit 95ccb3a9c4
24 changed files with 1728 additions and 31 deletions

BIN
db/ginxsom.db Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

52
db/init.sh Executable file
View File

@@ -0,0 +1,52 @@
#!/bin/bash
# Ginxsom Database Initialization Script
# This script creates the SQLite database with the proper schema
set -e # Exit on any error
# Configuration
DB_DIR="$(dirname "$0")"
DB_FILE="$DB_DIR/ginxsom.db"
SCHEMA_FILE="$DB_DIR/schema.sql"
echo "Initializing Ginxsom database..."
# Check if schema file exists
if [ ! -f "$SCHEMA_FILE" ]; then
echo "Error: Schema file not found at $SCHEMA_FILE"
exit 1
fi
# Create database directory if it doesn't exist
mkdir -p "$DB_DIR"
# Remove existing database if it exists (for clean initialization)
if [ -f "$DB_FILE" ]; then
echo "Warning: Existing database found. Creating backup..."
cp "$DB_FILE" "$DB_FILE.backup.$(date +%s)"
rm "$DB_FILE"
fi
# Create new database with schema
echo "Creating database at $DB_FILE..."
sqlite3 "$DB_FILE" < "$SCHEMA_FILE"
# Set proper permissions
chmod 644 "$DB_FILE"
chmod 755 "$DB_DIR"
echo "Database initialized successfully!"
echo "Database location: $DB_FILE"
# Show database info
echo ""
echo "Database tables:"
sqlite3 "$DB_FILE" ".tables"
echo ""
echo "Server configuration:"
sqlite3 "$DB_FILE" "SELECT key, value, description FROM server_config;"
echo ""
echo "Database ready for use."

67
db/schema.sql Normal file
View File

@@ -0,0 +1,67 @@
-- Ginxsom Blossom Server Database Schema
-- SQLite database for blob metadata and server configuration
-- Enable foreign key constraints
PRAGMA foreign_keys = ON;
-- Main blobs table for storing blob metadata
CREATE TABLE IF NOT EXISTS blobs (
sha256 TEXT PRIMARY KEY NOT NULL, -- SHA-256 hash (64 hex chars)
size INTEGER NOT NULL, -- File size in bytes
type TEXT NOT NULL, -- MIME type
uploaded_at INTEGER NOT NULL, -- Unix timestamp
uploader_pubkey TEXT, -- Nostr public key (optional)
filename TEXT, -- Original filename (optional)
CHECK (length(sha256) = 64), -- Ensure valid SHA-256 hash length
CHECK (size >= 0), -- Ensure non-negative size
CHECK (uploaded_at > 0) -- Ensure valid timestamp
);
-- Server configuration table for key-value settings
CREATE TABLE IF NOT EXISTS server_config (
key TEXT PRIMARY KEY NOT NULL, -- Configuration key
value TEXT NOT NULL, -- Configuration value
description TEXT, -- Human-readable description
updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now')) -- Last update timestamp
);
-- Indexes for performance optimization
CREATE INDEX IF NOT EXISTS idx_blobs_uploaded_at ON blobs(uploaded_at);
CREATE INDEX IF NOT EXISTS idx_blobs_uploader_pubkey ON blobs(uploader_pubkey);
CREATE INDEX IF NOT EXISTS idx_blobs_type ON blobs(type);
CREATE INDEX IF NOT EXISTS idx_blobs_size ON blobs(size);
-- Insert default server configuration
INSERT OR IGNORE INTO server_config (key, value, description) VALUES
('max_file_size', '104857600', 'Maximum file size in bytes (100MB)'),
('require_auth', 'false', 'Whether authentication is required for uploads'),
('allowed_types', '*', 'Allowed MIME types (* for all)'),
('server_name', 'ginxsom', 'Server name for responses'),
('storage_quota', '10737418240', 'Total storage quota in bytes (10GB)'),
('cleanup_interval', '86400', 'Cleanup interval in seconds (daily)'),
('max_upload_rate', '1048576', 'Max upload rate per client in bytes/sec (1MB/s)');
-- View for storage statistics
CREATE VIEW IF NOT EXISTS storage_stats AS
SELECT
COUNT(*) as total_blobs,
SUM(size) as total_bytes,
AVG(size) as avg_blob_size,
MIN(uploaded_at) as first_upload,
MAX(uploaded_at) as last_upload,
COUNT(DISTINCT uploader_pubkey) as unique_uploaders
FROM blobs;
-- View for recent uploads (last 24 hours)
CREATE VIEW IF NOT EXISTS recent_uploads AS
SELECT
sha256,
size,
type,
uploaded_at,
uploader_pubkey,
filename,
datetime(uploaded_at, 'unixepoch') as uploaded_datetime
FROM blobs
WHERE uploaded_at > (strftime('%s', 'now') - 86400)
ORDER BY uploaded_at DESC;