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W2EIi41Y
OsDoo/AmMVZYMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQELBQADggEBACgmgSHr
YffpiSMbZUzMtC8rcexeoMDw1X9UobdanPU0FBgYruj4yjdoEIsrlaXTgHf3PEki
NWShCsRCAOZIPQkEzwN/0wom+JhB6xbURPiwSrG6TEhz8YFBn94eiHz1OycHxpf6
QD85+tN847Q0V0KXmrwoQkIEVXT4of/NRhzHQlCkHuDoT5oQdkN7W3cZFqWkXGRM
t6fZ03zeXlSH5HLmysm2DU2uB182orzWYABdwLmINR44isw8/6RS1mx8BGI+08l8
sfxrWyWaDOPnwLWbFWTqnv94w4frHyHoRY+lBPZ4DT8MAFxYXI5Rf/hBUGav5NM4
yivphjaSSCTRqyE=
-----END CERTIFICATE-----"

Instance Method Summary collapse

Instance Method Details

#check_image_exists(fpath) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/phoseum/phoseum-cli-lib.rb', line 179

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



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/phoseum/phoseum-common-lib.rb', line 17

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



161
162
163
164
165
166
# File 'lib/phoseum/phoseum-cli-lib.rb', line 161

def clean_html(msg)
  msg.gsub!(/<\/?[^>]*>/, ' ')
  msg.gsub!(/\n\n+/, '\n')
  msg.gsub!(/^\n|\n$/, ' ')
  return msg
end

#clean_name(nameval) ⇒ Object



168
169
170
171
172
173
174
175
176
177
# File 'lib/phoseum/phoseum-cli-lib.rb', line 168

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_checksObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/phoseum/phoseum-cli-lib.rb', line 133

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['DEFAULT_SECRET']
    puts "I could not find the DEFAULT_SECRET from Phoseum config, this will limit our actions.".red
    exit 1
  elsif $config['DEFAULT_SECRET'] == 'copy-secret-from-server'
    puts "DEFAULT_SECRET from Phoseum config. Still on self generated value, copy a valid one from the server.".red
    exit 1
  elsif $config['DEFAULT_SECRET'] == ''
    puts "I could not find the DEFAULT_SECRET from Phoseum config, Variable is empty.".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



91
92
93
94
95
96
97
98
99
100
# File 'lib/phoseum/phoseum-cli-lib.rb', line 91

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_versionsObject



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/phoseum/phoseum-cli-lib.rb', line 264

def cross_versions
  headers = {}
  base = URI.parse("#{$config['SERVERURL']}")
  request = Net::HTTP::Post.new(base,headers)
  request.basic_auth("cli", "loginNOauth")
  request.body = JSON.generate({"action" => "version-check" })
  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
    # if list['error']
    #   return false
    # end
    # if list['success']
    #   return true
    # end
  rescue
    puts "\nThe server sent out an Error:".red
    puts clean_html(response.body)
    exit 1
  end
end

#delete_user(username) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/phoseum/phoseum-cli-lib.rb', line 295

def delete_user(username)
  headers = {}
  if $config['TOKEN']
    headers={ "bearer" => "#{$config['TOKEN']}" }
  else
    return false
  end
  base = URI.parse("#{$config['SERVERURL']}")
  request = Net::HTTP::Post.new(base,headers)
  request.body = JSON.generate({"action" => "delete-user", "username" => username })
  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['error']
      return false
    end
    if list['success']
      return true
    end
  rescue
    puts "\nThe server sent out an Error:".red
    puts clean_html(response.body)
    exit 1
  end
end

#gen_config(who) ⇒ Object



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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/phoseum/phoseum-common-lib.rb', line 109

def gen_config(who)

  local_config={}
  file_to_write=''

  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.expand_path('~/.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_dir=Dir.pwd
    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}"

  end

  File.open(file_to_write, 'w') do |f|
    f.write(Psych.dump(local_config))
  end
  return true

end

#load_config(who) ⇒ Object



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
105
106
107
# File 'lib/phoseum/phoseum-common-lib.rb', line 67

