Skip to content

Commit

Permalink
release v0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm360 committed Aug 4, 2024
2 parents 40ae61d + ea633c6 commit e235a06
Show file tree
Hide file tree
Showing 17 changed files with 988 additions and 878 deletions.
3 changes: 2 additions & 1 deletion UniversalFiles.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ 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 &lt;https://www.gnu.org/licenses/&gt;.</s:String>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EFeature_002EServices_002ECodeCleanup_002EFileHeader_002EFileHeaderSettingsMigrate/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=etherna/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
<s:Boolean x:Key="/Default/UserDictionary/Words/=etherna/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=uuri/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,28 @@
// If not, see <https://www.gnu.org/licenses/>.

using Etherna.BeeNet;
using Etherna.BeeNet.Models;
using Etherna.UniversalFiles.Handlers;
using System;

namespace Etherna.UniversalFiles.Extensions
{
public static class UniversalUriProviderExtensions
public static class UFileProviderExtension
{
public static UniversalUri GetNewUri(
this IUniversalUriProvider uriProvider,
SwarmUri swarmUri,
IBeeClient beeClient,
UniversalUriKind allowedUriKinds = UniversalUriKind.Online,
SwarmAddress? defaultBaseAddress = null)
public static SwarmUFile BuildNewUFile(
this IUFileProvider fileProvider,
SwarmUUri uuri)
{
ArgumentNullException.ThrowIfNull(uriProvider, nameof(uriProvider));

if ((allowedUriKinds & UniversalUriKind.Local) != 0)
throw new ArgumentException("Swarm uri can't be local");
ArgumentNullException.ThrowIfNull(fileProvider, nameof(fileProvider));
return (SwarmUFile)fileProvider.BuildNewUFile(uuri);
}

return new(
swarmUri.ToString(),
new SwarmHandler(beeClient),
allowedUriKinds,
defaultBaseAddress.ToString());
public static UFileProvider UseSwarmUFiles(
this UFileProvider fileProvider,
IBeeClient beeClient)
{
ArgumentNullException.ThrowIfNull(fileProvider, nameof(fileProvider));

fileProvider.RegisterUUriType<SwarmUUri>(uuri => new SwarmUFile(beeClient, uuri));
return fileProvider;
}
}
}
142 changes: 0 additions & 142 deletions src/UniversalFiles.Swarm/Handlers/SwarmHandler.cs

This file was deleted.

95 changes: 95 additions & 0 deletions src/UniversalFiles.Swarm/SwarmUFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// 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;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace Etherna.UniversalFiles
{
public class SwarmUFile(
IBeeClient beeClient,
UUri fileUri)
: UFile(fileUri)
{
protected override async Task<(bool Result, (byte[] ByteArray, Encoding? Encoding)? ContentCache)> ExistsAsync(
string absoluteUri,
UUriKind absoluteUriKind)
{
if (absoluteUriKind != UUriKind.OnlineAbsolute)
throw new InvalidOperationException(
"Invalid online absolute uri kind. It can't be casted to SwarmAddress");

// Try to get file head.
try
{
var headers = await beeClient.TryGetFileHeadersAsync(absoluteUri).ConfigureAwait(false);
if (headers is null)
return (false, null);
}
catch { return (false, null); }

return (true, null);
}

protected override async Task<(long Result, (byte[] ByteArray, Encoding? Encoding)? ContentCache)> GetByteSizeAsync(
string absoluteUri,
UUriKind absoluteUriKind)
{
var size = await beeClient.TryGetFileSizeAsync(absoluteUri).ConfigureAwait(false);
if (size is null)
throw new InvalidOperationException();
return (size.Value, null);
}

protected override async Task<(byte[] ByteArray, Encoding? Encoding)> ReadToByteArrayAsync(
string absoluteUri,
UUriKind absoluteUriKind)
{
var (contentStream, encoding) = await ReadToStreamAsync(absoluteUri, absoluteUriKind).ConfigureAwait(false);

// Copy stream to memory stream.
using var memoryStream = new MemoryStream();
await contentStream.CopyToAsync(memoryStream).ConfigureAwait(false);
memoryStream.Position = 0;

var byteArrayContent = memoryStream.ToArray();
await contentStream.DisposeAsync().ConfigureAwait(false);

return (byteArrayContent, encoding);
}

protected override async Task<(Stream Stream, Encoding? Encoding)> ReadToStreamAsync(
string absoluteUri,
UUriKind absoluteUriKind)
{
var result = await beeClient.GetFileAsync(absoluteUri).ConfigureAwait(false);

// Try to extract the encoding from the Content-Type header.
Encoding? contentEncoding = null;
if (result.ContentHeaders?.ContentType?.CharSet != null)
{
try { contentEncoding = Encoding.GetEncoding(result.ContentHeaders.ContentType.CharSet); }
catch (ArgumentException) { }
}

return (result.Stream, contentEncoding);
}

protected override Task<string?> TryGetFileNameAsync(string originalUri) =>
beeClient.TryGetFileNameAsync(originalUri);
}
}
74 changes: 74 additions & 0 deletions src/UniversalFiles.Swarm/SwarmUUri.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// 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.Models;
using System;

namespace Etherna.UniversalFiles
{
public class SwarmUUri(
SwarmUri uri,
UUriKind allowedUriKinds = UUriKind.All,
string? defaultBaseDirectory = null)
: UUri(uri.ToString(), GetUriKind(uri.ToString()) & allowedUriKinds, defaultBaseDirectory)
{
// Public static methods.
public static UUriKind GetUriKind(string uri)
{
ArgumentNullException.ThrowIfNull(uri, nameof(uri));
return SwarmHash.IsValidHash(uri.Split(SwarmAddress.Separator)[0])
? UUriKind.OnlineAbsolute
: UUriKind.OnlineRelative;
}

// Protected methods.
protected internal override UUriKind GetUriKindHelper(string uri) => GetUriKind(uri);

protected internal override (string AbsoluteUri, UUriKind UriKind)? TryGetParentDirectoryAsAbsoluteUri(
string absoluteUri,
UUriKind absoluteUriKind) =>
throw new InvalidOperationException("Swarm doesn't implement concept of directories");

protected internal override (string AbsoluteUri, UUriKind UriKind) UriToAbsoluteUri(
string originalUri,
string? baseDirectory,
UUriKind uriKind)
{
ArgumentNullException.ThrowIfNull(originalUri, nameof(originalUri));

// Resolve absolute url.
switch (uriKind)
{
case UUriKind.OnlineAbsolute:
return (new SwarmAddress(originalUri).ToString(), UUriKind.OnlineAbsolute);

case UUriKind.OnlineRelative:
if (baseDirectory is null)
throw new ArgumentNullException(nameof(baseDirectory),
"Base directory can't be null with relative original uri");

if ((GetUriKind(baseDirectory) & UUriKind.Absolute) == 0)
throw new InvalidOperationException(
"If uri kind is relative, base directory must be absolute");

var swarmUri = new SwarmUri(originalUri, System.UriKind.Relative);
var swarmAddress = swarmUri.ToSwarmAddress(baseDirectory);

return (swarmAddress.ToString(), UUriKind.OnlineAbsolute);

default: throw new InvalidOperationException("Can't find a valid uri kind");
}
}
}
}
Loading

0 comments on commit e235a06

Please sign in to comment.