fileTransfer
authortm
Wed, 09 Jul 2003 14:55:42 +0200
changeset 1258 a0eda4db4dad
parent 1257 9e730dd97a7f
child 1259 0a557e59ea4a
fileTransfer
FileURI.st
FtpURI.st
--- a/FileURI.st	Wed Jul 09 14:51:46 2003 +0200
+++ b/FileURI.st	Wed Jul 09 14:55:42 2003 +0200
@@ -61,9 +61,9 @@
     ]
 
     "
-      self fromFilename:'/a/b/c' asFilename
-      self fromFilename:'//a/b/c' asFilename
-      self fromFilename:'a/b/c' asFilename
+      self fromFilename:'/a/b/c'  asFilename   
+      self fromFilename:'//a/b/c' asFilename  
+      self fromFilename:'a/b/c'   asFilename    
     "
 ! !
 
@@ -120,6 +120,15 @@
      - a collection with a stream on a single file,
      - or a collection with streams on a directorie's files, but not recursive"
 
+    self readStreamsDo:aBlock thenRemove:false.
+!
+
+readStreamsDo:aBlock thenRemove:doRemoveSource
+    "evaluate the block with a Collection of streams as first argument
+     and a dictionary containing attributes as second argument,
+     - a collection with a stream on a single file,
+     - or a collection with streams on a directorie's files, but not recursive"
+
     |attributes fn files|
 
     fn := self asFilename.
@@ -141,15 +150,18 @@
             attributes at:#fileSize put:(aFile fileSize).
             attributes at:#baseName put:baseName.  
             (self pathSegements includes:baseName) ifTrue:[
-                attributes at:#uriInfo put: self printString.  
+                attributes at:#uriInfo put:self.  
             ] ifFalse:[
-                attributes at:#uriInfo put: ((self copy) addComponent:baseName) printString.  
+                attributes at:#uriInfo put:((self copy) addComponent:baseName).  
             ].
             stream := aFile readStream.
-            aBlock value:stream value:attributes
+            aBlock value:stream value:attributes.
         ] ensure:[
             stream notNil ifTrue:[stream close]
         ].
+        doRemoveSource == true ifTrue:[
+            aFile remove
+        ].
     ].
 
     "
@@ -185,10 +197,43 @@
                 Transcript showCR:(stream isWritable).
             ].
     "
