Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtask.rb
More file actions
Latest commit
75 lines (59 loc) · 1.74 KB
/
Copy pathtask.rb
File metadata and controls
75 lines (59 loc) · 1.74 KB
1
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
moduleConvertApi
classTask
definitialize(from_format,to_format,params,conversion_timeout: nil)
@from_format=from_format
@to_format=to_format
@params=params
@conversion_timeout=conversion_timeout || config.conversion_timeout
end
defrun
params=normalize_params(@params).merge(
Timeout: @conversion_timeout,
StoreFile: true,
)
from_format=@from_format || detect_format(params)
read_timeout=@conversion_timeout + config.conversion_timeout_deltaif@conversion_timeout
response=ConvertApi.client.post(
"convert/#{from_format}/to/#{@to_format}",
params,
read_timeout: read_timeout,
)
Result.new(response)
end
private
defnormalize_params(params)
result={}
symbolize_keys(params).eachdo |key,value|
case
whenkey != :StoreFile && key.to_s.end_with?('File')
result[key]=FileParam.build(value)
whenkey == :Files
result[:Files]=files_batch(value)
else
result[key]=value
end
end
result
end
defsymbolize_keys(hash)
hash.map{ |k,v| [k.to_sym,v]}.to_h
end
deffiles_batch(values)
files=Array(values).map{ |file| FileParam.build(file)}
# upload files in parallel
files
.select{ |file| file.is_a?(UploadIO)}
.map{ |upload_io| Thread.new{upload_io.file_id}}
.map(&:join)
files
end
defdetect_format(params)
returnDEFAULT_URL_FORMATifparams[:Url]
resource=params[:File] || Array(params[:Files]).first
FormatDetector.new(resource,@to_format).run
end
defconfig
ConvertApi.config
end
end
end