Skip to content

Directory creation not observed #5

@cyrilzakka

Description

@cyrilzakka

Hey! Really like this package. I'm running into a small issue whereby directory creation inside of the monitored directory is not detected. Renaming it on the other hand, is detected in logger.info("Event received: \(event.description)"). Here's a sample implementation:

func run() async throws {
        logger.info("Monitoring files in \(self.sourceURL.standardized.path)")
        
        monitor = try FileMonitor(directory: self.sourceURL.standardized)
        try monitor?.start()
        
        // MARK: - AsyncStream
        for await event in monitor!.stream {
            logger.info("Event received: \(event.description)")
            DispatchQueue.main.async {
                switch event {
                case .added(let file):
                    if file.pathExtension == "pdf" {
                        self.files.append(file)
                    } else if file.isDirectory {
                        print("A directory was just created. \(file)")
                        let category = Category(name: file.lastPathComponent, url: file)
                        self.folders.append(category)
                    }
                case .deleted(let deletedFile):
                    if deletedFile.pathExtension == "pdf" {
                        if let index = self.files.firstIndex(of: deletedFile) {
                            self.files.remove(at: index)
                        }
                    } else if deletedFile.isDirectory {
                        if let index = self.folders.firstIndex(where: { $0.url == deletedFile }) {
                            self.folders.remove(at: index)
                        }
                    }
                case .changed(let changedFile):
                    if changedFile.pathExtension == "pdf" {
                        print("Change observed in file: \(changedFile.path)")
                    } else if changedFile.isDirectory {
                        print("Change observed in directory: \(changedFile.path)")
                    }
                }
            }
        }
    }

Am I doing something general wrong, or is this not supported as of yet?

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions