mymedia_ftp 0.1.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: cee82f89d2fec3cd42459555c373688c981a25dd
4
- data.tar.gz: a4d18c1ba3dd02f95145d218faefcd1a0e9f9f6a
2
+ SHA256:
3
+ metadata.gz: 70b64854b4af1689d9c02b6db9af47760a956c428630a05ab9681fcb109a8e33
4
+ data.tar.gz: 7e216ea42a4aa8635f31ea0d0bdd63e3efd25ba743e53b8c5d8623967dfeffbe
5
5
  SHA512:
6
- metadata.gz: bf61e6ef142660c9094a8ee5f2bc2402155423f396e94be2b883c5a5ff1fe49a6d15d06123c8d68d02c7bbdf2368090c7ee24da93e0c9249e6e732bc276d1e45
7
- data.tar.gz: 589c4e23cccad086cfef34eba11be44cb7991d319e5101ed95a0615ceb24aaeb161c4f0562620e43343d91e0cf47cda7ac8a456fbc3e601de8ae5f927f55b114
6
+ metadata.gz: 5ba16fcace08fd38a254afefcd4f97eda8b8a14914873ab24ab5deb29c007c6498edb59d4296302642f539761a503893eb8c57a473f9c5df3880874fa52b6106
7
+ data.tar.gz: 0563b8523d8050a66dd683a0ea390c9f25cebfe9650471eec542f121a6cacb611a8de55ec5a319f7dbf8b7aa1c52fb368f7c4856dd7c78692da39d0ed61474c6
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -3,62 +3,213 @@
3
3
  # file: mymedia_ftp.rb
4
4
 
5
5
  require 'net/ftp'
6
+ require 'tempfile'
6
7
  require 'fileutils'
7
8
 
8
9
 
9
10
  class MyMediaFTP < Net::FTP
10
11
 
11
- def initialize(host: '127.0.0.1', user: 'user', password: '1234', port: 21)
12
+ def self.cp(src, dest, debug: false)
13
+
14
+ if src =~ /^ftp:\/\// then
15
+ uri, remotepath = src.match(/^(ftp:\/\/[^\/]+)(\/[^$]+)/).captures
16
+ new(uri, debug: debug).cp(remotepath, dest, :inbound)
17
+ else
18
+ uri, remotepath = dest.match(/^(ftp:\/\/[^\/]+)(\/[^$]+)/).captures
19
+ new(uri, debug: debug).cp(src, remotepath, :outbound)
20
+ end
21
+
22
+ end
23
+
24
+ def self.ls(s, debug: false)
25
+
26
+ uri, remotepath = s.match(/^(ftp:\/\/[^\/]+)(\/[^$]*)?/).captures
27
+ puts 'remotepath' + remotepath.inspect if debug
28
+ new(uri, debug: debug).ls(remotepath || '/')
29
+
30
+ end
31
+
32
+ def self.read(s, debug: false)
33
+
34
+ uri, remotepath = s.match(/^(ftp:\/\/[^\/]+)(\/[^$]+)/).captures
35
+ ftp = new(uri, debug: debug)
36
+ p ftp if debug
37
+
38
+ tmpfile = Tempfile.new('ftp')
39
+
40
+ ftp.cp remotepath, tmpfile.path
41
+ File.read tmpfile.path
42
+
43
+ end
44
+
45
+ def self.rm(s, debug: false)
46
+
47
+ uri, remotepath = s.match(/^(ftp:\/\/[^\/]+)(\/[^$]+)/).captures
48
+ new(uri, debug: debug).rm(remotepath)
49
+
50
+ end
51
+
52
+ def self.write(s, content='', debug: false)
53
+
54
+ tmpfile = Tempfile.new('ftp')
55
+ File.write tmpfile.path, content
56
+ self.cp tmpfile.path, s
57
+
58
+ end
59
+
60
+ def initialize(s=nil, host: '127.0.0.1', user: 'user', password: '1234',
61
+ port: 21, debug: false)
62
+
63
+ if s then
64
+
65
+ r = s.match(/(?<user>\w+):(?<password>\w+)@(?<host>[^:]+)(?:\:(?<port>\d+))?/)
66
+ h = r.named_captures.map {|k,v| [k.to_sym, v]}.to_h
67
+ puts 'h: ' + h.inspect if debug
68
+ user, password, host = h.values.take(3)
69
+ port = h[:port] if h[:port]
70
+
71
+ end
72
+
73
+ @debug = debug
12
74
 
