]> uap-core.de Git - note.git/commitdiff
determine local storage path when saving a note main
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Fri, 3 Jul 2026 18:52:39 +0000 (20:52 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Fri, 3 Jul 2026 18:52:39 +0000 (20:52 +0200)
application/src/backend.rs
entity/src/collection.rs
entity/src/repository.rs
migration/src/m20260502_184134_create_settings.rs

index 66c88daef77a8255cba4da38bd9af2d3c64df95e..057682e1090561db452a498fdab33c1a6f21e9ce 100644 (file)
 
 use std::future::Future;
 use std::pin::Pin;
-use sea_orm::{ActiveModelTrait, Database, DatabaseConnection, EntityTrait, QueryFilter, ColumnTrait, Set, QueryOrder, DbErr, ExprTrait};
+use sea_orm::{ActiveModelTrait, Database, DatabaseConnection, EntityTrait, QueryFilter, ColumnTrait, Set, QueryOrder, DbErr, ExprTrait, RelationTrait, QuerySelect};
 use tokio::runtime::Runtime;
 use std::sync::{Arc};
 use std::thread::JoinHandle;
 use tokio::sync::{broadcast, mpsc};
 use tokio::sync::broadcast::error::SendError;
-use migration::{Expr, Migrator, MigratorTrait};
+use migration::{Expr, JoinType, Migrator, MigratorTrait};
 use ui_rs::ui;
 
 use entity::{collection, note, notecontent, profile, repository};
 use entity::profile::Entity as Profile;
-use entity::collection::{create_notebook_hierarchy, CollectionType, Entity as Collection, Node};
+use entity::collection::{create_notebook_hierarchy, CollectionType, Entity as Collection, LocalStorageSetting, Node};
 use entity::note::{Column, Entity as Note};
 use entity::notecontent::{Entity as NoteContent};
 use migration::prelude::Utc;
@@ -395,6 +395,29 @@ impl BackendHandle {
     where F: FnOnce(SaveNoteResult) + Send + 'static {
         let bhandle = self.clone();
         let cmd = Box::pin(async move {
+            let local_storage_path = if let Set(collection_id) = note.collection_id {
+                let result = collection::Entity::find_by_id(collection_id)
+                    .join(JoinType::InnerJoin, collection::Relation::Repository.def())
+                    .select_only()
+                        .column(collection::Column::Storage)
+                        .column(repository::Column::UseLocalStorage)
+                        .column(repository::Column::LocalPath)
+                    .into_tuple::<(LocalStorageSetting, bool, Option<String>)>()
+                    .one(&bhandle.backend.db).await;
+                let col = result.ok().flatten().unwrap();
+
+                if col.0 == LocalStorageSetting::FileSystem || (col.0 == LocalStorageSetting::Default && col.1) {
+                    if col.2.is_none() {
+                        println!("Warning: collection is configured to use local storage, but there is no local storage path configured in the repository");
+                    }
+                    col.2
+                } else {
+                    None
+                }
+            } else {
+                return;
+            };
+
             let result = if let Set(note_id) = note.note_id {
                 let mut update = Note::update_many();
                 if let Set(kind) = note.kind {
index 91a5fa2991f7b4b157fd9aa41d19144ce1ef1eef..858ebd24385c07e1dbc190b967c4b1fe888f0603 100644 (file)
@@ -1,6 +1,7 @@
 use std::collections::HashMap;
 use sea_orm::entity::prelude::*;
 
+#[sea_orm::model]
 #[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
 #[sea_orm(table_name = "collection")]
 pub struct Model {
@@ -9,6 +10,9 @@ pub struct Model {
     pub profile_id: i32,
     pub repository_id: i32,
 
+    #[sea_orm(belongs_to, from = "repository_id", to = "repository_id")]
+    pub repository: HasOne<super::repository::Entity>,
+
     pub name: String,
     pub parent: String,
     pub icon: String,
@@ -39,8 +43,8 @@ pub enum LocalStorageSetting {
     Database = 2
 }
 
-#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
-pub enum Relation {}
+//#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
+//pub enum Relation {}
 
 impl ActiveModelBehavior for ActiveModel {}
 
index 7fa18c6f6410fca7eacba1d6179b05e9978aa519..17ed22bd39743cb6a6296a8ed37000af4e4ee6b4 100644 (file)
@@ -11,6 +11,8 @@ pub struct Model {
     pub name: String,
     pub local_path: Option<String>,
     pub base_url: Option<String>,
+
+    pub use_local_storage: bool,
 }
 
 impl ActiveModelBehavior for ActiveModel {}
index fac991bd51e9a919740e71d419bda95e8b6ae3ca..89b54b34805a11143645c477353773331946a0f8 100644 (file)
@@ -28,6 +28,7 @@ impl MigrationTrait for Migration {
                     .col(string("name"))
                     .col(string_null("local_path"))
                     .col(string_null("base_url"))
+                    .col(boolean("use_local_storage").default(false))
                     .to_owned(),
             )
             .await?;