feat: refactor zip controller to use new zip module

The zip controller has been refactored to use the new zip module. This change includes updating the import statements and modifying the main function to generate the tauri context with the new zip module.

Additionally, the zip controller now has a new struct called Controller, which contains the necessary fields for handling zip operations. The TryFrom trait has been implemented for the Controller struct to allow for easy creation from the AppHandle.

The main changes include:
- Importing the zip module and updating import statements
- Refactoring the main function to generate the tauri context with the new zip module
- Creating a new Controller struct for handling zip operations
- Implementing the TryFrom trait for the Controller struct to allow for easy creation from the AppHandle
This commit is contained in:
Nikita Galaiko 2023-10-06 15:08:42 +02:00 committed by GitButler
parent 372adef021
commit 295ace965c
6 changed files with 165 additions and 50 deletions

View File

@ -7,49 +7,8 @@ use tracing::instrument;
use crate::{
app, assets, bookmarks, deltas, error::Error, git, reader, search, sessions, virtual_branches,
zip,
};
#[tauri::command(async)]
#[instrument(skip(handle))]
pub async fn get_project_archive_path(
handle: tauri::AppHandle,
project_id: &str,
) -> Result<String, Error> {
let app = handle.state::<app::App>();
let project = app.get_project(project_id)?;
let zipper = handle.state::<zip::Zipper>();
let zipped_logs = zipper.zip(project.path)?;
Ok(zipped_logs.to_str().unwrap().to_string())
}
#[tauri::command(async)]
#[instrument(skip(handle))]
pub async fn get_project_data_archive_path(
handle: tauri::AppHandle,
project_id: &str,
) -> Result<String, Error> {
let zipper = handle.state::<zip::Zipper>();
let zipped_logs = zipper.zip(
handle
.path_resolver()
.app_local_data_dir()
.unwrap()
.join("projects")
.join(project_id),
)?;
Ok(zipped_logs.to_str().unwrap().to_string())
}
#[tauri::command(async)]
#[instrument(skip(handle))]
pub async fn get_logs_archive_path(handle: tauri::AppHandle) -> Result<String, Error> {
let zipper = handle.state::<zip::Zipper>();
let zipped_logs = zipper.zip(handle.path_resolver().app_log_dir().unwrap())?;
Ok(zipped_logs.to_str().unwrap().to_string())
}
#[tauri::command(async)]
#[instrument(skip(handle))]
pub async fn search(

View File

@ -2,7 +2,7 @@ use anyhow::Context;
use futures::executor::block_on;
use tauri::{generate_context, Manager};
use gitbutler::*;
use gitbutler::{zip, *};
fn main() {
let tauri_context = generate_context!();
@ -98,10 +98,6 @@ fn main() {
.expect("failed to initialize watchers");
tauri_app.manage(watchers);
let zipper =
zip::Zipper::try_from(&app_handle).expect("failed to initialize zipper");
tauri_app.manage(zipper);
let proxy =
assets::Proxy::try_from(&app_handle).expect("failed to initialize proxy");
tauri_app.manage(proxy);
@ -114,6 +110,10 @@ fn main() {
.expect("failed to initialize storage");
app_handle.manage(storage);
let zipper = zip::Controller::try_from(&app_handle)
.expect("failed to initialize zipc controller ");
tauri_app.manage(zipper);
let search = search::Searcher::try_from(&app_handle)
.expect("failed to initialize search");
app_handle.manage(search);
@ -154,15 +154,15 @@ fn main() {
commands::git_head,
commands::git_wd_diff,
commands::delete_all_data,
commands::get_logs_archive_path,
commands::get_project_archive_path,
commands::get_project_data_archive_path,
commands::upsert_bookmark,
commands::list_bookmarks,
commands::fetch_from_target,
commands::mark_resolved,
commands::git_set_global_config,
commands::git_get_global_config,
zip::commands::get_logs_archive_path,
zip::commands::get_project_archive_path,
zip::commands::get_project_data_archive_path,
users::commands::set_user,
users::commands::delete_user,
users::commands::get_user,

View File

@ -3,6 +3,6 @@ mod controller;
mod project;
mod storage;
pub use controller::Controller;
pub use controller::{Controller, GetError};
pub use project::{ApiProject, AuthKey, FetchResult, Project};
pub use storage::{Error as StorageError, Storage, UpdateRequest};

View File

@ -0,0 +1,76 @@
use std::path;
use tauri::{AppHandle, Manager};
use tracing::instrument;
use crate::error::Error;
use super::controller;
impl From<controller::ArchiveError> for Error {
fn from(error: controller::ArchiveError) -> Self {
match error {
controller::ArchiveError::GetProject(error) => error.into(),
controller::ArchiveError::Other(error) => {
tracing::error!(?error, "failed to archive project");
Error::Unknown
}
}
}
}
#[tauri::command(async)]
#[instrument(skip(handle))]
pub async fn get_project_archive_path(
handle: AppHandle,
project_id: &str,
) -> Result<path::PathBuf, Error> {
handle
.state::<controller::Controller>()
.archive(project_id)
.map_err(Into::into)
}
impl From<controller::DataArchiveError> for Error {
fn from(value: controller::DataArchiveError) -> Self {
match value {
controller::DataArchiveError::GetProject(error) => error.into(),
controller::DataArchiveError::Other(error) => {
tracing::error!(?error, "failed to archive project data");
Error::Unknown
}
}
}
}
#[tauri::command(async)]
#[instrument(skip(handle))]
pub async fn get_project_data_archive_path(
handle: AppHandle,
project_id: &str,
) -> Result<path::PathBuf, Error> {
handle
.state::<controller::Controller>()
.data_archive(project_id)
.map_err(Into::into)
}
impl From<controller::LogsArchiveError> for Error {
fn from(error: controller::LogsArchiveError) -> Self {
match error {
controller::LogsArchiveError::Other(error) => {
tracing::error!(?error, "failed to archive logs");
Error::Unknown
}
}
}
}
#[tauri::command(async)]
#[instrument(skip(handle))]
pub async fn get_logs_archive_path(handle: AppHandle) -> Result<path::PathBuf, Error> {
handle
.state::<controller::Controller>()
.logs_archive()
.map_err(Into::into)
}

View File

@ -0,0 +1,76 @@
use std::path;
use anyhow::Context;
use tauri::AppHandle;
use crate::projects;
use super::Zipper;
pub struct Controller {
local_data_dir: path::PathBuf,
logs_dir: path::PathBuf,
zipper: Zipper,
projects_controller: projects::Controller,
}
impl TryFrom<&AppHandle> for Controller {
type Error = anyhow::Error;
fn try_from(value: &AppHandle) -> Result<Self, Self::Error> {
let local_data_dir = value
.path_resolver()
.app_local_data_dir()
.context("failed to get local data dir")?;
let logs_dir = value
.path_resolver()
.app_log_dir()
.context("failed to get log dir")?;
Ok(Self {
local_data_dir,
logs_dir,
zipper: Zipper::try_from(value)?,
projects_controller: projects::Controller::try_from(value)?,
})
}
}
impl Controller {
pub fn archive(&self, project_id: &str) -> Result<path::PathBuf, ArchiveError> {
let project = self.projects_controller.get_project(project_id)?;
self.zipper.zip(project.path).map_err(Into::into)
}
pub fn data_archive(&self, project_id: &str) -> Result<path::PathBuf, DataArchiveError> {
let project = self.projects_controller.get_project(project_id)?;
self.zipper
.zip(self.local_data_dir.join("projects").join(project.id))
.map_err(Into::into)
}
pub fn logs_archive(&self) -> Result<path::PathBuf, LogsArchiveError> {
self.zipper.zip(&self.logs_dir).map_err(Into::into)
}
}
#[derive(thiserror::Error, Debug)]
pub enum ArchiveError {
#[error(transparent)]
GetProject(#[from] projects::GetError),
#[error(transparent)]
Other(#[from] anyhow::Error),
}
#[derive(thiserror::Error, Debug)]
pub enum DataArchiveError {
#[error(transparent)]
GetProject(#[from] projects::GetError),
#[error(transparent)]
Other(#[from] anyhow::Error),
}
#[derive(thiserror::Error, Debug)]
pub enum LogsArchiveError {
#[error(transparent)]
Other(#[from] anyhow::Error),
}

View File

@ -1,3 +1,7 @@
pub mod commands;
mod controller;
pub use controller::Controller;
use std::{
fs,
io::{self, Read, Write},