Skip to content

Commit

Permalink
Merge pull request #15 from Etherna/refactor-upload-service
Browse files Browse the repository at this point in the history
Refactor upload service
  • Loading branch information
tmm360 authored Feb 27, 2023
2 parents 2a0f668 + 9d1736f commit beb7192
Show file tree
Hide file tree
Showing 16 changed files with 233 additions and 301 deletions.
25 changes: 13 additions & 12 deletions src/EthernaVideoImporter.Core/CommonConsts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ namespace Etherna.VideoImporter.Core
{
public sealed class CommonConsts
{
public const int BEENODE_GATEWAYPORT = 443;
public const GatewayApiVersion BEENODE_GATEWAYVERSION = GatewayApiVersion.v4_0_0;
public const DebugApiVersion BEENODE_DEBUGVERSION = DebugApiVersion.v4_0_0;
public const int DOWNLOAD_MAX_RETRY = 3;
public static readonly TimeSpan DOWNLOAD_RETRY_TIMESPAN = TimeSpan.FromMilliseconds(3500);
public const string ETHERNA_CREDIT = "https://credit.etherna.io/";
public const string ETHERNA_INDEX = "https://index.etherna.io/";
public const string ETHERNA_GATEWAY = "https://gateway.etherna.io/";
public const int BeeNodeGatewayPort = 443;
public const GatewayApiVersion BeeNodeGatewayVersion = GatewayApiVersion.v4_0_0;
public const DebugApiVersion BeeNodeDebugVersion = DebugApiVersion.v4_0_0;
public const int DownloadMaxRetry = 3;
public static readonly TimeSpan DownloadTimespanRetry = TimeSpan.FromMilliseconds(3500);
public const string EthernaCreditUrl = "https://credit.etherna.io/";
public const string EthernaIndexUrl = "https://index.etherna.io/";
public const string EthernaIndexContentUrlPrefix = "https://etherna.io/embed/";
public const string EthernaGatewayUrl = "https://gateway.etherna.io/";
public const string EthernaPermalinkContentUrlPrefix = "https://etherna.io/embed/";
public const string EthernaSsoClientId = "ethernaVideoImporterId";
public const string EthernaSsoUrl = "https://sso.etherna.io/";
public static string FFMpegBinaryName
{
get
Expand All @@ -43,9 +47,6 @@ public static string FFMpegBinaryName
throw new InvalidOperationException("OS not supported");
}
}
public const string SSO_AUTHORITY = "https://sso.etherna.io/";
public const string SSO_CLIENT_ID = "ethernaVideoImporterId";
public const string PREFIX_ETHERNA_INDEX = "https://etherna.io/embed/";
public const string PREFIX_ETHERNA_PERMALINK = "https://etherna.io/embed/";
public static readonly TimeSpan GnosisBlockTime = TimeSpan.FromSeconds(5);
}
}
30 changes: 27 additions & 3 deletions src/EthernaVideoImporter.Core/Dtos/ManifestDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,45 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using Etherna.VideoImporter.Core.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;

