Skip to content

Commit

Permalink
Dev/fileservice copy (KevinJump#98)
Browse files Browse the repository at this point in the history
* Add copy command to the syncFileService.

* Comment up the file service.

Co-authored-by: Kevin Jump <kevin@jumoo.co.uk>
  • Loading branch information
KevinJump and Kevin Jump authored May 4, 2020
1 parent 6775b1a commit 4115f58
Showing 1 changed file with 58 additions and 12 deletions.
70 changes: 58 additions & 12 deletions uSync8.BackOffice/Services/SyncFileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,32 +46,32 @@ public string GetAbsPath(string path)
}

public bool FileExists(string path)
{
return File.Exists(GetAbsPath(path));
}
=> File.Exists(GetAbsPath(path));

public bool DirectoryExists(string path)
{
return Directory.Exists(GetAbsPath(path));
}

=> Directory.Exists(GetAbsPath(path));

public bool RootExists(string path)
{
return DirectoryExists(path);
}
=> DirectoryExists(path);

public void DeleteFile(string path)
{
if (FileExists(path))
File.Delete(GetAbsPath(path));
}

/// <summary>
/// Check if a file exists throw an exception if it doesn't
/// </summary>
public void EnsureFileExists(string path)
{
if (!FileExists(path))
throw new FileNotFoundException("Missing File", path);
}

/// <summary>
/// open a file stream for reading a file
/// </summary>
public FileStream OpenRead(string path)
{
if (!FileExists(path)) return null;
Expand All @@ -80,6 +80,9 @@ public FileStream OpenRead(string path)
return File.OpenRead(absPath);
}

/// <summary>
/// Open a file stream for writing a file
/// </summary>
public FileStream OpenWrite(string path)
{
if (FileExists(path))
Expand All @@ -91,7 +94,21 @@ public FileStream OpenWrite(string path)
return File.OpenWrite(absPath);
}

/// <summary>
/// copy a file from one location to another - creating the directory if it is missing
/// </summary>
public void CopyFile(string source, string target)
{
var absSource = GetAbsPath(source);
var absTarget = GetAbsPath(target);
Directory.CreateDirectory(Path.GetDirectoryName(absTarget));
File.Copy(absSource, absTarget, true);
}

/// <summary>
/// create the directory for a given file.
/// </summary>
/// <param name="filePath"></param>
public void CreateFoldersForFile(string filePath)
{
var absPath = Path.GetDirectoryName(GetAbsPath(filePath));
Expand All @@ -110,16 +127,22 @@ public void CleanFolder(string folder)
Directory.Delete(absPath, true);
}

/// <summary>
/// Get a list of files from a folder.
/// </summary>
public IEnumerable<string> GetFiles(string folder, string extensions)
{
if (DirectoryExists(folder))
{
return Directory.GetFiles(GetAbsPath(folder));
return Directory.GetFiles(GetAbsPath(folder), extensions);
}

return Enumerable.Empty<string>();
}

/// <summary>
/// get a list of child folders in a folder
/// </summary>
public IEnumerable<string> GetDirectories(string folder)
{
if (DirectoryExists(folder))
Expand All @@ -130,6 +153,9 @@ public IEnumerable<string> GetDirectories(string folder)
return Enumerable.Empty<string>();
}

/// <summary>
/// load a file into a XElement object.
/// </summary>
public XElement LoadXElement(string file)
{
EnsureFileExists(file);
Expand All @@ -148,6 +174,9 @@ public XElement LoadXElement(string file)
}
}

/// <summary>
/// save a stream to disk
/// </summary>
public void SaveFile(string filename, Stream stream)
{
logger.Debug<SyncFileService>("Saving File: {0}", filename);
Expand All @@ -160,6 +189,9 @@ public void SaveFile(string filename, Stream stream)
}
}

/// <summary>
/// save a string to disk
/// </summary>
public void SaveFile(string filename, string content)
{
logger.Debug<SyncFileService>("Saving File: {0} [{1}]", filename, content.Length);
Expand All @@ -173,6 +205,9 @@ public void SaveFile(string filename, string content)
}
}

/// <summary>
/// Save an XML Element to disk
/// </summary>
public void SaveXElement(XElement node, string filename)
{
using (var stream = OpenWrite(filename))
Expand All @@ -183,11 +218,13 @@ public void SaveXElement(XElement node, string filename)
}
}

/// <summary>
/// Load an object from XML representation on disk.
/// </summary>
public TObject LoadXml<TObject>(string file)
{
if (FileExists(file))
{

XmlSerializer xmlSerializer = new XmlSerializer(typeof(TObject));
using (var stream = OpenRead(file))
{
Expand All @@ -198,6 +235,9 @@ public TObject LoadXml<TObject>(string file)
return default(TObject);
}

/// <summary>
/// load the contents of a file into a string
/// </summary>
public string LoadContent(string file)
{
if (FileExists(file))
Expand All @@ -211,6 +251,12 @@ public string LoadContent(string file)

public static object _saveLock = new object();

/// <summary>
/// save an object to an XML file representing it.
/// </summary>
/// <typeparam name="TObject"></typeparam>
/// <param name="file"></param>
/// <param name="item"></param>
public void SaveXml<TObject>(string file, TObject item)
{
lock (_saveLock)
Expand Down

0 comments on commit 4115f58

Please sign in to comment.