Class: Zm::Client::Upload

Inherits:
Object
  • Object
show all
Defined in:
lib/zm/client/upload/upload.rb

Overview

class for upload mailbox file

Constant Summary collapse

FMT_TYPES_H =
{
  'ics' => FolderView::APPOINTMENT,
  'vcard' => FolderView::CONTACT,
  'eml' => FolderView::MESSAGE,
  'vcf' => FolderView::CONTACT
}.freeze
AUTHORIZED_FOLDER_FMT =
%w[zip tgz ics csv tar xml json rss atom html ifb].freeze
AUTHORIZED_PARAMS =
%i[charset auth fmt id list types emptyname disp resolve query meta csvfmt].freeze
AUTHORIZED_RESOLVE =
%w[replace modify reset skip].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url, token, is_token_admin: false, **rest_options) {|_self| ... } ⇒ Upload

Returns a new instance of Upload.

Yields:

  • (_self)

Yield Parameters:

Raises:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/zm/client/upload/upload.rb', line 23

def initialize(base_url, token, is_token_admin: false, **rest_options)
  raise ZmError, 'base_url must to be present' if base_url.nil?

  @charset = 'UTF-8'
  @auth = 'co'
  @emptyname = 'Empty'
  @disp = 'a'

  yield(self) if block_given?

  @base_url = base_url

  @rest_connector = RestConnector.new(**rest_options) do |conn|
    conn.cookies = if is_token_admin
                     "ZM_ADMIN_AUTH_TOKEN=#{token}"
                   else
                     "ZM_AUTH_TOKEN=#{token}"
                   end
  end
end

Instance Attribute Details

#authObject

Returns the value of attribute auth.



21
22
23
# File 'lib/zm/client/upload/upload.rb', line 21

def auth
  @auth
end

#charsetObject

Returns the value of attribute charset.



21
22
23
# File 'lib/zm/client/upload/upload.rb', line 21

def charset
  @charset
end

#dispObject

Returns the value of attribute disp.



21
22
23
# File 'lib/zm/client/upload/upload.rb', line 21

def disp
  @disp
end

#emptynameObject

Returns the value of attribute emptyname.



21
22
23
# File 'lib/zm/client/upload/upload.rb', line 21

def emptyname
  @emptyname
end

#rest_connectorObject (readonly)

Returns the value of attribute rest_connector.



20
21
22
# File 'lib/zm/client/upload/upload.rb', line 20

def rest_connector
  @rest_connector
end

Instance Method Details

#build_params(**params) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/zm/client/upload/upload.rb', line 99

def build_params(**params)
  params.select! { |k, _| AUTHORIZED_PARAMS.include?(k) }

  params[:charset] ||= @charset
  params[:auth] ||= @auth
  params[:emptyname] ||= @emptyname
  params[:disp] ||= @disp

  params.compact!

  params
end

#download_file(dest_file_path, file_id, type, fmt: nil) ⇒ Object

Raises:



44
45
46
47
48
49
50
51
52
# File 'lib/zm/client/upload/upload.rb', line 44

def download_file(dest_file_path, file_id, type, fmt: nil)
  fmt ||= File.extname(dest_file_path)[1..]
  type ||= FMT_TYPES_H[fmt]

  raise RestError, 'Invalid fmt type' if type.nil? && fmt.nil?

  remote_url = download_file_url(fmt, type, file_id)
  @rest_connector.download(remote_url, dest_file_path)
end

#download_file_url(fmt, type, id) ⇒ Object



133
134
135
136
137
# File 'lib/zm/client/upload/upload.rb', line 133

def download_file_url(fmt, type, id)
  h = build_params(fmt:, types: type, id:)

  format_url(@base_url, format_url_params(h))
end

#download_file_with_url(url, dest_file_path) ⇒ Object



122
123
124
125
# File 'lib/zm/client/upload/upload.rb', line 122

def download_file_with_url(url, dest_file_path)
  url = File.join(@base_url, url) unless url.start_with?('http')
  @rest_connector.download(url, dest_file_path)
end

#download_files(dest_file_path, file_ids, type, fmt: nil) ⇒ Object

Raises:



63
64
65
66
67
68
69
70
71
# File 'lib/zm/client/upload/upload.rb', line 63

