phoseum-cli 0.0.7 → 0.0.12

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
  SHA256:
3
- metadata.gz: e62ec1e1eb6c93dc221c38db811a6c2fd4da16c6a4f1f557c7abb94eb75c4af3
4
- data.tar.gz: 01eb24cf20c9c6a9845d83cebea9ef1c584258df42010caf2a13748df489013c
3
+ metadata.gz: 7421fbbac2cf512f6be93770f895e789840cb887864d8b2d45839726cc65204f
4
+ data.tar.gz: f0fac7c15624a6652ec739c44e3af2fd6c64980da1cf5eceb41f188aa4734255
5
5
  SHA512:
6
- metadata.gz: aa277283780b5c30e36b43b3184055a4a83d869408915ce20c66e31d894a91a4415ba954c623d1b265ad2677f648cfb8434bd03706c95b71cd49dddd2ff90e48
7
- data.tar.gz: 4aaeae62b2875999db462317bfb0af842c4d7933df07a11937784c2e14aafaa9a04141f48e548792e60bec361b3b09a16c038ce215d6101d618dfe9236c262df
6
+ metadata.gz: '0779e4712800a3a8312128db722b25b99a2084b1a7eb1faa1f609ba83b27d423dd3f7174f1f787618452ffbd0617f722c0db9758356fc94d4f1b7d3daaaf7d69'
7
+ data.tar.gz: b299ad615f973916909dcb52f7ef1e4dc9b52a5c6d80d773a2985db5400394036cc8e0802ccaffcc74d35f04eab139079ac9d89bc01b704bd826af80549d22a2
@@ -24,16 +24,16 @@ $MULTI = options[:multiple] ? true : false
24
24
  client_checks
25
25
 
26
26
  def upload(album)
27
-
28
- if $config['TOKEN']
29
- headers={ "bearer" => "#{$config['TOKEN']}" }
30
- else
27
+ headers = {}
28
+ if !validate_token($config['TOKEN'])
31
29
  if token = user_login()
32
30
  headers = { "bearer" => token }
33
31
  else
34
32
  puts "Login Failed".red
35
33
  exit 1
36
34
  end
35
+ else
36
+ headers={ "bearer" => "#{$config['TOKEN']}" }
37
37
  end
38
38
 
39
39
  list=Dir.entries("./")
@@ -65,7 +65,7 @@ def upload(album)
65
65
  # last resort solution for accents and unrecognizable characters
66
66
  # but then will need to remove extension, pass the filter, then add extension again.
67
67
  # human_name = check_string_sanity(filename)
68
- human_name = filename
68
+ human_name = filename.gsub!(/(.*)\.JPG|JPEG|PNG|GIF$/i,'\1')
69
69
 
70
70
  somefound = search_image(image.signature)
71
71
  if somefound
@@ -108,6 +108,7 @@ def upload(album)
108
108
  else
109
109
  puts "\n#{msg_resp['error']}".red
110
110
  error += 1
111
+ break
111
112
  end
112
113
  rescue
113
114
  puts "\nThe server sent out an Error:".red
@@ -153,15 +154,16 @@ end
153
154
 
154
155
  def health(what='',name='')
155
156
  headers = {}
156
- if $config['TOKEN']
157
- headers={ "bearer" => "#{$config['TOKEN']}" }
158
- else
157
+
158
+ if !validate_token($config['TOKEN']) || !$config['TOKEN']
159
159
  if token = user_login()
160
160
  headers = { "bearer" => token }
161
161
  else
162
162
  puts "Login Failed".red
163
163
  exit 1
164
164
  end
165
+ else
166
+ headers={ "bearer" => "#{$config['TOKEN']}" }
165
167
  end
166
168
  some_uri=''
167
169
  if what == "album"
@@ -232,6 +234,10 @@ def user_mgmt(action,user,pass='',role='')
232
234
  post_body={}
233
235
  if action == "add_user"
234
236
  post_body=JSON.generate({"action" => "create-user", "user" => "#{user}", "password" => "#{pass}", "role" => "#{role}"})
237
+ if !$config['DEFAULT_SECRET']
238
+ puts "You must have a DEFAULT_SECRET setting to be able to create a user".red
239
+ exit 1
240
+ end
235
241
  end
236
242
  base = URI.parse("#{$config['SERVERURL']}")
237
243
  puts "\nConnecting to: #{$config['SERVERURL']}".yellow if !$QUIET
@@ -263,29 +269,28 @@ def user_mgmt(action,user,pass='',role='')
263
269
  exit 0
264
270
  end
265
271
 
266
- def user_login()
272
+ def user_login(puser='',ppass='')
267
273
  post_body={}
268
- user=''
269
- pass=''
274
+ user= !puser ? '' : puser
275
+ pass= !ppass ? '' : ppass
270
276
  if $config['USER']
