diff --git a/addons/EGFramework/Module/SaveTools/File/EGLocalFileSave.cs b/addons/EGFramework/Module/SaveTools/File/EGLocalFileSave.cs index 5ad0ea5..db35faf 100644 --- a/addons/EGFramework/Module/SaveTools/File/EGLocalFileSave.cs +++ b/addons/EGFramework/Module/SaveTools/File/EGLocalFileSave.cs @@ -1,77 +1,173 @@ using System.Collections.Generic; using System.IO; +using System.Runtime.InteropServices; namespace EGFramework{ + /// + /// This class is used to save files locally. such as u disk or other local path. used for backup or other purpose. + /// public class EGLocalFileSave : IEGSave, IEGSaveFile { + public string RootPath { get; set; } + public void InitSave(string path) { - throw new System.NotImplementedException(); + this.RootPath = path; } public void CopyFile(string sourcePath, string copyPath) { - throw new System.NotImplementedException(); + if(IsRemoteFileExist(sourcePath)){ + string sourceCombinedPath = Path.Combine(RootPath, sourcePath); + string copyCombinedPath = Path.Combine(RootPath, copyPath); + File.Copy(sourceCombinedPath,copyCombinedPath,true); + }else{ + throw new FileNotFoundException("File not exist!"); + } } public void DownloadFile(string remotePath, string localPath) { - throw new System.NotImplementedException(); + if(IsRemoteFileExist(remotePath)){ + string combinedPath = Path.Combine(RootPath, remotePath); + File.Copy(combinedPath,localPath,true); + }else{ + throw new FileNotFoundException("File not exist!"); + } } public Stream DownloadFile(string remotePath) { - throw new System.NotImplementedException(); + if(IsRemoteFileExist(remotePath)){ + string combinedPath = Path.Combine(RootPath, remotePath); + FileStream fileStream = new FileStream(combinedPath,FileMode.Open,FileAccess.Read); + return fileStream; + }else{ + throw new FileNotFoundException("File not exist!"); + } } public bool IsRemoteDirectoryExist(string remotePath) { - throw new System.NotImplementedException(); + string combinedPath = Path.Combine(RootPath, remotePath); + return Directory.Exists(combinedPath); } public bool IsRemoteFileExist(string remotePath) { - throw new System.NotImplementedException(); + string combinedPath = Path.Combine(RootPath, remotePath); + return File.Exists(combinedPath); } public IEnumerable ListLocalFilePath(string localPath) { - throw new System.NotImplementedException(); + string [] filePaths = Directory.GetFiles(localPath); + string [] directoryPaths = Directory.GetDirectories(localPath); + List fileMsgs = new List(); + foreach(string filePath in filePaths){ + FileInfo fileInfo = new FileInfo(filePath); + EGFileMsg msg = new EGFileMsg(); + msg.Init(fileInfo.Name,false,filePath,fileInfo.Length,fileInfo.LastWriteTime); + fileMsgs.Add(msg); + } + foreach(string directoryPath in directoryPaths){ + DirectoryInfo directoryInfo = new DirectoryInfo(directoryPath); + EGFileMsg msg = new EGFileMsg(); + msg.Init(directoryInfo.Name,true,directoryPath,null,directoryInfo.LastWriteTime); + fileMsgs.Add(msg); + } + return fileMsgs; } public IEnumerable ListRemoteFilePath(string remotePath) { - throw new System.NotImplementedException(); + string combinedPath = Path.Combine(RootPath, remotePath); + string [] filePaths = Directory.GetFiles(combinedPath); + string [] directoryPaths = Directory.GetDirectories(combinedPath); + List fileMsgs = new List(); + foreach(string filePath in filePaths){ + FileInfo fileInfo = new FileInfo(filePath); + EGFileMsg msg = new EGFileMsg(); + msg.Init(fileInfo.Name,false,filePath,fileInfo.Length,fileInfo.LastWriteTime); + fileMsgs.Add(msg); + } + foreach(string directoryPath in directoryPaths){ + DirectoryInfo directoryInfo = new DirectoryInfo(directoryPath); + EGFileMsg msg = new EGFileMsg(); + msg.Init(directoryInfo.Name,true,directoryPath,null,directoryInfo.LastWriteTime); + fileMsgs.Add(msg); + } + return fileMsgs; } public void MakeDirectory(string remotePath) { - throw new System.NotImplementedException(); + string combinedPath = Path.Combine(RootPath, remotePath); + if(Directory.Exists(combinedPath) == false){ + Directory.CreateDirectory(combinedPath); + } } public void MoveFile(string sourcePath, string movePath) { - throw new System.NotImplementedException(); + if(IsRemoteFileExist(sourcePath)){ + string sourceCombinedPath = Path.Combine(RootPath, sourcePath); + string moveCombinedPath = Path.Combine(RootPath, movePath); + File.Move(sourceCombinedPath,moveCombinedPath); + }else{ + throw new FileNotFoundException("File not exist!"); + } } public void RemoveFile(string remotePath) { - throw new System.NotImplementedException(); + if(IsRemoteFileExist(remotePath)){ + string combinedPath = Path.Combine(RootPath, remotePath); + File.Delete(combinedPath); + }else{ + throw new FileNotFoundException("File not exist!"); + } } public void SyncFile(string remotePath, string localPath) { - throw new System.NotImplementedException(); + FileInfo fileInfo = new FileInfo(localPath); + if(fileInfo.Exists == false){ + throw new FileNotFoundException("Local File not exist!"); + } + FileInfo remoteFileInfo = new FileInfo(remotePath); + if(remoteFileInfo.Exists == false){ + throw new FileNotFoundException("Remote File not exist!"); + } + if(fileInfo.LastWriteTime > remoteFileInfo.LastWriteTime){ + string combinedPath = Path.Combine(RootPath, remotePath); + File.Copy(localPath,combinedPath,true); + }else if(fileInfo.LastWriteTime < remoteFileInfo.LastWriteTime){ + string combinedPath = Path.Combine(RootPath, remotePath); + File.Copy(combinedPath,localPath,true); + }else{ + EG.Print("File is same, no need to sync!"); + } } public void UploadFile(FileStream localFileStream, string remotePath) { - throw new System.NotImplementedException(); + string combinedPath = Path.Combine(RootPath, remotePath); + FileStream fileStream = new FileStream(combinedPath,FileMode.Create,FileAccess.Write); + localFileStream.CopyTo(fileStream); + fileStream.Close(); + localFileStream.Close(); + fileStream.Dispose(); } public void UploadFile(string localPath, string remotePath) { - throw new System.NotImplementedException(); + if(File.Exists(localPath)){ + string combinedPath = Path.Combine(RootPath, remotePath); + File.Copy(localPath,combinedPath,true); + }else{ + throw new FileNotFoundException("File not exist!"); + } } } } \ No newline at end of file