From a4548bb3e8252134dad278afc8e9fc0caa485264 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Jim=C3=A9nez=20Rivera?= Date: Thu, 6 Feb 2025 14:53:56 +0100 Subject: [PATCH] Add try catch in detect-context-menu-action --- .../callbacks/notify-fetch-data.callback.ts | 2 + examples/handlers/handle-change-size.ts | 8 ++-- examples/populate.ts | 5 ++- src/types/placeholder.type.ts | 14 ------- src/virtual-drive.ts | 4 +- .../detect-context-menu-action.service.ts | 2 +- src/watcher/events/on-add.service.ts | 6 +-- src/watcher/events/on-raw.service.ts | 37 ++++++++----------- 8 files changed, 30 insertions(+), 48 deletions(-) diff --git a/examples/callbacks/notify-fetch-data.callback.ts b/examples/callbacks/notify-fetch-data.callback.ts index 0b9103cb..0ba096da 100644 --- a/examples/callbacks/notify-fetch-data.callback.ts +++ b/examples/callbacks/notify-fetch-data.callback.ts @@ -1,3 +1,4 @@ +import { logger } from "examples/drive"; import { getInfoItem } from "examples/info-items-manager"; import { sleep } from "@/utils"; @@ -5,6 +6,7 @@ import { sleep } from "@/utils"; type TCallback = (data: boolean, path: string, errorHandler?: () => void) => Promise<{ finished: boolean; progress: number }>; export const fetchDataCallback = async (id: string, callback: TCallback) => { + logger.info({ fn: "fetchDataCallback", id }); const path = await getInfoItem(id); let finish = false; diff --git a/examples/handlers/handle-change-size.ts b/examples/handlers/handle-change-size.ts index d4713f8e..f3206cc4 100644 --- a/examples/handlers/handle-change-size.ts +++ b/examples/handlers/handle-change-size.ts @@ -1,14 +1,14 @@ import { drive, logger } from "examples/drive"; +import { v4 } from "uuid"; import { QueueItem } from "@/queue/queueManager"; export const handleChangeSize = async (task: QueueItem) => { try { logger.info({ fn: "handleChangeSize", path: task.path }); - const result = Math.random().toString(36).substring(2, 7); - drive.convertToPlaceholder(task.path, result); - drive.updateFileIdentity(task.path, result, false); - drive.updateSyncStatus(task.path, task.isFolder, true); + const id = v4(); + drive.convertToPlaceholder(task.path, id); + drive.updateFileIdentity(task.path, id, false); } catch (error) { logger.error("handleChangeSize", error); } diff --git a/examples/populate.ts b/examples/populate.ts index 4857ea51..0843a9d7 100644 --- a/examples/populate.ts +++ b/examples/populate.ts @@ -6,7 +6,9 @@ import { v4 } from "uuid"; import settings from "./settings"; -const rootFile1 = join(settings.syncRootPath, v4()); +const rootFileName1 = v4(); +const rootZipFileName = `${v4()}.zip`; +const rootFile1 = join(settings.syncRootPath, rootFileName1); const rootFile2ChangeSize = join(settings.syncRootPath, `change-size-${v4()}.txt`); const rootFile3 = join(settings.syncRootPath, `${v4()}.txt`); const rootFile3Moved = join(settings.syncRootPath, `moved-${v4()}.txt`); @@ -22,6 +24,7 @@ execSync(`echo Hello, world! > ${rootFile2ChangeSize}`); execSync(`echo Hello, world! >> ${rootFile2ChangeSize}`); // Sync execSync(`echo Hello, world! > ${rootFile3}`); execSync(`type nul > ${rootFile4}`); // No sync (0 bytes) +execSync(`cd ${settings.syncRootPath} && tar -cf ${rootZipFileName} ${rootFileName1}`); // Sync execSync(`mv ${rootFile3} ${rootFile3Moved}`); // Sync execSync(`mkdir ${rootFolder1}`); // Sync execSync(`mkdir ${rootFolder2}`); // Cloud (no files inside) diff --git a/src/types/placeholder.type.ts b/src/types/placeholder.type.ts index fba17cc3..0a779aa6 100644 --- a/src/types/placeholder.type.ts +++ b/src/types/placeholder.type.ts @@ -1,11 +1,9 @@ -// Enum para SyncState export enum SyncState { Undefined = -1, NotInSync = 0, InSync = 1, } -// Enum para PinState export enum PinState { Inherited = 0, AlwaysLocal = 1, @@ -13,15 +11,3 @@ export enum PinState { Unspecified = 3, Excluded = 4, } - -// Tipo para combinar ambos estados en un objeto de estado -export type Status = { - pinState: PinState; - syncState: SyncState; -}; - -export enum Attributes { - OTHER = 0, - NOT_PINNED = 1, - PINNED = 2, -} diff --git a/src/virtual-drive.ts b/src/virtual-drive.ts index a5810212..1c54e249 100644 --- a/src/virtual-drive.ts +++ b/src/virtual-drive.ts @@ -2,7 +2,6 @@ import path, { join, win32 } from "path"; import fs from "fs"; import { Watcher } from "./watcher/watcher"; import { ExtraCallbacks, InputSyncCallbacks } from "./types/callbacks.type"; -import { Status } from "./types/placeholder.type"; import { IQueueManager } from "./queue/queueManager"; import { createLogger } from "./logger"; @@ -16,7 +15,6 @@ class VirtualDrive { syncRootPath: string; callbacks?: Callbacks; - // private watcherBuilder: WatcherBuilder; private watcher: Watcher; constructor(syncRootPath: string, loggerPath?: string) { @@ -57,7 +55,7 @@ class VirtualDrive { } } - getPlaceholderState(path: string): Status { + getPlaceholderState(path: string) { return addon.getPlaceholderState({ path: this.fixPath(path) }); } diff --git a/src/watcher/detect-context-menu-action.service.ts b/src/watcher/detect-context-menu-action.service.ts index 3b22d2d1..f4606cb8 100644 --- a/src/watcher/detect-context-menu-action.service.ts +++ b/src/watcher/detect-context-menu-action.service.ts @@ -14,7 +14,7 @@ export class DetectContextMenuActionService { const isInDevice = self.fileInDevice.has(path); self.logger.info({ - event: "onRaw", + event: "change", path, status, itemId, diff --git a/src/watcher/events/on-add.service.ts b/src/watcher/events/on-add.service.ts index 88838f18..25e50fec 100644 --- a/src/watcher/events/on-add.service.ts +++ b/src/watcher/events/on-add.service.ts @@ -10,9 +10,9 @@ export class OnAddService { try { const ext = path.split(".").pop(); const { size, birthtime, mtime } = stats; - const fileIntenty = self.addon.getFileIdentity({ path }); + const itemId = self.addon.getFileIdentity({ path }); - self.logger.info({ fn: "onAdd", path, ext, size, birthtime, mtime, fileIntenty }); + self.logger.info({ fn: "onAdd", path, ext, size, birthtime, mtime, itemId }); if (!ext || size === 0 || size > 20 * 1024 * 1024 * 1024) return; @@ -27,7 +27,7 @@ export class OnAddService { let isNewFile = false; let isMovedFile = false; - if (!fileIntenty) { + if (!itemId) { // El archivo fue creado recientemente (dentro de los últimos 60 segundos) isNewFile = true; } else if (creationTime !== modificationTime) { diff --git a/src/watcher/events/on-raw.service.ts b/src/watcher/events/on-raw.service.ts index ed3c1c56..585c8b51 100644 --- a/src/watcher/events/on-raw.service.ts +++ b/src/watcher/events/on-raw.service.ts @@ -8,29 +8,22 @@ export class OnRawService { constructor(private readonly detectContextMenuAction: DetectContextMenuActionService = new DetectContextMenuActionService()) {} async execute({ self, event, path, details }: TProps) { - if (event === "change" && details.prev && details.curr) { - if (extname(path) === "") { - self.logger.info({ event: "onRaw", path, details: "No extension" }); - return; - } - - const item = await stat(path); - if (item.isDirectory()) { - self.logger.info({ event: "onRaw", path, details: "Is directory" }); - return; - } - - // // Ignorar archivos vacíos - // if (item.size === 0) { - // self.logger.info("Archivo vacío ignorado", path); - // return; - // } - - const action = await this.detectContextMenuAction.execute({ self, details, path, isFolder: false }); - - if (action) { - self.logger.info({ event: "onRaw", path, action }); + try { + if (event === "change" && details.prev && details.curr) { + const item = await stat(path); + if (item.isDirectory()) { + self.logger.info({ event: "change", path, details: "Is directory" }); + return; + } + + const action = await this.detectContextMenuAction.execute({ self, details, path, isFolder: false }); + + if (action) { + self.logger.info({ event: "change", path, action }); + } } + } catch (error) { + self.logger.error("Error on change", error); } } }