From 9c5baddf130920e1a153c40c284d9d970eb4baf0 Mon Sep 17 00:00:00 2001 From: Razim Saidov Date: Wed, 23 May 2018 21:06:33 +0200 Subject: [PATCH] use string instead of ObjectId in GridFS --- .../Assets/MongoGridFsAssetStore.cs | 23 ++++++++----------- .../Assets/MongoGridFsAssetStoreTests.cs | 13 +++++++---- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/Squidex.Infrastructure.MongoGridFs/Assets/MongoGridFsAssetStore.cs b/src/Squidex.Infrastructure.MongoGridFs/Assets/MongoGridFsAssetStore.cs index 7996ac316..c6a00aa26 100644 --- a/src/Squidex.Infrastructure.MongoGridFs/Assets/MongoGridFsAssetStore.cs +++ b/src/Squidex.Infrastructure.MongoGridFs/Assets/MongoGridFsAssetStore.cs @@ -19,10 +19,10 @@ namespace Squidex.Infrastructure.Assets public class MongoGridFsAssetStore : IAssetStore, IInitializable { private readonly string path; - private readonly IGridFSBucket bucket; + private readonly IGridFSBucket bucket; private readonly DirectoryInfo directory; - public MongoGridFsAssetStore(IGridFSBucket bucket, string path) + public MongoGridFsAssetStore(IGridFSBucket bucket, string path) { Guard.NotNull(bucket, nameof(bucket)); Guard.NotNullOrEmpty(path, nameof(path)); @@ -73,13 +73,13 @@ namespace Squidex.Infrastructure.Assets { var file = GetFile(name); + file.CopyTo(GetPath(id, version, suffix)); + using (var stream = new MemoryStream()) { - await bucket.DownloadToStreamByNameAsync(file.Name, stream, cancellationToken: ct); - await bucket.UploadFromStreamAsync(file.Name, stream, cancellationToken: ct); + await bucket.DownloadToStreamAsync(file.Name, stream, cancellationToken: ct); + await bucket.UploadFromStreamAsync(file.Name, file.Name, stream, cancellationToken: ct); } - - file.CopyTo(GetPath(id, version, suffix)); } catch (FileNotFoundException ex) { @@ -109,7 +109,7 @@ namespace Squidex.Infrastructure.Assets { // file not found locally // read from GridFS - await bucket.DownloadToStreamByNameAsync(file.Name, stream, cancellationToken: ct); + await bucket.DownloadToStreamAsync(file.Name, stream, cancellationToken: ct); // add to local assets using (var fileStream = file.OpenWrite()) @@ -142,12 +142,7 @@ namespace Squidex.Infrastructure.Assets try { file.Delete(); - using (var cursor = await bucket.FindAsync(Builders.Filter.And( - Builders.Filter.Eq(x => x.Filename, file.Name) - ))) - { - await cursor.ForEachAsync(fileInfo => bucket.DeleteAsync(fileInfo.Id)); - } + await bucket.DeleteAsync(file.Name); } catch (FileNotFoundException ex) { @@ -166,7 +161,7 @@ namespace Squidex.Infrastructure.Assets try { // upload file to GridFS first - await bucket.UploadFromStreamAsync(file.Name, stream, cancellationToken: ct); + await bucket.UploadFromStreamAsync(file.Name, file.Name, stream, cancellationToken: ct); // create file locally // even if this stage will fail, file will be recreated on the next Download call diff --git a/tests/Squidex.Infrastructure.Tests/Assets/MongoGridFsAssetStoreTests.cs b/tests/Squidex.Infrastructure.Tests/Assets/MongoGridFsAssetStoreTests.cs index bd05add97..8cdf0b38b 100644 --- a/tests/Squidex.Infrastructure.Tests/Assets/MongoGridFsAssetStoreTests.cs +++ b/tests/Squidex.Infrastructure.Tests/Assets/MongoGridFsAssetStoreTests.cs @@ -19,7 +19,7 @@ namespace Squidex.Infrastructure.Assets public class MongoGridFsAssetStoreTests : AssetStoreTests { private readonly string testFolder = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); - private readonly IGridFSBucket bucket = A.Fake(); + private readonly IGridFSBucket bucket = A.Fake>(); public override MongoGridFsAssetStore CreateStore() { @@ -48,9 +48,10 @@ namespace Squidex.Infrastructure.Assets public override Task Should_throw_exception_if_asset_to_download_is_not_found() { var id = Id(); + var filename = $"{id}_1_suffix"; A.CallTo(() => - bucket.DownloadToStreamByNameAsync($"{id}_1_suffix", A.Ignored, null, + bucket.DownloadToStreamAsync(filename, A.Ignored, null, A.Ignored)) .Throws(); @@ -64,6 +65,7 @@ namespace Squidex.Infrastructure.Assets public async Task Should_try_to_download_asset_if_is_not_found_locally() { var id = Id(); + var filename = $"{id}_1_suffix"; using (var stream = new MemoryStream()) { ((IInitializable)Sut).Initialize(); @@ -71,7 +73,7 @@ namespace Squidex.Infrastructure.Assets await Sut.DownloadAsync(id, 1, "suffix", stream); A.CallTo(() => - bucket.DownloadToStreamByNameAsync($"{id}_1_suffix", stream, + bucket.DownloadToStreamAsync(filename, stream, A.Ignored, A.Ignored)) .MustHaveHappened(); @@ -82,7 +84,8 @@ namespace Squidex.Infrastructure.Assets public async Task Should_try_to_upload_asset_and_save_locally() { var id = Id(); - var file = new FileInfo(testFolder + Path.DirectorySeparatorChar + $"{id}_1_suffix"); + var filename = $"{id}_1_suffix"; + var file = new FileInfo(testFolder + Path.DirectorySeparatorChar + filename); using (var stream = new MemoryStream(new byte[] { 0x1, 0x2, 0x3, 0x4 })) { ((IInitializable)Sut).Initialize(); @@ -90,7 +93,7 @@ namespace Squidex.Infrastructure.Assets await Sut.UploadAsync(id, 1, "suffix", stream); A.CallTo(() => - bucket.UploadFromStreamAsync($"{id}_1_suffix", stream, A.Ignored, + bucket.UploadFromStreamAsync(filename, filename, stream, A.Ignored, A.Ignored)) .MustHaveHappened();