mirror of https://github.com/Squidex/squidex.git
28 changed files with 120 additions and 172 deletions
@ -1,75 +0,0 @@ |
|||||
// ==========================================================================
|
|
||||
// Squidex Headless CMS
|
|
||||
// ==========================================================================
|
|
||||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|
||||
// All rights reserved. Licensed under the MIT license.
|
|
||||
// ==========================================================================
|
|
||||
|
|
||||
using System.IO; |
|
||||
using System.Text; |
|
||||
using System.Threading; |
|
||||
using System.Threading.Tasks; |
|
||||
using Squidex.Assets; |
|
||||
using Squidex.Domain.Apps.Events.Assets; |
|
||||
using Squidex.Infrastructure; |
|
||||
using Squidex.Infrastructure.EventSourcing; |
|
||||
|
|
||||
namespace Squidex.Domain.Apps.Entities.Assets |
|
||||
{ |
|
||||
public sealed class RepairFiles |
|
||||
{ |
|
||||
private static readonly MemoryStream DummyStream = new MemoryStream(Encoding.UTF8.GetBytes("dummy")); |
|
||||
private readonly IAssetFileStore assetFileStore; |
|
||||
private readonly IEventStore eventStore; |
|
||||
private readonly IEventDataFormatter eventDataFormatter; |
|
||||
|
|
||||
public RepairFiles( |
|
||||
IAssetFileStore assetFileStore, |
|
||||
IEventStore eventStore, |
|
||||
IEventDataFormatter eventDataFormatter) |
|
||||
{ |
|
||||
Guard.NotNull(assetFileStore, nameof(assetFileStore)); |
|
||||
Guard.NotNull(eventStore, nameof(eventStore)); |
|
||||
Guard.NotNull(eventDataFormatter, nameof(eventDataFormatter)); |
|
||||
|
|
||||
this.assetFileStore = assetFileStore; |
|
||||
this.eventStore = eventStore; |
|
||||
this.eventDataFormatter = eventDataFormatter; |
|
||||
} |
|
||||
|
|
||||
public async Task RepairAsync(CancellationToken ct = default) |
|
||||
{ |
|
||||
await foreach (var storedEvent in eventStore.QueryAllAsync("^asset\\-", ct: ct)) |
|
||||
{ |
|
||||
var @event = eventDataFormatter.ParseIfKnown(storedEvent); |
|
||||
|
|
||||
if (@event != null) |
|
||||
{ |
|
||||
switch (@event.Payload) |
|
||||
{ |
|
||||
case AssetCreated assetCreated: |
|
||||
await TryRepairAsync(assetCreated.AppId, assetCreated.AssetId, assetCreated.FileVersion, ct); |
|
||||
break; |
|
||||
case AssetUpdated assetUpdated: |
|
||||
await TryRepairAsync(assetUpdated.AppId, assetUpdated.AssetId, assetUpdated.FileVersion, ct); |
|
||||
break; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
private async Task TryRepairAsync(NamedId<DomainId> appId, DomainId id, long fileVersion, CancellationToken ct) |
|
||||
{ |
|
||||
try |
|
||||
{ |
|
||||
await assetFileStore.GetFileSizeAsync(appId.Id, id, fileVersion, ct); |
|
||||
} |
|
||||
catch (AssetNotFoundException) |
|
||||
{ |
|
||||
DummyStream.Position = 0; |
|
||||
|
|
||||
await assetFileStore.UploadAsync(appId.Id, id, fileVersion, DummyStream, ct); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
Loading…
Reference in new issue