Wire upload and library controls
This commit is contained in:
1 parent
b9ecd8b133
commit
fb4f19cea8
10 files changed
+922
-94
No files matched your search
+84
-15
@@ -26,6 +26,7 @@ impl ClipDatabase {
|
||||
"
|
||||
CREATE TABLE IF NOT EXISTS clips (
|
||||
id TEXT PRIMARY KEY NOT NULL,
|
||||
title TEXT,
|
||||
session_id TEXT NOT NULL,
|
||||
game_id TEXT NOT NULL,
|
||||
path TEXT NOT NULL,
|
||||
@@ -40,14 +41,29 @@ impl ClipDatabase {
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS clips_created_at_idx ON clips(created_at_ms DESC);
|
||||
CREATE INDEX IF NOT EXISTS clips_game_id_idx ON clips(game_id);
|
||||
CREATE TABLE IF NOT EXISTS upload_history (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
clip_id TEXT NOT NULL,
|
||||
provider TEXT NOT NULL,
|
||||
status TEXT NOT NULL,
|
||||
url TEXT,
|
||||
error TEXT,
|
||||
attempted_at_ms INTEGER NOT NULL
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS upload_history_clip_id_idx ON upload_history(clip_id);
|
||||
",
|
||||
)
|
||||
)?;
|
||||
if !self.column_exists("clips", "title")? {
|
||||
self.connection
|
||||
.execute("ALTER TABLE clips ADD COLUMN title TEXT", [])?;
|
||||
}
|
||||
self.connection.pragma_update(None, "user_version", 2)
|
||||
}
|
||||
|
||||
pub fn load_clips(&self) -> rusqlite::Result<Vec<Clip>> {
|
||||
let mut statement = self.connection.prepare(
|
||||
"
|
||||
SELECT id, session_id, game_id, path, thumbnail_path, created_at_ms, duration_ms,
|
||||
SELECT id, title, session_id, game_id, path, thumbnail_path, created_at_ms, duration_ms,
|
||||
source, event_type, tags_json, upload_url, upload_provider
|
||||
FROM clips
|
||||
ORDER BY created_at_ms DESC
|
||||
@@ -55,27 +71,28 @@ impl ClipDatabase {
|
||||
)?;
|
||||
|
||||
let rows = statement.query_map([], |row| {
|
||||
let tags_json: String = row.get(9)?;
|
||||
let tags_json: String = row.get(10)?;
|
||||
let tags = serde_json::from_str::<Vec<String>>(&tags_json).unwrap_or_default();
|
||||
let event_type = row
|
||||
.get::<_, Option<String>>(8)?
|
||||
.get::<_, Option<String>>(9)?
|
||||
.map(|value| parse_event_type(&value));
|
||||
let upload_provider = row
|
||||
.get::<_, Option<String>>(11)?
|
||||
.get::<_, Option<String>>(12)?
|
||||
.and_then(|value| parse_upload_provider(&value));
|
||||
|
||||
Ok(Clip {
|
||||
id: row.get(0)?,
|
||||
session_id: row.get(1)?,
|
||||
game_id: row.get(2)?,
|
||||
path: PathBuf::from(row.get::<_, String>(3)?),
|
||||
thumbnail_path: row.get::<_, Option<String>>(4)?.map(PathBuf::from),
|
||||
created_at: millis_to_system_time(row.get(5)?),
|
||||
duration: Duration::from_millis(row.get::<_, i64>(6)?.max(0) as u64),
|
||||
source: parse_clip_source(&row.get::<_, String>(7)?),
|
||||
title: row.get(1)?,
|
||||
session_id: row.get(2)?,
|
||||
game_id: row.get(3)?,
|
||||
path: PathBuf::from(row.get::<_, String>(4)?),
|
||||
thumbnail_path: row.get::<_, Option<String>>(5)?.map(PathBuf::from),
|
||||
created_at: millis_to_system_time(row.get(6)?),
|
||||
duration: Duration::from_millis(row.get::<_, i64>(7)?.max(0) as u64),
|
||||
source: parse_clip_source(&row.get::<_, String>(8)?),
|
||||
event_type,
|
||||
tags,
|
||||
upload_url: row.get(10)?,
|
||||
upload_url: row.get(11)?,
|
||||
upload_provider,
|
||||
})
|
||||
})?;
|
||||
@@ -88,11 +105,12 @@ impl ClipDatabase {
|
||||
self.connection.execute(
|
||||
"
|
||||
INSERT INTO clips (
|
||||
id, session_id, game_id, path, thumbnail_path, created_at_ms, duration_ms,
|
||||
id, title, session_id, game_id, path, thumbnail_path, created_at_ms, duration_ms,
|
||||
source, event_type, tags_json, upload_url, upload_provider
|
||||
)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13)
|
||||
ON CONFLICT(id) DO UPDATE SET
|
||||
title = excluded.title,
|
||||
session_id = excluded.session_id,
|
||||
game_id = excluded.game_id,
|
||||
path = excluded.path,
|
||||
@@ -107,6 +125,7 @@ impl ClipDatabase {
|
||||
",
|
||||
params![
|
||||
clip.id,
|
||||
clip.title.as_deref(),
|
||||
clip.session_id,
|
||||
clip.game_id,
|
||||
clip.path.display().to_string(),
|
||||
@@ -125,11 +144,50 @@ impl ClipDatabase {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn insert_upload_history(
|
||||
&self,
|
||||
clip_id: &str,
|
||||
provider: &str,
|
||||
status: &str,
|
||||
url: Option<&str>,
|
||||
error: Option<&str>,
|
||||
attempted_at: SystemTime,
|
||||
) -> rusqlite::Result<()> {
|
||||
self.connection.execute(
|
||||
"
|
||||
INSERT INTO upload_history (clip_id, provider, status, url, error, attempted_at_ms)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5, ?6)
|
||||
",
|
||||
params![
|
||||
clip_id,
|
||||
provider,
|
||||
status,
|
||||
url,
|
||||
error,
|
||||
system_time_to_millis(attempted_at)
|
||||
],
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn delete_clip(&self, clip_id: &str) -> rusqlite::Result<()> {
|
||||
self.connection
|
||||
.execute("DELETE FROM clips WHERE id = ?1", params![clip_id])?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn column_exists(&self, table: &str, column: &str) -> rusqlite::Result<bool> {
|
||||
let mut statement = self
|
||||
.connection
|
||||
.prepare(&format!("PRAGMA table_info({table})"))?;
|
||||
let rows = statement.query_map([], |row| row.get::<_, String>(1))?;
|
||||
for row in rows {
|
||||
if row? == column {
|
||||
return Ok(true);
|
||||
}
|
||||
}
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_clip_source(value: &str) -> ClipSource {
|
||||
@@ -207,6 +265,7 @@ mod tests {
|
||||
let database = ClipDatabase::open(&db_path).expect("database");
|
||||
let clip = Clip {
|
||||
id: "clip-1".to_string(),
|
||||
title: Some("Opening ace".to_string()),
|
||||
session_id: "session-1".to_string(),
|
||||
game_id: "counter-strike-2".to_string(),
|
||||
path: PathBuf::from("C:/clips/clip-1.mp4"),
|
||||
@@ -224,6 +283,16 @@ mod tests {
|
||||
let loaded = database.load_clips().expect("load");
|
||||
|
||||
assert_eq!(loaded, vec![clip]);
|
||||
database
|
||||
.insert_upload_history(
|
||||
"clip-1",
|
||||
"custom_http",
|
||||
"uploaded",
|
||||
Some("https://example.test/clip.mp4"),
|
||||
None,
|
||||
SystemTime::now(),
|
||||
)
|
||||
.expect("upload history");
|
||||
database.delete_clip("clip-1").expect("delete");
|
||||
assert!(database.load_clips().expect("load empty").is_empty());
|
||||
let _ = std::fs::remove_file(db_path);
|
||||
|
||||
+7
-2
@@ -60,6 +60,10 @@ impl ClipLibrary {
|
||||
&self.clips
|
||||
}
|
||||
|
||||
pub fn all_mut(&mut self) -> &mut [Clip] {
|
||||
&mut self.clips
|
||||
}
|
||||
|
||||
pub fn filter(&self, filter: &ClipFilter) -> Vec<&Clip> {
|
||||
self.clips
|
||||
.iter()
|
||||
@@ -96,12 +100,13 @@ impl ClipLibrary {
|
||||
}
|
||||
|
||||
pub fn to_manifest(&self) -> String {
|
||||
let mut manifest = String::from("id\tgame_id\tsession_id\tpath\tduration_ms\tsource\tevent_type\ttags\tupload_provider\tupload_url\n");
|
||||
let mut manifest = String::from("id\ttitle\tgame_id\tsession_id\tpath\tduration_ms\tsource\tevent_type\ttags\tupload_provider\tupload_url\n");
|
||||
for clip in &self.clips {
|
||||
let _ = writeln!(
|
||||
manifest,
|
||||
"{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}",
|
||||
"{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}",
|
||||
escape(&clip.id),
|
||||
escape(clip.title.as_deref().unwrap_or_default()),
|
||||
escape(&clip.game_id),
|
||||
escape(&clip.session_id),
|
||||
escape(&clip.path.to_string_lossy()),
|
||||
|
||||
@@ -82,9 +82,22 @@ pub enum UploadProvider {
|
||||
CustomHttp,
|
||||
}
|
||||
|
||||
impl fmt::Display for UploadProvider {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let value = match self {
|
||||
Self::Catbox => "catbox",
|
||||
Self::Litterbox => "litterbox",
|
||||
Self::Lustful => "lustful",
|
||||
Self::CustomHttp => "custom_http",
|
||||
};
|
||||
f.write_str(value)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Clip {
|
||||
pub id: String,
|
||||
pub title: Option<String>,
|
||||
pub session_id: String,
|
||||
pub game_id: String,
|
||||
pub path: PathBuf,
|
||||
|
||||
@@ -354,6 +354,7 @@ impl RecorderService {
|
||||
) -> Clip {
|
||||
Clip {
|
||||
id: clip_id.to_string(),
|
||||
title: None,
|
||||
session_id: session_id.to_string(),
|
||||
game_id: game_id.to_string(),
|
||||
path: clip_path(&self.paths, game_id, created_at, clip_id),
|
||||
|
||||
+85
-3
@@ -6,6 +6,8 @@ use std::time::Duration;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub const CURRENT_SCHEMA_VERSION: u32 = 2;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct QualityPreset {
|
||||
pub name: String,
|
||||
@@ -46,8 +48,34 @@ pub struct AutoClipSettings {
|
||||
pub enabled_events_by_game: BTreeMap<String, BTreeSet<String>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct UploadSettings {
|
||||
pub auto_upload_enabled: bool,
|
||||
pub provider: String,
|
||||
pub catbox_userhash: Option<String>,
|
||||
pub litterbox_expiry_hours: u8,
|
||||
pub custom_endpoint: Option<String>,
|
||||
pub custom_response_url_path: String,
|
||||
pub custom_headers: Vec<(String, String)>,
|
||||
}
|
||||
|
||||
impl Default for UploadSettings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
auto_upload_enabled: false,
|
||||
provider: "catbox".to_string(),
|
||||
catbox_userhash: None,
|
||||
litterbox_expiry_hours: 24,
|
||||
custom_endpoint: None,
|
||||
custom_response_url_path: "url".to_string(),
|
||||
custom_headers: Vec::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct AppSettings {
|
||||
#[serde(default = "default_schema_version")]
|
||||
pub schema_version: u32,
|
||||
pub clip_root: PathBuf,
|
||||
pub buffer_root: PathBuf,
|
||||
@@ -57,6 +85,8 @@ pub struct AppSettings {
|
||||
pub hotkeys: HotkeySettings,
|
||||
pub privacy: PrivacySettings,
|
||||
pub auto_clip: AutoClipSettings,
|
||||
#[serde(default)]
|
||||
pub upload: UploadSettings,
|
||||
}
|
||||
|
||||
impl AppSettings {
|
||||
@@ -66,7 +96,7 @@ impl AppSettings {
|
||||
let buffer_root = root.join("buffer");
|
||||
|
||||
Self {
|
||||
schema_version: 1,
|
||||
schema_version: CURRENT_SCHEMA_VERSION,
|
||||
clip_root,
|
||||
buffer_root,
|
||||
storage_limit_gb: 50,
|
||||
@@ -100,6 +130,7 @@ impl AppSettings {
|
||||
merge_window: Duration::from_secs(12),
|
||||
enabled_events_by_game: BTreeMap::new(),
|
||||
},
|
||||
upload: UploadSettings::default(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,6 +139,21 @@ impl AppSettings {
|
||||
.replay_buffer
|
||||
.clamp(Duration::from_secs(15), Duration::from_secs(600));
|
||||
}
|
||||
|
||||
pub fn migrate(&mut self) {
|
||||
self.clamp_replay_buffer();
|
||||
self.upload.litterbox_expiry_hours = match self.upload.litterbox_expiry_hours {
|
||||
1 | 12 | 24 | 72 => self.upload.litterbox_expiry_hours,
|
||||
_ => 24,
|
||||
};
|
||||
if self.upload.provider.trim().is_empty() {
|
||||
self.upload.provider = "catbox".to_string();
|
||||
}
|
||||
if self.upload.custom_response_url_path.trim().is_empty() {
|
||||
self.upload.custom_response_url_path = "url".to_string();
|
||||
}
|
||||
self.schema_version = CURRENT_SCHEMA_VERSION;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load_or_create_settings(root: impl Into<PathBuf>) -> io::Result<AppSettings> {
|
||||
@@ -116,7 +162,11 @@ pub fn load_or_create_settings(root: impl Into<PathBuf>) -> io::Result<AppSettin
|
||||
if path.exists() {
|
||||
let mut settings: AppSettings = serde_json::from_str(&fs::read_to_string(&path)?)
|
||||
.map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))?;
|
||||
settings.clamp_replay_buffer();
|
||||
let previous_version = settings.schema_version;
|
||||
settings.migrate();
|
||||
if previous_version != settings.schema_version {
|
||||
save_settings(&root, &settings)?;
|
||||
}
|
||||
Ok(settings)
|
||||
} else {
|
||||
let settings = AppSettings::default_for_root(&root);
|
||||
@@ -141,6 +191,10 @@ fn default_true() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn default_schema_version() -> u32 {
|
||||
CURRENT_SCHEMA_VERSION
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -155,11 +209,39 @@ mod tests {
|
||||
save_settings(&root, &settings).expect("save");
|
||||
let loaded = load_or_create_settings(&root).expect("load");
|
||||
|
||||
assert_eq!(loaded.schema_version, 1);
|
||||
assert_eq!(loaded.schema_version, CURRENT_SCHEMA_VERSION);
|
||||
assert_eq!(loaded.replay_buffer, Duration::from_secs(120));
|
||||
let _ = fs::remove_dir_all(root);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legacy_settings_migrate_upload_defaults() {
|
||||
let root = std::env::temp_dir().join(format!(
|
||||
"clipforge-settings-migrate-{}",
|
||||
SystemTimeCompat::millis()
|
||||
));
|
||||
fs::create_dir_all(&root).expect("settings root");
|
||||
let mut value =
|
||||
serde_json::to_value(AppSettings::default_for_root(&root)).expect("settings json");
|
||||
value["schema_version"] = serde_json::json!(1);
|
||||
value
|
||||
.as_object_mut()
|
||||
.expect("settings object")
|
||||
.remove("upload");
|
||||
fs::write(
|
||||
settings_path(&root),
|
||||
serde_json::to_string_pretty(&value).expect("legacy settings"),
|
||||
)
|
||||
.expect("write legacy settings");
|
||||
|
||||
let loaded = load_or_create_settings(&root).expect("load migrated settings");
|
||||
|
||||
assert_eq!(loaded.schema_version, CURRENT_SCHEMA_VERSION);
|
||||
assert_eq!(loaded.upload.provider, "catbox");
|
||||
assert!(!loaded.upload.auto_upload_enabled);
|
||||
let _ = fs::remove_dir_all(root);
|
||||
}
|
||||
|
||||
struct SystemTimeCompat;
|
||||
|
||||
impl SystemTimeCompat {
|
||||
|
||||
@@ -56,6 +56,7 @@ pub fn build_test_clip(
|
||||
let created_at = SystemTime::now();
|
||||
Clip {
|
||||
id: clip_id.to_string(),
|
||||
title: None,
|
||||
session_id: session_id.to_string(),
|
||||
game_id: game_id.to_string(),
|
||||
path: clip_path(paths, game_id, created_at, clip_id),
|
||||
|
||||
Reference in new issue
Block a user