Class: Boxlet::App

Inherits:
Object
  • Object
show all
Includes:
Sys
Defined in:
lib/boxlet/app.rb

Constant Summary collapse

PUBLIC_COMMANDS =
{
  add_user: "Add a new user to the database",
  change_password: "Change a user's password",
  setup: "Initialize a directory as a Boxlet app (create folders, check space, etc.)."
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.routesObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/boxlet/app.rb', line 22

def self.routes
  routes = {
    # ["/", :get]                 => :index,
    # ["/auth"]                   => :auth,
    # ["/register_device", :post] => :register_device,
    # ["/notifications", :*]   => :notifications,
    ["/stats", :post]           => :stats,
    ["/push_files", :post]      => :push_files,
    ["/file_list"]              => :file_list,
    ["/file_info"]              => :file_info,
    ["/resync", :get]           => :resync,
    ["/flashback", :post]       => :flashback,
    ["/gallery", :get]          => :gallery,
    ["/gallery/images", :get]   => :gallery_images,
    ["/hello", :get]            => :hello,
  }
end

Instance Method Details

#add_user(args) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/boxlet/app.rb', line 99

def add_user(args)
  unless username = args['-u']
    raise 'You must specify a username with -u'
  end
  unless password = args['-p']
    raise 'You must specify a password with -p'
  end

  password = Boxlet::Util.encrypt(password)
  db = Boxlet::Db.connection
  query_params = {username: username, password: password}
  if db.collection('users').find(query_params).count > 0
    raise "Username \"#{username}\" already exists"
  else
    user = Boxlet::Models.user_model.merge({username: username, password: password})
    db.collection('users').insert(user)
    Boxlet.log(:info, "User created successfully")
  end
rescue Exception => e
  Boxlet.log(:fatal, "ERROR: #{e}")
end

#bindObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/boxlet/app.rb', line 40

def bind
  usage = Boxlet::Util.app_space_usage
  capacity = Boxlet::Util.app_space_capacity
  Boxlet.log(:info, "INFO: Space Utilization: #{usage}MB / #{capacity}MB (#{(usage.to_f / capacity).round(3)}%)")

  Rack::Builder.new do
    use Rack::Reloader
    # use Rack::FileUpload, :upload_dir => [Boxlet.config[:upload_dir] || APP_ROOT + "/uploads"]
    use Rack::FileUpload, Boxlet.config
    use Rack::Static, :urls => ["/uploads"]

    Boxlet::App.routes.each do |route, action|
      map route[0] do
        run Boxlet::Router.new(route[1] || :*, action)
      end
    end
  end.to_app
end

#change_password(args) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/boxlet/app.rb', line 121

def change_password(args)
  unless username = args['-u']
    raise 'You must specify a username with -u'
  end
  unless current_password = args['-p']
    raise 'You must provide the current password with -p'
  end
  unless new_password = args['--new']
    raise 'You must specify a new password with --new'
  end

  current_password = Boxlet::Util.encrypt(current_password)
  new_password = Boxlet::Util.encrypt(new_password)
  query_params = {username: username, password: current_password}
  db = Boxlet::Db.connection
  if db.collection('users').find(query_params).count > 0
    db.collection('users').update({username: username}, {'$set' => {password: new_password}})
    Boxlet.log(:info, "Password updated successfully for \"#{username}\"")
  else
    raise 'Username does not exist or password incorrect'
  end
rescue Exception => e
  Boxlet.log(:fatal, "ERROR: #{e}")
end

#setup(args) ⇒ Object



59
60
61
62
63
64
65
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
# File 'lib/boxlet/app.rb', line 59

def setup(args)
  begin
    Boxlet.log(:debug, Boxlet.config)
    # Create upload and tmp directories
    upload_dir = Boxlet.config[:upload_dir]
    tmp_dir = Boxlet.config[:tmp_dir]
    if !File.exists?(upload_dir) || !File.exists?(tmp_dir)
      if !File.exists?(upload_dir)
        Boxlet.log(:info, "Upload directory (#{upload_dir}) does not exist.  Creating...")
        Dir.mkdir(upload_dir)
        Boxlet.log(:info, "Upload directory created!")
      end
      if !File.exists?(tmp_dir)
        Boxlet.log(:info, "Temp directory (#{tmp_dir}) does not exist.  Creating...")
        Dir.mkdir(tmp_dir)
        Boxlet.log(:info, "Temp directory created!")
      end
      if File.exists?(upload_dir) && File.exists?(tmp_dir)
        Boxlet.log(:info, "Done creating directories.")
      else
        raise "Error creating directories.  Please check your config and file permissions, and retry."
      end
    end

    # Check for free space
    if !Boxlet.config[:s3][:enabled]
      if Boxlet::Util.free_space <= 50
        raise "Not enough free space"
      end
      if Boxlet::Util.app_space_usage / Boxlet::Util.app_space_capacity >= 0.9
        Boxlet.log(:warn, "App is over 90% full")
      end
    end

    Boxlet.log(:info, "Boxlet setup is done!")
  rescue Exception => e
    Boxlet.log(:fatal, "ERROR: #{e}")
  end
end