Module: QiniuHelper

Extended by:
QiniuHelper
Included in:
QiniuHelper
Defined in:
lib/rails_com/helpers/qiniu_helper.rb

Instance Method Summary collapse

Instance Method Details

#bsearch_lastObject



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
# File 'lib/rails_com/helpers/qiniu_helper.rb', line 35

def bsearch_last
  ary = (0..9).to_a.reverse
  search = 'chem_0'
  begin_index = 0
  end_index = ary.length - 1
  result = nil

  while true do
    if end_index - begin_index > 1
      index = ((begin_index + end_index) / 2.0).floor
      end_flag = false
    else
      index = end_index
      end_flag = true
    end

    search.sub! /\d$/, ary[index].to_s
    list = self.list(search)

    puts 'index: ' + index.to_s
    puts 'search: ' + search
    puts 'count: ' + list.size.to_s
    puts '-------------'

    if list.blank?
      begin_index = index
    elsif list.size >= 1
      end_index = index

      if end_flag
        search << '9'
        begin_index = 0
        end_index = ary.length - 1

        if list.size == 1
          break result = list[0]['key']
        end
      end
    end
  end

  result
end

#configObject



109
110
111
# File 'lib/rails_com/helpers/qiniu_helper.rb', line 109

def config
  @config ||= Rails.application.config_for('qiniu')
end

#download_url(key) ⇒ Object



4
5
6
# File 'lib/rails_com/helpers/qiniu_helper.rb', line 4

def download_url(key)
  Qiniu::Auth.authorize_download_url_2(config['host'], key)
end

#generate_uptoken(key = nil) ⇒ Object



13
14
15
16
# File 'lib/rails_com/helpers/qiniu_helper.rb', line 13

def generate_uptoken(key = nil)
  put_policy = Qiniu::Auth::PutPolicy.new(config['bucket'], key)
  @uptoken = Qiniu::Auth.generate_uptoken(put_policy)
end

#lastObject



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/rails_com/helpers/qiniu_helper.rb', line 79

def last
  ary = (0..9).to_a.reverse
  search = 'chem_0'
  result = nil

  while true do
    break result if result
    ary.each_with_index do |value, index|
      search.sub! /\d$/, value.to_s
      list = self.list(search)

      puts 'index: ' + index.to_s
      puts 'search: ' + search
      puts 'count: ' + list.size.to_s
      puts '-------------'

      if list.blank?
        next
      elsif list.size > 1
        search << '9'
        break
      elsif list.size == 1
        break result = list[0]['key']
      end
    end
  end

  result
end

#list(prefix = '') ⇒ Object



29
30
31
32
33
# File 'lib/rails_com/helpers/qiniu_helper.rb', line 29

def list(prefix = '')
  list_policy = Qiniu::Storage::ListPolicy.new(config['bucket'], 10, prefix, '/')
  code, result, response_headers, s, d = Qiniu::Storage.list(list_policy)
  result['items']
end

#qiniu_url(key) ⇒ Object



8
9
10
11
# File 'lib/rails_com/helpers/qiniu_helper.rb', line 8

def qiniu_url(key)
  config['host'] << '/' unless config['host'].end_with? '/'
  config['host'] + key.to_s
end

#upload(local_file, key = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/rails_com/helpers/qiniu_helper.rb', line 18

def upload(local_file, key = nil)
  code, result, response_headers = Qiniu::Storage.upload_with_token_2(
    generate_uptoken(key),
    local_file,
    key,
    nil,
    bucket: config['bucket']
  )
  result['key']
end