Skip to content

Instantly share code, notes, and snippets.

@fitzchak
Created March 27, 2018 09:51
Show Gist options
  • Save fitzchak/4881ef078b1a0c9adf1d80136d2b4b8f to your computer and use it in GitHub Desktop.
Save fitzchak/4881ef078b1a0c9adf1d80136d2b4b8f to your computer and use it in GitHub Desktop.
Compare attachments hash
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using FastTests;
using Raven.Client.Documents.Operations.Attachments;
using Raven.Server.Documents;
using Raven.Server.ServerWide.Context;
using Raven.Tests.Core.Utils.Entities;
using Xunit;
namespace SlowTests.Client.Attachments
{
public class AttachmentsSessionTemp : RavenTestBase
{
[Fact]
public async Task PutAttachmentAndCalculateHash()
{
using (var store = GetDocumentStore())
{
var names = new[]
{
"snagit.exe",
"Docker for Windows Installer.exe",
"JetBrains.Rider-2018.1-EAP1-181.3782.222.Checked.exe"
};
using (var session = store.OpenSession())
using (var stream1 = File.Open("c:/users/fitzchak/Downloads/" + names[0], FileMode.Open))
using (var stream2 = File.Open("c:/users/fitzchak/Downloads/" + names[1], FileMode.Open))
using (var stream3 = File.Open("c:/users/fitzchak/Downloads/" + names[2], FileMode.Open))
{
var user = new User { Name = "Fitzchak" };
session.Store(user, "users/1");
session.Advanced.Attachments.Store("users/1", names[0], stream1, "image/png");
session.Advanced.Attachments.Store(user, names[1], stream2, "ImGgE/jPeG");
session.Advanced.Attachments.Store(user, names[2], stream3);
session.SaveChanges();
}
using (var session = store.OpenSession())
{
var user = session.Load<User>("users/1");
var metadata = session.Advanced.GetMetadataFor(user);
Assert.Equal(DocumentFlags.HasAttachments.ToString(), metadata[Constants.Documents.Metadata.Flags]);
var attachments = metadata.GetObjects(Constants.Documents.Metadata.Attachments);
Assert.Equal(3, attachments.Length);
var orderedNames = names.OrderBy(x => x).ToArray();
for (var i = 0; i < names.Length; i++)
{
var name = orderedNames[i];
var attachmentMetadata = attachments[i];
Assert.Equal(name, attachmentMetadata.GetString(nameof(AttachmentName.Name)));
var hash1 = attachmentMetadata.GetString(nameof(AttachmentName.Hash));
var dbId1 = new Guid("00000000-48c4-421e-9466-000000000000");
await SetDatabaseId(store, dbId1);
using (var attachmentStream = new MemoryStream())
using (var attachment = session.Advanced.Attachments.Get(user, name))
{
attachment.Stream.CopyTo(attachmentStream);
attachmentStream.Position = 0;
var database = await GetDatabase(store.Database);
using (var context = DocumentsOperationContext.ShortTermSingleUse(database))
{
var hash = await AttachmentsStorageHelper.CopyStreamToFileAndCalculateHash(context, attachmentStream, new MemoryStream(), CancellationToken.None);
Console.WriteLine(name);
Console.WriteLine(hash1 == hash);
Console.WriteLine(hash);
Console.WriteLine(hash1);
}
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment