Skip to content

Commit

Permalink
merge hotfix
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm360 committed Jun 15, 2024
2 parents 4d47038 + 8ff57be commit 27a66a7
Show file tree
Hide file tree
Showing 36 changed files with 284 additions and 323 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ Bee Node Options:
--bee-node Use bee native node
--bee-url URL of Bee node (default: http://localhost/)
--bee-api-port Port used by API (default: 1633)
--bee-debug-port Port used by Debug (default: 1635)
Json videos metadata format:
To import from a video list you need a metadata descriptor file. Metadata is a JSON file with the following structure:
Expand Down Expand Up @@ -102,7 +101,6 @@ Bee Node Options:
--bee-node Use bee native node
--bee-url URL of Bee node (default: http://localhost/)
--bee-api-port Port used by API (default: 1633)
--bee-debug-port Port used by Debug (default: 1635)
Run 'evid -h' or 'evid --help' to print help.
```
Expand Down
8 changes: 1 addition & 7 deletions src/EthernaVideoImporter.Core/CommonConsts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using Etherna.BeeNet.Clients.DebugApi;
using Etherna.BeeNet.Clients.GatewayApi;
using System;
using System.IO;
using System.Runtime.InteropServices;
Expand All @@ -22,13 +20,11 @@ namespace Etherna.VideoImporter.Core
{
public sealed class CommonConsts
{
public const int BeeApiPort = 1633;
public const int BeeDebugPort = 1635;
public const int BeePort = 1633;
public const string BeeNodeUrl = "http://localhost/";
public static readonly string DefaultFFmpegFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "FFmpeg");
public const int DownloadMaxRetry = 3;
public static readonly TimeSpan DownloadTimespanRetry = TimeSpan.FromMilliseconds(3500);
public const string EthereumNullAddress = "0x0000000000000000000000000000000000000000";
public const string EthernaIndexUrl = "https://index.etherna.io/";
public const string EthernaGatewayUrl = "https://gateway.etherna.io/";
public const string EthernaSsoUrl = "https://sso.etherna.io/";
Expand Down Expand Up @@ -61,9 +57,7 @@ public static string FFprobeBinaryName
throw new InvalidOperationException("OS not supported");
}
}
public static readonly TimeSpan GnosisBlockTime = TimeSpan.FromSeconds(5);
public const string ImporterIdentifier = "EthernaImporter";
public const string SwarmNullReference = "0000000000000000000000000000000000000000000000000000000000000000";
public static DirectoryInfo TempDirectory { get; } = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), ImporterIdentifier));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Bee.Net" Version="0.2.0-alpha.239" />
<PackageReference Include="Blurhash.SkiaSharp" Version="2.0.0" />
<PackageReference Include="Etherna.YoutubeDownloader.Converter" Version="6.3.10" />
<PackageReference Include="EthernaSdk.Users.Native" Version="0.4.0-alpha.29" />
<PackageReference Include="EthernaSdk.Users.Native" Version="0.4.0-alpha.52" />
<PackageReference Include="GitVersion.MsBuild" Version="5.12.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
32 changes: 17 additions & 15 deletions src/EthernaVideoImporter.Core/EthernaVideoImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@

using Etherna.Authentication;
using Etherna.Authentication.Native;
using Etherna.Sdk.GeneratedClients.Index;
using Etherna.Sdk.Users;
using Etherna.BeeNet.Models;
using Etherna.Sdk.Common.Models;
using Etherna.Sdk.Users.Clients;
using Etherna.VideoImporter.Core.Models.Domain;
using Etherna.VideoImporter.Core.Models.Index;
using Etherna.VideoImporter.Core.Models.ManifestDtos;
Expand All @@ -24,9 +25,9 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using Video = Etherna.VideoImporter.Core.Models.Domain.Video;

namespace Etherna.VideoImporter.Core
{
Expand Down Expand Up @@ -147,13 +148,13 @@ Etherna Video Importer (v{appVersionService.CurrentVersion})
var sourceVideosMetadata = await videoProvider.GetVideosMetadataAsync();
var totalSourceVideo = sourceVideosMetadata.Count();

ioService.WriteLine($"Found {totalSourceVideo} valid videos from source");
ioService.WriteLine($"Found {totalSourceVideo} valid distinct videos from source");

// Get information from etherna index.
ioService.WriteLine("Get user's videos on etherna index");

var userVideosOnIndex = await GetUserVideosOnEthernaAsync(userEthAddress);
var ethernaIndexParameters = await ethernaIndexClient.SystemClient.ParametersAsync();
var ethernaIndexParameters = await ethernaIndexClient.GetIndexParametersAsync();

ioService.WriteLine($"Found {userVideosOnIndex.Count()} videos already published on etherna index");

Expand Down Expand Up @@ -216,9 +217,9 @@ Etherna Video Importer (v{appVersionService.CurrentVersion})
new ThumbnailSwarmFile(
alreadyPresentVideo.LastValidManifest.Thumbnail.AspectRatio,
alreadyPresentVideo.LastValidManifest.Thumbnail.Blurhash,
t.Value,
t.Address.Hash,
0, /*currently we don't have actual size. Acceptable workaround until it is provided in manifest*/
int.Parse(t.Key.Replace("w", "", StringComparison.OrdinalIgnoreCase), CultureInfo.InvariantCulture)));
t.Width));

var videoMetadata = new SwarmVideoMetadata(
sourceMetadata.Id,
Expand All @@ -227,7 +228,8 @@ Etherna Video Importer (v{appVersionService.CurrentVersion})
TimeSpan.FromSeconds(alreadyPresentVideo.LastValidManifest!.Duration),
sourceMetadata.OriginVideoQualityLabel);

var videoSwarmFile = alreadyPresentVideo.LastValidManifest.Sources.Select(v => new VideoSwarmFile(v.Size, v.Quality, v.Reference));
var videoSwarmFile = alreadyPresentVideo.LastValidManifest.Sources.Select(
v => new VideoSwarmFile(v.Size, v.Quality!, v.Address.Hash));

var video = new Video(videoMetadata, videoSwarmFile, thumbnailFiles);

Expand All @@ -240,7 +242,7 @@ Etherna Video Importer (v{appVersionService.CurrentVersion})
var updatedPermalinkHash = await videoUploaderService.UploadVideoManifestAsync(metadataVideo, pinVideos, offerVideos);

// Update on index.
await ethernaIndexClient.VideosClient.VideosPutAsync(
await ethernaIndexClient.UpdateVideoManifestAsync(
alreadyPresentVideo.IndexId,
updatedPermalinkHash);

Expand Down Expand Up @@ -275,7 +277,7 @@ await ethernaIndexClient.VideosClient.VideosPutAsync(
importResult = VideoImportResultSucceeded.FullUploaded(
sourceMetadata,
video.EthernaIndexId!,
video.EthernaPermalinkHash!);
video.EthernaPermalinkHash!.Value);
}

// Import succeeded.
Expand Down Expand Up @@ -324,7 +326,7 @@ await ethernaIndexClient.VideosClient.VideosPutAsync(
}

// Clean up user channel on etherna index.
IEnumerable<string>? gatewayPinnedHashes = null;
IEnumerable<SwarmHash>? gatewayPinnedHashes = null;
if (unpinRemovedVideos)
gatewayPinnedHashes = await gatewayService.GetPinnedResourcesAsync();

Expand Down Expand Up @@ -362,15 +364,15 @@ Total video skipped (already present): {results.OfType<VideoImportResultSucceede
// Helpers.
private async Task<IEnumerable<IndexedVideo>> GetUserVideosOnEthernaAsync(string userAddress)
{
var videos = new List<VideoDto>();
var videos = new List<Sdk.Common.Models.Video>();
const int MaxForPage = 100;

VideoDtoPaginatedEnumerableDto? page = null;
PaginatedResult<Sdk.Common.Models.Video>? page = null;
do
{
page = await ethernaIndexClient.UsersClient.Videos2Async(userAddress, page is null ? 0 : page.CurrentPage + 1, MaxForPage);
page = await ethernaIndexClient.GetVideosByOwnerAsync(userAddress, page is null ? 0 : page.CurrentPage + 1, MaxForPage);
videos.AddRange(page.Elements);
} while (page.Elements.Count != 0);
} while (page.Elements.Any());

return videos.Select(v => new IndexedVideo(v));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using Etherna.BeeNet.Models;

namespace Etherna.VideoImporter.Core.Models.Domain
{
public class AudioSwarmFile : SwarmFileBase
{
// Constructor.
public AudioSwarmFile(
string hash,
SwarmHash hash,
long byteSize)
: base(hash, byteSize)
{
Expand Down
3 changes: 2 additions & 1 deletion src/EthernaVideoImporter.Core/Models/Domain/FileBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using Etherna.BeeNet.Models;
using System.Threading.Tasks;

namespace Etherna.VideoImporter.Core.Models.Domain
{
public abstract class FileBase : IFile
{
// Properties.
public string? SwarmHash { get; protected set; }
public SwarmHash? SwarmHash { get; protected set; }

// Methods.
public abstract Task<long> GetByteSizeAsync();
Expand Down
3 changes: 2 additions & 1 deletion src/EthernaVideoImporter.Core/Models/Domain/IFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using Etherna.BeeNet.Models;
using System.Threading.Tasks;

namespace Etherna.VideoImporter.Core.Models.Domain
{
public interface IFile
{
// Properties.
string? SwarmHash { get; }
SwarmHash? SwarmHash { get; }

// Methods.
Task<long> GetByteSizeAsync();
Expand Down
13 changes: 7 additions & 6 deletions src/EthernaVideoImporter.Core/Models/Domain/SourceFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using Etherna.BeeNet.Models;
using System;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -113,7 +114,7 @@ public async Task<long> GetByteSizeAsync(
}
}

public async Task<(byte[] ByteArray, Encoding? Encoding)> ReadAsByteArrayAsync(
public async Task<(byte[] ByteArray, Encoding? Encoding)> ReadToByteArrayAsync(
bool useCacheIfOnline = false,
SourceUriKind allowedUriKinds = SourceUriKind.All,
string? baseDirectory = null)
Expand Down Expand Up @@ -142,7 +143,7 @@ public async Task<long> GetByteSizeAsync(
}
}

public async Task<(Stream Stream, Encoding? Encoding)> ReadAsStreamAsync(
public async Task<(Stream Stream, Encoding? Encoding)> ReadToStreamAsync(
SourceUriKind allowedUriKinds = SourceUriKind.All,
string? baseDirectory = null)
{
Expand All @@ -159,21 +160,21 @@ public async Task<long> GetByteSizeAsync(
};
}

public async Task<string> ReadAsStringAsync(
public async Task<string> ReadToStringAsync(
bool useCacheIfOnline = false,
SourceUriKind allowedUriKinds = SourceUriKind.All,
string? baseDirectory = null)
{
var (content, encoding) = await ReadAsByteArrayAsync(useCacheIfOnline, allowedUriKinds, baseDirectory);
var (content, encoding) = await ReadToByteArrayAsync(useCacheIfOnline, allowedUriKinds, baseDirectory);
encoding ??= Encoding.UTF8;
return encoding.GetString(content);
}

public void SetSwarmHash(string swarmHash)
public void SetSwarmHash(SwarmHash hash)
{
if (SwarmHash != null)
throw new InvalidOperationException("Swarm hash already set");
SwarmHash = swarmHash;
SwarmHash = hash;
}

public string? TryGetFileName()
Expand Down
3 changes: 2 additions & 1 deletion src/EthernaVideoImporter.Core/Models/Domain/SwarmFileBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using Etherna.BeeNet.Models;
using System.Threading.Tasks;

namespace Etherna.VideoImporter.Core.Models.Domain
Expand All @@ -23,7 +24,7 @@ public abstract class SwarmFileBase : FileBase

// Constructor.
protected SwarmFileBase(
string hash,
SwarmHash hash,
long byteSize)
{
SwarmHash = hash;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static async Task<ThumbnailSourceFile> BuildNewAsync(
{
var thumbnail = new ThumbnailSourceFile(fileUri, httpClientFactory);

using var thumbFileStream = (await thumbnail.ReadAsStreamAsync()).Stream;
using var thumbFileStream = (await thumbnail.ReadToStreamAsync()).Stream;
using var thumbManagedStream = new SKManagedStream(thumbFileStream);
using var thumbBitmap = SKBitmap.Decode(thumbManagedStream);

Expand Down Expand Up @@ -80,7 +80,7 @@ public async Task<IEnumerable<ThumbnailSourceFile>> GetScaledThumbnailsAsync(

List<ThumbnailSourceFile> thumbnails = new();

using var thumbFileStream = (await ReadAsStreamAsync()).Stream;
using var thumbFileStream = (await ReadToStreamAsync()).Stream;
using var thumbManagedStream = new SKManagedStream(thumbFileStream);
using var thumbBitmap = SKBitmap.Decode(thumbManagedStream);

Expand Down
28 changes: 12 additions & 16 deletions src/EthernaVideoImporter.Core/Models/Domain/ThumbnailSwarmFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,24 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using Etherna.BeeNet.Models;

namespace Etherna.VideoImporter.Core.Models.Domain
{
public sealed class ThumbnailSwarmFile : SwarmFileBase, IThumbnailFile
public sealed class ThumbnailSwarmFile(
float aspectRatio,
string blurhash,
SwarmHash hash,
long byteSize,
int width)
: SwarmFileBase(hash, byteSize), IThumbnailFile
{
// Constructor.
public ThumbnailSwarmFile(
float aspectRatio,
string blurhash,
string hash,
long byteSize,
int width)
: base(hash, byteSize)
{
AspectRatio = aspectRatio;
Blurhash = blurhash;
Width = width;
}

// Properties.
public float AspectRatio { get; }
public string Blurhash { get; }
public float AspectRatio { get; } = aspectRatio;
public string Blurhash { get; } = blurhash;
public int Height => (int)(Width / AspectRatio);
public int Width { get; }
public int Width { get; } = width;
}
}
25 changes: 2 additions & 23 deletions src/EthernaVideoImporter.Core/Models/Domain/Video.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using Etherna.VideoImporter.Core.Models.ManifestDtos;
using Etherna.BeeNet.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;

namespace Etherna.VideoImporter.Core.Models.Domain
{
Expand All @@ -40,27 +38,8 @@ public Video(
// Properties.
public IEnumerable<FileBase> EncodedFiles { get; }
public string? EthernaIndexId { get; set; }
public string? EthernaPermalinkHash { get; set; }
public SwarmHash? EthernaPermalinkHash { get; set; }
public VideoMetadataBase Metadata { get; }
public IEnumerable<IThumbnailFile> ThumbnailFiles { get; }

// Methods.
public async Task<long> GetTotalByteSizeAsync()
{
long totalByteSize = 0;

foreach (var file in EncodedFiles)
totalByteSize += await file.GetByteSizeAsync();
foreach (var file in ThumbnailFiles)
totalByteSize += await file.GetByteSizeAsync();
totalByteSize += JsonSerializer.Serialize(await ManifestDto.BuildNewAsync(
this,
CommonConsts.SwarmNullReference,
CommonConsts.EthereumNullAddress,
null,
allowFakeReferences: true)).Length;

return totalByteSize;
}
}
}
Loading

0 comments on commit 27a66a7

Please sign in to comment.