Class: DropboxFolderSync::App

Inherits:
Object
  • Object
show all
Defined in:
lib/dropbox-folder-sync/app.rb

Constant Summary collapse

APP_KEY =
ENV['DROPBOX_FOLDER_SYNC_APP_KEY']
APP_SECRET =
ENV['DROPBOX_FOLDER_SYNC_APP_SECRET']

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApp

Returns a new instance of App.



13
14
15
16
17
18
# File 'lib/dropbox-folder-sync/app.rb', line 13

def initialize
  unless (APP_KEY and APP_SECRET)
    raise "set env vars 'DROPBOX_FOLDER_SYNC_APP_KEY' and 'DROPBOX_FOLDER_SYNC_APP_SECRET'"
  end
  @session = DropboxSession.new(APP_KEY, APP_SECRET)
end

Class Method Details

.json(name) ⇒ Object



205
206
207
# File 'lib/dropbox-folder-sync/app.rb', line 205

def json name
  new.json name
end

.login(name) ⇒ Object



198
199
200
# File 'lib/dropbox-folder-sync/app.rb', line 198

def  name
  new.(name)
end

.logout(name) ⇒ Object



201
202
203
# File 'lib/dropbox-folder-sync/app.rb', line 201

def logout name
  new.logout(name)
end

.sync(remote, local, options) ⇒ Object



195
196
197
# File 'lib/dropbox-folder-sync/app.rb', line 195

def sync remote,local,options
  new.sync(remote,local,options)
end

Instance Method Details

#check_local_modifiedObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/dropbox-folder-sync/app.rb', line 126

def check_local_modified
  # check local modified
  Dir::glob(@local_root + "/**/*").each { |path|
    remote = remote_path(path)
    # new file
    unless @local_files.include?(path)
      @local_files[path] = local_file_meta(path)
      log "<---- #{path}"
      if File.file?(path)
        @client.put_file(remote, open(path))
      elsif File.directory?(path)
        @client.file_create_folder(remote)
      end
      next
    end

    # modified
    if File.file?(path) and File.mtime(path) > @local_files[path][:modified]
      @local_files[path] = local_file_meta(path)
      @client.put_file(remote, open(path))
    end
  }
end

#check_local_removedObject



150
151
152
153
154
155
156
157
158
159
# File 'lib/dropbox-folder-sync/app.rb', line 150

def check_local_removed
  # check removed
  @local_files.each { |path,v|
    unless File.exists?(path)
      log "remote delete #{path}"
      @local_files.reject! { |k,v| k == path }
      @client.file_delete(remote_path(path))
    end
  }
end

#json(name) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/dropbox-folder-sync/app.rb', line 20

def json name
  req_key = Keystorage.get("DROPBOX_APP_"+APP_KEY+"_REQ_KEY",name)
  req_secret = Keystorage.get("DROPBOX_APP_"+APP_KEY+"_REQ_SECRET",name)
  key = Keystorage.get("DROPBOX_APP_"+APP_KEY+"_USER_KEY",name)
  secret = Keystorage.get("DROPBOX_APP_"+APP_KEY+"_USER_SECRET",name)
  h = {:name => name, :req_key =>req_key, :req_secret =>req_secret, :key =>key, :secret =>secret}
  puts h.to_json
end

#local_file_meta(path) ⇒ Object



72
73
74
75
76
# File 'lib/dropbox-folder-sync/app.rb', line 72

def local_file_meta(path)
  { :path => path,
    :modified => File.mtime(path),
    :id_dir => File.directory?(path)}
end

#local_path(remote) ⇒ Object



82
83
84
# File 'lib/dropbox-folder-sync/app.rb', line 82

def local_path remote
  @local_root+remote[@remote_root.length..-1]
end

#log(message) ⇒ Object



68
69
70
# File 'lib/dropbox-folder-sync/app.rb', line 68

def log(message)
  puts message
end

#login(name) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/dropbox-folder-sync/app.rb', line 29

