Browse Source

Fake seek stream.

pull/470/head
Sebastian 7 years ago
parent
commit
c9d3cabf2f
  1. 2
      backend/src/Squidex.Domain.Apps.Core.Operations/HandleRules/EventEnricher.cs
  2. 2
      backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/AppQueriesGraphType.cs
  3. 18
      backend/src/Squidex.Infrastructure.Amazon/Assets/AmazonS3AssetStore.cs
  4. 85
      backend/src/Squidex.Infrastructure.Amazon/Assets/SeekFakerStream.cs
  5. 2
      backend/src/Squidex.Infrastructure.GoogleCloud/Assets/GoogleCloudAssetStore.cs
  6. 2
      backend/src/Squidex.Infrastructure.MongoDb/Assets/MongoGridFsAssetStore.cs
  7. 5
      backend/src/Squidex.Infrastructure/Assets/HasherStream.cs
  8. 5
      backend/tests/Squidex.Infrastructure.Tests/Assets/AmazonS3AssetStoreFixture.cs

2
backend/src/Squidex.Domain.Apps.Core.Operations/HandleRules/EventEnricher.cs

@ -50,7 +50,7 @@ namespace Squidex.Domain.Apps.Core.HandleRules
private Task<IUser?> FindUserAsync(RefToken actor) private Task<IUser?> FindUserAsync(RefToken actor)
{ {
var key = $"EventEnrichers_Users_${actor.Identifier}"; var key = $"EventEnrichers_Users_{actor.Identifier}";
return userCache.GetOrCreateAsync(key, async x => return userCache.GetOrCreateAsync(key, async x =>
{ {

2
backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/AppQueriesGraphType.cs

@ -246,7 +246,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL.Types
string.Join("&", string.Join("&",
c.Arguments c.Arguments
.Select(x => new { x.Key, Value = x.Value.ToString() }).Where(x => !string.IsNullOrWhiteSpace(x.Value)) .Select(x => new { x.Key, Value = x.Value.ToString() }).Where(x => !string.IsNullOrWhiteSpace(x.Value))
.Select(x => $"${x.Key}={x.Value}")); .Select(x => $"{x.Key}={x.Value}"));
return odataQuery; return odataQuery;
} }

18
backend/src/Squidex.Infrastructure.Amazon/Assets/AmazonS3AssetStore.cs

@ -5,6 +5,7 @@
// All rights reserved. Licensed under the MIT license. // All rights reserved. Licensed under the MIT license.
// ========================================================================== // ==========================================================================
using System;
using System.IO; using System.IO;
using System.Net; using System.Net;
using System.Threading; using System.Threading;
@ -65,12 +66,12 @@ namespace Squidex.Infrastructure.Assets
if (!exists) if (!exists)
{ {
throw new ConfigurationException($"Cannot connect to Amazon S3 bucket '${options.Bucket}'."); throw new ConfigurationException($"Cannot connect to Amazon S3 bucket '{options.Bucket}'.");
} }
} }
catch (AmazonS3Exception ex) catch (AmazonS3Exception ex)
{ {
throw new ConfigurationException($"Cannot connect to Amazon S3 bucket '${options.Bucket}'.", ex); throw new ConfigurationException($"Cannot connect to Amazon S3 bucket '{options.Bucket}'.", ex);
} }
} }
@ -142,12 +143,13 @@ namespace Squidex.Infrastructure.Assets
var request = new TransferUtilityUploadRequest var request = new TransferUtilityUploadRequest
{ {
AutoCloseStream = false,
BucketName = options.Bucket,
InputStream = stream,
Key = GetKey(fileName) Key = GetKey(fileName)
}; };
ConfigureDefaults(request);
request.InputStream = new SeekFakerStream(stream);
await transferUtility.UploadAsync(request, ct); await transferUtility.UploadAsync(request, ct);
} }
catch (AmazonS3Exception ex) when (ex.StatusCode == HttpStatusCode.PreconditionFailed) catch (AmazonS3Exception ex) when (ex.StatusCode == HttpStatusCode.PreconditionFailed)
@ -197,5 +199,11 @@ namespace Squidex.Infrastructure.Assets
throw new AssetAlreadyExistsException(fileName); throw new AssetAlreadyExistsException(fileName);
} }
private void ConfigureDefaults(TransferUtilityUploadRequest request)
{
request.AutoCloseStream = false;
request.BucketName = options.Bucket;
}
} }
} }

