private bool MergeExistingFileWithRemoteFile(IFileInfo file, IDocument remoteDoc, Guid guid, out byte[] localHash) {
byte[] remoteHash = remoteDoc.ContentStreamHash();
localHash = null;
if (file.Length.Equals(remoteDoc.ContentStreamLength)) {
using (var f = file.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete)) {
localHash = SHA1Managed.Create().ComputeHash(f);
}
if (remoteHash != null) {
if (localHash != null && localHash.SequenceEqual(remoteHash)) {
if (remoteDoc.LastModificationDate != null) {
try {
file.LastWriteTimeUtc = (DateTime)remoteDoc.LastModificationDate;
} catch(IOException e) {
Logger.Debug("Cannot set last modification date", e);
}
}
file.Uuid = guid;
MappedObject mappedObject = new MappedObject(
file.Name,
remoteDoc.Id,
MappedObjectType.File,
remoteDoc.Parents[0].Id,
remoteDoc.ChangeToken,
remoteDoc.ContentStreamLength ?? file.Length)
{
Guid = guid,
LastLocalWriteTimeUtc = file.LastWriteTimeUtc,
LastRemoteWriteTimeUtc = remoteDoc.LastModificationDate,
LastChecksum = localHash,
ChecksumAlgorithmName = "SHA-1"
};
this.Storage.SaveMappedObject(mappedObject);
return true;
}
}
}
return false;
}
}