def  name
  key = Keystorage.get("DROPBOX_APP_"+APP_KEY+"_USER_KEY",name)
  secret = Keystorage.get("DROPBOX_APP_"+APP_KEY+"_USER_SECRET",name)
  if key and secret
    @session.set_access_token(key,secret)
    @session.get_access_token rescue {}
    return true if @session.authorized?
  end

  req_key = Keystorage.get("DROPBOX_APP_"+APP_KEY+"_REQ_KEY",name)
  req_secret = Keystorage.get("DROPBOX_APP_"+APP_KEY+"_REQ_SECRET",name)
  if req_key and req_secret
    @session.set_request_token(req_key,req_secret)
    @session.get_access_token rescue {}
    if @session.authorized?
      Keystorage.set("DROPBOX_APP_"+APP_KEY+"_USER_KEY",name,@session.access_token.key.to_s)
      Keystorage.set("DROPBOX_APP_"+APP_KEY+"_USER_SECRET",name,@session.access_token.secret.to_s)
      Keystorage.delete("DROPBOX_APP_"+APP_KEY+"_REQ_KEY",name)
      Keystorage.delete("DROPBOX_APP_"+APP_KEY+"_REQ_SECRET",name)
      return true
    end
  end

  @session.get_request_token
  authorize_url = @session.get_authorize_url
  log "Login: [#{name}] ---> #{authorize_url}"
  Launchy.open authorize_url
  Keystorage.set("DROPBOX_APP_"+APP_KEY+"_REQ_KEY",name,@session.request_token.key.to_s)
  Keystorage.set("DROPBOX_APP_"+APP_KEY+"_REQ_SECRET",name,@session.request_token.secret.to_s)
end

#logout(name) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/dropbox-folder-sync/app.rb', line 60

def logout name
  Keystorage.delete("DROPBOX_APP_"+APP_KEY+"_REQ_KEY",name)
  Keystorage.delete("DROPBOX_APP_"+APP_KEY+"_REQ_SECRET",name)
  Keystorage.delete("DROPBOX_APP_"+APP_KEY+"_USER_KEY",name)
  Keystorage.delete("DROPBOX_APP_"+APP_KEY+"_USER_SECRET",name)
  true
end

#parse_remote(remote) ⇒ Object



185
186
187
188
189
190
191
192
# File 'lib/dropbox-folder-sync/app.rb', line 185

def parse_remote remote
  name = "default"
  if /^([^\/]+):(.*)/ =~ remote
    name = $1
    remote = $2
  end
  [name,path_normalize(remote)]
end

#path_normalize(path) ⇒ Object



179
180
181
182
183
# File 'lib/dropbox-folder-sync/app.rb', line 179

def path_normalize path
  path = "/" + path if path[0,1] != "/"
  path = path[0,path.length - 1] if path[-1,1] == "/"
  path
end

#remote_delta(cur) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/dropbox-folder-sync/app.rb', line 86

def remote_delta cur
  delta = @client.delta(cur)
  cur = delta["cursor"]

  if delta["reset"] == "true"
    @local_files = {}
    Dir::glob(@local_root + "/**/*").each { |path|
      @local_files[path] = local_file_meta(path)
    }
  end

  delta["entries"].each { |path,meta|
    local = local_path(path)
    if path == @remote_root or /^#{@remote_root}\// =~ path
      unless meta
        if File.exists?(local)
          log "remove #{local}"
          FileUtils.remove_entry(local)
          @local_files.reject! { |k,v| k == local }
          if File.directory?(local)
            @local_files.reject! { |k,v| /^#{local}\/.*/ =~ k }
          end
        end
        next
      end

      if meta["is_dir"] #dir
        log "----> #{local}"
        FileUtils.mkdir_p local unless File.exists?(local)
      else #file
        out = @client.get_file(path)
        open(local, 'w'){|f| f.puts out }
        log "----> #{local}"
      end
      @local_files[local] = local_file_meta(local)
    end
  } # delta
  cur
end

#remote_path(local) ⇒ Object



78
79
80
# File 'lib/dropbox-folder-sync/app.rb', line 78

def remote_path local
  @remote_root+local[@local_root.length..-1]
end

#sync(remote, local, options) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/dropbox-folder-sync/app.rb', line 161

def sync(remote,local,options)
  name,@remote_root = parse_remote(remote)
  @local_root = path_normalize(File.expand_path(local))
  (name) unless @session.authorized?
  raise "login before sync" unless @session.authorized?

  @client = DropboxClient.new(@session, :dropbox)
  log "#{@remote_root} <---> #{@local_root}"
  @local_files = {}
  cur = nil
  while true
    cur = remote_delta(cur)
    check_local_modified
    check_local_removed
    sleep options[:interval]
  end
end