def download_files(dest_file_path, file_ids, type, fmt: nil)
  fmt ||= File.extname(dest_file_path)[1..]
  type ||= FMT_TYPES_H[fmt]

  raise RestError, 'Invalid fmt type' if type.nil? && fmt.nil?

  remote_url = download_files_url(fmt, type, file_ids)
  @rest_connector.download(remote_url, dest_file_path)
end

#download_files_url(fmt, type, ids) ⇒ Object



139
140
141
142
143
# File 'lib/zm/client/upload/upload.rb', line 139

def download_files_url(fmt, type, ids)
  h = build_params(fmt:, types: type, list: ids.join(','))

  format_url(@base_url, format_url_params(h))
end

#download_folder(dest_file_path, folder_id, fmt: nil) ⇒ Object

Raises:



73
74
75
76
77
78
79
80
# File 'lib/zm/client/upload/upload.rb', line 73

def download_folder(dest_file_path, folder_id, fmt: nil)
  fmt ||= File.extname(dest_file_path)[1..]

  raise RestError, 'Unauthorized fmt parameter' unless AUTHORIZED_FOLDER_FMT.include?(fmt)

  remote_url = download_folder_url(folder_id, fmt)
  @rest_connector.download(remote_url, dest_file_path)
end

#download_folder_url(folder_id, fmt) ⇒ Object



127
128
129
130
131
# File 'lib/zm/client/upload/upload.rb', line 127

def download_folder_url(folder_id, fmt)
  h = build_params(fmt:, id: folder_id)

  format_url(@base_url, format_url_params(h))
end

#format_url(url, str_params) ⇒ Object



116
117
118
119
120
# File 'lib/zm/client/upload/upload.rb', line 116

def format_url(url, str_params)
  return url if str_params.nil? || str_params.empty?

  "#{url}?#{str_params}"
end

#format_url_params(params) ⇒ Object



112
113
114
# File 'lib/zm/client/upload/upload.rb', line 112

def format_url_params(params)
  params.map { |k, v| "#{k}=#{v}" }.join('&')
end

#read_file(file_id, type, fmt: nil) ⇒ Object

Raises:



54
55
56
57
58
59
60
61
# File 'lib/zm/client/upload/upload.rb', line 54

def read_file(file_id, type, fmt: nil)
  type ||= FMT_TYPES_H[fmt]

  raise RestError, 'Invalid fmt type' if type.nil? && fmt.nil?

  remote_url = download_file_url(fmt, type, file_id)
  @rest_connector.read(remote_url)
end

#send_file(src_file_path, folder_id, fmt: nil, type: nil, resolve: nil) ⇒ Object

Raises:



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/zm/client/upload/upload.rb', line 82

def send_file(src_file_path, folder_id, fmt: nil, type: nil, resolve: nil)
  fmt ||= File.extname(src_file_path)[1..]
  type ||= FMT_TYPES_H[fmt]
  resolve ||= AUTHORIZED_RESOLVE.first

  raise RestError, 'Unauthorized resolve parameter' unless AUTHORIZED_RESOLVE.include?(resolve)

  raise RestError, 'Invalid fmt type' if type.nil? && fmt.nil?

  remote_url = send_file_url(folder_id, fmt:, type:, resolve:)
  @rest_connector.upload(remote_url, src_file_path)
end

#send_file_url(folder_id, fmt:, type:, resolve:) ⇒ Object



145
146
147
148
149
# File 'lib/zm/client/upload/upload.rb', line 145

def send_file_url(folder_id, fmt:, type:, resolve:)
  h = build_params(fmt:, types: type, resolve:, id: folder_id)

  format_url(@base_url, format_url_params(h))
end

#upload_attachment(src_file_path) ⇒ Object



95
96
97
# File 'lib/zm/client/upload/upload.rb', line 95

def upload_attachment(src_file_path)
  @rest_connector.upload(upload_attachment_url, src_file_path)
end

#upload_attachment_urlObject



151
152
153
154
155
156
157
# File 'lib/zm/client/upload/upload.rb', line 151

def upload_attachment_url
  h = {
    fmt: 'extended,raw'
  }

  File.join(@base_url, 'service/upload') << '?' << format_url_params(h)
end