85
backend/src/Squidex.Infrastructure.Amazon/Assets/SeekFakerStream.cs

@ -0,0 +1,85 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System;
using System.IO;
namespace Squidex.Infrastructure.Assets
{
public sealed class SeekFakerStream : Stream
{
private readonly Stream inner;
public override bool CanRead
{
get { return inner.CanRead; }
}
public override bool CanSeek
{
get { return true; }
}
public override bool CanWrite
{
get { return false; }
}
public override long Length
{
get { return inner.Length; }
}
public override long Position
{
get { return inner.Position; }
set { throw new NotSupportedException(); }
}
public SeekFakerStream(Stream inner)
{
Guard.NotNull(inner);
if (!inner.CanRead)
{
throw new ArgumentException("Inner stream must be readable.");
}
this.inner = inner;
}
public override int Read(byte[] buffer, int offset, int count)
{
return inner.Read(buffer, offset, count);
}
public override void Flush()
{
throw new NotSupportedException();
}
public override long Seek(long offset, SeekOrigin origin)
{
if (offset != 0 || origin != SeekOrigin.Begin)
{
throw new NotSupportedException();
}
return 0;
}
public override void SetLength(long value)
{
throw new NotSupportedException();
}
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotSupportedException();
}
}
}

2
backend/src/Squidex.Infrastructure.GoogleCloud/Assets/GoogleCloudAssetStore.cs

@ -39,7 +39,7 @@ namespace Squidex.Infrastructure.Assets
} }
catch (Exception ex) catch (Exception ex)
{ {
throw new ConfigurationException($"Cannot connect to google cloud bucket '${bucketName}'.", ex); throw new ConfigurationException($"Cannot connect to google cloud bucket '{bucketName}'.", ex);
} }
} }

2
backend/src/Squidex.Infrastructure.MongoDb/Assets/MongoGridFsAssetStore.cs

@ -35,7 +35,7 @@ namespace Squidex.Infrastructure.Assets
} }
catch (MongoException ex) catch (MongoException ex)
{ {
throw new ConfigurationException($"Cannot connect to Mongo GridFS bucket '${bucket.Options.BucketName}'.", ex); throw new ConfigurationException($"Cannot connect to Mongo GridFS bucket '{bucket.Options.BucketName}'.", ex);
} }
} }

5
backend/src/Squidex.Infrastructure/Assets/HasherStream.cs

@ -46,6 +46,11 @@ namespace Squidex.Infrastructure.Assets
{ {
Guard.NotNull(inner); Guard.NotNull(inner);
if (!inner.CanRead)
{
throw new ArgumentException("Inner stream must be readable.");
}
this.inner = inner; this.inner = inner;
hasher = IncrementalHash.CreateHash(hashAlgorithmName); hasher = IncrementalHash.CreateHash(hashAlgorithmName);

5
backend/tests/Squidex.Infrastructure.Tests/Assets/AmazonS3AssetStoreFixture.cs

@ -13,14 +13,15 @@ namespace Squidex.Infrastructure.Assets
public AmazonS3AssetStoreFixture() public AmazonS3AssetStoreFixture()
{ {
// From: https://console.aws.amazon.com/iam/home?region=eu-central-1#/users/s3?section=security_credentials
AssetStore = new AmazonS3AssetStore(new AmazonS3Options AssetStore = new AmazonS3AssetStore(new AmazonS3Options
{ {
AccessKey = "secret", AccessKey = "AKIAYR4IRKRWJVE7BEUV",
Bucket = "squidex-test", Bucket = "squidex-test",
BucketFolder = "squidex-assets", BucketFolder = "squidex-assets",
ForcePathStyle = false, ForcePathStyle = false,
RegionName = "eu-central-1", RegionName = "eu-central-1",
SecretKey = "secret", SecretKey = "m7fJ0QzqcQ+2RGaYM7eGRs2QC0efOD+0oHn5UUho",
ServiceUrl = null ServiceUrl = null
}); });
AssetStore.InitializeAsync().Wait(); AssetStore.InitializeAsync().Wait();

Loading…
Cancel
Save