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.



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

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

.login(name) ⇒ Object



176
177
178
# File 'lib/dropbox-folder-sync/app.rb', line 176

def  name
  new.(name)
end

.logout(name) ⇒ Object



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

def logout name
  new.logout(name)
end

.sync(remote, local, options) ⇒ Object



173
174
175
# File 'lib/dropbox-folder-sync/app.rb', line 173

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

Instance Method Details

#check_local_modifiedObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/dropbox-folder-sync/app.rb', line 106

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



130
131
132
133
134
135
136
137
138
139
# File 'lib/dropbox-folder-sync/app.rb', line 130

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

#local_file_meta(path) ⇒ Object



52
53
54
55
56
# File 'lib/dropbox-folder-sync/app.rb', line 52

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

#local_path(remote) ⇒ Object



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

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

#log(message) ⇒ Object



48
49
50
# File 'lib/dropbox-folder-sync/app.rb', line 48

def log(message)
  puts message
end

#login(name) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/dropbox-folder-sync/app.rb', line 19

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

  @session.get_request_token
  authorize_url = @session.get_authorize_url
  puts "Login: [#{name}] ---> #{authorize_url}"
  Launchy.open authorize_url
  while 1
    @session.get_access_token rescue {}
    break if @session.authorized?
    sleep 1
  end
  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)
  true
end

#logout(name) ⇒ Object



42
43
44
45
46
# File 'lib/dropbox-folder-sync/app.rb', line 42

def logout 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



163
164
165
166
167
168
169
170
# File 'lib/dropbox-folder-sync/app.rb', line 163

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

#path_normalize(path) ⇒ Object



157
158
159
160
161
# File 'lib/dropbox-folder-sync/app.rb', line 157

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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/dropbox-folder-sync/app.rb', line 66

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



58
59
60
# File 'lib/dropbox-folder-sync/app.rb', line 58

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

#sync(remote, local, options) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/dropbox-folder-sync/app.rb', line 141

def sync(remote,local,options)
  name,@remote_root = parse_remote(remote)
  @local_root = path_normalize(File.expand_path(local))
  (name) 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