13
- @curdir = Dir.pwd
75
+ @curdir = '/'
14
76
  super()
15
77
  connect(host, port)
16
78
  login(user, password)
17
79
 
18
80
  end
81
+
82
+ def chdir(dir)
83
+ super(dir)
84
+ @curdir = pwd
85
+ end
86
+
87
+ alias cd chdir
19
88
 
20
- def cp(src='', dest='')
89
+ def cp(src='', dest='', direction=:inbound, &blk)
21
90
 
91
+ return outbound_cp(src, dest) if direction == :outbound
92
+
93
+ puts 'cp: ' + src.inspect if @debug
22
94
  chdir File.dirname(src)
23
- FileUtils.mkdir_p dest
24
- Dir.chdir dest
25
-
26
- files = list_filenames(src)
27
-
28
- puts 'copying ...'
95
+ dir = File.dirname(dest)
96
+ FileUtils.mkdir_p dir
97
+ Dir.chdir dir
98
+
99
+ puts 'copying ...' if @debug
100
+
101
+ files = if src =~ /[\*\?]/ then
102
+
103
+ cp_files(src)
104
+
105
+ else
106
+
107
+ begin
108
+ getbinaryfile src, dest
109
+ rescue Net::FTPPermError => e
110
+ puts 'e: ' + e.inspect
111
+ end
112
+
113
+ end
29
114
 
30
- files.each do |x|
115
+ end
116
+
117
+ def delete(filename)
118
+ super(filename)
119
+ 'file deleted'
120
+ end
121
+
122
+ def list_filenames(s=@curdir+'/*')
123
+
124
+ if @debug
125
+ puts 'inside list_filenames'
126
+ puts 's: ' + s.inspect
127
+ end
128
+
129
+ if s =~ /\*/ then
130
+
131
+ src = File.dirname(s)
132
+
133
+ raw_q = File.basename(s)
134
+ puts 'raw_q: ' + raw_q.inspect if @debug
135
+
136
+ q = raw_q.gsub('.','\.').gsub('*','.*').gsub('?','.?')\
137
+ .sub(/[^\*\?\.]$/,'.*')
138
+ else
139
+ src = s
140
+ end
31
141
 