+!
+
+writeStreamDo:aBlock create:doCreate
+    "evaluate a block with the write stream as first argument
+     and a dictionary containing attributes as second argument.
+     The stream is closed after aBlock has been evaluated.
+     Attributes may be the mime type (key #MIME)"
+
+    |stream fileName|
+
+    fileName := self asFilename.
+    [
+        Stream streamErrorSignal handle:[:ex|
+            doCreate ifFalse:[
+                ex reject
+            ].    
+            (fileName directory) recursiveMakeDirectory.
+            stream := fileName writeStream.
+        ] do:[
+            stream := fileName writeStream.
+        ].
+        aBlock value:stream value:self class attributes
+    ] ensure:[
+        stream notNil ifTrue:[stream close]
+    ].
+
+    "
+        (URI fromString:'file:/home/tm/tmp') 
+            readStreamsDo:[:stream :attributes| 
+                Transcript showCR:(attributes at:#MIME).
+                Transcript showCR:(stream isWritable).
+            ].
+    "
 ! !
 
 !FileURI class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic2/FileURI.st,v 1.2 2003-06-24 14:10:53 tm Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/FileURI.st,v 1.3 2003-07-09 12:55:42 tm Exp $'
 ! !
--- a/FtpURI.st	Wed Jul 09 14:51:46 2003 +0200
+++ b/FtpURI.st	Wed Jul 09 14:55:42 2003 +0200
@@ -15,28 +15,6 @@
     ^ #(ftp)
 ! !
 
-!FtpURI methodsFor:'connecting'!
-
-connectThenDo:aOneArgBlock
-
-    |ftp|
-
-    ftp := FTPClient new.
-
-
-    [
-        ftp connectTo:self host 
-            port:self port 
-            user:(self user ? self defaultUser)
-            password:(self password ? self defaultPassword).
-        aOneArgBlock value:ftp
-    ] ensure:[
-        ftp notNil ifTrue:[
-            ftp close.
-        ].
-    ]
-! !
-
 !FtpURI methodsFor:'defaults'!
 
 defaultPassword
@@ -55,25 +33,101 @@
     ^ 'anonymous'
 ! !
 
-!FtpURI methodsFor:'queries'!
+!FtpURI methodsFor:'file operations'!
+
+ftp:aFtpClient copyContentsOfStream:aReadStream toDestFilePath:aDestiantionPath
+
+    |writestream buffer n|
+
+    writestream := aFtpClient putStreamFor:aDestiantionPath.
+
+    (writestream notNil and:[writestream isWritable]) ifTrue:[
+        buffer := ByteArray new:(self class transferBufferSize).
+        [aReadStream atEnd] whileFalse:[
+            n := aReadStream nextBytes:(buffer size) into:buffer.
+            writestream isExternalStream ifTrue:[
+                writestream writeWait.
+            ].
+            writestream nextPutBytes:n from:buffer.
+        ].
+    ] ifFalse:[ 
+        OperatingSystem fileNotFoundErrorSignal
+            raiseRequestWith:aDestiantionPath
+            errorString:('Write: %1' bindWith:aDestiantionPath asString)
+    ].
+! !
+
+!FtpURI methodsFor:'ftp requests'!
+
+connectThenDo:aOneArgBlock
+
+    |ftp|
+
+    ftp := FTPClient new.
+
+
+    [
+        ftp connectTo:self host 
+            port:self port 
+            user:(self user ? self defaultUser)
+            password:(self password ? self defaultPassword).
+        aOneArgBlock value:ftp
+    ] ensure:[
+        ftp notNil ifTrue:[
+            ftp close.
+        ].
+    ]
+!
 
 pathExists
-    "use FTPClient for now"
+    "establish a connection for try to get a readSteream"
 
     |exists|
 
-    self connectThenDo:[:ftp| |stream|
-        [
-            exists := (ftp getStreamFor:self path) isExists.
-        ] ensure:[
-            stream notNil ifTrue:[
-                stream close.
-            ].
-        ].
+    self connectThenDo:[:aFtpClient|
+        exists := self pathExistsFtp:aFtpClient.
     ].
 
+    ^ exists ? false
 
-    ^ exists ? false
+"
+    |pwd uri|
+
+    pwd := Dialog requestPassword:'Password:'. 
+    uri := (URI fromString:('ftp://tm:%1@exept/home/tm/tmp/test.txt' bindWith:pwd) ).
+    uri pathExists
+"
+!
+
+pathExists:aPathname ftpClient:aFtpClient
+
+    |list|
+
+    list := aFtpClient list:aPathname.
+    ^ list contains:[:aLine| (aLine asCollectionOfWords last) = aPathname ] 
+!
+
+pathExistsFtp:aFtpClient
+
+    ^ self pathExists:self path ftpClient:aFtpClient
+! !
+
+!FtpURI methodsFor:'private'!
+
+ftp:aFtpClient moveStream:anOpenedStream ofPath:aSourcePath baseName:aBasename intoDir:aDirectoryPathName
+
+    |path dir|
+
+    dir := aDirectoryPathName asFilename.
+    path := (dir construct:aBasename) asString.
+    (self pathExists:path ftpClient:aFtpClient) ifTrue:[
+        path := AbsoluteTime now printString.
+        path := (dir construct:(aBasename, '-', path)) asString.
+    ].
+    anOpenedStream resetPosition.
+    self ftp:aFtpClient copyContentsOfStream:anOpenedStream toDestFilePath:path.
+    anOpenedStream close.
+    aFtpClient delete:aSourcePath.
 ! !
 
 !FtpURI methodsFor:'stream access'!
@@ -114,6 +168,18 @@
      The streams are closed after aBlock has been evaluated.
      Attributes may be the mime type (key #MIME)"
 
+    self readStreamsDo:aBlock thenRemove:false
+!
+
+readStreamsDo:aBlock thenRemove:doRemoveSource
+    "evaluate the block with a Collection of streams as first argument
+     and a dictionary containing attributes as second argument,
+     - a collection with a stream on a single file,
+     - or a collection with streams on a directorie's files, but not recursive
+
+     The streams are closed after aBlock has been evaluated.
+     Attributes may be the mime type (key #MIME)"
+
     |attributes list requestDirectory path|
 
     requestDirectory := false.
@@ -122,11 +188,6 @@
     list := OrderedCollection new.
 
     self connectThenDo:[:ftp|
-        ftp connectTo:self host 
-            port:self port 
-            user:(self user ? self defaultUser)
-            password:(self password ? self defaultPassword).
-
         FTPClient fileErrorSignal handle:[:ex|
             list add:path.
             attributes at:#requestDirectory put:false.
@@ -139,34 +200,36 @@
 
         list do:[:aPathName| |baseName stream|
             FTPClient fileErrorSignal handle:[:ex| 
-                "/ skip directories and the summary of the list
-"/                Transcript showCR:('%1, %2' bindWith:aPathName with:ex description)
+                "/ skip subdirectories and the summary of the list
             ] do:[
                 stream := ftp getStreamFor:aPathName.
                 attributes at:#fileSize put:(ftp sizeOf:aPathName).
-                requestDirectory ifTrue:[
-                    baseName := aPathName.
-                ] ifFalse:[
-                    baseName := self pathSegements last.
-                ].
+                requestDirectory 
+                    ifTrue:[  baseName := aPathName ] 
+                    ifFalse:[ baseName := self pathSegements last ].
                 attributes at:#baseName put:baseName
             ].
 
-            stream notNil ifTrue:[
-                (self pathSegements includes:baseName) ifTrue:[
-                    attributes at:#uriInfo put:self printString.  
+            stream notNil ifTrue:[ |src srcPath|
+                (self pathSegements includes:baseName) ifTrue:[ 
+                    srcPath := self path.
+                    attributes at:#uriInfo put:self.  
                 ] ifFalse:[
-                    attributes at:#uriInfo put:((self copy) addComponent:baseName) printString.  
+                    src := ((self copy) addComponent:baseName).
+                    srcPath := src path.
+                    attributes at:#uriInfo put:src.  
                 ].
-                [
-                    aBlock value:stream value:attributes.
-                ] ensure:[ stream close ].
+
+                [ aBlock value:stream value:attributes ] 
+                    ensure:[ stream close ].
+                doRemoveSource == true ifTrue:[ 
+                    ftp delete:srcPath.
+                ].
             ].
         ].
     ].
 
 
-
     "
         |pwd|
 
@@ -209,5 +272,5 @@
 !FtpURI class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic2/FtpURI.st,v 1.2 2003-06-24 14:10:45 tm Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/FtpURI.st,v 1.3 2003-07-09 12:55:24 tm Exp $'
 ! !