boxlet 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e2ec1c5b89c7db9e74e07a6b34f8d9ae898551fd
4
- data.tar.gz: 235577cc755e9701464c44ac48b4da0b4415f4c2
3
+ metadata.gz: 7254a1f35c0da22694d61a08e83789c1e5bf6b52
4
+ data.tar.gz: 18808b8a2b7794adb1b7923499d4062538c9c56b
5
5
  SHA512:
6
- metadata.gz: f16e15fb92292ce0d554fe4896b08486c94391b4305165295886ac1984553abf6bf2e473c35f10fd7a2591ee86429602fcbd37db6c68dfb919357785ccb8b477
7
- data.tar.gz: aaa795ced180ab9961897faaa64bac91c3b2423fcac6a55a099544fd083dfa28f9b286c5877e42e5e769dd04e0dba8e21a1cbbd2a90ebe2f2fa9ae3af7a486b2
6
+ metadata.gz: 2092efcac80e84ffab64f5983f4f21001f808696f78ffc0e94958e99b4185b9fa7fccc5233364ed4a56931e535b2cb82b740811012aa9ca1286d47144813a7b3
7
+ data.tar.gz: 90e72504780fd88192c61c27848adfd8e995534b2d1bb6ebe6ebd458e862a5a336cec89eafce27cda2d353da2dbfe495527d03597324798f3f3bbe4b325b948e
data/bin/boxlet CHANGED
@@ -3,7 +3,9 @@
3
3
  require 'boxlet'
4
4
 
5
5
  argv = *ARGV
6
- command = argv.count % 2 != 0 ? argv.shift : 'run'
6
+ raise 'Invalid command' if argv[0] && argv[0][0] == '-'
7
+ command = argv.shift
8
+ raise 'Invalid parameters' if argv.count % 2 != 0
7
9
  argv_hash = Hash[*argv]
8
10
 
9
11
  config = argv_hash["-c"] || argv_hash["--config"]
@@ -1,10 +1,10 @@
1
- require "yaml"
2
- require "rack/contrib"
3
- require "boxlet/version"
4
- require "boxlet/app"
5
- require "boxlet/config"
6
- require "boxlet/runner"
7
- require "pp" #unless ENV['RACK_ENV'] == 'production'
1
+ require 'yaml'
2
+ require 'rack/contrib'
3
+ require 'boxlet/version'
4
+ require 'boxlet/app'
5
+ require 'boxlet/log'
6
+ require 'boxlet/config'
7
+ require 'boxlet/runner'
8
8
 
9
9
 
10
10
  APP_ROOT = Dir.pwd
@@ -19,36 +19,39 @@ module Boxlet
19
19
 
20
20
  def run!(argv, command='run', config_file='config.yml', &blk)
21
21
  populate_params!(argv, config_file)
22
- app = Boxlet::App.new
22
+ @log = Boxlet::Log.new(@config[:log_file], (debug? ? Logger::DEBUG : Logger::INFO))
23
+ @app = Boxlet::App.new
23
24
 
24
25
  case command
25
26
  when 'run'
27
+ Boxlet.log(:debug, @config)
26
28
  @runner = Boxlet::Runner.new
27
- @runner.start(app.bind, &blk)
29
+ @runner.start(@app.bind, &blk)
28
30
  else
29
- app.send(command, Boxlet.config)
31
+ @app.send(command, argv)
30
32
  end
31
33
 
32
- app
34
+ @app
33
35
  end
34
36
 
35
37
  def stop!
36
38
  @runner.stop
37
- app
39
+ @app
38
40
  end
39
41
 
40
42
 
41
43
  def debug?
42
- @config[:debug] == true ? true : false
44
+ @config[:debug] == true
43
45
  end
44
-
45
46
  def config
46
47
  @config
47
48
  end
48
-
49
49
  def params
50
50
  @params
51
51
  end
52
+ def log(level, message)
53
+ @log.write(level, message)
54
+ end
52
55
 
53
56
  end
54
57
 
@@ -1,10 +1,11 @@
1
- require "sys/filesystem"
2
1
  #require "rack/boxlet_url_builder"
