1 changed files with 110 additions and 14 deletions
@ -1,77 +1,173 @@ |
|||||||
using System.Collections.Generic; |
using System.Collections.Generic; |
||||||
using System.IO; |
using System.IO; |
||||||
|
using System.Runtime.InteropServices; |
||||||
|
|
||||||
namespace EGFramework{ |
namespace EGFramework{ |
||||||
|
/// <summary> |
||||||
|
/// This class is used to save files locally. such as u disk or other local path. used for backup or other purpose. |
||||||
|
/// </summary> |
||||||
public class EGLocalFileSave : IEGSave, IEGSaveFile |
public class EGLocalFileSave : IEGSave, IEGSaveFile |
||||||
{ |
{ |
||||||
|
public string RootPath { get; set; } |
||||||
|
|
||||||
public void InitSave(string path) |
public void InitSave(string path) |
||||||
{ |
{ |
||||||
throw new System.NotImplementedException(); |
this.RootPath = path; |
||||||
} |
} |
||||||
|
|
||||||
public void CopyFile(string sourcePath, string copyPath) |
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) |
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) |
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) |
public bool IsRemoteDirectoryExist(string remotePath) |
||||||
{ |
{ |
||||||
throw new System.NotImplementedException(); |
string combinedPath = Path.Combine(RootPath, remotePath); |
||||||
|
return Directory.Exists(combinedPath); |
||||||
} |
} |
||||||
|
|
||||||
public bool IsRemoteFileExist(string remotePath) |
public bool IsRemoteFileExist(string remotePath) |
||||||
{ |
{ |
||||||
throw new System.NotImplementedException(); |
string combinedPath = Path.Combine(RootPath, remotePath); |
||||||
|
return File.Exists(combinedPath); |
||||||
} |
} |
||||||
|
|
||||||
public IEnumerable<IEGFileMsg> ListLocalFilePath(string localPath) |
public IEnumerable<IEGFileMsg> ListLocalFilePath(string localPath) |
||||||
{ |
{ |
||||||
throw new System.NotImplementedException(); |
string [] filePaths = Directory.GetFiles(localPath); |
||||||
|
string [] directoryPaths = Directory.GetDirectories(localPath); |
||||||
|
List<IEGFileMsg> fileMsgs = new List<IEGFileMsg>(); |
||||||
|
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<IEGFileMsg> ListRemoteFilePath(string remotePath) |
public IEnumerable<IEGFileMsg> ListRemoteFilePath(string remotePath) |
||||||
{ |
{ |
||||||
throw new System.NotImplementedException(); |
string combinedPath = Path.Combine(RootPath, remotePath); |
||||||
|
string [] filePaths = Directory.GetFiles(combinedPath); |
||||||
|
string [] directoryPaths = Directory.GetDirectories(combinedPath); |
||||||
|
List<IEGFileMsg> fileMsgs = new List<IEGFileMsg>(); |
||||||
|
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) |
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) |
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) |
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) |
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) |
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) |
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!"); |
||||||
|
} |
||||||
} |
} |
||||||
} |
} |
||||||
} |
} |
Loading…
Reference in new issue