Skip to content

Commit

Permalink
add back UFileProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm360 committed Aug 4, 2024
1 parent e2455d9 commit 39e1efd
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/UniversalFiles.Swarm/Extensions/UFileProviderExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2023-present Etherna SA
// This file is part of UniversalFiles.
//
// UniversalFiles is free software: you can redistribute it and/or modify it under the terms of the
// GNU Lesser General Public License as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// UniversalFiles is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License along with UniversalFiles.
// If not, see <https://www.gnu.org/licenses/>.

using Etherna.BeeNet;
using System;

namespace Etherna.UniversalFiles.Extensions
{
public static class UFileProviderExtension
{
public static UFileProvider UseSwarmUFiles(
this UFileProvider fileProvider,
IBeeClient beeClient)
{
ArgumentNullException.ThrowIfNull(fileProvider, nameof(fileProvider));

fileProvider.RegisterUUriType<SwarmUUri>(uuri => new SwarmUFile(beeClient, uuri));
return fileProvider;
}
}
}
21 changes: 21 additions & 0 deletions src/UniversalFiles/IUFileProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2023-present Etherna SA
// This file is part of UniversalFiles.
//
// UniversalFiles is free software: you can redistribute it and/or modify it under the terms of the
// GNU Lesser General Public License as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// UniversalFiles is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License along with UniversalFiles.
// If not, see <https://www.gnu.org/licenses/>.

namespace Etherna.UniversalFiles
{
public interface IUFileProvider
{
UFile BuildNewUFile(UUri uuri);
}
}
43 changes: 43 additions & 0 deletions src/UniversalFiles/UFileProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2023-present Etherna SA
// This file is part of UniversalFiles.
//
// UniversalFiles is free software: you can redistribute it and/or modify it under the terms of the
// GNU Lesser General Public License as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// UniversalFiles is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License along with UniversalFiles.
// If not, see <https://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Net.Http;

namespace Etherna.UniversalFiles
{
public class UFileProvider : IUFileProvider
{
// Fields.
private readonly Dictionary<Type, Func<UUri, UFile>> uFileBuilders = new(); //<typeof(UUri), UFile builder>

public UFileProvider(IHttpClientFactory httpClientFactory)
{
uFileBuilders[typeof(BasicUUri)] = uuri => new BasicUFile((BasicUUri)uuri, httpClientFactory);
}

// Methods.
public UFile BuildNewUFile(UUri uuri)
{
ArgumentNullException.ThrowIfNull(uuri, nameof(uuri));

var builder = uFileBuilders[uuri.GetType()];
return builder(uuri);
}

public void RegisterUUriType<TUUri>(Func<TUUri, UFile> builder) where TUUri : UUri =>
uFileBuilders[typeof(TUUri)] = uuri => builder((TUUri)uuri);
}
}

0 comments on commit 39e1efd

Please sign in to comment.