Top Level Namespace
Defined Under Namespace
Classes: String
Constant Summary collapse
- VERSION_SIGN =
"0.0.30"
- MIN_USER =
3
- MIN_PASS =
7
- CA_PHOSEUM =
"-----BEGIN CERTIFICATE----- MIID9zCCAt+gAwIBAgIJAMKPNoBSyDt9MA0GCSqGSIb3DQEBCwUAMIGRMQswCQYD VQQGEwJOTDEVMBMGA1UECAwMWnVpZC1Ib2xsYW5kMSAwHgYDVQQKDBdTcGVjdHJv TkVUIEhhY2tpbmcgQ29ycDEMMAoGA1UECwwDSUNUMRYwFAYDVQQDDA0qLnNjaWVu Y2UubmV0MSMwIQYJKoZIhvcNAQkBFhRzcGVjdHJvbWFuQHlhaG9vLmNvbTAeFw0x NTEwMDUxNDQ1MjJaFw00NTA5MjcxNDQ1MjJaMIGRMQswCQYDVQQGEwJOTDEVMBMG A1UECAwMWnVpZC1Ib2xsYW5kMSAwHgYDVQQKDBdTcGVjdHJvTkVUIEhhY2tpbmcg Q29ycDEMMAoGA1UECwwDSUNUMRYwFAYDVQQDDA0qLnNjaWVuY2UubmV0MSMwIQYJ KoZIhvcNAQkBFhRzcGVjdHJvbWFuQHlhaG9vLmNvbTCCASIwDQYJKoZIhvcNAQEB BQADggEPADCCAQoCggEBAJeWOJx0h1yzLdPx0tYv9IP2YtUzkGYonM5X8hEgOkRh jxL+WwSWvaHjGUkvowzveh5gEeRAET+TkGslxG8yefaFJ9J2+0/83gqcd4A2iFpd wp8aHqFs7Qg8Y4TIY8Ww1OTeXqqxuRnsn3oF5HS8U2W5atkd7kpdUOYPw8siSuQV xVKoDQMVo6ZE+RPspRRuEwnyk3zQHnaEcgCGtssVFuvhsObCB55rWMafxP4UbGvX WzqxmVcHDAgHXl/neAhg1EPhrRqYDf6pIo207eWvuqAVCtFQfXf4YtzuCml7GHrM TGCRDf/g9KBdtvispd4VCVKEUFKx3okqQIzW3dsXPy0CAwEAAaNQME4wHQYDVR0O BBYEFF6zq/J7W2EIi41YOsDoo/AmMVZYMB8GA1UdIwQYMBaAFF6zq/J7W2EIi41Y OsDoo/AmMVZYMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQELBQADggEBACgmgSHr YffpiSMbZUzMtC8rcexeoMDw1X9UobdanPU0FBgYruj4yjdoEIsrlaXTgHf3PEki NWShCsRCAOZIPQkEzwN/0wom+JhB6xbURPiwSrG6TEhz8YFBn94eiHz1OycHxpf6 QD85+tN847Q0V0KXmrwoQkIEVXT4of/NRhzHQlCkHuDoT5oQdkN7W3cZFqWkXGRM t6fZ03zeXlSH5HLmysm2DU2uB182orzWYABdwLmINR44isw8/6RS1mx8BGI+08l8 sfxrWyWaDOPnwLWbFWTqnv94w4frHyHoRY+lBPZ4DT8MAFxYXI5Rf/hBUGav5NM4 yivphjaSSCTRqyE= -----END CERTIFICATE-----"
Instance Method Summary collapse
- #add_user(user, password, role) ⇒ Object
- #api_caller(json_body, auth = false, cli = false) ⇒ Object
- #change_role(user, new_role) ⇒ Object
- #change_user_password(user, pass, auth = false) ⇒ Object
- #change_username(user, new_user) ⇒ Object
- #check_image_exists(fpath) ⇒ Object
- #check_string_sanity(album) ⇒ Object
- #clean_html(msg) ⇒ Object
- #clean_name(nameval) ⇒ Object
- #client_checks ⇒ Object
-
#confirm_action(msg) ⇒ Object
Give yes/no prompt!.
- #cross_versions ⇒ Object
- #delete_user(username) ⇒ Object
- #gen_config(who) ⇒ Object
- #load_config(who) ⇒ Object
- #load_gem(gem_name) ⇒ Object
- #local_version ⇒ Object
-
#option_parser(opts) ⇒ Object
Provide some help and options!.
- #password_sanity(password) ⇒ Object
- #sane_config(type, key) ⇒ Object
- #save_config(which, config_var) ⇒ Object
- #search_image(sign, album = '') ⇒ Object
- #username_sanity(username) ⇒ Object
- #validate_token(token) ⇒ Object
Instance Method Details
#add_user(user, password, role) ⇒ Object
315 316 317 318 319 320 321 322 323 |
# File 'lib/phoseum/phoseum-cli-lib.rb', line 315 def add_user(user,password,role) result = api_caller({"action" => "create-user", "user" => user, "password" => password, "role" => role},true) if result['error'] return false end if result['success'] return true end end |
#api_caller(json_body, auth = false, cli = false) ⇒ Object
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/phoseum/phoseum-cli-lib.rb', line 238 def api_caller(json_body,auth=false,cli=false) headers = {} if !auth if $config['TOKEN'] headers={ "bearer" => "#{$config['TOKEN']}" } else return false end end base = URI.parse("#{$config['SERVERURL']}") request = Net::HTTP::Post.new(base,headers) request.body = JSON.generate(json_body) if auth if cli request.basic_auth("cli", "loginNOauth") else request.basic_auth("auth", $config['DEFAULT_SECRET']) end else if cli request.basic_auth("cli", "loginNOauth") end end response = Net::HTTP.start(base.hostname, $config['PORT'], :timeout => $config['CALL_TIMEOUT'], :use_ssl => base.scheme == "https", :verify_mode => OpenSSL::SSL::VERIFY_PEER, :ca_file => $config['CA_TRUST'] ) do |http| http.request(request) end begin list = JSON.parse(response.body) return list rescue if !auth puts "\nThe server sent out an Error:".red puts clean_html(response.body) exit 1 else return '{"error": "Secret is invalid"}' end end end |
#change_role(user, new_role) ⇒ Object
326 327 328 329 330 331 332 333 334 |
# File 'lib/phoseum/phoseum-cli-lib.rb', line 326 def change_role(user,new_role) result = api_caller({"action" => "change-role", "username" => user, "new-role" => new_role},true) if result['error'] return false end if result['success'] return true end end |
#change_user_password(user, pass, auth = false) ⇒ Object
295 296 297 298 299 300 301 302 303 |
# File 'lib/phoseum/phoseum-cli-lib.rb', line 295 def change_user_password(user,pass,auth=false) result = api_caller({"action" => "change-password", "username" => user, "password" => pass},auth) if result['error'] return false end if result['success'] return true end end |
#change_username(user, new_user) ⇒ Object
305 306 307 308 309 310 311 312 313 |
# File 'lib/phoseum/phoseum-cli-lib.rb', line 305 def change_username(user,new_user) result = api_caller({"action" => "change-username", "username" => user, "new-username" => new_user},true) if result['error'] return false end if result['success'] return true end end |
#check_image_exists(fpath) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/phoseum/phoseum-cli-lib.rb', line 186 def check_image_exists(fpath) flocal = fpath.split('/') filename = flocal.last album = fpath.clone album.gsub!(/#{filename}/,'') if ffile = search_image(filename,album) return ffile else return false end exit 0 end |
#check_string_sanity(album) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/phoseum/phoseum-common-lib.rb', line 50 def check_string_sanity(album) album.gsub!(/^\//,'') album.gsub!(/^#{$config['ALBUM_URL']}/,'') album.gsub!(/^\//,'') folders=album.split("/") r_folders='' folders.each do |f| r_folders= [r_folders,f.remove_non_ascii].join("/") end r_folders = r_folders.gsub!(/^\//,'') return r_folders end |
#clean_html(msg) ⇒ Object
168 169 170 171 172 173 |
# File 'lib/phoseum/phoseum-cli-lib.rb', line 168 def clean_html(msg) msg.gsub!(/<\/?[^>]*>/, ' ') msg.gsub!(/\n\n+/, '\n') msg.gsub!(/^\n|\n$/, ' ') return msg end |
#clean_name(nameval) ⇒ Object
175 176 177 178 179 180 181 182 183 184 |
# File 'lib/phoseum/phoseum-cli-lib.rb', line 175 def clean_name(nameval) name_words = nameval.split(' ') name = '' name_words.each do |word| cword = check_string_sanity(word) name = name.nil? ? cword : [name,cword].join(' ') end name.gsub!(/^\ /,'') return name end |
#client_checks ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/phoseum/phoseum-cli-lib.rb', line 150 def client_checks if $config['SERVERURL'] == '' puts "I could not find a valid SERVERURL configuration. CFG is empty.".red exit 1 elsif $config['SERVERURL'] !~ /\A#{URI::regexp(['https', 'http'])}\z/ puts "I could not find a valid SERVERURL configuration. Contains: #{$config['SERVERURL']}".red exit 1 end if !$config['SERVERURL'] puts "I could not find the SERVERURL from Phoseum config, this client is then useless.".red exit 1 end if !$config['PORT'] puts "I could not find the PORT from Phoseum config, this client is then useless.".red exit 1 end end |
#confirm_action(msg) ⇒ Object
Give yes/no prompt!
Example:
>> confirm_action("message")
=> You must write 'YES' to confirm, otherwise NO is assumed
=> message :[YES/NO]:
Arguments:
msg: (String)
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/phoseum/phoseum-cli-lib.rb', line 108 def confirm_action(msg) puts "You must write 'YES' to confirm, otherwise NO is assumed".yellow print "#{msg} :[YES/NO]: " confirm=STDIN.gets.chomp if confirm == "YES" return true else return false end end |
#cross_versions ⇒ Object
354 355 356 |
# File 'lib/phoseum/phoseum-cli-lib.rb', line 354 def cross_versions return api_caller({"action" => "version-check" },true,true) end |
#delete_user(username) ⇒ Object
358 359 360 361 362 363 364 365 366 |
# File 'lib/phoseum/phoseum-cli-lib.rb', line 358 def delete_user(username) result = api_caller({"action" => "delete-user", "username" => username},true) if result['error'] return false end if result['success'] return true end end |
#gen_config(who) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/phoseum/phoseum-common-lib.rb', line 174 def gen_config(who) local_config={} file_to_write='' local_dir=Dir.pwd case who when "cli" local_config['SERVERURL']="" local_config['DEBUG']=false local_config['PORT']=2207 local_config['CALL_TIMEOUT']=120 local_config['DEFAULT_SECRET']="copy-secret-from-server" local_config['TOKEN']="make-a-successful-login" home_cfg=File.('~/.phoseum/') local_config['CA_TRUST']=[home_cfg,"CA-PHOSEUM"].join('/') if !Dir.exist?(home_cfg) Dir.mkdir(home_cfg) File.open("#{home_cfg}/CA-PHOSEUM", 'w') do |ca| ca.write(CA_PHOSEUM) end end file_to_write = [home_cfg,"cli-config.yml"].join('/') puts "Creating config at #{file_to_write}" when "api" local_config['THREADED']=false local_config['SUBCHECK']=true local_config['BASEALBUM']="#{local_dir}/Albums" local_config['ALBUM_URL']="Albums" local_config['STORE_DIR']="#{local_dir}" local_config['DEBUG']=true local_config['PORT']=2207 local_config['EXT_URL']="" local_config['MAX_IMG_SIZE']=50000000 local_config['RESIZE']="640x640" local_config['RESAMPLE']="72x72" local_config['CALL_TIMEOUT']=120 home_cfg="#{local_dir}/cfg" if !Dir.exist?(home_cfg) Dir.mkdir(home_cfg) end file_to_write = [home_cfg,"phoseum-api-config.yml"].join('/') puts "Creating config at #{file_to_write}" new_secret = SecureRandom.base64(32) local_config['DEFAULT_SECRET'] = new_secret puts "\nGenerating new secret: #{new_secret}" when "fe" local_config['THEME']="Default" local_config['THEME_STYLE']="single" local_config['LIMIT']=30 local_config['NXT_LOAD']=10 local_config['SCROLL_SPEED']=40 local_config['SCROLL_BACK']="smooth" local_config['ROTATE_TO']="left" local_config['DEF_VIEW']="basic" local_config['LOGO']="logo-phoseum.png" local_config['TITLE_P']="Your personal museum of photography" local_config['TITLE_B']="Phoseum" local_config['IMG_LABEL']=false local_config['REFRESH_DELETE']=false local_config['LOAD_METHOD']="auto" local_config['IMG_COLUMNS']=4 local_config['ALB_COLUMNS']=4 home_cfg="#{local_dir}/cfg" if !Dir.exist?(home_cfg) Dir.mkdir(home_cfg) end file_to_write = [home_cfg,"phoseum-fe-config.yml"].join('/') puts "Creating config at #{file_to_write}" end File.open(file_to_write, 'w') do |f| f.write(Psych.dump(local_config)) end return true end |
#load_config(who) ⇒ Object
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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/phoseum/phoseum-common-lib.rb', line 100 def load_config(who) load_gem('yaml') if who == "api" local_dir=Dir.pwd home_cfg=File.("#{local_dir}/cfg") file_to_load = [home_cfg,"phoseum-api-config.yml"].join('/') if !Dir.exist?(home_cfg) gen_config(who) puts "New configuration was generated at #{file_to_load}. Please complete missing information and run again" exit 1 end if !File.file?(file_to_load) puts "API CFG Not found: #{file_to_load} " exit 1 end elsif who == "cli" home_cfg=File.('~/.phoseum/') file_to_load = [home_cfg,"cli-config.yml"].join('/') if !Dir.exist?(home_cfg) gen_config(who) puts "New configuration was generated at #{file_to_load}. Please complete missing information and run again" exit 1 end if !File.file?(file_to_load) puts "CLI CFG Not found: #{file_to_load} " exit 1 end elsif who == "fe" local_dir=Dir.pwd home_cfg=File.("#{local_dir}/cfg") file_to_load = [home_cfg,"phoseum-fe-config.yml"].join('/') if !Dir.exist?(home_cfg) gen_config(who) puts "New configuration was generated for #{file_to_load} FrontEnd." end if !File.file?(file_to_load) puts "FE CFG Not found: #{file_to_load} " end else puts "Not sure which configuration to load. Abort." exit 1 end begin config_data = YAML.load_file(file_to_load) puts "Opening configuration from #{file_to_load}" if config_data['DEBUG'] rescue puts "Failed loading configuration from #{file_to_load}" exit 1 if who != 'fe' return false end return config_data end |
#load_gem(gem_name) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/phoseum/phoseum-common-lib.rb', line 63 def load_gem(gem_name) found_gem = false begin found_gem = Gem::Specification.find_by_name(gem_name) rescue Gem::LoadError puts "Could not find gem '#{gem_name}'. Please run 'gem install #{gem_name}' as user #{ENV["USER"]}" exit 1 else require(gem_name) end end |
#local_version ⇒ Object
368 369 370 371 |
# File 'lib/phoseum/phoseum-cli-lib.rb', line 368 def local_version puts "This CLI library is running version: #{VERSION_SIGN}" return end |
#option_parser(opts) ⇒ Object
Provide some help and options!
Example:
>> options = option_parser(ARGV)
=> puts options[:option]
Arguments:
ARGV: (Array or command line parameters)
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 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 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/phoseum/phoseum-cli-lib.rb', line 10 def option_parser(opts) = {} OptionParser.new do |opts| opts. = "Usage: phoseum-cli [options]\n\nRead about details on album names and types of images supported on official website:\nphoseum.org\n\n".green opts.on("-a", "--album-path STRING", "Choose destination album. Use with: [update, delete, check]") do |a| [:album] = a end opts.on("-c", "--check", "Check against Phoseum API Server. Use with: [album-path, image, <alone>]") do |c| [:check] = c end opts.on("-C", "--create-user USERNAME", "Create user on the application") do |cre| [:create_user] = cre end opts.on("-d", "--delete", "Delete Album or Image. Use with: [album-path, image]") do |d| [:delete] = d end opts.on("-D", "--description 'SHORTDESC'", "Add album description (up to 255 chars). Use with: [album-path]") do |ds| [:description] = ds end opts.on("-f", "--file-cfg FILENAME", "Use alternative configuration file") do |f| [:filecfg] = f end opts.on("-i", "--image FILENAME", "Manipulate one image. Use with: [check, upload, delete]") do |i| [:image] = i end opts.on("-l", "--login", "Make a login to save a new token") do |l| [:login] = l end opts.on("-m", "--multiple", "Ignore repeated images, keep multiple") do |m| [:multiple] = m end opts.on("-N", "--name 'REALNAME'", "Set Album's synthetic name. Use with: [album-path]") do |n| [:name] = n end opts.on("-v", "--verbose", "Run verbosely (DEBUG mode)") do |v| [:verbose] = v end opts.on("-V", "--version", "What version we are") do |vs| [:version] = vs end opts.on("-x", "--cross-versions", "Check version match between Client and API") do |x| [:crossv] = x end opts.on("-r", "--role ROLE", "Role for user: [User, Super]. Use with 'create-user'") do |r| [:role] = r end opts.on("-o", "--options", "Update options from User. Use with: [user]") do |o| [:options] = o end opts.on("-q", "--quiet", "Avoid regular info, useful for JSON output") do |q| [:quiet] = q end opts.on("-t", "--test", "Test local files. Used to identify on local directory what will be uploaded.") do |t| [:test] = t end opts.on("-u", "--upload", "Upload files to Phoseum API Server. Use with: [album-path, image]") do |u| [:upload] = u end opts.on("-U", "--User USERNAME", "Username for delete or change password / role. Use with: [delete, options]") do |usr| [:user] = usr end opts.on("-h", "-h", "That is this...") do |h| [:help] = h end end.parse! return end |
#password_sanity(password) ⇒ Object
336 337 338 339 340 341 342 343 344 345 |
# File 'lib/phoseum/phoseum-cli-lib.rb', line 336 def password_sanity(password) if password.length < MIN_PASS puts "New password is shorter than #{MIN_PASS} chars, please use a bigger password.".red exit 1 end if !password.test_password puts "Password must be at least #{MIN_PASS} and contain at least one capital, one symbol, one number, one regular characters.".red exit 1 end end |
#sane_config(type, key) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/phoseum/phoseum-common-lib.rb', line 154 def sane_config(type,key) ct = {} case type when 'cli' ct = { type => ['SERVER','DEBUG','PORT','CALL_TIMEOUT','DEFAULT_SECRET','TOKEN']} when 'api' ct = { type => ['THREADED','BASEALBUM','ALBUM_URL','STORE_DIR','DEBUG','PORT','EXT_URL','MAX_IMG_SIZE','RESIZE','RESAMPLE','CALL_TIMEOUT','DEFAULT_SECRET']} when 'fe' ct = { type => ['THEME','THEME_STYLE','LIMIT','NXT_LOAD','SCROLL_SPEED','ROTATE_TO','SCROLL_BACK','LOGO','TITLE_P','TITLE_B','IMG_LABEL','REFRESH_DELETE','IMG_COLUMNS','ALB_COLUMNS','DEF_VIEW','LOAD_METHOD']} end good_key=false ct[type].each do |k| if k == key good_key=true end end return good_key end |
#save_config(which, config_var) ⇒ Object
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/phoseum/phoseum-common-lib.rb', line 253 def save_config(which,config_var) file_to_write='' local_dir=Dir.pwd case which when "cli" home_cfg=File.('~/.phoseum/') file_to_write = [home_cfg,"cli-config.yml"].join('/') when "api" home_cfg="#{local_dir}/cfg" file_to_write = [home_cfg,"phoseum-api-config.yml"].join('/') when "fe" home_cfg="#{local_dir}/cfg" file_to_write = [home_cfg,"phoseum-fe-config.yml"].join('/') end puts "Saving #{which} config at #{file_to_write}" if $config['DEBUG'] File.open(file_to_write, 'w') do |f| f.write(Psych.dump(config_var)) end return true end |
#search_image(sign, album = '') ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/phoseum/phoseum-cli-lib.rb', line 200 def search_image(sign,album='') if !validate_token($config['TOKEN']) if token = user_login() headers = { "bearer" => token } else puts "Login Failed".red exit 1 end else headers={ "bearer" => "#{$config['TOKEN']}" } end some_uri= album == '' ? "?signature=#{sign}" : "?signature=#{sign}&inside_album=#{album}" base = URI.parse("#{$config['SERVERURL']}#{some_uri}") request = Net::HTTP::Get.new(base,headers) response = Net::HTTP.start(base.hostname, $config['PORT'], :timeout => $config['CALL_TIMEOUT'], :use_ssl => base.scheme == "https", :verify_mode => OpenSSL::SSL::VERIFY_PEER, :ca_file => $config['CA_TRUST'] ) do |http| http.request(request) end begin list = JSON.parse(response.body) if list['warning'] # puts list['warning'] return list['warning'] end if list['success'] return false end rescue puts "\nThe server sent out an Error:".red puts clean_html(response.body) exit 1 end end |
#username_sanity(username) ⇒ Object
347 348 349 350 351 352 |
# File 'lib/phoseum/phoseum-cli-lib.rb', line 347 def username_sanity(username) if username.length < MIN_USER puts "New Username is shorter than #{MIN_USER} chars, please use a bigger username.".red exit 1 end end |
#validate_token(token) ⇒ Object
285 286 287 288 289 290 291 292 293 |
# File 'lib/phoseum/phoseum-cli-lib.rb', line 285 def validate_token(token) result = api_caller({"action" => "check-token" }) if result['error'] return false end if result['success'] return true end end |