Skip to content

Commit

Permalink
added further implementations for #6 + breaking change + fixed bug wi…
Browse files Browse the repository at this point in the history
…th DirectoryExists

* BREAKING CHANGE: File/DirInfo.Path as string array (instead of string list) to force read-only access
* IOClient.RootDirectory.DirectoryExists: if path argument starts with directory separator, route checks to root directory
  • Loading branch information
jochenwezel committed May 23, 2022
1 parent 30700e3 commit f20cba3
Show file tree
Hide file tree
Showing 5 changed files with 394 additions and 23 deletions.
111 changes: 98 additions & 13 deletions CenterDevice.Rest/IO/DirectoryInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,64 @@ public DirectoryInfo(CenterDevice.IO.IOClientBase client, CenterDevice.IO.Direct
this.restCollection = null;
}

/// <summary>
/// Copy a file (WARNING: limited to file sizes &lt; 2 GB, downloads + re-uploads file data)
/// </summary>
/// <param name="source"></param>
/// <param name="newFileName"></param>
/// <exception cref="NotImplementedException"></exception>
public void AddCopy(DirectoryInfo source, string newFileName)
{
throw new NotImplementedException();
}

/// <summary>
/// Copy a file (WARNING: limited to file sizes &lt; 2 GB, downloads + re-uploads file data)
/// </summary>
/// <param name="source"></param>
/// <param name="newFileName"></param>
/// <exception cref="Model.Exceptions.FileAlreadyExistsException"></exception>
public void AddCopy(FileInfo source, string newFileName)
{
if (this.FileExists(newFileName))
throw new Model.Exceptions.FileAlreadyExistsException(this.ioClient.Paths.CombinePath(this.Path, newFileName));
System.IO.Stream dataStream = CopyToMemoryStream(source.Download());
System.Func<System.IO.Stream> data = () => dataStream;
this.UploadAndCreateNewFile(data, newFileName);
}

/// <summary>
/// Create a new memory stream from a stream (WARNING: uses array feature, limits max. file size to 2 GB)
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
private System.IO.Stream CopyToMemoryStream(System.IO.Stream stream)
{
var ms = new System.IO.MemoryStream(ReadAllBytesFromStream(stream));
//var ms = new System.IO.MemoryStream();
//stream.CopyTo(ms);
return ms;
}

/// <summary>
/// Read binary data from stream to array (WARNING: array feature limits max. file size to 2 GB)
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
private Byte[] ReadAllBytesFromStream(System.IO.Stream stream)
{
var ms = new System.IO.MemoryStream();
stream.CopyTo(ms);
return ms.ToArray();
}

/// <summary>
/// Add a link to an existing file of another collection
/// </summary>
/// <param name="file"></param>
/// <exception cref="System.NotSupportedException"></exception>
/// <exception cref="System.NotImplementedException"></exception>
/// <remarks>These file links use special behaviour: a file/document can be referenced in a 1st collection to an origin source in a 2nd collection, but there can't be more than 1 link per collection -> 1st collection can contain (including all sub folders) max. 1 link -> adding a 2nd file link in a folder of 1st collection directly removes the origin 1st link in 1st collection</remarks>
public void AddExistingFile(FileInfo file)
{
if (this.IsRootDirectory)
Expand All @@ -69,7 +127,7 @@ public void AddExistingFile(FileInfo file)


protected readonly CenterDevice.IO.IOClientBase ioClient;
protected readonly CenterDevice.IO.DirectoryInfo parentDirectory;
private CenterDevice.IO.DirectoryInfo parentDirectory;
public CenterDevice.IO.DirectoryInfo ParentDirectory
{
get
Expand Down Expand Up @@ -373,7 +431,7 @@ public FileInfo GetFile(string fileName)
if (file.FileName == fileName)
return file;
}
throw new CenterDevice.Model.Exceptions.FileNotFoundException(this.ioClient.Paths.CombinePath(this.Path.ToArray(), fileName));
throw new CenterDevice.Model.Exceptions.FileNotFoundException(this.ioClient.Paths.CombinePath(this.Path, fileName));
}