3
- require "rack/request"
4
- require "rack/response"
5
- require "rack/file_upload"
6
- require "boxlet/db"
7
- require "boxlet/app/router"
2
+ require 'rack/request'
3
+ require 'rack/response'
4
+ require 'rack/file_upload'
5
+ require 'boxlet/db'
6
+ require 'boxlet/util'
7
+ require 'boxlet/app/router'
8
+ require 'boxlet/app/models'
8
9
 
9
10
 
10
11
  module Boxlet
@@ -22,16 +23,15 @@ module Boxlet
22
23
  ["/push_files", :post] => :push_files,
23
24
  ["/file_list"] => :file_list,
24
25
  ["/file_info"] => :file_info,
25
- ["/resync", :get] => :resync
26
+ ["/resync", :get] => :resync,
27
+ ["/flashback", :post] => :flashback
26
28
  }
27
29
  end
28
30
 
29
31
  def bind
30
- if Boxlet.debug?
31
- usage = Boxlet::App.app_space_usage
32
- capacity = Boxlet::App.app_space_capacity
33
- puts "Space Utilization: #{usage}MB / #{capacity}MB (#{(usage.to_f / capacity).round(3)}%)"
34
- end
32
+ usage = Boxlet::Util.app_space_usage
33
+ capacity = Boxlet::Util.app_space_capacity
34
+ Boxlet.log(:info, "INFO: Space Utilization: #{usage}MB / #{capacity}MB (#{(usage.to_f / capacity).round(3)}%)")
35
35
 
36
36
  Rack::Builder.new do
37
37
  use Rack::Reloader
@@ -48,24 +48,25 @@ module Boxlet
48
48
  end
49
49
 
50
50
 
51
- def setup(config)
51
+ def setup(args)
52
52
  begin
53
+ Boxlet.log(:debug, Boxlet.config)
53
54
  # Create upload and tmp directories
54
55
  upload_dir = Boxlet.config[:upload_dir]
55
56
  tmp_dir = Boxlet.config[:tmp_dir]
56
57
  if !File.exists?(upload_dir) || !File.exists?(tmp_dir)
57
58
  if !File.exists?(upload_dir)
58
- puts "Upload directory (#{upload_dir}) does not exist. Creating..."
59
+ Boxlet.log(:info, "Upload directory (#{upload_dir}) does not exist. Creating...")
59
60
  Dir.mkdir(upload_dir)
60
- puts "Upload directory created!"
61
+ Boxlet.log(:info, "Upload directory created!")
61
62
  end
62
63
  if !File.exists?(tmp_dir)
63
- puts "Temp directory (#{tmp_dir}) does not exist. Creating..."
64
+ Boxlet.log(:info, "Temp directory (#{tmp_dir}) does not exist. Creating...")
64
65
  Dir.mkdir(tmp_dir)
65
- puts "Temp directory created!"
66
+ Boxlet.log(:info, "Temp directory created!")
66
67
  end
67
68
  if File.exists?(upload_dir) && File.exists?(tmp_dir)
68
- puts "Done creating directories."
69
+ Boxlet.log(:info, "Done creating directories.")
69
70
  else
70
71
  raise "Error creating directories. Please check your config and file permissions, and retry."
71
72
  end
@@ -73,61 +74,67 @@ module Boxlet
73
74
 
74
75
  # Check for free space
75
76
  if !Boxlet.config[:s3][:enabled]
76
- if Boxlet::App.free_space <= 50
77
+ if Boxlet::Util.free_space <= 50
77
78
  raise "Not enough free space"
78
79
  end
79
- if Boxlet::App.app_space_usage / Boxlet::App.app_space_capacity >= 0.9
80
- puts "App is over 90% full"
80
+ if Boxlet::Util.app_space_usage / Boxlet::Util.app_space_capacity >= 0.9
81
+ Boxlet.log(:warn, "App is over 90% full")
81
82
  end
82
83
  end
83
84
 
84
- puts "\nBoxlet setup is done!"
85
- rescue => e
86
- puts "\nERROR: #{e}"
85
+ Boxlet.log(:info, "Boxlet setup is done!")
86
+ rescue Exception => e
87
+ Boxlet.log(:fatal, "ERROR: #{e}")
87
88
  end
88
89
  end
89
90
 
90
91
 