271
277
  user=$config['USER']
272
278
  else
273
279
  print "Username: "
274
- user=STDIN.noecho(&:gets).chomp
280
+ user=STDIN.gets.chomp
275
281
  end
276
282
  if $config['PASS']
277
283
  pass=$config['PASS']
278
- puts "Keeping your password on the configuration file is UNSAFE".red
284
+ puts "\nKeeping your password on the configuration file is UNSAFE".red
279
285
  else
280
- print "\nPassword: "
281
- pass=STDIN.noecho(&:gets).chomp
286
+ pass=STDIN.getpass('Password: ')
282
287
  end
283
288
  post_body=JSON.generate({"action" => "user-login", "user" => "#{user}", "password" => "#{pass}"})
284
289
  base = URI.parse("#{$config['SERVERURL']}")
285
290
  puts "\nLogging to: #{$config['SERVERURL']}".yellow if !$QUIET
286
291
  request = Net::HTTP::Post.new(base)
287
292
  request.body = post_body
288
- request.basic_auth("auth", $config['DEFAULT_SECRET'])
293
+ request.basic_auth("cli", "loginNOauth")
289
294
  response = Net::HTTP.start(base.hostname, $config['PORT'],
290
295
  :timeout => $config['CALL_TIMEOUT'],
291
296
  :use_ssl => base.scheme == "https",
@@ -322,37 +327,40 @@ end
322
327
 
323
328
 
324
329
  def delete(what='',path='')
325
- puts "You must write 'YES' to confirm, otherwise NO is assumed".yellow
326
- print "Are you sure you want to delete the Album #{path} :[YES/NO]: "
327
- confirm=STDIN.gets.chomp
328
- if confirm == "YES"
329
- headers={}
330
- if $config['TOKEN']
331
- headers={ "bearer" => "#{$config['TOKEN']}" }
330
+ if !validate_token($config['TOKEN'])
331
+ if token = user_login()
332
+ headers = { "bearer" => token }
332
333
  else
333
- if token = user_login()
334
- headers = { "bearer" => token }
335
- else
336
- puts "Login Failed".red
337
- exit 1
338
- end
339
- end
340
-
341
- base = URI.parse("#{$config['SERVERURL']}/#{path}")
342
- puts "Connecting to: #{$config['SERVERURL']}".yellow if !$QUIET
343
- request = Net::HTTP::Delete.new(base,headers)
344
- response = Net::HTTP.start(base.hostname, $config['PORT'],
345
- :timeout => $config['CALL_TIMEOUT'],
346
- :use_ssl => base.scheme == "https",
347
- :verify_mode => OpenSSL::SSL::VERIFY_PEER,
348
- :ca_file => $config['CA_TRUST']
349
- ) do |http|
350
- http.request(request)
334
+ puts "Login Failed".red
335
+ exit 1
351
336
  end
352
- list = JSON.parse(response.body)
353
- puts list
354
337
  else
355
- puts "Deleting cancelled.".green
338
+ headers={ "bearer" => "#{$config['TOKEN']}" }
339
+ end
340
+ base = URI.parse("#{$config['SERVERURL']}/#{path}")
341
+ puts "Connecting to: #{$config['SERVERURL']}".yellow if !$QUIET
342
+ request = Net::HTTP::Delete.new(base,headers)
343
+ response = Net::HTTP.start(base.hostname, $config['PORT'],
344
+ :timeout => $config['CALL_TIMEOUT'],
345
+ :use_ssl => base.scheme == "https",
346
+ :verify_mode => OpenSSL::SSL::VERIFY_PEER,
347
+ :ca_file => $config['CA_TRUST']
348
+ ) do |http|
349
+ http.request(request)
350
+ end
351
+ begin
352
+ list = JSON.parse(response.body)
353
+ if list['error']
354
+ puts list['error'].red
355
+ exit 1
356
+ else
357
+ puts list['success'].green
358
+ exit 0
359
+ end
360
+ rescue
361
+ puts "\nThe server sent out an Error:".red
362
+ puts clean_html(response.body)
363
+ exit 1
356
364
  end
357
365
  end
358
366
 
@@ -373,10 +381,10 @@ case options
373
381
  puts "You need to select one of the valid Roles".red
374
382
  exit 1
375
383
  else
376
- print "Please type the password twice for user #{value}.\n\tPassword: "
377
- new_password=STDIN.noecho(&:gets).chomp
378
- print "\n\tConfirm: "
379
- confirm=STDIN.noecho(&:gets).chomp
384
+ print "Please type the password twice for user #{value}.\n\t"
385
+ new_password=STDIN.getpass('Password: ')
386
+ print "\t"
387
+ confirm=STDIN.getpass('Confirm: ')
380
388
  if new_password == confirm
381
389
  user_mgmt(what,value,new_password,options[:role])
382
390
  end
@@ -387,13 +395,70 @@ case options
387
395
  if options[:album]
388
396
  value=check_string_sanity(options[:album])
389
397
  what='album'
398
+ if value == "/" or value == '' or value.nil?
399
+ puts "It is Not possible to delete base album".red
400
+ exit 1
401
+ end
402
+ if !confirm_action("Are you sure you want to delete the #{what} #{value}")
403
+ puts "Deleting #{what} cancelled.".green
404
+ end
390
405
  end
391
- if value == "/" or value == '' or value.nil?
392
- puts "It is Not possible to delete base album".red
406
+ if options[:image]
407
+ if _found = check_image_exists(options[:image])
408
+ if _found =~ /^Signature Found at \[\"(.*)\"\]/
409
+ capt = _found.match(/^Signature Found at \[\"(.*)\"\]/).captures
410
+ capt_part = capt[0].match(/.*(Albums.*)$/).captures
411
+ value = capt_part[0]
412
+ else
413
+ img_data = JSON.parse(_found)
414
+ value="#{img_data['web_path']}/#{img_data['image']}"
415
+ end
416
+ what='image'
417
+ else
418
+ puts "Image not found on server".red
419
+ exit 1
420
+ end
421
+ if confirm_action("Are you sure you want to delete the #{what} #{value}")
422
+ puts "Deleting #{what} #{value}".green if !$QUIET
423
+ delete(what,value)
424
+ else
425
+ puts "Deleting #{what} cancelled.".green
426
+ end
427
+ end
428
+ if options[:user]
429
+ what='user'
430
+ value=options[:user]
431
+ if confirm_action("Are you sure you want to delete the #{what} #{value}")
432
+ puts "Deleting #{what} #{value}".green if !$QUIET
433
+ if delete_user(value)
434
+ puts "#{value} successfully deleted.".green
435
+ else
436
+ puts "#{value} failed to be deleted.".red
437
+ end
438
+ else
439
+ puts "Deleting #{what} cancelled.".green
440
+ end
441
+ end
442
+ if !what
443
+ puts "Got no object to delete. Exiting.".red
444
+ exit 1
445
+ end
446
+ when -> (o) { o[:options] }
447
+ if options[:user]
448
+ print "Please confirm #{options[:user]} password.\n\t"
449
+ current_password=STDIN.getpass('Password: ')
450
+ if token = user_login(options[:user],current_password)
451
+ print "\n Change [P]assword, [U]sername, [R]ole :[P/U/R]: "
452
+ confirm=STDIN.gets.chomp
453
+ else
454
+ puts "User Check Failed".red
455
+ exit 1
456
+ end
457
+ else
458
+ # if !what
459
+ puts "Got no user to work with. Exiting.".red
393
460
  exit 1
394
461
  end
395
- puts "Deleting Album #{value}".green if !$QUIET
396
- delete(what,value)
397
462
  when -> (u) { u[:upload] }
398
463
  puts "Preparing to upload images to API server".green if !$QUIET
399
464
  if !options.key?(:album)
@@ -44,6 +44,10 @@ def option_parser(opts)
44
44
  options[:role] = r
45
45
  end
46
46
 
47
+ opts.on("-o", "--options", "Update options from object. Use with: [user]") do |o|
48
+ options[:options] = o
49
+ end
50
+
47
51
  opts.on("-q", "--quiet", "Avoid regular info, useful for JSON output") do |q|
48
52
  options[:quiet] = q
49
53
  end
@@ -56,6 +60,10 @@ def option_parser(opts)
56
60
  options[:upload] = u
57
61
  end
58
62
 
63
+ opts.on("-U", "--User USERNAME", "Username for delete or change password / role. Use with: [delete, options]") do |usr|
64
+ options[:user] = usr
65
+ end
66
+
59
67
  opts.on("-h", "-h", "That is this...") do |h|
60
68
  options[:help] = h
61
69
  end
@@ -64,6 +72,17 @@ def option_parser(opts)
64
72
  return options
65
73
  end
66
74
 
75
+ def confirm_action(msg)
76
+ puts "You must write 'YES' to confirm, otherwise NO is assumed".yellow
77
+ print "#{msg} :[YES/NO]: "
78
+ confirm=STDIN.gets.chomp
79
+ if confirm == "YES"
80
+ return true
81
+ else
82
+ return false
83
+ end
84
+ end
85
+
67
86
  class String
68
87
  # colorization
69
88
  def colorize(color_code)
@@ -96,15 +115,13 @@ class String
96
115
  end
97
116
 
98
117
  def client_checks
99
-
100
118
  if $config['SERVERURL'] == ''
101
- puts "I could not find a valid SERVERURL configuration. CFG is empty.".red
102
- exit 1
119
+ puts "I could not find a valid SERVERURL configuration. CFG is empty.".red
120
+ exit 1
103
121
  elsif $config['SERVERURL'] !~ /\A#{URI::regexp(['https', 'http'])}\z/
104
122
  puts "I could not find a valid SERVERURL configuration. Contains: #{$config['SERVERURL']}".red
105
123
  exit 1
106
124
  end
107
-
108
125
  if !$config['DEFAULT_SECRET']
109
126
  puts "I could not find the DEFAULT_SECRET from Phoseum config, this will limit our actions.".red
110
127
  exit 1
@@ -132,19 +149,32 @@ def clean_html(msg)
132
149
  return msg
133
150
  end
134
151
 
135
- def search_image(sign)
136
- headers = {}
137
- if $config['TOKEN']
138
- headers={ "bearer" => "#{$config['TOKEN']}" }
152
+ def check_image_exists(fpath)
153
+ flocal = fpath.split('/')
154
+ filename = flocal.last
155
+ album = fpath.clone
156
+ album.gsub!(/#{filename}/,'')
157
+
158
+ if ffile = search_image(filename,album)
159
+ return ffile
139
160
  else
161
+ return false
162
+ end
163
+ exit 0
164
+ end
165
+
166
+ def search_image(sign,album='')
167
+ if !validate_token($config['TOKEN'])
140
168
  if token = user_login()
141
169
  headers = { "bearer" => token }
142
170
  else
143
171
  puts "Login Failed".red
144
172
  exit 1
145
173
  end
174
+ else
175
+ headers={ "bearer" => "#{$config['TOKEN']}" }
146
176
  end
147
- some_uri="?signature=#{sign}"
177
+ some_uri= album == '' ? "?signature=#{sign}" : "?signature=#{sign}&inside_album=#{album}"
148
178
  base = URI.parse("#{$config['SERVERURL']}#{some_uri}")
149
179
  request = Net::HTTP::Get.new(base,headers)
150
180
  response = Net::HTTP.start(base.hostname, $config['PORT'],
@@ -158,6 +188,7 @@ def search_image(sign)
158
188
  begin
159
189
  list = JSON.parse(response.body)
160
190
  if list['warning']
191
+ # puts list['warning']
161
192
  return list['warning']
162
193
  end
163
194
  if list['success']
@@ -169,3 +200,69 @@ def search_image(sign)
169
200
  exit 1
170
201
  end
171
202
  end
203
+
204
+ def validate_token(token)
205
+ headers = {}
206
+ if $config['TOKEN']
207
+ headers={ "bearer" => "#{$config['TOKEN']}" }
208
+ else
209
+ return false
210
+ end
211
+ base = URI.parse("#{$config['SERVERURL']}")
212
+ request = Net::HTTP::Post.new(base,headers)
213
+ request.body = JSON.generate({"action" => "check-token" })
214
+ response = Net::HTTP.start(base.hostname, $config['PORT'],
215
+ :timeout => $config['CALL_TIMEOUT'],
216
+ :use_ssl => base.scheme == "https",
217
+ :verify_mode => OpenSSL::SSL::VERIFY_PEER,
218
+ :ca_file => $config['CA_TRUST']
219
+ ) do |http|
220
+ http.request(request)
221
+ end
222
+ begin
223
+ list = JSON.parse(response.body)
224
+ if list['error']
225
+ return false
226
+ end
227
+ if list['success']
228
+ return true
229
+ end
230
+ rescue
231
+ puts "\nThe server sent out an Error:".red
232
+ puts clean_html(response.body)
233
+ exit 1
234
+ end
235
+ end
236
+
237
+ def delete_user(username)
238
+ headers = {}
239
+ if $config['TOKEN']
240
+ headers={ "bearer" => "#{$config['TOKEN']}" }
241
+ else
242
+ return false
243
+ end
244
+ base = URI.parse("#{$config['SERVERURL']}")
245
+ request = Net::HTTP::Post.new(base,headers)
246
+ request.body = JSON.generate({"action" => "delete-user", "username" => username })
247
+ response = Net::HTTP.start(base.hostname, $config['PORT'],
248
+ :timeout => $config['CALL_TIMEOUT'],
249
+ :use_ssl => base.scheme == "https",
250
+ :verify_mode => OpenSSL::SSL::VERIFY_PEER,
251
+ :ca_file => $config['CA_TRUST']
252
+ ) do |http|
253
+ http.request(request)
254
+ end
255
+ begin
256
+ list = JSON.parse(response.body)
257
+ if list['error']
258
+ return false
259
+ end
260
+ if list['success']
261
+ return true
262
+ end
263
+ rescue
264
+ puts "\nThe server sent out an Error:".red
265
+ puts clean_html(response.body)
266
+ exit 1
267
+ end
268
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phoseum-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julio C Hegedus
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-09-05 00:00:00.000000000 Z
11
+ date: 2020-09-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: yaml