1005
|
1 |
"{ Package: 'stx:libbasic2' }"
|
|
2 |
|
|
3 |
HierarchicalURI subclass:#FtpURI
|
|
4 |
instanceVariableNames:''
|
|
5 |
classVariableNames:''
|
|
6 |
poolDictionaries:''
|
|
7 |
category:'Resources'
|
|
8 |
!
|
|
9 |
|
|
10 |
|
|
11 |
!FtpURI class methodsFor:'accessing'!
|
|
12 |
|
|
13 |
schemes
|
|
14 |
|
|
15 |
^ #(ftp)
|
|
16 |
! !
|
|
17 |
|
|
18 |
!FtpURI methodsFor:'defaults'!
|
|
19 |
|
|
20 |
defaultPassword
|
|
21 |
|
|
22 |
^ 'secret@secret'
|
|
23 |
!
|
|
24 |
|
|
25 |
defaultPort
|
|
26 |
"answer the default command-port here"
|
|
27 |
|
|
28 |
^ 21
|
|
29 |
!
|
|
30 |
|
|
31 |
defaultUser
|
|
32 |
|
|
33 |
^ 'anonymous'
|
|
34 |
! !
|
|
35 |
|
1258
|
36 |
!FtpURI methodsFor:'file operations'!
|
|
37 |
|
|
38 |
ftp:aFtpClient copyContentsOfStream:aReadStream toDestFilePath:aDestiantionPath
|
|
39 |
|
|
40 |
|writestream buffer n|
|
|
41 |
|
|
42 |
writestream := aFtpClient putStreamFor:aDestiantionPath.
|
|
43 |
|
|
44 |
(writestream notNil and:[writestream isWritable]) ifTrue:[
|
|
45 |
buffer := ByteArray new:(self class transferBufferSize).
|
|
46 |
[aReadStream atEnd] whileFalse:[
|
|
47 |
n := aReadStream nextBytes:(buffer size) into:buffer.
|
|
48 |
writestream isExternalStream ifTrue:[
|
|
49 |
writestream writeWait.
|
|
50 |
].
|
|
51 |
writestream nextPutBytes:n from:buffer.
|
|
52 |
].
|
|
53 |
] ifFalse:[
|
|
54 |
OperatingSystem fileNotFoundErrorSignal
|
|
55 |
raiseRequestWith:aDestiantionPath
|
|
56 |
errorString:('Write: %1' bindWith:aDestiantionPath asString)
|
|
57 |
].
|
|
58 |
! !
|
|
59 |
|
|
60 |
!FtpURI methodsFor:'ftp requests'!
|
|
61 |
|
|
62 |
connectThenDo:aOneArgBlock
|
|
63 |
|
|
64 |
|ftp|
|
|
65 |
|
|
66 |
ftp := FTPClient new.
|
|
67 |
|
|
68 |
|
|
69 |
[
|
|
70 |
ftp connectTo:self host
|
|
71 |
port:self port
|
|
72 |
user:(self user ? self defaultUser)
|
|
73 |
password:(self password ? self defaultPassword).
|
|
74 |
aOneArgBlock value:ftp
|
|
75 |
] ensure:[
|
|
76 |
ftp notNil ifTrue:[
|
|
77 |
ftp close.
|
|
78 |
].
|
|
79 |
]
|
|
80 |
!
|
1254
|
81 |
|
|
82 |
pathExists
|
1258
|
83 |
"establish a connection for try to get a readSteream"
|
1254
|
84 |
|
|
85 |
|exists|
|
|
86 |
|
1258
|
87 |
self connectThenDo:[:aFtpClient|
|
|
88 |
exists := self pathExistsFtp:aFtpClient.
|
1254
|
89 |
].
|
|
90 |
|
1258
|
91 |
^ exists ? false
|
1254
|
92 |
|
1258
|
93 |
"
|
|
94 |
|pwd uri|
|
|
95 |
|
|
96 |
pwd := Dialog requestPassword:'Password:'.
|
|
97 |
uri := (URI fromString:('ftp://tm:%1@exept/home/tm/tmp/test.txt' bindWith:pwd) ).
|
|
98 |
uri pathExists
|
|
99 |
"
|
|
100 |
!
|
|
101 |
|
|
102 |
pathExists:aPathname ftpClient:aFtpClient
|
|
103 |
|
|
104 |
|list|
|
|
105 |
|
|
106 |
list := aFtpClient list:aPathname.
|
|
107 |
^ list contains:[:aLine| (aLine asCollectionOfWords last) = aPathname ]
|
|
108 |
!
|
|
109 |
|
|
110 |
pathExistsFtp:aFtpClient
|
|
111 |
|
|
112 |
^ self pathExists:self path ftpClient:aFtpClient
|
|
113 |
! !
|
|
114 |
|
|
115 |
!FtpURI methodsFor:'private'!
|
|
116 |
|
|
117 |
ftp:aFtpClient moveStream:anOpenedStream ofPath:aSourcePath baseName:aBasename intoDir:aDirectoryPathName
|
|
118 |
|
|
119 |
|path dir|
|
|
120 |
|
|
121 |
dir := aDirectoryPathName asFilename.
|
|
122 |
path := (dir construct:aBasename) asString.
|
|
123 |
(self pathExists:path ftpClient:aFtpClient) ifTrue:[
|
|
124 |
path := AbsoluteTime now printString.
|
|
125 |
path := (dir construct:(aBasename, '-', path)) asString.
|
|
126 |
].
|
|
127 |
anOpenedStream resetPosition.
|
|
128 |
self ftp:aFtpClient copyContentsOfStream:anOpenedStream toDestFilePath:path.
|
|
129 |
anOpenedStream close.
|
|
130 |
aFtpClient delete:aSourcePath.
|
1254
|
131 |
! !
|
|
132 |
|
1005
|
133 |
!FtpURI methodsFor:'stream access'!
|
|
134 |
|
|
135 |
readStreamDo:aBlock
|
|
136 |
"use FTPClient for now"
|
|
137 |
|
1254
|
138 |
self connectThenDo:[:ftp| |stream path attributes|
|
|
139 |
[
|
|
140 |
path := self path.
|
|
141 |
attributes := self class attributes.
|
|
142 |
attributes at:#fileSize put:(ftp sizeOf:path).
|
|
143 |
attributes at:#baseName put:self pathSegements last.
|
|
144 |
attributes at:#uriInfo put:self printString.
|
|
145 |
|
|
146 |
stream := ftp getStreamFor:path.
|
|
147 |
aBlock value:stream value:attributes.
|
|
148 |
] ensure:[
|
|
149 |
stream notNil ifTrue:[
|
|
150 |
stream close.
|
|
151 |
].
|
|
152 |
].
|
|
153 |
].
|
1005
|
154 |
|
1254
|
155 |
"
|
|
156 |
'ftp://stefan:mschrat.14@hippo/etc/group' asURI readStreamDo:[:stream :attributes |
|
|
157 |
self halt
|
|
158 |
].
|
|
159 |
"
|
|
160 |
!
|
|
161 |
|
|
162 |
readStreamsDo:aBlock
|
|
163 |
"evaluate the block with a Collection of streams as first argument
|
|
164 |
and a dictionary containing attributes as second argument,
|
|
165 |
- a collection with a stream on a single file,
|
|
166 |
- or a collection with streams on a directorie's files, but not recursive
|
|
167 |
|
|
168 |
The streams are closed after aBlock has been evaluated.
|
|
169 |
Attributes may be the mime type (key #MIME)"
|
|
170 |
|
1258
|
171 |
self readStreamsDo:aBlock thenRemove:false
|
|
172 |
!
|
|
173 |
|
|
174 |
readStreamsDo:aBlock thenRemove:doRemoveSource
|
|
175 |
"evaluate the block with a Collection of streams as first argument
|
|
176 |
and a dictionary containing attributes as second argument,
|
|
177 |
- a collection with a stream on a single file,
|
|
178 |
- or a collection with streams on a directorie's files, but not recursive
|
|
179 |
|
|
180 |
The streams are closed after aBlock has been evaluated.
|
|
181 |
Attributes may be the mime type (key #MIME)"
|
|
182 |
|
1254
|
183 |
|attributes list requestDirectory path|
|
|
184 |
|
|
185 |
requestDirectory := false.
|
|
186 |
path := self path.
|
|
187 |
attributes := self class attributes.
|
|
188 |
list := OrderedCollection new.
|
|
189 |
|
|
190 |
self connectThenDo:[:ftp|
|
|
191 |
FTPClient fileErrorSignal handle:[:ex|
|
|
192 |
list add:path.
|
|
193 |
attributes at:#requestDirectory put:false.
|
|
194 |
] do:[
|
|
195 |
ftp cd:path.
|
|
196 |
requestDirectory := true.
|
|
197 |
attributes at:#requestDirectory put:true.
|
|
198 |
list addAll:((ftp list) collect:[:aLine| aLine asCollectionOfWords last]).
|
1005
|
199 |
].
|
1254
|
200 |
|
|
201 |
list do:[:aPathName| |baseName stream|
|
|
202 |
FTPClient fileErrorSignal handle:[:ex|
|
1258
|
203 |
"/ skip subdirectories and the summary of the list
|
1254
|
204 |
] do:[
|
|
205 |
stream := ftp getStreamFor:aPathName.
|
|
206 |
attributes at:#fileSize put:(ftp sizeOf:aPathName).
|
1258
|
207 |
requestDirectory
|
|
208 |
ifTrue:[ baseName := aPathName ]
|
|
209 |
ifFalse:[ baseName := self pathSegements last ].
|
1254
|
210 |
attributes at:#baseName put:baseName
|
|
211 |
].
|
|
212 |
|
1258
|
213 |
stream notNil ifTrue:[ |src srcPath|
|
|
214 |
(self pathSegements includes:baseName) ifTrue:[
|
|
215 |
srcPath := self path.
|
|
216 |
attributes at:#uriInfo put:self.
|
1254
|
217 |
] ifFalse:[
|
1258
|
218 |
src := ((self copy) addComponent:baseName).
|
|
219 |
srcPath := src path.
|
|
220 |
attributes at:#uriInfo put:src.
|
1254
|
221 |
].
|
1258
|
222 |
|
|
223 |
[ aBlock value:stream value:attributes ]
|
|
224 |
ensure:[ stream close ].
|
|
225 |
doRemoveSource == true ifTrue:[
|
|
226 |
ftp delete:srcPath.
|
|
227 |
].
|
1254
|
228 |
].
|
|
229 |
].
|
|
230 |
].
|
|
231 |
|
|
232 |
|
|
233 |
"
|
|
234 |
|pwd|
|
|
235 |
|
|
236 |
pwd := Dialog requestPassword:''.
|
|
237 |
(URI fromString:('ftp://tm:%1@exept/home/tm/tmp' bindWith:pwd) )
|
|
238 |
readStreamsDo:[:stream :attributes |
|
|
239 |
Transcript showCR:(attributes at:#baseName).
|
|
240 |
Transcript showCR:(attributes at:#fileSize).
|
|
241 |
Transcript showCR:(attributes at:#requestDirectory).
|
|
242 |
Transcript showCR:(attributes at:#uriInfo).
|
|
243 |
].
|
|
244 |
"
|
|
245 |
!
|
|
246 |
|
|
247 |
writeStreamDo:aBlock
|
|
248 |
"use FTPClient for now"
|
|
249 |
|
|
250 |
self connectThenDo:[:ftp| |stream|
|
|
251 |
[
|
|
252 |
ftp connectTo:self host
|
|
253 |
port:self port
|
|
254 |
user:(self user ? self defaultUser)
|
|
255 |
password:(self password ? self defaultPassword).
|
|
256 |
stream := ftp putStreamFor:self path.
|
|
257 |
aBlock value:stream value:self class attributes.
|
|
258 |
] ensure:[
|
|
259 |
stream notNil ifTrue:[
|
|
260 |
stream close.
|
|
261 |
].
|
1005
|
262 |
].
|
|
263 |
]
|
1254
|
264 |
|
1005
|
265 |
"
|
|
266 |
'ftp://stefan:mschrat.14@hippo/etc/group' asURI readStreamDo:[:stream :attributes |
|
|
267 |
self halt
|
|
268 |
].
|
|
269 |
"
|
|
270 |
! !
|
|
271 |
|
|
272 |
!FtpURI class methodsFor:'documentation'!
|
|
273 |
|
|
274 |
version
|
1258
|
275 |
^ '$Header: /cvs/stx/stx/libbasic2/FtpURI.st,v 1.3 2003-07-09 12:55:24 tm Exp $'
|
1005
|
276 |
! !
|