react-native-fs icon indicating copy to clipboard operation
react-native-fs copied to clipboard

Monitoring File Changes similar to fs.watch from node.js

Open simitii opened this issue 7 years ago • 15 comments

Hi, I need to watch file changes in my project. This may be a little bit advanced functionality for this repository. However, I wonder what you think about it. If you find it improper for this repository, I may open a new repo(react-native-file-observer). I am looking forward to hear your comments...

IMPORTANT: I am not sure if Apple has any special policy about this kind of functionality in iOS apps. I didn't read any negative comment while I was searching about it. Please tell me, if you know...

Here is the API I planned to implement: enum ChangeEvent{ "ATTRIB", // Change in metadata of the file being watched "MODIFY", // Change in the file being watched "DELETE", // Delete the file being watched "CREATE_IN_DIR", // A new file is created in the directory being watched (maybe) }

watch(filepath: string, options: {recursive: boolean, background: boolean}, listener: function<eventType: ChangeEvent, filepath: string>) Starts watching the file at given filepath and calls listener function with (eventType, filepath) parameters when ChangeEvent happens. recursive parameter indicates whether all subdirectories should be watched or not. background parameter indicates if watching should continue while app is background or not.

unwatch(filepath: string, recursive: boolean, listener: function<eventType: ChangeEvent, filepath: string>) If listener function is specified, stops calling listener when ChangeEvent happens in the file at given filepath. Otherwise, stops watching the file at given filepath completely, which means all of the listener functions for the file won't be called anymore. recursive parameter indicates whether all subdirectories should be unwatched or not.

I planned to base iOS implementation on iMonitorMyFiles or I may use similar ideas explained in that repo. Thanks to @tblank555 and Android implementation on FileObserver Class which looks enough for this purpose.

simitii avatar Jul 27 '17 15:07 simitii

Thank your for reaching out @simitii! I am fine with it. Looking forward for your PR.

itinance avatar Jul 29 '17 09:07 itinance

Hello @simitii, I'm very interested with this because it fit with my use-case: trigger push notification when somebody capture screenshot (by observe screenshot folder). I'm curious of one thing: shall it work when app is killed? I'm working on Android project btw.

pewh avatar Jul 29 '17 19:07 pewh

And how different is FileObserver with ContentObserver class? What is the better for a situation to watch file has been added in a folder? Thanks :beers:

pewh avatar Jul 29 '17 19:07 pewh

Hi @pewh I don't know much about ContentObserver class but I think FileObserver seems better for watching files and folders. Check CREATE event from FileObserver for your purpose. Check this link for Working Background Services even if app is killed.

simitii avatar Jul 29 '17 22:07 simitii

Hi @simitii I'm trying to create something like react native file observer on Android, but unfortunately I'm struggling on error because my java skill is limited :/ I'm just curious, have you made a progress on yours?

pewh avatar Jul 31 '17 05:07 pewh

Hi @pewh I opened this issue in order to see comments early. I am now focusing on the other parts of my project. It may take 1-2 weeks to start implementing this. Good luck with yours...

simitii avatar Jul 31 '17 12:07 simitii

@simitii Glad to hear that my iOS file monitoring implementation will help in this feature addition! I'm happy to answer any questions you have about that code. Also, to address your concern about Apple having a special policy about this, I'm fairly certain that they do not care if you monitor the filesystem. You will, of course, be limited to monitoring the section of the filesystem that your app has access to, which is your app's sandbox.

tblank555 avatar Aug 02 '17 23:08 tblank555

hello @tblank555, what do you mean on "app's sandbox"? Btw my project is monitor screenshot folder, and shall it being rejected by Apple? Thanks

pewh avatar Aug 03 '17 03:08 pewh

@pewh "Apps Sandbox" means that every iOS-app got its own "home"-folder with several sub-directories like an own "tmp" and "Documents" and "Caches" and so on. This lib will not be able to watch global picture folders on iOS due to the sandbox-concept. But Apple offers an API for detecting screenshot. https://stackoverflow.com/a/30691533/1397160

itinance avatar Aug 03 '17 06:08 itinance

Hey Guys, I think this is a great feature and fits perfectly with this repo too as this is also seems to be an operation of file system (like node.js fs). I'm also starting to work on similar thing in my android app which will monitor the photos library and will ask user to sync it with cloud if they allow it. So it fits correctly in my use case i think. 😊

Do we have any progress on it ? Or any starting point for me or code example would save my time. Looking forward to a reply @pewh @simitii Thanks 👍

hamxabaig avatar Oct 05 '17 18:10 hamxabaig

@hamxabaig Hi,unfortunately I suspended my project for a while so I haven't made any progress on this feature. As a starting point, I think you should check out FileObserver Class. There are plenty of examples showing how to use it if search about "FileObserver Android". Good luck. 👍

simitii avatar Oct 06 '17 16:10 simitii

No problem @simitii I've already started my implementation. Looks quite good so far and works well. Maybe i can work on it. Will be updating here what i get.

hamxabaig avatar Oct 06 '17 21:10 hamxabaig

Hi any update on this ?

AdityaPedagada avatar Nov 08 '20 17:11 AdityaPedagada

Is there any other library available, to integrate in react native

AdityaPedagada avatar Nov 08 '20 17:11 AdityaPedagada

I write some simple code to make this: https://www.npmjs.com/package/react-native-file-observer Tks for ideals 👯

cuongnm2301 avatar Feb 22 '22 10:02 cuongnm2301