91
- # App disk space functions
92
-
93
- def self.free_space
94
- return -1 if Boxlet.config[:s3][:enabled]
95
- Boxlet::App.app_space_capacity - Boxlet::App.app_space_usage
96
- end
92
+ def add_user(args)
93
+ unless username = args['-u']
94
+ raise 'You must specify a username with -u'
95
+ end
96
+ unless password = args['-p']
97
+ raise 'You must specify a password with -p'
98
+ end
97
99
 
98
- def self.app_space_capacity
99
- return -1 if Boxlet.config[:s3][:enabled]
100
- drive_free_space = Boxlet::App.drive_free_space
101
- if Boxlet.config[:capacity].is_a? String
102
- Boxlet::App.drive_free_space * Boxlet.config[:capacity].to_i / 100
100
+ password = Boxlet::Util.encrypt(password)
101
+ db = Boxlet::Db.connection
102
+ query_params = {username: username, password: password}
103
+ if db.collection('users').find(query_params).count > 0
104
+ raise "Username \"#{username}\" already exists"
103
105
  else
104
- Boxlet.config[:capacity]
106
+ user = Boxlet::Models.user_model.merge({username: username, password: password})
107
+ db.collection('users').insert(user)
108
+ Boxlet.log(:info, "User created successfully")
105
109
  end
110
+ rescue Exception => e
111
+ Boxlet.log(:fatal, "ERROR: #{e}")
106
112
  end
107
113
 
108
- def self.app_space_usage
109
- raise RuntimeError, "#{Boxlet.config[:upload_dir]} is not a directory" unless File.directory?(Boxlet.config[:upload_dir])
110
- return -1 if Boxlet.config[:s3][:enabled]
111
-
112
- total_size = 0
113
- Dir["#{Boxlet.config[:upload_dir]}/**/*"].each do |f|
114
- total_size += File.size(f) if File.file?(f) && File.size?(f)
114
+ def change_password(args)
115
+ unless username = args['-u']
116
+ raise 'You must specify a username with -u'
117
+ end
118
+ unless current_password = args['-p']
119
+ raise 'You must provide the current password with -p'
120
+ end
121
+ unless new_password = args['--new']
122
+ raise 'You must specify a new password with --new'
115
123
  end
116
- total_size / 1000000 # / 1048576 # to megabytes
117
- end
118
-
119
- # Drive disk space functions
120
-
121
- def self.drive_free_space
122
- stat = Filesystem.stat(Boxlet.config[:file_system_root])
123
- (stat.block_size * stat.blocks_available).to_mb
124
- end
125
124
 
126
- def self.drive_capacity
127
- stat = Filesystem.stat(Boxlet.config[:file_system_root])
128
- (stat.block_size * stat.blocks).to_mb
125
+ current_password = Boxlet::Util.encrypt(current_password)
126
+ new_password = Boxlet::Util.encrypt(new_password)
127
+ query_params = {username: username, password: current_password}
128
+ db = Boxlet::Db.connection
129
+ if db.collection('users').find(query_params).count > 0
130
+ db.collection('users').update({username: username}, {'$set' => {password: new_password}})
131
+ Boxlet.log(:info, "Password updated successfully for \"#{username}\"")
132
+ else
133
+ raise 'Username does not exist or password incorrect'
134
+ end
135
+ rescue Exception => e
136
+ Boxlet.log(:fatal, "ERROR: #{e}")
129
137
  end
130
138
 
131
-
132
139
  end
133
140
  end
@@ -11,7 +11,9 @@ require 'aws-sdk'
11
11
  # ["/notifications", :post] => :notifications,
12
12
  # ["/push_files", :post] => :push_files,
13
13
  # ["/file_list"] => :file_list,
14
- # ["/file_info"] => :file_info
14
+ # ["/file_info"] => :file_info,
15
+ # ["/resync", :get] => :resync,
16
+ # ["/flashback", :post] => :flashback
15
17
  # }
16
18
 
17
19
 
@@ -21,8 +23,8 @@ module Boxlet
21
23
 
22
24
  def initialize(request)
23
25
  @request = request
24
- @params = Boxlet.symbolize_keys request.params
25
- pp request.params
26
+ @params = Boxlet.symbolize_keys(request.params)
27
+ Boxlet.log(:info, request.params)
26
28
 
27
29
  @format = :html
28
30
  end