/// <summary>
Expand Down Expand Up @@ -416,17 +474,25 @@ public bool DirectoryExists(string directoryName)
if (directoryName == null) throw new ArgumentNullException(nameof(directoryName));
string[] subDirs = directoryName.Split(this.ioClient.Paths.DirectorySeparatorChar, this.ioClient.Paths.AltDirectorySeparatorChar);
if (subDirs.Length == 0) throw new ArgumentNullException(nameof(directoryName));
foreach (DirectoryInfo dir in this.GetDirectories())
if (directoryName.StartsWith(this.ioClient.Paths.DirectorySeparatorChar.ToString()) || directoryName.StartsWith(this.ioClient.Paths.AltDirectorySeparatorChar.ToString()))
{
if (subDirs.Length == 1 && dir.Name == subDirs[0])
return true;
else if (dir.Name == subDirs[0])
string[] childSubDirs = subDirs.AsSpan(1, subDirs.Length - 1).ToArray();
return this.RootDirectory.DirectoryExists(String.Join(this.ioClient.Paths.DirectorySeparatorChar.ToString(), childSubDirs));
}
else
{
foreach (DirectoryInfo dir in this.GetDirectories())
{
string[] childSubDirs = subDirs.AsSpan(1, subDirs.Length - 1).ToArray();
return dir.DirectoryExists(String.Join(this.ioClient.Paths.DirectorySeparatorChar.ToString(), childSubDirs));
if (subDirs.Length == 1 && dir.Name == subDirs[0])
return true;
else if (dir.Name == subDirs[0])
{
string[] childSubDirs = subDirs.AsSpan(1, subDirs.Length - 1).ToArray();
return dir.DirectoryExists(String.Join(this.ioClient.Paths.DirectorySeparatorChar.ToString(), childSubDirs));
}
}
return false;
}
return false;
}

/// <summary>
Expand Down Expand Up @@ -491,6 +557,23 @@ public string Name
}
}

/// <summary>
/// The name of the directory (a CenterDevice collection or folder) or null if root directory
/// </summary>
protected string name
{
set
{
if (this.restCollection != null)
this.restCollection.Name = value;
else if (this.restFolder != null)
this.restFolder.Name = value;
else
//root folder
throw new NotSupportedException("Renaming of root folder not supported");
}
}

/// <summary>
/// Archiving date
/// </summary>
Expand Down Expand Up @@ -705,7 +788,7 @@ public string FullName
{
get
{
return this.ioClient.Paths.CombinePath(this.Path.ToArray());
return this.ioClient.Paths.CombinePath(this.Path);
}
}

Expand Down Expand Up @@ -771,17 +854,17 @@ private string Indent(string value)
/// <summary>
/// The full path starting from root
/// </summary>
public List<string> Path
public string[] Path
{
get
{
List<string> result;
if (this.parentDirectory == null)
result = new List<string>();
else
result = this.parentDirectory.Path;
result = new List<string>(this.parentDirectory.Path);
result.Add(this.Name ?? this.ioClient.Paths.DirectorySeparatorChar.ToString());
return result;
return result.ToArray();
}
}

Expand Down Expand Up @@ -1017,6 +1100,7 @@ public void Move(DirectoryInfo targetDirectory)
throw new NotImplementedException("Delecte action for this directory type hasn't been implemented, yet");
this.ParentDirectory.getDirectories = null; //force reload on next request
targetDirectory.getDirectories = null; //force reload on next request
this.parentDirectory = targetDirectory; //correctly re-assign current file to new parent directory
}

/// <summary>
Expand All @@ -1034,6 +1118,7 @@ public void Rename(string targetName)
else
throw new NotImplementedException("Rename action for this directory type hasn't been implemented, yet");
this.ParentDirectory.getDirectories = null; //force reload on next request
this.name = targetName;
}

