@@ -30,6 +30,66 @@ module.exports = function(RED) {
3030 }
3131 } ) ;
3232
33+ function DropboxInNode ( n ) {
34+ RED . nodes . createNode ( this , n ) ;
35+ this . filepattern = n . filepattern || "" ;
36+ this . dropboxConfig = RED . nodes . getNode ( n . dropbox ) ;
37+ var credentials = this . dropboxConfig ? this . dropboxConfig . credentials : { } ;
38+ if ( ! credentials . appkey || ! credentials . appsecret ||
39+ ! credentials . accesstoken ) {
40+ this . warn ( "Missing dropbox credentials" ) ;
41+ return ;
42+ }
43+
44+ var node = this ;
45+ var dropbox = new Dropbox . Client ( {
46+ //uid: credentials.uid,
47+ key : credentials . appkey ,
48+ secret : credentials . appsecret ,
49+ token : credentials . accesstoken ,
50+ } ) ;
51+ node . status ( { fill :"blue" , shape :"dot" , text :"initializing" } ) ;
52+ dropbox . pullChanges ( function ( err , data ) {
53+ if ( err ) {
54+ node . error ( "initialization failed " + err . toString ( ) ) ;
55+ node . status ( { fill :"red" , shape :"ring" , text :"failed" } ) ;
56+ return ;
57+ }
58+ node . status ( { } ) ;
59+ node . state = data . cursor ( ) ;
60+ node . on ( "input" , function ( msg ) {
61+ node . status ( { fill :"blue" , shape :"dot" , text :"checking for changes" } ) ;
62+ dropbox . pullChanges ( node . state , function ( err , data ) {
63+ if ( err ) {
64+ node . warn ( "failed to fetch changes" + err . toString ( ) ) ;
65+ node . status ( { } ) ; // clear status since poll retries anyway
66+ return ;
67+ }
68+ node . state = data . cursor ( ) ;
69+ node . status ( { } ) ;
70+ var changes = data . changes ;
71+ for ( var i = 0 ; i < changes . length ; i ++ ) {
72+ var change = changes [ i ] ;
73+ msg . payload = change . path ;
74+ msg . file = change . path . substring ( change . path . lastIndexOf ( '/' ) + 1 ) ;
75+ msg . event = change . wasRemoved ? 'delete' : 'add' ;
76+ msg . data = change ;
77+ node . send ( msg ) ;
78+ }
79+ } ) ;
80+ } ) ;
81+ var interval = setInterval ( function ( ) {
82+ node . emit ( "input" , { } ) ;
83+ } , 900000 ) ; // 15 minutes
84+ node . on ( "close" , function ( ) {
85+ if ( interval !== null ) {
86+ clearInterval ( interval ) ;
87+ }
88+ } ) ;
89+ } ) ;
90+ }
91+ RED . nodes . registerType ( "dropbox in" , DropboxInNode ) ;
92+
3393 function DropboxQueryNode ( n ) {
3494 RED . nodes . createNode ( this , n ) ;
3595 this . filename = n . filename || "" ;
@@ -71,7 +131,6 @@ module.exports = function(RED) {
71131 }
72132 RED . nodes . registerType ( "dropbox" , DropboxQueryNode ) ;
73133
74-
75134 function DropboxOutNode ( n ) {
76135 RED . nodes . createNode ( this , n ) ;
77136 this . filename = n . filename || "" ;
0 commit comments