Skip to content

Commit

Permalink
feat: keep files for one day and clean older on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
thewh1teagle committed Nov 29, 2024
1 parent 0322460 commit c6f139e
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 17 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ eyre = { workspace = true }
hound = "3.5.1"
reqwest = { version = "0.11.23", features = ["stream"] }
tempfile = "3.9.0"
chrono = "0.4.38"
tokio = { version = "1.35.1", features = [
"tokio-macros",
"macros",
Expand Down
11 changes: 11 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,14 @@ pub mod transcript;

#[cfg(test)]
mod test;

pub fn get_vibe_temp_folder() -> std::path::PathBuf {
use chrono::Local;
let current_datetime = Local::now();
let formatted_datetime = current_datetime.format("%Y-%m-%d").to_string();
let dir = std::env::temp_dir().join(format!("vibe_temp_{}", formatted_datetime));
if let Ok(_) = std::fs::create_dir_all(&dir) {
return dir;
}
std::env::temp_dir()
}
9 changes: 2 additions & 7 deletions core/src/transcribe.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::audio;
use crate::config::TranscribeOptions;
use crate::transcript::{Segment, Transcript};
use crate::{audio, get_vibe_temp_folder};
use eyre::{bail, eyre, Context, OptionExt, Result};
use hound::WavReader;
use std::collections::hash_map::DefaultHasher;
Expand Down Expand Up @@ -81,7 +81,7 @@ pub fn create_normalized_audio(source: PathBuf, additional_ffmpeg_args: Option<V
tracing::debug!("normalize {:?}", source.display());

let cache_key = generate_cache_key(&source, &additional_ffmpeg_args);
let out_path = std::env::temp_dir().join(format!("{:x}.wav", cache_key));
let out_path = get_vibe_temp_folder().join(format!("{:x}.wav", cache_key));
if out_path.exists() {
tracing::info!("Using cached normalized audio: {}", out_path.display());
return Ok(out_path);
Expand Down Expand Up @@ -330,10 +330,5 @@ pub fn transcribe(
processing_time_sec: Instant::now().duration_since(st).as_secs(),
};

// cleanup
if out_path.starts_with(std::env::temp_dir()) {
std::fs::remove_file(out_path)?;
}

Ok(transcript)
}
27 changes: 26 additions & 1 deletion desktop/src-tauri/src/cleaner.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::utils::LogError;
use crate::{cmd::get_logs_folder, config, logging::get_log_path};
use eyre::{ContextCompat, Result};
use eyre::{eyre, ContextCompat, Result};
use vibe_core::get_vibe_temp_folder;

pub fn clean_old_logs(app: &tauri::AppHandle) -> Result<()> {
tracing::debug!("clean old logs");
Expand Down Expand Up @@ -30,3 +32,26 @@ pub fn clean_old_logs(app: &tauri::AppHandle) -> Result<()> {
}
Ok(())
}

pub fn clean_old_files() -> Result<()> {
let current_temp_dir = get_vibe_temp_folder();
let temp_dir = std::env::temp_dir();
let temp_dir = temp_dir.to_str().unwrap_or_default();
// Remove suffix
let temp_dir = temp_dir.strip_suffix('/').unwrap_or(temp_dir);
let temp_dir = temp_dir.strip_suffix('\\').unwrap_or(temp_dir);
let pattern = format!("{}/vibe_temp*", temp_dir);
tracing::debug!("searching old files in {}", pattern);
for path in glob::glob(&pattern)? {
let path = path?;
if path == current_temp_dir {
tracing::debug!("Skip deletion of {}", current_temp_dir.display());
continue;
}
tracing::debug!("Clean old folder {}", path.clone().display());
std::fs::remove_dir_all(path.clone())
.map_err(|e| eyre!("failed to delete {}: {:?}", path.display(), e))
.log_error();
}
Ok(())
}
9 changes: 5 additions & 4 deletions desktop/src-tauri/src/cmd/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::io::BufWriter;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use tauri::{AppHandle, Emitter, Listener, Manager};
use vibe_core::get_vibe_temp_folder;

#[cfg(target_os = "macos")]
use crate::screen_capture_kit;
Expand Down Expand Up @@ -108,7 +109,7 @@ pub async fn start_record(app_handle: AppHandle, devices: Vec<AudioDevice>, stor
};
let spec = wav_spec_from_config(&config);

let path = std::env::temp_dir().join(format!("{}.wav", random_string(10)));
let path = get_vibe_temp_folder().join(format!("{}.wav", random_string(10)));
tracing::debug!("WAV file path: {:?}", path);
wav_paths.push((path.clone(), 0));

Expand Down Expand Up @@ -194,7 +195,7 @@ pub async fn start_record(app_handle: AppHandle, devices: Vec<AudioDevice>, stor
{
if let Some(stream) = screencapture_stream {
screen_capture_kit::stop_capture(&stream).map_err(|e| eyre!("{:?}", e)).log_error();
let output_path = std::env::temp_dir().join(format!("{}.wav", random_string(5)));
let output_path = get_vibe_temp_folder().join(format!("{}.wav", random_string(5)));
screen_capture_kit::screencapturekit_to_wav(output_path.clone()).map_err(|e| eyre!("{e:?}")).log_error();
tracing::debug!("output path is {}", output_path.display());
wav_paths.push((output_path, 1));
Expand All @@ -204,7 +205,7 @@ pub async fn start_record(app_handle: AppHandle, devices: Vec<AudioDevice>, stor
let dst = if wav_paths.len() == 1 {
wav_paths[0].0.clone()
} else if wav_paths[0].1 > 0 && wav_paths[1].1 > 0 {
let dst = std::env::temp_dir().join(format!("{}.wav", random_string(10)));
let dst = get_vibe_temp_folder().join(format!("{}.wav", random_string(10)));
tracing::debug!("Merging WAV files");
vibe_core::audio::merge_wav_files(wav_paths[0].0.clone(), wav_paths[1].0.clone(), dst.clone()).map_err(|e| eyre!("{e:?}")).log_error();
dst
Expand All @@ -218,7 +219,7 @@ pub async fn start_record(app_handle: AppHandle, devices: Vec<AudioDevice>, stor
};

tracing::debug!("Emitting record_finish event");
let mut normalized = std::env::temp_dir().join(format!("{}.wav", get_local_time()));
let mut normalized = get_vibe_temp_folder().join(format!("{}.wav", get_local_time()));
vibe_core::audio::normalize(dst.clone(), normalized.clone(), None).map_err(|e| eyre!("{e:?}")).log_error();

if store_in_documents {
Expand Down
5 changes: 3 additions & 2 deletions desktop/src-tauri/src/cmd/ytdlp.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use eyre::{bail, Context, Result};
use tauri::{AppHandle, Manager};
use vibe_core::get_vibe_temp_folder;

use super::get_ffmpeg_path;

Expand All @@ -22,9 +23,9 @@ fn get_binary_name() -> &'static str {
#[tauri::command]
pub fn get_temp_path(app_handle: AppHandle, ext: String, in_documents: Option<bool>) -> String {
let mut base_path = if in_documents.unwrap_or_default() {
app_handle.path().document_dir().unwrap_or(std::env::temp_dir())
app_handle.path().document_dir().unwrap_or(get_vibe_temp_folder())
} else {
std::env::temp_dir()
get_vibe_temp_folder()
};

base_path.push(format!("{}.{}", crate::utils::get_local_time(), ext));
Expand Down
7 changes: 4 additions & 3 deletions desktop/src-tauri/src/screen_capture_kit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::ops::Deref;
use std::path::PathBuf;
use std::process::{Command, Stdio};
use vibe_core::audio::find_ffmpeg_path;
use vibe_core::get_vibe_temp_folder;

use crate::utils::LogError;

Expand All @@ -31,7 +32,7 @@ impl UnsafeSCStreamOutput for StoreAudioHandler {
fn did_output_sample_buffer(&self, sample: Id<CMSampleBufferRef>, _of_type: u8) {
let audio_buffers = sample.get_av_audio_buffer_list();

let base_path = std::env::temp_dir();
let base_path = get_vibe_temp_folder();

for (i, buffer) in audio_buffers.into_iter().enumerate() {
if i > MAX_CHANNELS {
Expand Down Expand Up @@ -97,7 +98,7 @@ pub fn init() -> Result<Id<UnsafeSCStream>> {
}

pub fn start_capture(stream: &Id<UnsafeSCStream>) -> Result<()> {
let base_path = std::env::temp_dir();
let base_path = get_vibe_temp_folder();
for i in 0..MAX_CHANNELS {
let output_path = base_path.join(format!("output{}.raw", i));
if output_path.exists() {
Expand Down Expand Up @@ -128,7 +129,7 @@ pub fn resume_capture(stream: &Id<UnsafeSCStream>) -> Result<()> {
pub fn screencapturekit_to_wav(output_path: PathBuf) -> Result<()> {
// TODO: convert to wav
// ffmpeg -f f32le -ar 48000 -ac 1 -i output0.raw -f f32le -ar 48000 -ac 1 -i output1.raw -filter_complex "[0:a][1:a]amerge=inputs=2" -ac 2 output.wav
let base_path = std::env::temp_dir();
let base_path = get_vibe_temp_folder();
let output_0 = base_path.join(format!("output{}.raw", 0));
let output_1 = base_path.join(format!("output{}.raw", 1));
let mut pid = Command::new(find_ffmpeg_path().context("no ffmpeg")?)
Expand Down
1 change: 1 addition & 0 deletions desktop/src-tauri/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub fn setup(app: &App) -> Result<(), Box<dyn std::error::Error>> {
}
crate::logging::setup_logging(app.handle(), store).unwrap();
crate::cleaner::clean_old_logs(app.handle()).log_error();
crate::cleaner::clean_old_files().log_error();
tracing::debug!("Vibe App Running");

// Crash handler
Expand Down

0 comments on commit c6f139e

Please sign in to comment.