fix(sql-file): stream sqlserver zip imports incrementally

This commit is contained in:
КГБ Lieu authored and GitHub committed 2026-10-01 02:40:02 +08:00
1 parent e0f802c031
commit 64e5847f74
7 files changed
+659 -66

No files matched your search

@@ -8,6 +8,35 @@ pub const SQL_FILE_ZIP_MAX_MANIFEST_BYTES: u64 = 1024 * 1024;
pub const SQL_FILE_ZIP_MAX_TOTAL_BYTES: u64 = 4 * 1024 * 1024 * 1024;
pub const SQL_FILE_ZIP_MAX_PART_BYTES: u64 = 1024 * 1024 * 1024;
/// Extraction-directory prefix used by the desktop (Tauri) dispatch site,
/// under the OS temp dir: `dbx-sql-package-<uuid>`.
pub const SQL_FILE_ZIP_EXTRACTION_DIR_PREFIX_DESKTOP: &str = "dbx-sql-package-";
/// Extraction-directory prefix used by the web dispatch site, under the
/// managed upload tmp dir: `package-<uuid>`.
pub const SQL_FILE_ZIP_EXTRACTION_DIR_PREFIX_WEB: &str = "package-";
/// True if `path`'s parent directory name starts with `prefix`, i.e. `path` is
/// one of the parts `extract_sql_file_zip_package` wrote out for a `.zip`
/// upload extracted under that prefix.
///
/// Dispatch sites must derive "is this execution a ZIP package" from the
/// actual paths they are about to execute (via this helper), not from the
/// original upload's file name/extension: the frontend always sends the
/// extracted `.sql` part paths as the execution paths, never the original
/// `.zip` path, so a suffix check on the original request field never
/// matches. Each site must pass only the prefix it extracts under itself:
/// the prefixes are per transport, and `package-` is generic enough that
/// user-chosen directories can share it, so such a path must never be
/// mistaken for an extraction dir and routed through the ZIP importer or its
/// `remove_dir_all` cleanup.
pub fn is_extracted_sql_zip_package_path(path: &Path, prefix: &str) -> bool {
path.parent()
.and_then(|parent| parent.file_name())
.and_then(|name| name.to_str())
.is_some_and(|name| name.starts_with(prefix))
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
struct SqlZipManifest {
@@ -135,6 +164,32 @@ mod tests {
zip.finish().unwrap();
}
#[test]
fn is_extracted_sql_zip_package_path_matches_only_the_requested_prefix() {
let desktop_part = Path::new("/tmp/dbx-sql-package-11111111-1111-1111-1111-111111111111/00001-dump.sql");
let web_part = Path::new("/var/data/tmp/sql_file/package-22222222-2222-2222-2222-222222222222/00001-dump.sql");
assert!(is_extracted_sql_zip_package_path(desktop_part, SQL_FILE_ZIP_EXTRACTION_DIR_PREFIX_DESKTOP));
assert!(!is_extracted_sql_zip_package_path(desktop_part, SQL_FILE_ZIP_EXTRACTION_DIR_PREFIX_WEB));
assert!(is_extracted_sql_zip_package_path(web_part, SQL_FILE_ZIP_EXTRACTION_DIR_PREFIX_WEB));
assert!(!is_extracted_sql_zip_package_path(web_part, SQL_FILE_ZIP_EXTRACTION_DIR_PREFIX_DESKTOP));
assert!(!is_extracted_sql_zip_package_path(
Path::new("/tmp/upload-33333333.sql"),
SQL_FILE_ZIP_EXTRACTION_DIR_PREFIX_DESKTOP
));
assert!(!is_extracted_sql_zip_package_path(
Path::new("/tmp/upload-33333333.sql"),
SQL_FILE_ZIP_EXTRACTION_DIR_PREFIX_WEB
));
assert!(!is_extracted_sql_zip_package_path(
Path::new("/var/data/tmp/sql_file/restore-44444444/backup.sql"),
SQL_FILE_ZIP_EXTRACTION_DIR_PREFIX_WEB
));
assert!(!is_extracted_sql_zip_package_path(
Path::new("standalone.sql"),
SQL_FILE_ZIP_EXTRACTION_DIR_PREFIX_DESKTOP
));
}
#[test]
fn preserves_manifest_order_when_extracting_parts() {
let dir = tempfile::tempdir().unwrap();