Skip to content

Commit

Permalink
Full update
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauro Cardillo committed Sep 13, 2023
1 parent 1222728 commit 3fd048f
Show file tree
Hide file tree
Showing 23 changed files with 1,418 additions and 590 deletions.
292 changes: 209 additions & 83 deletions src/FileSystem.Adapters.Ftp/src/FtpAdapter.cs

Large diffs are not rendered by default.

102 changes: 52 additions & 50 deletions src/FileSystem.Adapters.Memory/src/MemoryAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,51 @@ public MemoryAdapter(string prefix, string rootPath) : base(prefix, rootPath)
_directories.Add(System.IO.Path.Combine(rootPath), new MemoryDirectory() { FullName = rootPath, Name = GetLastPathPart(rootPath), Root = "" });
}

public override void DisposeAdapter(bool disposing)
public override async Task AppendFileAsync(string path, byte[] contents, CancellationToken cancellationToken = default)
{
var file = await GetFileAsync(path, cancellationToken);

var content = file.Content ?? Array.Empty<byte>();
file.Content = content.Concat(contents).ToArray();

await Task.Run(() => _files[PrependRootPath(path)].Content = file.Content);
}

public override void Connect() => Logger?.Information("{Adapter} - Connected succsefull", nameof(MemoryAdapter));

public override async Task<IFile> GetFileAsync(string path, CancellationToken cancellationToken = default)
public override async Task CreateDirectoryAsync(string path, CancellationToken cancellationToken = default)
{
path = PrependRootPath(path);
if (await DirectoryExistsAsync(path, cancellationToken))
throw new DirectoryExistsException(PrependRootPath(path), Prefix);

try
{
var found = await Task.Run(() => _files.ContainsKey(path), cancellationToken);

if (!found)
throw new FileNotFoundException(path, Prefix);

_files.TryGetValue(path, out var memoryFile);

return ModelFactory.CreateFile(memoryFile);
}
catch (FileSystemException)
{
throw;
await Task.Run(() => _directories.Add(PrependRootPath(path), new MemoryDirectory() { Name = System.IO.Path.GetFileName(path), Root = System.IO.Path.GetDirectoryName(PrependRootPath(path)).Replace("\\", "/"), FullName = PrependRootPath(path) }), cancellationToken);
}
catch (Exception exception)
{
throw new AdapterRuntimeException(exception);
}
}

public override async Task DeleteDirectoryAsync(string path, CancellationToken cancellationToken = default)
{
await GetDirectoryAsync(path, cancellationToken);
await Task.Run(() => _directories.Remove(PrependRootPath(path), true), cancellationToken);
}

public override async Task DeleteFileAsync(string path, CancellationToken cancellationToken = default)
{
await GetFileAsync(path, cancellationToken);
await Task.Run(() => _files.Remove(PrependRootPath(path)), cancellationToken);
}

public override void Disconnect() => Logger?.Information("{Adapter} - Disconnect succsefull", nameof(MemoryAdapter));

public override void DisposeAdapter(bool disposing)
{
}

public override async Task<IDirectory> GetDirectoryAsync(string path, CancellationToken cancellationToken = default)
{
path = PrependRootPath(path);
Expand All @@ -79,7 +93,7 @@ public override async Task<IDirectory> GetDirectoryAsync(string path, Cancellati
}
}