def load_config(who)
  load_gem('yaml')
  if who == "api"
    local_dir=Dir.pwd
    home_cfg=File.expand_path("#{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.expand_path('~/.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
  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
  end
  return config_data
end

#load_gem(gem_name) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/phoseum/phoseum-common-lib.rb', line 30

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_versionObject



328
329
330
331
# File 'lib/phoseum/phoseum-cli-lib.rb', line 328

def local_version
  puts "This CLI library is running version: #{VERSION_SIGN}"
  return
end

#option_parser(opts) ⇒ Object



2
3
4
5
6
7
8
9
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
# File 'lib/phoseum/phoseum-cli-lib.rb', line 2

def option_parser(opts)
  options = {}
  OptionParser.new do |opts|
    opts.banner = "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|
      options[:album] = a
    end

    opts.on("-c", "--check", "Check against Phoseum API Server. Use with: [album-path, image, <alone>]") do |c|
      options[:check] = c
    end

    opts.on("-C", "--create-user USERNAME", "Create user on the application") do |cre|
      options[:create_user] = cre
    end

    opts.on("-d", "--delete", "Delete Album or Image. Use with: [album-path, image]") do |d|
      options[:delete] = d
    end

    opts.on("-D", "--description 'SHORTDESC'", "Add album description (up to 255 chars). Use with: [album-path]") do |ds|
      options[:description] = ds
    end

    opts.on("-f", "--file-cfg FILENAME", "Use alternative configuration file") do |f|
      options[:filecfg] = f
    end

    opts.on("-i", "--image FILENAME", "Manipulate one image. Use with: [check, upload, delete]") do |i|
      options[:image] = i
    end

    opts.on("-l", "--login", "Make a login to save a new token") do |l|
      options[:login] = l
    end

    opts.on("-m", "--multiple", "Ignore repeated images, keep multiple") do |m|
      options[:multiple] = m
    end

    opts.on("-N", "--name 'REALNAME'", "Set Album's synthetic name. Use with: [album-path]") do |n|
      options[:name] = n
    end

    opts.on("-v", "--verbose", "Run verbosely (DEBUG mode)") do |v|
      options[:verbose] = v
    end

    opts.on("-V", "--version", "What version we are") do |vs|
      options[:version] = vs
    end

    opts.on("-x", "--cross-versions", "Check version match between Client and API") do |x|
      options[:crossv] = x
    end

    opts.on("-r", "--role ROLE", "Role for user: [User, Super]. Use with 'create-user'") do |r|
      options[:role] = r
    end

    opts.on("-o", "--options", "Update options from object. Use with: [user]") do |o|
      options[:options] = o
    end

    opts.on("-q", "--quiet", "Avoid regular info, useful for JSON output") do |q|
      options[:quiet] = q
    end

    opts.on("-t", "--test", "Test local files. Used to identify on local directory what will be uploaded.") do |t|
      options[:test] = t
    end

    opts.on("-u", "--upload", "Upload files to Phoseum API Server. Use with: [album-path, image]") do |u|
      options[:upload] = u
    end

    opts.on("-U", "--User USERNAME", "Username for delete or change password / role. Use with: [delete, options]") do |usr|
      options[:user] = usr
    end

    opts.on("-h", "-h", "That is this...") do |h|
      options[:help] = h
    end

  end.parse!
  return options
end

#search_image(sign, album = '') ⇒ Object



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
# File 'lib/phoseum/phoseum-cli-lib.rb', line 193

def search_image(sign,album='')
  if !validate_token($config['TOKEN'])
    if token = ()
      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

#validate_token(token) ⇒ Object



231
232
233
234
235
236
237
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
# File 'lib/phoseum/phoseum-cli-lib.rb', line 231

def validate_token(token)
  headers = {}
  if $config['TOKEN']
    headers={ "bearer" => "#{$config['TOKEN']}" }
  else
    return false
  end
  base = URI.parse("#{$config['SERVERURL']}")
  request = Net::HTTP::Post.new(base,headers)
  request.body = JSON.generate({"action" => "check-token" })
  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['error']
      return false
    end
    if list['success']
      return true
    end
  rescue
    puts "\nThe server sent out an Error:".red
    puts clean_html(response.body)
    exit 1
  end
end