BuildFeed/BuildFeed.Model/Build.cs

197 lines
5.8 KiB
C#
Raw Normal View History

using System;
2017-04-16 22:46:02 +08:00
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Text;
using BuildFeed.Local;
using HtmlAgilityPack;
using MongoDB.Bson.Serialization.Attributes;
using Required = System.ComponentModel.DataAnnotations.RequiredAttribute;
namespace BuildFeed.Model
{
2017-02-24 04:53:49 +08:00
[DataObject]
[BsonIgnoreExtraElements]
2017-04-17 06:22:48 +08:00
public class Build : BuildDetails
2017-02-24 04:53:49 +08:00
{
[Key]
[BsonId]
public Guid Id { get; set; }
public long? LegacyId { get; set; }
[@Required]
[Display(ResourceType = typeof(VariantTerms), Name = nameof(VariantTerms.Model_Added))]
public DateTime Added { get; set; }
[@Required]
[Display(ResourceType = typeof(VariantTerms), Name = nameof(VariantTerms.Model_Modified))]
public DateTime Modified { get; set; }
2017-04-16 22:46:02 +08:00
public string LabUrl { get; private set; }
public bool IsLeaked => SourceType == TypeOfSource.PublicRelease || SourceType == TypeOfSource.InternalLeak || SourceType == TypeOfSource.UpdateGDR;
2017-02-24 04:53:49 +08:00
2017-04-16 22:46:02 +08:00
public string FullBuildString { get; private set; }
2017-02-24 04:53:49 +08:00
2017-04-16 22:46:02 +08:00
public string AlternateBuildString { get; private set; }
2017-02-24 04:53:49 +08:00
2017-04-17 06:22:48 +08:00
public List<ItemHistory<BuildDetails>> History { get; set; }
2017-02-24 04:53:49 +08:00
[Display(ResourceType = typeof(VariantTerms), Name = nameof(VariantTerms.Search_Version))]
2017-02-24 04:53:49 +08:00
public ProjectFamily Family
{
get
{
if (Number >= 15140)
{
return ProjectFamily.Redstone3;
}
2017-02-24 04:53:49 +08:00
if (Number >= 14800)
{
return ProjectFamily.Redstone2;
}
if (Number >= 11000)
{
return ProjectFamily.Redstone;
}
if (Number >= 10500)
{
return ProjectFamily.Threshold2;
}
if (Number >= 9700)
{
return ProjectFamily.Threshold;
}
if (Number >= 9250)
{
return ProjectFamily.Windows81;
}
if (Number >= 7650)
{
return ProjectFamily.Windows8;
}
if (Number >= 6020)
{
return ProjectFamily.Windows7;
}
if (MajorVersion == 6
&& Number >= 5000)
{
return ProjectFamily.WindowsVista;
}
if (MajorVersion == 6)
{
return ProjectFamily.Longhorn;
}
if (MajorVersion == 5
&& Number >= 3000)
{
return ProjectFamily.Server2003;
}
if (MajorVersion == 5
&& Number >= 2205)
{
return ProjectFamily.WindowsXP;
}
if (MajorVersion == 5
&& MinorVersion == 50)
{
return ProjectFamily.Neptune;
}
if (MajorVersion == 5)
{
return ProjectFamily.Windows2000;
}
return ProjectFamily.None;
}
}
public string SourceDetailsFiltered
{
get
{
HtmlDocument hDoc = new HtmlDocument();
hDoc.LoadHtml($"<div>{SourceDetails}</div>");
if (string.IsNullOrWhiteSpace(hDoc.DocumentNode.InnerText))
{
return "";
}
if (Uri.IsWellFormedUriString(hDoc.DocumentNode.InnerText, UriKind.Absolute))
{
Uri uri = new Uri(hDoc.DocumentNode.InnerText, UriKind.Absolute);
return $"<a href=\"{uri}\" target=\"_blank\">{VariantTerms.Model_ExternalLink} <i class=\"fa fa-external-link\"></i></a>";
}
return SourceDetails;
}
}
2017-04-16 22:46:02 +08:00
public void RegenerateCachedProperties()
{
GenerateFullBuildString();
GenerateAlternateBuildString();
GenerateLabUrl();
}
private void GenerateFullBuildString()
2017-02-24 04:53:49 +08:00
{
StringBuilder sb = new StringBuilder();
sb.Append($"{MajorVersion}.{MinorVersion}.{Number}");
if (Revision.HasValue)
{
sb.Append($".{Revision}");
}
if (!string.IsNullOrWhiteSpace(Lab))
{
sb.Append($".{Lab}");
}
2017-02-24 04:53:49 +08:00
if (BuildTime.HasValue)
{
2017-02-24 04:53:49 +08:00
sb.Append($".{BuildTime.Value.ToString("yyMMdd-HHmm", CultureInfo.InvariantCulture.DateTimeFormat)}");
}
2016-05-02 20:02:42 +08:00
2017-04-16 22:46:02 +08:00
FullBuildString = sb.ToString();
2017-02-24 04:53:49 +08:00
}
2016-12-10 10:08:24 +08:00
2017-04-16 22:46:02 +08:00
private void GenerateAlternateBuildString()
2017-02-24 04:53:49 +08:00
{
StringBuilder sb = new StringBuilder();
sb.Append($"{MajorVersion}.{MinorVersion}.{Number}");
2016-12-10 10:08:24 +08:00
2017-02-24 04:53:49 +08:00
if (Revision.HasValue)
{
sb.Append($".{Revision}");
}
2016-12-10 10:08:24 +08:00
2017-02-24 04:53:49 +08:00
if (!string.IsNullOrWhiteSpace(Lab))
{
sb.Append($" ({Lab}");
2016-12-10 10:08:24 +08:00
2017-02-24 04:53:49 +08:00
if (BuildTime.HasValue)
{
sb.Append($".{BuildTime.Value.ToString("yyMMdd-HHmm", CultureInfo.InvariantCulture.DateTimeFormat)}");
}
2016-12-10 10:08:24 +08:00
2017-02-24 04:53:49 +08:00
sb.Append(")");
2016-12-10 10:08:24 +08:00
}
2017-04-16 22:46:02 +08:00
AlternateBuildString = sb.ToString();
2017-02-24 04:53:49 +08:00
}
2016-12-10 10:08:24 +08:00
2017-04-16 22:46:02 +08:00
private void GenerateLabUrl()
{
string url = !string.IsNullOrEmpty(Lab)
? Lab.Replace('/', '-').ToLower()
: "";
LabUrl = url;
}
2017-02-24 04:53:49 +08:00
}
}