Manav Rathi пре 1 година
родитељ
комит
ca36b3c750
3 измењених фајлова са 49 додато и 5 уклоњено
  1. 2 0
      desktop/src/main.ts
  2. 20 4
      desktop/src/main/services/watch.ts
  3. 27 1
      desktop/src/main/stores/watch.ts

+ 2 - 0
desktop/src/main.ts

@@ -27,6 +27,7 @@ import { setupAutoUpdater } from "./main/services/app-update";
 import autoLauncher from "./main/services/auto-launcher";
 import { createWatcher } from "./main/services/watch";
 import { userPreferences } from "./main/stores/user-preferences";
+import { migrateLegacyWatchStoreIfNeeded } from "./main/stores/watch";
 import { registerStreamProtocol } from "./main/stream";
 import { isDev } from "./main/util";
 
@@ -324,6 +325,7 @@ const main = () => {
     setupRendererServer();
     registerPrivilegedSchemes();
     increaseDiskCache();
+    migrateLegacyWatchStoreIfNeeded();
 
     app.on("second-instance", () => {
         // Someone tried to run a second instance, we should focus our window.

+ 20 - 4
desktop/src/main/services/watch.ts

@@ -56,6 +56,26 @@ export const watchGet = () => {
     return folderWatches();
 };
 
+const folderWatches = () => {
+    let watches = watchStore.get("mappings") ?? [];
+
+    // Previous versions of the store used to store an integer to indicate the
+    // collection mapping, migrate these to the new schema if we see them still.
+    let needsUpdate = false;
+    watches = watches.map((watch) => {
+        const cm = watch.collectionMapping;
+        if (cm != "root" && cm != "parent") {
+            const uploadStrategy = watch.uploadStrategy;
+            const collectionMapping = uploadStrategy == 1 ? "parent" : "root";
+            needsUpdate = true;
+            return { ...watch, collectionMapping }
+        }
+    })
+    if (watches.length && watches)
+    return mappings;
+};
+
+
 export const watchAdd = async (
     watcher: FSWatcher,
     folderPath: string,
@@ -139,10 +159,6 @@ export function updateWatchMappingIgnoredFiles(
     setWatchMappings(watchMappings);
 }
 
-const folderWatches = () => {
-    const mappings = watchStore.get("mappings") ?? [];
-    return mappings;
-};
 
 function setWatchMappings(watchMappings: WatchStoreType["mappings"]) {
     watchStore.set("mappings", watchMappings);

+ 27 - 1
desktop/src/main/stores/watch.ts

@@ -1,10 +1,16 @@
 import Store, { Schema } from "electron-store";
 import { type FolderWatch } from "../../types/ipc";
+import log from "../log";
 
 interface WatchStore {
-    mappings: FolderWatch[];
+    mappings: FolderWatchWithLegacyFields[];
 }
 
+type FolderWatchWithLegacyFields = FolderWatch & {
+    /** @deprecated Only retained for migration, do not use in other code */
+    uploadStrategy: number;
+};
+
 const watchStoreSchema: Schema<WatchStore> = {
     mappings: {
         type: "array",
@@ -39,3 +45,23 @@ export const watchStore = new Store({
     name: "watch-status",
     schema: watchStoreSchema,
 });
+
+/**
+ * Previous versions of the store used to store an integer to indicate the
+ * collection mapping, migrate these to the new schema if we encounter them.
+ */
+export const migrateLegacyWatchStoreIfNeeded = () => {
+    let needsUpdate = false;
+    const watches = watchStore.get("mappings")?.map((watch) => {
+        let collectionMapping = watch.collectionMapping;
+        if (!collectionMapping) {
+            collectionMapping = watch.uploadStrategy == 1 ? "parent" : "root";
+            needsUpdate = true;
+        }
+        return { ...watch, collectionMapping };
+    });
+    if (needsUpdate) {
+        watchStore.set("mappings", watches);
+        log.info("Migrated legacy watch store data to new schema");
+    }
+};