32
- puts x
33
- getbinaryfile x, x.downcase.gsub(/ +/,'-')
34
- yield(x) if block_given?
142
+ list(src).inject([]) do |r, x|
143
+
144
+ raw_attr, _, owner, group, filesize, month, day, time, filename = \
145
+ x.split(/ +/,9)
146
+ type = raw_attr =~ /d/ ? :directory : :file
147
+
148
+ if q then
149
+ filename[/^#{q}$/] ? r << {name: filename, type: type} : r
150
+ else
151
+ r << {name: filename, type: type}
152
+ end
153
+ r
35
154
  end
36
155
 
37
156
  end
38
-
157
+
158
+ alias ls list_filenames
39
159
 
40
160
  def mv(src='', dest='')
41
161
 
42
162
  puts 'moving ...'
43
- cp(src, dest) {|file| delete file }
44
-
163
+ cp(src, dest) do |file, type|
164
+ type == :file ? delete(file) : rmdir(file)
165
+ end
45
166
 
46
167
  end
47
-
168
+
169
+ alias rm delete
170
+
48
171
  private
49
-
50
- def list_filenames(src)
51
-
52
- raw_q = File.basename(src)
53
-
54
- q = raw_q.gsub('.','\.').gsub('*','.*').gsub('?','.?')
55
-
56
- list.inject([]) do |r, x|
57
-
58
- filename = x.split(/ +/,9).last
59
- filename[/^#{q}$/] ? r << filename : r
60
- end
61
-
172
+
173
+ def cp_dir(directory, &blk)
174
+
175
+ puts 'inside cp_dir: ' + directory.inspect if @debug
176
+ FileUtils.mkdir_p directory
177
+ parent_dir = pwd
178
+ chdir directory
179
+ cp('*', directory, &blk)
180
+ chdir parent_dir
181
+ end
182
+
183
+ def cp_files(src)
184
+
185
+ files = list_filenames(src)
186
+
187
+ files.each do |h|
188
+
189
+ name, type = h[:name], h[:type]
190
+
191
+ puts name
192
+
193
+ if type == :file then
194
+ begin
195
+ getbinaryfile name, name.downcase.gsub(/ +/,'-')
196
+ rescue Net::FTPPermError => e
197
+ puts 'e: ' + e.inspect
198
+ end
199
+ else
200
+ cp_dir(name, &blk)
201
+ end
202
+ blk.call(name, type) if block_given?
203
+ end
62
204
  end
63
205
 
206
+ def outbound_cp(src, destination='.')
207
+
208
+ if File.basename(destination) == '.' then
209
+ destination.sub!(/\.$/, File.basename(src))
210
+ end
211
+
212
+ putbinaryfile(src, destination)
213
+ end
214
+
64
215
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mymedia_ftp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -10,28 +10,32 @@ bindir: bin
10
10
  cert_chain:
11
11
  - |
12
12
  -----BEGIN CERTIFICAT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AF6OXqVDN9bjYQqckCAwEA
25
+ AaOBijCBhzAJBgNVHRMEAjAAMAsGA1UdDwQEAwIEsDAdBgNVHQ4EFgQU/PN/KSth
26
+ nZ4pBrLLklzgnH00nkMwJgYDVR0RBB8wHYEbZ2VtbWFzdGVyQGphbWVzcm9iZXJ0
27
+ c29uLmV1MCYGA1UdEgQfMB2BG2dlbW1hc3RlckBqYW1lc3JvYmVydHNvbi5ldTAN
28
+ BgkqhkiG9w0BAQsFAAOCAYEAHM0WneD/luRLhn4hraEIWTNCUaVzz94DUcoo4umJ
29
+ 2APA5QsBXnnh33rUwCx3bWtTCR7mW0cShSVbGFwgvB7dRyegHczXlQcx2AgwB1k1
30
+ FzhR/37ef8Vdu44nXhhKj6NDhzVi89XuNLxSXzEHoGde6liAjTiGA7ydCyxSBdzq
31
+ Ld3WBJ+LycfVWgoNmW2csBCgrALXa145QKO5HW34n03wbZ5fAaCVy8GjAaHRf6Tf
32
+ 3QFe8+mYaXbHnqf2qEkNPIPQmdBKrfiyJK2kMIqQ3ldUGZZj+H5e1VOH8NKlSvr8
33
+ MtTHQ/vWwolPDjip3vyPbPMyNsdwC8QJHXiYl5M4rxYHSCaG6/+dMWMFAGMYVs52
34
+ 4LpA7DyCXsIeDLLZm4GvsGU+ntoCovwRCl4ZtgXtZt19QRze/nd9rcIxwULjVOBR
35
+ vmQbOdyo1gepCZyCN+UN/SCimjgu1yZtpI0S9lPHE1w/fMob3ohXflP/mnwUFxsN
36
+ KVwzt+QIecFrRei1GVykx8z3
33
37
  -----END CERTIFICATE-----
34
- date: 2017-01-24 00:00:00.000000000 Z
38
+ date: 2020-09-12 00:00:00.000000000 Z
35
39
  dependencies: []
36
40
  description:
37
41
  email: james@jamesrobertson.eu
@@ -59,8 +63,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
59
63
  - !ruby/object:Gem::Version
60
64
  version: '0'
61
65
  requirements: []
62
- rubyforge_project:
63
- rubygems_version: 2.6.8
66
+ rubygems_version: 3.0.3
64
67
  signing_key:
65
68
  specification_version: 4
66
69
  summary: A MyMedia FTP client which uses Net/FTP.
metadata.gz.sig CHANGED
Binary file