/// <summary>
Expand Down
20 changes: 15 additions & 5 deletions CenterDevice.Rest/IO/FileInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public FileInfo(CenterDevice.IO.IOClientBase client, CenterDevice.IO.DirectoryIn
this.document = document;
}

readonly CenterDevice.IO.IOClientBase ioClient;
protected readonly CenterDevice.IO.IOClientBase ioClient;

readonly CenterDevice.IO.DirectoryInfo parentDirectory;
private CenterDevice.IO.DirectoryInfo parentDirectory;
/// <summary>
/// The directory containing this file
/// </summary>
Expand Down Expand Up @@ -108,6 +108,14 @@ public bool IsShared
/// </summary>
public string FileName { get => this.document.Filename; }

protected string fileName
{
set
{
this.document.Filename = value;
}
}

/// <summary>
/// The full path starting from root
/// </summary>
Expand Down Expand Up @@ -142,13 +150,13 @@ public bool HasCollidingDuplicateFile
/// <summary>
/// The full path starting from root
/// </summary>
public List<string> Path
public string[] Path
{
get
{
List<string> result = this.parentDirectory.Path;
var result = new List<string>(this.parentDirectory.Path);
result.Add(this.FileName);
return result;
return result.ToArray();
}
}

Expand Down Expand Up @@ -239,6 +247,7 @@ public void Rename(string targetFileName)
{
this.ioClient.ApiClient.Document.RenameDocument(this.ioClient.CurrentAuthenticationContextUserID, this.ID, targetFileName);
this.parentDirectory.getFiles = null; //force reload on next request since changed file properties must be reloaded
this.fileName = targetFileName;
}

/// <summary>
Expand Down Expand Up @@ -319,6 +328,7 @@ public void Move(DirectoryInfo targetDirectory)
targetDirectory.AssociatedCollection.CollectionID, targetDirectory.FolderID);
this.parentDirectory.getFiles = null; //force reload on next request since changed file properties must be reloaded
targetDirectory.getFiles = null; //force reload on next request since changed file properties must be reloaded
this.parentDirectory = targetDirectory; //correctly re-assign current file to new parent directory
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions CenterDevice.Rest/IO/PathTransformations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ public string CombinePath(string[] directoryNames, string fileName)
return this.CombinePath(combinedPaths.ToArray());
}

///// <summary>
///// Combine directory names and file name to a path
///// </summary>
///// <param name="directoryNames"></param>
///// <param name="fileName"></param>
///// <returns></returns>
//public string CombinePath(List<string> directoryNames, string fileName)
//{
// directoryNames.Add(fileName);
// return this.CombinePath(directoryNames.ToArray());
//}

/// <summary>
/// Combine directory names to a path
/// </summary>
Expand Down
36 changes: 36 additions & 0 deletions CenterDevice.Rest/Model/Exceptions/FileAlreadyExistsException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace CenterDevice.Model.Exceptions
{
/// <summary>
/// Thrown when a required file already exists
/// </summary>
public class FileAlreadyExistsException : System.Exception
{
/// <summary>
/// Thrown when a required file already exists
/// </summary>
/// <param name="remotePath"></param>
public FileAlreadyExistsException(string remotePath) : base("File already existing: " + remotePath)
{
this.RemotePath = remotePath;
}

/// <summary>
/// Thrown when a required file already exists
/// </summary>
/// <param name="remotePath"></param>
/// <param name="innerException"></param>
public FileAlreadyExistsException(string remotePath, Exception innerException) : base("File already existing: " + remotePath, innerException)
{
this.RemotePath = remotePath;
}

/// <summary>
/// The path of the missing file
/// </summary>
public string RemotePath { get; set; }
}
}
Loading

0 comments on commit f20cba3

Please sign in to comment.