You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Zero/ZeroLevel/Models/BinaryDocument.cs

133 lines
3.8 KiB

6 years ago
using System;
using System.Collections.Generic;
using ZeroLevel.DocumentObjectModel;
using ZeroLevel.Services.Serialization;
namespace ZeroLevel.Models
{
/// <summary>
/// Binary data represantation
6 years ago
/// </summary>
public class BinaryDocument :
IBinarySerializable,
IEquatable<BinaryDocument>,
ICloneable
{
/// <summary>
/// Id
6 years ago
/// </summary>
public Guid Id { get; set; }
6 years ago
/// <summary>
/// File name
6 years ago
/// </summary>
public string FileName { get; set; }
6 years ago
/// <summary>
/// Content type (pdf, doc, etc.)
6 years ago
/// </summary>
public string ContentType { get; set; }
6 years ago
/// <summary>
/// Content
6 years ago
/// </summary>
public byte[] Document { get; set; }
6 years ago
/// <summary>
/// Creation date
6 years ago
/// </summary>
public DateTime Created { get; set; }
6 years ago
/// <summary>
/// Optional headers
6 years ago
/// </summary>
public List<Header> Headers { get; set; }
6 years ago
/// <summary>
/// Categories
6 years ago
/// </summary>
public List<Category> Categories { get; set; }
#region Ctors
6 years ago
public BinaryDocument()
{
Created = DateTime.Now;
Headers = new List<Header>();
Categories = new List<Category>();
}
public BinaryDocument(BinaryDocument other)
{
var data = MessageSerializer.Serialize(other);
using (var reader = new MemoryStreamReader(data))
{
Deserialize(reader);
}
}
#endregion Ctors
6 years ago
#region IBinarySerializable
6 years ago
public void Serialize(IBinaryWriter writer)
{
writer.WriteGuid(this.Id);
writer.WriteString(this.FileName);
writer.WriteString(this.ContentType);
writer.WriteBytes(this.Document);
writer.WriteDateTime(this.Created);
writer.WriteCollection(this.Headers);
writer.WriteCollection(this.Categories);
}
public void Deserialize(IBinaryReader reader)
{
this.Id = reader.ReadGuid();
this.FileName = reader.ReadString();
this.ContentType = reader.ReadString();
this.Document = reader.ReadBytes();
this.Created = reader.ReadDateTime() ?? DateTime.Now;
this.Headers = reader.ReadCollection<Header>();
this.Categories = reader.ReadCollection<Category>();
}
#endregion IBinarySerializable
6 years ago
#region Equals & Hash
6 years ago
public override bool Equals(object obj)
{
return this.Equals(obj as BinaryDocument);
}
public bool Equals(BinaryDocument other)
{
if (this == null)
throw new NullReferenceException();
if (other == null) return false;
if (ReferenceEquals(this, other)) return true;
if (this.GetType() != other.GetType()) return false;
if (this.Id != other.Id) return false;
if (DateTime.Compare(this.Created, other.Created) != 0) return false;
if (ArrayExtensions.UnsafeEquals(this.Document, other.Document) == false) return false;
if (string.Compare(this.ContentType, other.ContentType) != 0) return false;
if (string.Compare(this.FileName, other.FileName) != 0) return false;
if (this.Headers.NoOrderingEquals(other.Headers) == false) return false;
if (this.Categories.NoOrderingEquals(other.Categories) == false) return false;
return true;
}
public override int GetHashCode()
{
return Id.GetHashCode();
}
#endregion Equals & Hash
6 years ago
public object Clone()
{
return new BinaryDocument(this);
}
}
}

Powered by TurnKey Linux.