Browse Source

use string instead of ObjectId in GridFS

pull/296/head
Razim Saidov 8 years ago
parent
commit
9c5baddf13
  1. 23
      src/Squidex.Infrastructure.MongoGridFs/Assets/MongoGridFsAssetStore.cs
  2. 13
      tests/Squidex.Infrastructure.Tests/Assets/MongoGridFsAssetStoreTests.cs

23
src/Squidex.Infrastructure.MongoGridFs/Assets/MongoGridFsAssetStore.cs

@ -19,10 +19,10 @@ namespace Squidex.Infrastructure.Assets
public class MongoGridFsAssetStore : IAssetStore, IInitializable public class MongoGridFsAssetStore : IAssetStore, IInitializable
{ {
private readonly string path; private readonly string path;
private readonly IGridFSBucket bucket; private readonly IGridFSBucket<string> bucket;
private readonly DirectoryInfo directory; private readonly DirectoryInfo directory;
public MongoGridFsAssetStore(IGridFSBucket bucket, string path) public MongoGridFsAssetStore(IGridFSBucket<string> bucket, string path)
{ {
Guard.NotNull(bucket, nameof(bucket)); Guard.NotNull(bucket, nameof(bucket));
Guard.NotNullOrEmpty(path, nameof(path)); Guard.NotNullOrEmpty(path, nameof(path));
@ -73,13 +73,13 @@ namespace Squidex.Infrastructure.Assets
{ {
var file = GetFile(name); var file = GetFile(name);
file.CopyTo(GetPath(id, version, suffix));
using (var stream = new MemoryStream()) using (var stream = new MemoryStream())
{ {
await bucket.DownloadToStreamByNameAsync(file.Name, stream, cancellationToken: ct); await bucket.DownloadToStreamAsync(file.Name, stream, cancellationToken: ct);
await bucket.UploadFromStreamAsync(file.Name, stream, cancellationToken: ct); await bucket.UploadFromStreamAsync(file.Name, file.Name, stream, cancellationToken: ct);
} }
file.CopyTo(GetPath(id, version, suffix));
} }
catch (FileNotFoundException ex) catch (FileNotFoundException ex)
{ {
@ -109,7 +109,7 @@ namespace Squidex.Infrastructure.Assets
{ {
// file not found locally // file not found locally
// read from GridFS // read from GridFS
await bucket.DownloadToStreamByNameAsync(file.Name, stream, cancellationToken: ct); await bucket.DownloadToStreamAsync(file.Name, stream, cancellationToken: ct);
// add to local assets // add to local assets
using (var fileStream = file.OpenWrite()) using (var fileStream = file.OpenWrite())
@ -142,12 +142,7 @@ namespace Squidex.Infrastructure.Assets
try try
{ {
file.Delete(); file.Delete();
using (var cursor = await bucket.FindAsync(Builders<GridFSFileInfo>.Filter.And( await bucket.DeleteAsync(file.Name);
Builders<GridFSFileInfo>.Filter.Eq(x => x.Filename, file.Name)
)))
{
await cursor.ForEachAsync(fileInfo => bucket.DeleteAsync(fileInfo.Id));
}
} }
catch (FileNotFoundException ex) catch (FileNotFoundException ex)
{ {
@ -166,7 +161,7 @@ namespace Squidex.Infrastructure.Assets
try try
{ {
// upload file to GridFS first // 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 // create file locally
// even if this stage will fail, file will be recreated on the next Download call // even if this stage will fail, file will be recreated on the next Download call

13
tests/Squidex.Infrastructure.Tests/Assets/MongoGridFsAssetStoreTests.cs

@ -19,7 +19,7 @@ namespace Squidex.Infrastructure.Assets
public class MongoGridFsAssetStoreTests : AssetStoreTests<MongoGridFsAssetStore> public class MongoGridFsAssetStoreTests : AssetStoreTests<MongoGridFsAssetStore>
{ {
private readonly string testFolder = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); private readonly string testFolder = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
private readonly IGridFSBucket bucket = A.Fake<IGridFSBucket>(); private readonly IGridFSBucket<string> bucket = A.Fake<IGridFSBucket<string>>();
public override MongoGridFsAssetStore CreateStore() 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() public override Task Should_throw_exception_if_asset_to_download_is_not_found()
{ {
var id = Id(); var id = Id();
var filename = $"{id}_1_suffix";
A.CallTo(() => A.CallTo(() =>
bucket.DownloadToStreamByNameAsync($"{id}_1_suffix", A<MemoryStream>.Ignored, null, bucket.DownloadToStreamAsync(filename, A<MemoryStream>.Ignored, null,
A<CancellationToken>.Ignored)) A<CancellationToken>.Ignored))
.Throws<AssetNotFoundException>(); .Throws<AssetNotFoundException>();
@ -64,6 +65,7 @@ namespace Squidex.Infrastructure.Assets
public async Task Should_try_to_download_asset_if_is_not_found_locally() public async Task Should_try_to_download_asset_if_is_not_found_locally()
{ {
var id = Id(); var id = Id();
var filename = $"{id}_1_suffix";
using (var stream = new MemoryStream()) using (var stream = new MemoryStream())
{ {
((IInitializable)Sut).Initialize(); ((IInitializable)Sut).Initialize();
@ -71,7 +73,7 @@ namespace Squidex.Infrastructure.Assets
await Sut.DownloadAsync(id, 1, "suffix", stream); await Sut.DownloadAsync(id, 1, "suffix", stream);
A.CallTo(() => A.CallTo(() =>
bucket.DownloadToStreamByNameAsync($"{id}_1_suffix", stream, bucket.DownloadToStreamAsync(filename, stream,
A<GridFSDownloadByNameOptions>.Ignored, A<GridFSDownloadByNameOptions>.Ignored,
A<CancellationToken>.Ignored)) A<CancellationToken>.Ignored))
.MustHaveHappened(); .MustHaveHappened();
@ -82,7 +84,8 @@ namespace Squidex.Infrastructure.Assets
public async Task Should_try_to_upload_asset_and_save_locally() public async Task Should_try_to_upload_asset_and_save_locally()
{ {
var id = Id(); 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 })) using (var stream = new MemoryStream(new byte[] { 0x1, 0x2, 0x3, 0x4 }))
{ {
((IInitializable)Sut).Initialize(); ((IInitializable)Sut).Initialize();
@ -90,7 +93,7 @@ namespace Squidex.Infrastructure.Assets
await Sut.UploadAsync(id, 1, "suffix", stream); await Sut.UploadAsync(id, 1, "suffix", stream);
A.CallTo(() => A.CallTo(() =>
bucket.UploadFromStreamAsync($"{id}_1_suffix", stream, A<GridFSUploadOptions>.Ignored, bucket.UploadFromStreamAsync(filename, filename, stream, A<GridFSUploadOptions>.Ignored,
A<CancellationToken>.Ignored)) A<CancellationToken>.Ignored))
.MustHaveHappened(); .MustHaveHappened();

Loading…
Cancel
Save