@@ -49,10 +51,10 @@ module Boxlet
49
51
  # def register_device
50
52
  # @format = :json
51
53
 
52
- # pp @params if Boxlet.config[:debug]
54
+ # Boxlet.log(:debug, @params)
53
55
 
54
56
  # uuid = @params[:uuid]
55
- # # user = user_model.merge { uuid: uuid }
57
+ # # user = Boxlet::Models.user_model.merge { uuid: uuid }
56
58
  # if db.collection('users').insert(user)
57
59
  # {response: true}
58
60
  # else
@@ -65,7 +67,6 @@ module Boxlet
65
67
 
66
68
  # uuid = @params[:uuid]
67
69
  # notifications = @params[:notifications]
68
- # pp uuid
69
70
  # # @user
70
71
  # "notifications"
71
72
  # end
@@ -74,8 +75,8 @@ module Boxlet
74
75
  @format = :json
75
76
 
76
77
  {
77
- capacity: Boxlet::App.app_space_capacity,
78
- usage: Boxlet::App.app_space_usage,
78
+ capacity: Boxlet::Util.app_space_capacity,
79
+ usage: Boxlet::Util.app_space_usage,
79
80
  free_space: free_space?
80
81
  }
81
82
  end
@@ -95,7 +96,7 @@ module Boxlet
95
96
  new_thumb_filename = "#{asset_path_params["id"]}-thumb.#{asset_path_params["ext"]}"
96
97
  new_thumb_path = File.join(upload_path, new_thumb_filename)
97
98
 
98
- if File.exists? new_path
99
+ if File.exists?(new_path)
99
100
  file = File.open(new_path, 'r')