public override async Task<IEnumerable<IFile>> GetFilesAsync(string path = "", CancellationToken cancellationToken = default)
public override async Task<IEnumerable<IDirectory>> GetDirectoriesAsync(string path = "", CancellationToken cancellationToken = default)
{
path = PrependRootPath(path);
var found = await Task.Run(() => _directories.ContainsKey(path), cancellationToken);
Expand All @@ -91,61 +105,59 @@ public override async Task<IEnumerable<IFile>> GetFilesAsync(string path = "", C

try
{
return await Task.Run(() => _files.Where(p => p.Value.Directory == memoryDirectory.FullName).Select(item => ModelFactory.CreateFile(item.Value)).AsEnumerable(), cancellationToken);
return await Task.Run(() => _directories.Where(p => p.Value.Root == path).Select(p => ModelFactory.CreateDirectory(p.Value)).AsEnumerable(), cancellationToken);
}
catch (Exception exception)
{
throw new AdapterRuntimeException(exception);
}
}

public override async Task<IEnumerable<IDirectory>> GetDirectoriesAsync(string path = "", CancellationToken cancellationToken = default)
public override async Task<IFile> GetFileAsync(string path, CancellationToken cancellationToken = default)
{
path = PrependRootPath(path);
var found = await Task.Run(() => _directories.ContainsKey(path), cancellationToken);

if (!found)
throw new DirectoryNotFoundException(path, Prefix);
try
{
var found = await Task.Run(() => _files.ContainsKey(path), cancellationToken);

_directories.TryGetValue(path, out var memoryDirectory);
if (!found)
throw new FileNotFoundException(path, Prefix);

try
_files.TryGetValue(path, out var memoryFile);

return ModelFactory.CreateFile(memoryFile);
}
catch (FileSystemException)
{
return await Task.Run(() => _directories.Where(p => p.Value.Root == path).Select(p => ModelFactory.CreateDirectory(p.Value)).AsEnumerable(), cancellationToken);
throw;
}
catch (Exception exception)
{
throw new AdapterRuntimeException(exception);
}
}

public override async Task CreateDirectoryAsync(string path, CancellationToken cancellationToken = default)
public override async Task<IEnumerable<IFile>> GetFilesAsync(string path = "", CancellationToken cancellationToken = default)
{
if (await DirectoryExistsAsync(path, cancellationToken))
throw new DirectoryExistsException(PrependRootPath(path), Prefix);
path = PrependRootPath(path);
var found = await Task.Run(() => _directories.ContainsKey(path), cancellationToken);

if (!found)
throw new DirectoryNotFoundException(path, Prefix);

_directories.TryGetValue(path, out var memoryDirectory);

try
{
await Task.Run(() => _directories.Add(PrependRootPath(path), new MemoryDirectory() { Name = System.IO.Path.GetFileName(path), Root = System.IO.Path.GetDirectoryName(PrependRootPath(path)).Replace("\\", "/"), FullName = PrependRootPath(path) }), cancellationToken);
return await Task.Run(() => _files.Where(p => p.Value.Directory == memoryDirectory.FullName).Select(item => ModelFactory.CreateFile(item.Value)).AsEnumerable(), cancellationToken);
}
catch (Exception exception)
{
throw new AdapterRuntimeException(exception);
}
}

public override async Task DeleteFileAsync(string path, CancellationToken cancellationToken = default)
{
await GetFileAsync(path, cancellationToken);
await Task.Run(() => _files.Remove(PrependRootPath(path)), cancellationToken);
}

public override async Task DeleteDirectoryAsync(string path, CancellationToken cancellationToken = default)
{
await GetDirectoryAsync(path, cancellationToken);
await Task.Run(() => _directories.Remove(PrependRootPath(path), true), cancellationToken);
}

public override async Task<byte[]> ReadFileAsync(string path, CancellationToken cancellationToken = default)
{
await GetFileAsync(path, cancellationToken);
Expand Down Expand Up @@ -188,15 +200,5 @@ public override async Task WriteFileAsync(string path, byte[] contents, bool ove
await Task.Run(() => _files.Add(PrependRootPath(path), memoryFile));
}
}

public override async Task AppendFileAsync(string path, byte[] contents, CancellationToken cancellationToken = default)
{
var file = await GetFileAsync(path, cancellationToken);

var content = file.Content ?? Array.Empty<byte>();
file.Content = content.Concat(contents).ToArray();

await Task.Run(() => _files[PrependRootPath(path)].Content = file.Content);
}
}
}
Loading

0 comments on commit 3fd048f

Please sign in to comment.