mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-12-03 08:42:40 +00:00
Moved temporary files into a folder of their own below the data directory (#1245)
This commit is contained in:
1 parent
ddfc632b63
commit
12d3e1ec6c
52 files changed
+1219
-396
No files matched your search
@@ -89,6 +89,13 @@ blocking handler can hold up the whole app. Therefore:
|
||||
- `runtime_api::test_support::assert_runtime_stays_free` tests a handler which waits for a lock. Run
|
||||
such a test with `#[tokio::test(flavor = "multi_thread", worker_threads = 1)]`.
|
||||
|
||||
**The runtime writes no temporary files of its own into the temporary folder of the system,** whose memory
|
||||
it would take on many Linux systems, and always in a Flatpak. The .NET app keeps its temporary files in `tmp/`
|
||||
below the data directory (`TemporaryFiles`) and hands the runtime the paths, e.g., for the output of a media
|
||||
job. Only tests use the temporary folder of the system, and the start log as its last resort when no other
|
||||
folder is writable (`log.rs`). `TEMPORARY_DIRECTORY_NAME` in
|
||||
`share_sheet.rs` names that folder, since the runtime shares only plugin archives from it.
|
||||
|
||||
**AI Studio restarts itself only through `own_restart.rs`.** Since Tauri 2.12.6, a restart on macOS starts
|
||||
the new instance in the background, so that it takes no focus away from the app the user works in. AI
|
||||
Studio restarts itself only on behalf of the user, after an update or the restore of a backup, and the
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
use crate::environment::DATA_DIRECTORY;
|
||||
use axum::Json;
|
||||
use log::{error, info};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
/// The directory the app creates its shareable plugin archives in. Keep in sync with
|
||||
/// PluginShareService.TEMPORARY_ARCHIVE_DIRECTORY on the .NET side.
|
||||
const SHARE_DIRECTORY_NAME: &str = "mindwork-ai-studio-plugin-shares";
|
||||
/// The temporary folder of the app below its data directory, in which the app creates its shareable
|
||||
/// plugin archives. Keep in sync with TemporaryFiles.FOLDER_NAME on the .NET side.
|
||||
const TEMPORARY_DIRECTORY_NAME: &str = "tmp";
|
||||
|
||||
/// The file extension of plugin archives, without the leading dot. Keep in sync with
|
||||
/// PluginShareService.PLUGIN_FILE_EXTENSION on the .NET side.
|
||||
@@ -99,17 +100,23 @@ pub async fn share_file(Json(request): Json<ShareFileRequest>) -> Json<ShareFile
|
||||
/// Checks that a path points to a plugin archive the app itself created for sharing. This keeps the
|
||||
/// endpoint from handing arbitrary readable files to the operating system's share UI.
|
||||
///
|
||||
/// We match the directory by name instead of comparing it against the temporary directory: Rust and
|
||||
/// .NET do not have to agree on where that is, and a mismatch would break sharing entirely.
|
||||
/// The app creates these archives in its temporary folder below the data directory, which the runtime
|
||||
/// reports to the app, so both agree on where that folder is.
|
||||
fn is_shareable_archive(path: &Path) -> bool {
|
||||
DATA_DIRECTORY.get().is_some_and(|data_directory| is_archive_in_temporary_folder(path, Path::new(data_directory)))
|
||||
}
|
||||
|
||||
/// Checks that a resolved path points to a plugin archive below the temporary folder of the app.
|
||||
///
|
||||
/// The temporary folder gets resolved as well, so that both paths are written the same way, e.g., on
|
||||
/// macOS, where `/var` is a link to `/private/var`, and on Windows, where a resolved path begins with `\\?\`.
|
||||
fn is_archive_in_temporary_folder(path: &Path, data_directory: &Path) -> bool {
|
||||
let has_archive_extension = path.extension().is_some_and(|extension| extension.eq_ignore_ascii_case(SHARE_FILE_EXTENSION));
|
||||
if !has_archive_extension {
|
||||
return false;
|
||||
}
|
||||
|
||||
path.parent()
|
||||
.and_then(|parent| parent.file_name())
|
||||
.is_some_and(|directory_name| directory_name == SHARE_DIRECTORY_NAME)
|
||||
std::fs::canonicalize(data_directory.join(TEMPORARY_DIRECTORY_NAME)).is_ok_and(|temporary_folder| path.starts_with(temporary_folder))
|
||||
}
|
||||
|
||||
fn failure(issue: impl Into<String>) -> Json<ShareFileResponse> {
|
||||
@@ -337,6 +344,60 @@ async fn share_file_on_platform(path: PathBuf) -> Result<(), ShareFailure> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::fs;
|
||||
|
||||
/// Creates a plugin archive in the given folder and returns its path resolved, as `share_file` resolves it.
|
||||
fn archive_in(folder: &Path) -> PathBuf {
|
||||
fs::create_dir_all(folder).unwrap();
|
||||
let archive = folder.join("Meeting Notes.mwplugin");
|
||||
fs::write(&archive, b"archive").unwrap();
|
||||
fs::canonicalize(archive).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_archive_in_the_temporary_folder_can_be_shared() {
|
||||
let data_directory = tempfile::tempdir().unwrap();
|
||||
let archive = archive_in(&data_directory.path().join(TEMPORARY_DIRECTORY_NAME).join("0123456789abcdef"));
|
||||
|
||||
assert!(is_archive_in_temporary_folder(&archive, data_directory.path()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_archive_outside_the_temporary_folder_cannot_be_shared() {
|
||||
let data_directory = tempfile::tempdir().unwrap();
|
||||
fs::create_dir_all(data_directory.path().join(TEMPORARY_DIRECTORY_NAME)).unwrap();
|
||||
let in_the_data_directory = archive_in(data_directory.path());
|
||||
let other_directory = tempfile::tempdir().unwrap();
|
||||
let in_a_folder_of_the_same_name = archive_in(&other_directory.path().join(TEMPORARY_DIRECTORY_NAME));
|
||||
|
||||
assert!(!is_archive_in_temporary_folder(&in_the_data_directory, data_directory.path()));
|
||||
assert!(!is_archive_in_temporary_folder(&in_a_folder_of_the_same_name, data_directory.path()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn another_file_in_the_temporary_folder_cannot_be_shared() {
|
||||
let data_directory = tempfile::tempdir().unwrap();
|
||||
let folder = data_directory.path().join(TEMPORARY_DIRECTORY_NAME);
|
||||
fs::create_dir_all(&folder).unwrap();
|
||||
let recording = folder.join("recording.webm");
|
||||
fs::write(&recording, b"audio").unwrap();
|
||||
|
||||
assert!(!is_archive_in_temporary_folder(&fs::canonicalize(recording).unwrap(), data_directory.path()));
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_link_in_the_temporary_folder_to_an_archive_outside_cannot_be_shared() {
|
||||
let data_directory = tempfile::tempdir().unwrap();
|
||||
let folder = data_directory.path().join(TEMPORARY_DIRECTORY_NAME);
|
||||
fs::create_dir_all(&folder).unwrap();
|
||||
let other_directory = tempfile::tempdir().unwrap();
|
||||
let outside = archive_in(other_directory.path());
|
||||
let link = folder.join("Meeting Notes.mwplugin");
|
||||
std::os::unix::fs::symlink(&outside, &link).unwrap();
|
||||
|
||||
assert!(!is_archive_in_temporary_folder(&fs::canonicalize(link).unwrap(), data_directory.path()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_plain_error_text_counts_as_a_failure() {
|
||||
|
||||
Reference in new issue
Block a user