100
101
  asset = {
101
102
  filename: new_filename,
@@ -112,7 +113,7 @@ module Boxlet
112
113
  Image.resize(new_path, new_thumb_path, 150, 150)
113
114
 
114
115
  if Boxlet.config[:s3][:enabled]
115
- pp 'Uploading to S3...' if Boxlet.config[:debug]
116
+ Boxlet.log(:debug, 'Uploading to S3...')
116
117
 
117
118
  s3 = AWS::S3.new(
118
119
  :access_key_id => Boxlet.config[:s3][:access_key_id],
@@ -127,9 +128,9 @@ module Boxlet
127
128
 
128
129
  FileUtils.rm(new_path)
129
130
  FileUtils.rm(new_thumb_path)
130
- pp 'Uploading to S3... Done!' if Boxlet.config[:debug]
131
+ Boxlet.log(:debug, 'Uploading to S3... Done!')
131
132
  else
132
- pp 'Uploading to S3... Error!!' if Boxlet.config[:debug]
133
+ Boxlet.log(:debug, 'Uploading to S3... Error!!')
133
134
  end
134
135
  end
135
136
  end
@@ -152,7 +153,7 @@ module Boxlet
152
153
 
153
154
  uuid = @params[:uuid]
154
155
  asset_path = @params[:asset_path]
155
- file_model.merge db.collection('assets').find({asset_path: asset_path, uuid: uuid}).to_a.first || {}
156
+ Boxlet::Models.file_model.merge db.collection('assets').find({asset_path: asset_path, uuid: uuid}).to_a.first || {}
156
157
  end
157
158
 
158
159
  def resync
@@ -165,6 +166,14 @@ module Boxlet
165
166
  end
166
167
  end
167
168
 
169
+ def flashback
170
+ @format = :json
171
+
172
+ date = Date.parse(@params[:date])
173
+ uuid = @params[:uuid]
174
+ db.collection('assets').find({uuid: uuid, asset_date: {:'$gte' => date.to_time, :'$lt' => (date + 1).to_time}}).to_a
175
+ end
176
+
168
177
 
169
178
  private
170
179
 
@@ -173,7 +182,7 @@ module Boxlet
173
182
  end
174
183
 
175
184
  def set_user
176
- user_model.merge db.collection('users').find({uuid: @params[:uuid]}).to_a.first || {}
185
+ Boxlet::Models.user_model.merge(db.collection('users').find({uuid: @params[:uuid]}).to_a.first || {})
177
186
  end
178
187
 
179
188
  def user_upload_dir
@@ -187,7 +196,7 @@ module Boxlet
187
196
 
188
197
  File.symlink(dir_name, user_upload_dir_shortname) if !File.symlink? user_upload_dir_shortname
189
198
 
190
- if File.symlink? user_upload_dir_shortname
199
+ if File.symlink?(user_upload_dir_shortname)
191
200
  user_upload_dir_shortname
192
201
  else
193
202
  user_upload_dir_name
@@ -198,31 +207,7 @@ module Boxlet
198
207
  end
199
208
 
200
209
  def free_space?
201
- Boxlet::App.free_space > 50
202
- end
203
-
204
-
205
- # Models
206
-
207
- def file_model
208
- {
209
- filename: '',
210
- size: 0,
211
- local_date: 0,
212
- thumbnail: '',
213
- asset_path: '',
214
- asset_date: '',
215
- uuid: []
216
- }
210
+ Boxlet::Util.free_space > 50
217
211
  end
218
-
219
- def user_model
220
- {
221
- uuid: '',
222
- notifications: 1,
223
- last_activity: Time.now
224
- }
225
- end
226
-
227
212
  end
228
213
  end
@@ -0,0 +1,26 @@
1
+ module Boxlet
2
+ module Models
3
+ def self.file_model
4
+ {
5
+ filename: '',
6
+ size: 0,
7
+ local_date: 0,
8
+ thumbnail: '',
9
+ asset_path: '',
10
+ asset_date: '',
11
+ uuid: []
12
+ }
13
+ end
14
+
15
+ def self.user_model
16
+ {
17
+ username: '',
18
+ password: '',
19
+ uuid: '',
20
+ notifications: 1,
21
+ last_activity: Time.now
22
+ }
23
+ end
24
+
25
+ end
26
+ end
@@ -1,7 +1,7 @@
1
- require "rack/request"
2
- require "rack/response"
3
- require "boxlet/app/controller"
4
- require "json"
1
+ require 'rack/request'
2
+ require 'rack/response'
3
+ require 'boxlet/app/controller'
4
+ require 'json'
5
5
 
6
6
 
7
7
  module Boxlet
@@ -17,14 +17,12 @@ module Boxlet
17
17
  def call(env)
18
18
  request = Rack::Request.new(env)
19
19
 
20
- if Boxlet.debug?
21
- puts "\n#{env["REMOTE_ADDR"]} - [#{Time.now.to_s}] #{@method.upcase} #{env["SERVER_PROTOCOL"]} => #{env["REQUEST_PATH"]}"
22
- end
20
+ Boxlet.log(:info, "INFO: #{env["REMOTE_ADDR"]} - [#{Time.now.to_s}] #{@method.upcase} #{env["SERVER_PROTOCOL"]} => #{env["REQUEST_PATH"]}")
23
21
 
24
22
  response = Rack::Response.new
25
23
  controller = Boxlet::Controller.new(request)
26
24
  if @method == :* || (request.get? && @method == :get) || (request.post? && @method == :post)
27
- puts "Responding: #{@method.upcase} => #{@action}" if Boxlet.debug?
25
+ Boxlet.log(:info, "INFO: Responding: #{@method.upcase} => #{@action}")
28
26
  action_response = controller.action(@action)
29
27
  response.status = 200
30
28
  else
@@ -33,9 +31,9 @@ module Boxlet
33
31
  end
34
32
 
35
33
  if action_response[:format] == :json
36
- response.write action_response[:content].to_json
34
+ response.write(action_response[:content].to_json)
37
35
  else
38
- response.write action_response[:content]
36
+ response.write(action_response[:content])
39
37
  end
40
38
 
41
39
  response.finish
@@ -70,11 +70,7 @@ module Boxlet
70
70
  @raw_params = parse_arguments(argv)
71
71
 
72
72
  @config = @raw_params
73
-
74
73
  @config[:debug] = @raw_config[:debug] || @raw_params[:debug]
75
- if @config[:debug]
76
- pp @config
77
- end
78
74
  end
79
75
 
80
76
 
@@ -88,44 +84,41 @@ module Boxlet
88
84
  }
89
85
  end
90
86
 
87
+
91
88
  private
92
89
 
93
- def parse_arguments(argv)
94
- # params = Hash.new
95
- params = @raw_config
90
+ def parse_arguments(argv)
91
+ params = @raw_config
96
92
 
97
- ARGS.each_pair do |param_name, param_attrs|
98
- param_short_name = param_attrs[:short]
99
- config_value = @raw_config[param_name.to_sym]
100
- default = param_attrs[:default]
101
- sanitizer = param_attrs[:sanitizer]
93
+ ARGS.each_pair do |param_name, param_attrs|
94
+ param_short_name = param_attrs[:short]
95
+ config_value = @raw_config[param_name.to_sym]
96
+ default = param_attrs[:default]
97
+ sanitizer = param_attrs[:sanitizer]
102
98
 
103
- param_value = argv["--#{param_name}"] ||
104
- (param_short_name.nil? ? nil : argv["-#{param_short_name}"]) ||
105
- (config_value.nil? ? nil : config_value) ||
106
- (default.is_a?(Proc) ? default.call : default)
99
+ param_value = argv["--#{param_name}"] ||
100
+ (param_short_name.nil? ? nil : argv["-#{param_short_name}"]) ||
101
+ (config_value.nil? ? nil : config_value) ||
102
+ (default.is_a?(Proc) ? default.call : default)
107
103
 
108
- param_value = sanitizer.call(param_value) if sanitizer.is_a?(Proc)
104
+ param_value = sanitizer.call(param_value) if sanitizer.is_a?(Proc)
109
105
 
110
- if !param_value.nil?
111
- params[param_name] = param_value
106
+ if !param_value.nil?
107
+ params[param_name] = param_value
108
+ end
112
109
  end
113
- end
114
110
 
115
- params
116
- end
111
+ params
112
+ end
117
113
 
118
- def load_config_file(path_to_config)
119
- begin
120
- loaded_config = YAML.load_file(path_to_config)
121
- symbolize_keys(loaded_config)
122
- rescue
123
- puts "Error loading config file! Using defaults..."
124
- {}
114
+ def load_config_file(path_to_config)
115
+ begin
116
+ loaded_config = YAML.load_file(path_to_config)
117
+ symbolize_keys(loaded_config)
118
+ rescue
119
+ Boxlet.log(:warn, "Error loading config file! Using defaults...")
120
+ {}
121
+ end
125
122
  end
126
123
  end
127
-
128
- end
129
-
130
-
131
124
  end
@@ -19,7 +19,7 @@ module Boxlet
19
19
 
20
20
  host = db_config[:host] || 'localhost'
21
21
  port = db_config[:port] || MongoClient::DEFAULT_PORT
22
- puts "Connecting to #{host}:#{port}" if Boxlet.debug?
22
+ Boxlet.log(:info, "INFO: Connecting to #{host}:#{port}")
23
23
  client = MongoClient.new(host, port)
24
24
  db = client.db(db_config[:db] || 'boxlet_development')
25
25
  return db
@@ -1,17 +1,44 @@
1
- require "logger"
2
- require "fileutils"
1
+ require 'logger'
2
+ require 'fileutils'
3
+ require 'pp'
3
4
 
4
5
 
5
- module Log
6
- def get_logger(name, shift_age, shift_size, level)
7
- # create the directory for the log if it doesn't exist
8
- if !File.exist? File.dirname(name) then
9
- FileUtils.mkdir_p File.dirname(name)
6
+ module Boxlet
7
+ class Log
8
+ attr_accessor :log
9
+
10
+ # The levels are:
11
+ # UNKNOWN: An unknown message that should always be logged.
12
+ # FATAL: An unhandleable error that results in a program crash.
13
+ # ERROR: A handleable error condition.
14
+ # WARN: A warning.
15
+ # INFO: Generic (useful) information about system operation.
16
+ # DEBUG: Low-level information for developers.
17
+ def initialize(filename, level, rotation=10, max_size=1024000)
18
+ # create the directory for the log if it doesn't exist
19
+ if !File.exist? File.dirname(filename)
20
+ FileUtils.mkdir_p File.dirname(filename)
21
+ end
22
+
23
+ @log ||= Logger.new(filename, rotation, max_size)
24
+ @log.level = level
25
+ @log
26
+ end
27
+
28
+ # Possible levels: (:debug, :info, :warn, :error, :fatal)
29
+ def write(level, message)
30
+ if Boxlet.debug?
31
+ if message.is_a? String
32
+ puts message
33
+ else
34
+ pp message
35
+ end
36
+ else
37
+ if level == :info
38
+ pp message
39
+ end
40
+ @log.call(level, message)
41
+ end
10
42
  end
11
-
12
- # create the logger and give it back
13
- log = Logger.new(name, shift_age, shift_size)
14
- log.level = level
15
- log
16
43
  end
17
44
  end
@@ -1,4 +1,4 @@
1
- require "handlers/thin"
1
+ require 'handlers/thin'
2
2
 
3
3
 
4
4
  module Boxlet
@@ -0,0 +1,56 @@
1
+ require 'sys/filesystem'
2
+
3
+ module Boxlet
4
+ module Util
5
+ include Sys
6
+
7
+ # Auth methods
8
+
9
+ def self.encrypt(string)
10
+ (Digest::SHA256.new << string).to_s
11
+ end
12
+
13
+ # App disk space functions
14
+
15
+ def self.free_space
16
+ return -1 if Boxlet.config[:s3][:enabled]
17
+ Boxlet::Util.app_space_capacity - Boxlet::Util.app_space_usage
18
+ end
19
+
20
+ def self.app_space_capacity
21
+ return -1 if Boxlet.config[:s3][:enabled]
22
+ drive_free_space = Boxlet::Util.drive_free_space
23
+ if Boxlet.config[:capacity].is_a?(String)
24
+ Boxlet::Util.drive_free_space * Boxlet.config[:capacity].to_i / 100
25
+ else
26
+ Boxlet.config[:capacity]
27
+ end
28
+ end
29
+
30
+ def self.app_space_usage
31
+ raise RuntimeError, "#{Boxlet.config[:upload_dir]} is not a directory" unless File.directory?(Boxlet.config[:upload_dir])
32
+ return -1 if Boxlet.config[:s3][:enabled]
33
+
34
+ total_size = 0
35
+ Dir["#{Boxlet.config[:upload_dir]}/**/*"].each do |f|
36
+ total_size += File.size(f) if File.file?(f) && File.size?(f)
37
+ end
38
+ total_size / 1000000 # / 1048576 # to megabytes
39
+ rescue Exception => e
40
+ Boxlet.log(:fatal, "ERROR: #{e}")
41
+ end
42
+
43
+ # Drive disk space functions
44
+
45
+ def self.drive_free_space
46
+ stat = Filesystem.stat(Boxlet.config[:file_system_root])
47
+ (stat.block_size * stat.blocks_available).to_mb
48
+ end
49
+
50
+ def self.drive_capacity
51
+ stat = Filesystem.stat(Boxlet.config[:file_system_root])
52
+ (stat.block_size * stat.blocks).to_mb
53
+ end
54
+
55
+ end
56
+ end
@@ -1,3 +1,3 @@
1
1
  module Boxlet
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
@@ -1,6 +1,6 @@
1
- require "thin"
2
- require "rack/content_length"
3
- require "rack/chunked"
1
+ require 'thin'
2
+ require 'rack/content_length'
3
+ require 'rack/chunked'
4
4
 
5
5
 
6
6
  module Boxlet
@@ -1,5 +1,5 @@
1
- require "rack"
2
- require "rack/boxlet_url_map"
1
+ require 'rack'
2
+ require 'rack/boxlet_url_map'
3
3
 
4
4
 
5
5
  module Rack
@@ -1,4 +1,4 @@
1
- require "rack"
1
+ require 'rack'
2
2
 
3
3
 
4
4
  module Rack
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: boxlet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - arktisklada
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-12 00:00:00.000000000 Z
11
+ date: 2015-03-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -205,11 +205,13 @@ files:
205
205
  - lib/boxlet.rb
206
206
  - lib/boxlet/app.rb
207
207
  - lib/boxlet/app/controller.rb
208
+ - lib/boxlet/app/models.rb
208
209
  - lib/boxlet/app/router.rb
209
210
  - lib/boxlet/config.rb
210
211
  - lib/boxlet/db.rb
211
212
  - lib/boxlet/log.rb
212
213
  - lib/boxlet/runner.rb
214
+ - lib/boxlet/util.rb
213
215
  - lib/boxlet/version.rb
214
216
  - lib/handlers/thin.rb
215
217
  - lib/rack/boxlet_url_builder.rb