sendl 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/sendl/uploader.rb +21 -21
- data/lib/sendl/version.rb +1 -1
- metadata +2 -2
data/lib/sendl/uploader.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
module Sendl
|
2
2
|
class Uploader
|
3
|
-
|
3
|
+
|
4
4
|
attr_accessor :endpoint, :api_key, :success, :errors, :recipients, :json_parser, :message, :share_url
|
5
|
-
|
5
|
+
|
6
6
|
def initialize(options)
|
7
7
|
argument_errors = []
|
8
8
|
argument_errors << "You must pass an API key!" unless options[:api_key]
|
@@ -18,16 +18,16 @@ module Sendl
|
|
18
18
|
@share_id = nil
|
19
19
|
@share_id = get_share_id
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
def debug(msg)
|
23
23
|
@debug && STDERR.puts(msg)
|
24
24
|
end
|
25
|
-
|
25
|
+
|
26
26
|
def upload(files, options = {})
|
27
27
|
files.each do |f|
|
28
28
|
upload_file(f)
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
31
|
if success?
|
32
32
|
debug("success? was true, finalising the share")
|
33
33
|
r = Curl::Easy.new(url_for("shares/#{@share_id}/finalise.json")) do |curl|
|
@@ -37,39 +37,39 @@ module Sendl
|
|
37
37
|
STDERR.puts "Could not complete share as there were errors"
|
38
38
|
end
|
39
39
|
end
|
40
|
-
|
40
|
+
|
41
41
|
def success?
|
42
42
|
errors.size == 0
|
43
43
|
end
|
44
|
-
|
44
|
+
|
45
45
|
private
|
46
|
-
|
46
|
+
|
47
47
|
def de_dupe_recipients(all_recpipents)
|
48
48
|
all_recpipents.map{|r| r.downcase.strip }.uniq
|
49
49
|
end
|
50
50
|
|
51
51
|
def upload_file(f)
|
52
52
|
debug("Uploading #{f.filename}")
|
53
|
-
|
53
|
+
|
54
54
|
mime_type = MIME::Types.type_for(f.filename).first || 'application/octet-stream'
|
55
|
-
|
55
|
+
|
56
56
|
debug("Determined MIME Type of #{mime_type}")
|
57
57
|
debug("Uploading by sha")
|
58
58
|
r = Curl::Easy.new(url_for("shares/#{@share_id}/share_files.json")) do |curl|
|
59
59
|
curl.http_post Curl::PostField.content("auth_token", api_key), Curl::PostField.content("file_data[sha512]", f.sha512), Curl::PostField.content("share_file[filename]", f.filename), Curl::PostField.content("share_file[content_type]", mime_type)
|
60
60
|
end
|
61
|
-
|
61
|
+
|
62
62
|
file_share_id = parse_json(r.body_str)["share_file"]["id"]
|
63
|
-
|
63
|
+
|
64
64
|
if r.response_code == 201
|
65
65
|
STDERR.puts "#{::File.basename(f.desired_name)} was already uploaded, skipping"
|
66
66
|
elsif r.response_code == 202
|
67
67
|
debug("Server requests the full file contents")
|
68
|
-
|
68
|
+
|
69
69
|
progress = ProgressBar.new(f.desired_name, f.size)
|
70
70
|
progress.file_transfer_mode
|
71
71
|
progress.bar_mark = "="
|
72
|
-
|
72
|
+
|
73
73
|
r = Curl::Easy.new(url_for("shares/#{@share_id}/share_files/#{file_share_id}.json")) do |curl|
|
74
74
|
curl.multipart_form_post = true
|
75
75
|
curl.on_progress do |dl_total, dl_now, ul_total, ul_now| # callback
|
@@ -84,22 +84,22 @@ module Sendl
|
|
84
84
|
if r.response_code == 413
|
85
85
|
debug("File was too large")
|
86
86
|
@errors << "Could not create share, a file was too big"
|
87
|
-
elsif r.response_code
|
87
|
+
elsif ![200,204].include? r.response_code
|
88
88
|
debug("An unknown error occurred")
|
89
89
|
@errors << "An unknown error occurred, the response code was #{r.response_code}"
|
90
90
|
end
|
91
|
-
|
91
|
+
|
92
92
|
STDERR.puts
|
93
93
|
else
|
94
94
|
@errors << "Something went wrong, please tell us if you continue to see problems (response code was #{r.response_code})"
|
95
95
|
end
|
96
96
|
end
|
97
|
-
|
97
|
+
|
98
98
|
def get_share_id
|
99
99
|
r = Curl::Easy.new(url_for('shares.json')) do |curl|
|
100
100
|
curl.http_post Curl::PostField.content("auth_token", api_key), Curl::PostField.content("recipients", encode_json(recipients)), Curl::PostField.content("share[message]", message)
|
101
101
|
end
|
102
|
-
|
102
|
+
|
103
103
|
if r.response_code != 201
|
104
104
|
STDERR.puts "Could not create share, HTTP response was #{r.response_code} (expected 201)"
|
105
105
|
exit 1
|
@@ -109,17 +109,17 @@ module Sendl
|
|
109
109
|
@share_url = share["url"]
|
110
110
|
@share_id = share["share"]["id"]
|
111
111
|
end
|
112
|
-
|
112
|
+
|
113
113
|
# FIXME: de-duplicate
|
114
114
|
def url_for(path)
|
115
115
|
"#{endpoint}/#{path}"
|
116
116
|
end
|
117
|
-
|
117
|
+
|
118
118
|
def parse_json(str)
|
119
119
|
parser = Yajl::Parser.new
|
120
120
|
parser.parse(str)
|
121
121
|
end
|
122
|
-
|
122
|
+
|
123
123
|
def encode_json(str)
|
124
124
|
encoder = Yajl::Encoder.new
|
125
125
|
encoder.encode(str)
|
data/lib/sendl/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sendl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-01-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: getopt
|