Class: Pione::Location::FTPLocation
Overview
FTPLocation represents locations on FTP server.
Constant Summary
Constants inherited
from DataLocation
DataLocation::KNOWN_ATTRS
Instance Attribute Summary
Attributes inherited from DataLocation
#path, #uri
#address
Instance Method Summary
collapse
#+, #==, #as_directory, #basename, #cached?, #ctime, define, #directory_entries, #dirname, #extname, #file_entries, #hash, #local, #local?, need_caching?, real_appendable?, set_scheme, #sha1, writable?, #write
#==, #hash, location_type
Constructor Details
Returns a new instance of FTPLocation.
14
15
16
17
|
# File 'lib/pione/location/ftp-location.rb', line 14
def initialize(uri)
uri = uri.to_ftp_scheme if uri.scheme == "myftp"
super(uri)
end
|
Instance Method Details
#append(data) ⇒ Object
42
43
44
45
46
47
48
49
|
# File 'lib/pione/location/ftp-location.rb', line 42
def append(data)
if exist?
update(read + data)
else
create(data)
end
return self
end
|
#copy(dest) ⇒ Object
133
134
135
|
# File 'lib/pione/location/ftp-location.rb', line 133
def copy(dest)
dest.create(read)
end
|
#create(data) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/pione/location/ftp-location.rb', line 28
def create(data)
if exist?
raise ExistAlready.new(self)
else
connect do |ftp|
makedirs(ftp, @path.dirname)
path = Temppath.create
Location[path].create(data)
ftp.put(path, @path.to_s)
end
end
return self
end
|
#delete ⇒ Object
71
72
73
|
# File 'lib/pione/location/ftp-location.rb', line 71
def delete
connect {|ftp| ftp.delete(@path.to_s)} if exist?
end
|
#directory? ⇒ Boolean
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/pione/location/ftp-location.rb', line 122
def directory?
connect do |ftp|
begin
ftp.chdir(@path.to_s)
return true
rescue
return false
end
end
end
|
#entries(option = {}) ⇒ Object
91
92
93
|
# File 'lib/pione/location/ftp-location.rb', line 91
def entries(option={})
rel_entries(option).map {|entry| rebuild(@path + entry)}
end
|
#exist? ⇒ Boolean
110
111
112
|
# File 'lib/pione/location/ftp-location.rb', line 110
def exist?
file? or directory?
end
|
#file? ⇒ Boolean
114
115
116
117
118
119
120
|
# File 'lib/pione/location/ftp-location.rb', line 114
def file?
begin
connect {|ftp| ftp.size(@path.to_s) > -1}
rescue
false
end
end
|
#inspect ⇒ Object
154
155
156
157
158
159
160
161
|
# File 'lib/pione/location/ftp-location.rb', line 154
def inspect
scheme = @uri.scheme
auth = "%s:%s@" % [@uri.user, @uri.password] if @uri.user and @uri.password
host = @uri.host
port = ":%i" % @uri.port
path = @path.expand_path("/").to_s
"#<%s %s://%s%s%s%s>" % [self.class, scheme, auth, host, port, path]
end
|
#link(orig) ⇒ Object
137
138
139
|
# File 'lib/pione/location/ftp-location.rb', line 137
def link(orig)
orig.copy(self)
end
|
#mkdir ⇒ Object
75
76
77
|
# File 'lib/pione/location/ftp-location.rb', line 75
def mkdir
connect {|ftp| makedirs(ftp, @path)} unless exist?
end
|
#move(dest) ⇒ Object
141
142
143
144
145
146
147
148
|
# File 'lib/pione/location/ftp-location.rb', line 141
def move(dest)
if dest.scheme == scheme and dest.host == host
connect{|ftp| ftp.rename(@path.to_s, dest.path.to_s)}
else
copy(dest)
delete
end
end
|
#mtime ⇒ Object
79
80
81
|
# File 'lib/pione/location/ftp-location.rb', line 79
def mtime
connect {|ftp| exist? ? ftp.mtime(@path.to_s) : (raise NotFound.new(self))}
end
|
#mtime=(time) ⇒ Object
83
84
85
|
# File 'lib/pione/location/ftp-location.rb', line 83
def mtime=(time)
end
|
#read ⇒ Object
51
52
53
54
55
56
57
|
# File 'lib/pione/location/ftp-location.rb', line 51
def read
file = Temppath.create
connect {|ftp| ftp.get(@path, file.to_s)}
return File.read(file.to_s)
rescue Net::FTPPermError
raise NotFound.new(@uri)
end
|
#rebuild(path) ⇒ Object
19
20
21
22
23
24
25
26
|
# File 'lib/pione/location/ftp-location.rb', line 19
def rebuild(path)
scheme = @uri.scheme
auth = "%s:%s@" % [@uri.user, @uri.password] if @uri.user and @uri.password
host = @uri.host
port = ":%i" % @uri.port
path = Pathname.new(path).expand_path("/").to_s
Location["%s://%s%s%s%s" % [scheme, auth, host, port, path]]
end
|
#rel_entries(option = {}) ⇒ Object
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
# File 'lib/pione/location/ftp-location.rb', line 95
def rel_entries(option={})
list = []
connect do |ftp|
ftp.nlst(@path.to_s).each do |entry|
list << entry
entry_location = rebuild(@path + entry)
if option[:rec] and entry_location.directory?
_list = entry_location.rel_entries(option).map {|subentry| entry + subentry}
list = list + _list
end
end
end
return list
end
|
#size ⇒ Object
87
88
89
|
# File 'lib/pione/location/ftp-location.rb', line 87
def size
connect {|ftp| exist? ? ftp.size(@path.to_s) : (raise NotFound.new(self))}
end
|
#turn(dest) ⇒ Object
150
151
152
|
# File 'lib/pione/location/ftp-location.rb', line 150
def turn(dest)
copy(dest)
end
|
#update(data) ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/pione/location/ftp-location.rb', line 59
def update(data)
connect do |ftp|
begin
ftp.dir(@path.dirname.to_s)
src = Temppath.create.tap{|x| x.open("w") {|f| f.write(data)}}.to_s
ftp.put(src, @path.to_s)
rescue Net::FTPPermError
raise NotFound.new(@uri)
end
end
end
|