using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Threading.Tasks; using WebDav; using System.Web; using System.Net.Http; namespace EGFramework{ public class EGWebDav : IModule { public string ServerUrl { set; get; } = ""; private string UserName { set; get; } = ""; private string Password { set; get; } = ""; public bool IsInit { set; get; } private WebDavClient WebDavClient { set; get; } private string CurrentPath { set; get; } = "/"; public List CurrentFileList { set; get; } = new List(); public void Init() { } public IArchitecture GetArchitecture() { return EGArchitectureImplement.Interface; } public void InitClient(string serverUrl, string userName,string password){ this.ServerUrl = serverUrl; this.UserName = userName; this.Password = password; Dictionary headersAdd = new Dictionary { { "Connection", "keep-alive" }, { "Authorization", "Basic "+ EGWebDavExtension.EncodeCredentials(userName,password) } }; WebDavClient = new WebDavClient(new WebDavClientParams { BaseAddress = new Uri(ServerUrl), Credentials = new NetworkCredential(userName, password), DefaultRequestHeaders = headersAdd }); Console.WriteLine("Client has been init"); } //---------download or upload from WebDav server---------// /// /// Download a file from dav path /// /// Such as /dav/Picture/Picture1.jpg /// download destination,such as C:\Users\W35\Pictures /// you can define file by this name,or by uri /// public async Task DownloadFile(string downloadUri,string localPath,string fileName = ""){ if (fileName.Equals("")){ fileName = Path.GetFileName(downloadUri); } using (var response = await WebDavClient.GetRawFile(downloadUri)) { if(response.IsSuccessful == true){ // use response.Stream using (FileStream DestinationStream = File.Create(localPath + "/" + fileName)) { await response.Stream.CopyToAsync(DestinationStream); //Print("【WebDav】" + fileName + "下载成功!"); } return true; }else{ return false; } } } public async Task DownloadFilProcessed(string downloadUri,string localPath,string fileName = ""){ if (fileName.Equals("")){ fileName = Path.GetFileName(downloadUri); } using (var response = await WebDavClient.GetProcessedFile(downloadUri)) { if(response.IsSuccessful == true){ // use response.Stream using (FileStream DestinationStream = File.Create(localPath + "/" + fileName)) { await response.Stream.CopyToAsync(DestinationStream); //Print("【WebDav】" + fileName + "下载成功!"); } return true; }else{ return false; } } } /// /// Upload a file by localUrl /// /// Such as C:\Users\W35\Pictures\Picture1.jpg /// upload destination,such as /dav/Picture /// you can define file by this name,or by local url /// public async Task UploadFile(string localUrl,string uploadPath,string fileName = ""){ if (fileName.Equals("")){ fileName = Path.GetFileName(localUrl); } // use response.Stream var result = await WebDavClient.PutFile(uploadPath+"/"+fileName, File.OpenRead(localUrl)); if(result.IsSuccessful){ return true; }else{ return false; } } //-----------operate disk-----------// /// /// Default root path is "/",any path should be start with "/" /// /// /// public async Task> GetList(string currentPath){ PropfindResponse result = await WebDavClient.Propfind(ServerUrl+currentPath); List ResultFileList = new List(); if (result.IsSuccessful) { foreach (WebDavResource res in result.Resources) { ResultFileList.Add(new WebDavFileMsg{ FileName = res.DisplayName , IsCollection = res.IsCollection , ContentLength = res.ContentLength , Uri = res.Uri , LastUpdateTime = res.LastModifiedDate }); } } return ResultFileList; } /// /// simple CD command, prop find all file message to CurrentFileList. /// /// /// public async Task ChangeDictionary(string destinationPath){ CurrentPath = destinationPath; PropfindResponse result = await WebDavClient.Propfind(ServerUrl+CurrentPath); CurrentFileList.Clear(); if (result.IsSuccessful) { foreach (WebDavResource res in result.Resources) { CurrentFileList.Add(new WebDavFileMsg{ FileName = res.DisplayName , IsCollection = res.IsCollection , ContentLength = res.ContentLength , Uri = res.Uri , LastUpdateTime = res.LastModifiedDate }); } } } /// /// create a directory /// /// /// public async Task MakeDictionary(string dictionaryName){ await WebDavClient.Mkcol(dictionaryName); } /// /// simple cp command, copy a file with differentName. /// /// /// /// public async Task Copy(string sourceFile,string copyFile){ await WebDavClient.Copy(sourceFile,copyFile); } /// /// simple mv command, move a file with change fileName or different path. /// /// /// /// public async Task Move(string sourceFile,string moveFile){ await WebDavClient.Move(sourceFile,moveFile); } /// /// simple rm command,delete a file. /// /// /// public async Task Remove(string fileName){ await WebDavClient.Delete(fileName); } } public struct WebDavFileMsg{ public string FileName { set; get; } public bool IsCollection { set; get; } /// /// unit is kb /// public long? ContentLength { set; get; } public string Uri { set; get; } public DateTime? LastUpdateTime { set; get; } } public static class EGWebDavExtension{ public static EGWebDav EGWebDav(this IEGFramework self) { return EGArchitectureImplement.Interface.GetModule(); } public static string EncodeCredentials(string username, string password) { string credentials = $"{username}:{password}"; byte[] credentialsBytes = System.Text.Encoding.UTF8.GetBytes(credentials); string encodedCredentials = Convert.ToBase64String(credentialsBytes); return encodedCredentials; } } }