namespace Etherna.VideoImporter.Core.Dtos
{
public sealed class ManifestDto
{
// Constructors.
public ManifestDto(
Video video,
string batchId,
string ownerAddress)
{
if (video is null)
throw new ArgumentNullException(nameof(video));

Title = video.Metadata.Title;
Description = video.Metadata.Description;
OriginalQuality = video.Metadata.OriginVideoQualityLabel;
OwnerAddress = ownerAddress;
Duration = (long)video.Metadata.Duration.TotalSeconds;
Thumbnail = video.ThumbnailFile is null ? null : new ManifestThumbnailDto(video.ThumbnailFile);
Sources = video.EncodedFiles.OfType<VideoFile>().Select(vf => new ManifestVideoSourceDto(vf));
CreatedAt = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
UpdatedAt = null;
BatchId = batchId;
PersonalData = JsonSerializer.Serialize(ManifestPersonalDataDto.BuildNew(video.Metadata.Id));
}

public ManifestDto(
string title,
string description,
string originalQuality,
string ownerAddress,
long duration,
ManifestThumbnailDto thumbnail,
ManifestThumbnailDto? thumbnail,
IEnumerable<ManifestVideoSourceDto> sources,
long createdAt,
long? updatedAt,
Expand All @@ -46,13 +71,12 @@ public ManifestDto(
}

// Properties.
//public string Id { get; }
public string Title { get; }
public string Description { get; }
public string OriginalQuality { get; }
public string OwnerAddress { get; }
public long Duration { get; }
public ManifestThumbnailDto Thumbnail { get; }
public ManifestThumbnailDto? Thumbnail { get; }
public IEnumerable<ManifestVideoSourceDto> Sources { get; }
public long CreatedAt { get; }
public long? UpdatedAt { get; }
Expand Down
20 changes: 19 additions & 1 deletion src/EthernaVideoImporter.Core/Dtos/ManifestThumbnailDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,32 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using Blurhash.SkiaSharp;
using Etherna.VideoImporter.Core.Models;
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.IO;

namespace Etherna.VideoImporter.Core.Dtos
{
public sealed class ManifestThumbnailDto
{
// Constructors.
public ManifestThumbnailDto(ThumbnailFile thumbnailFile)
{
if (thumbnailFile?.UploadedHashReference is null)
throw new ArgumentNullException(nameof(thumbnailFile));

using var thumbFileStream = File.OpenRead(thumbnailFile.DownloadedFilePath);
using var thumbManagedStream = new SKManagedStream(thumbFileStream);
using var thumbBitmap = SKBitmap.Decode(thumbManagedStream);

AspectRatio = thumbnailFile.Width / thumbnailFile.Height;
Blurhash = Blurhasher.Encode(thumbBitmap, 4, 4);
Sources = new Dictionary<string, string>() { {$"{thumbBitmap.Width}w", thumbnailFile.UploadedHashReference} };
}

public ManifestThumbnailDto(
float aspectRatio,
string blurhash,
Expand All @@ -33,6 +52,5 @@ public ManifestThumbnailDto(
public float AspectRatio { get; set; }
public string Blurhash { get; set; }
public IDictionary<string, string> Sources { get; }
public string V { get; } = "1.0";
}
}
15 changes: 14 additions & 1 deletion src/EthernaVideoImporter.Core/Models/Video.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System.Collections.Generic;
using Etherna.VideoImporter.Core.Dtos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;

namespace Etherna.VideoImporter.Core.Models
{
Expand All @@ -10,6 +14,9 @@ public Video(
IEnumerable<FileBase> encodedFiles,
ThumbnailFile? thumbnailFile)
{
if (!encodedFiles.Any())
throw new ArgumentException("Must exist at least a stream");

EncodedFiles = encodedFiles;
Metadata = metadata;
ThumbnailFile = thumbnailFile;
Expand All @@ -21,5 +28,11 @@ public Video(
public string? EthernaPermalinkHash { get; set; }
public VideoMetadataBase Metadata { get; }
public ThumbnailFile? ThumbnailFile { get; }

// Methods.
public long GetTotalByteSize() =>
EncodedFiles.Sum(f => f.ByteSize) +
ThumbnailFile?.ByteSize ?? 0 +
JsonSerializer.Serialize(new ManifestDto(this, "0000000000000000000000000000000000000000000000000000000000000000", "0x0000000000000000000000000000000000000000")).Length;
}
}
3 changes: 3 additions & 0 deletions src/EthernaVideoImporter.Core/Models/VideoMetadataBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,20 @@ public abstract class VideoMetadataBase
protected VideoMetadataBase(
string description,
TimeSpan duration,
string originVideoQualityLabel,
string title)
{
Description = description;
Duration = duration;
OriginVideoQualityLabel = originVideoQualityLabel;
Title = title;
}

// Properties.
public abstract string Id { get; }
public string Description { get; }
public TimeSpan Duration { get; }
public string OriginVideoQualityLabel { get; }
public string Title { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ public abstract class YouTubeVideoMetadataBase : VideoMetadataBase
protected YouTubeVideoMetadataBase(
string description,
TimeSpan duration,
string originVideoQualityLabel,
Thumbnail? thumbnail,
string title,
string youtubeUrl)
: base(description, duration, title)
: base(description, duration, originVideoQualityLabel, title)
{
Thumbnail = thumbnail;
YoutubeUrl = youtubeUrl;
Expand Down
4 changes: 2 additions & 2 deletions src/EthernaVideoImporter.Core/SSO/SignServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public static async Task<LoginResult> SigInSSO()

var options = new OidcClientOptions
{
Authority = CommonConsts.SSO_AUTHORITY,
ClientId = CommonConsts.SSO_CLIENT_ID,
Authority = CommonConsts.EthernaSsoUrl,
ClientId = CommonConsts.EthernaSsoClientId,
RedirectUri = redirectUri,
Scope = "openid profile offline_access ether_accounts userApi.gateway userApi.index",
FilterClaims = false,
Expand Down
Loading

0 comments on commit beb7192

Please sign in to comment.