Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add functionality for deleting files and folders to FTP Helper #1018

Merged
merged 1 commit into from
Nov 30, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/app/FakeLib/FtpHelper.fs
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,54 @@ and private upload server user pwd (fsi : FileSystemInfo) (rootDir : string) =
uploadAFolder server user pwd fsi.FullName (sprintf "%s\\%s" rootDir fsi.Name)
| "System.IO.FileInfo" -> uploadAFile server user pwd (sprintf "%s\\%s" rootDir fsi.Name) fsi.FullName
| _ -> logfn "Unknown object found at %A" fsi

/// Deletes a single file from remote FTP folder.
/// ## Parameters
/// - `destPath` - The full path to the file which needs to be deleted, including all its parent folders
/// - `server` - FTP Server name (ex: "ftp://10.100.200.300:21/")
/// - `user` - FTP Server login name (ex: "joebloggs")
/// - `pwd` - FTP Server login password (ex: "J0Eblogg5")
let deleteAFile (server : string) (user : string) (pwd : string) (destPath : string) =
logfn "Deleting %s" destPath
destPath
|> fun p -> getServerInfo (sprintf "%s/%s" server p) user pwd WebRequestMethods.Ftp.DeleteFile
|> fun si ->
use response = (si.Request.GetResponse() :?> FtpWebResponse)
logfn "Delete file %s status: %s" destPath response.StatusDescription

let private getFolderContents (server : string) (user : string) (pwd : string) (destPath : string) =
getServerInfo (sprintf "%s/%s" server destPath) user pwd WebRequestMethods.Ftp.ListDirectory
|> fun si ->
use response = (si.Request.GetResponse() :?> FtpWebResponse)
use responseStream = response.GetResponseStream()
use reader = new StreamReader(responseStream)
[ while not reader.EndOfStream do yield reader.ReadLine() ]

let private deleteEmptyFolder (server : string) (user : string) (pwd : string) (destPath : string) =
destPath
|> fun p -> getServerInfo (sprintf "%s/%s" server p) user pwd WebRequestMethods.Ftp.RemoveDirectory
|> fun si ->
use response = (si.Request.GetResponse() :?> FtpWebResponse)
logfn "Delete folder %s status: %s" destPath response.StatusDescription

/// Deletes a single folder from remote FTP folder.
/// ## Parameters
/// - `destPath` - The full path to the folder which needs to be deleted, including all its parent folders
/// - `server` - FTP Server name (ex: "ftp://10.100.200.300:21/")
/// - `user` - FTP Server login name (ex: "joebloggs")
/// - `pwd` - FTP Server login password (ex: "J0Eblogg5")
let rec deleteAFolder (server : string) (user : string) (pwd : string) (destPath : string) =
logfn "Deleting %s" destPath
let folderContents = getFolderContents server user pwd destPath

if folderContents |> List.isEmpty then
deleteEmptyFolder server user pwd destPath
else
folderContents
|> List.iter (fun entry ->
try
deleteAFile server user pwd (Path.Combine(destPath, entry))
with
| _ -> deleteAFolder server user pwd (Path.Combine(destPath, entry)))

deleteEmptyFolder server user pwd destPath