SFLink _mergeLinks(SFLink primary, SFLink secondary) { var p = primary; var s = secondary; if (p == null) { return(s); } if (s == null) { return(p); } if (p.MimeType != s.MimeType) { BasicMimeType mtye = p.MimeType.GetMostQualifiedMimeType(s.MimeType); return(p.MimeType == mtye ? p : s); } if (p.Rel != s.Rel) { return(p.Rel == SFRel.none ? s : p); } if (p.DiscoveredLink != s.DiscoveredLink) { return(p.DiscoveredLink ? s : p); } return(p); }
/// <summary> /// Constructor /// </summary> /// <param name="url"></param> /// <param name="mimeTypeStr"> /// The mime type. If set, is higher priority than <paramref name="mimeType"/> enum /// (as that does not have all enum values).</param> /// <param name="rel"></param> /// <param name="title"></param> /// <param name="length"></param> /// <param name="isRssEncl"></param> /// <param name="mimeType">A default mime-type. <paramref name="mimeTypeStr"/> if set will override this value, /// but this will still be set as a default type if the string value doesn't have a matching enum value.</param> public SFLink( string url, string mimeTypeStr = null, string rel = null, string title = null, int length = 0, bool isRssEncl = false, BasicMimeType mimeType = BasicMimeType.none) { IsValid = FixOrValidateInputUrl(url, out string _url, out Uri uri, out string ext); if (!IsValid) { return; } Url = _url; Uri = uri; Ext = ext; Title = title.NullIfEmptyTrimmed(); Length = length.Min(0); bool relWasValid = FixOrValidateInputRel(rel, isRssEncl, out SFRel _Rel, out string _RelOther); Rel = _Rel; RelOther = _RelOther; MimeType = GetMimeTypeFromTypeOrExtension(mimeTypeStr, Ext, Uri, detectYtubeVimeoTypes: true) .GetMostQualifiedMimeType(mimeType); }
///// <summary> ///// Attempts to get a <see cref="BasicMimeType"/> match for the input mimetype string. ///// Indirection call to <see cref="ParseMimeType(string, bool)"/>, see there ///// for further documentation. ///// </summary> ///// <param name="none">Ignore; simply allows easy findability to <see cref="ParseMimeType(string, bool)"/></param> ///// <param name="mimeTypeStr">String mime type, see notes above.</param> ///// <param name="allowGenericMatchOnNotFound">See notes above.</param> //public static BasicMimeType GetMimeTypeFromString(this BasicMimeType none, string mimeTypeStr, bool allowGenericMatchOnNotFound = false) // => ParseMimeType(mimeTypeStr, allowGenericMatchOnNotFound); public static BasicMimeType GetGenericMimeType(this BasicMimeType m) { int val = (int)m; if (val < 400) // if so, is text or nothing { if (val >= 100) { return(BasicMimeType.text); } else { return(BasicMimeType.none); } } else if (val < 500) { return(BasicMimeType.image); } else if (val < 600) { return(BasicMimeType.audio); } else if (val < 700) { return(BasicMimeType.video); } else //if (val < 800) { return(BasicMimeType.application); } }
public void Link1( string inUrl, string finUrl, string inMimeTypeStr, BasicMimeType inMimeType, BasicMimeType finMimeType, string inRel, bool inIsRssEncl, SFRel finRel) { var lk = new SFLink(inUrl, inMimeTypeStr, inRel, isRssEncl: inIsRssEncl, mimeType: inMimeType); True(lk.MimeType == finMimeType); True(lk.Rel == finRel); True(lk.Url == (finUrl.NullIfEmpty() ?? inUrl)); }
public void TestGenericTypes(BasicMimeType inMimeType, string inMimeTypeStr) { True(inMimeType != BasicMimeType.none); True(inMimeType.IsGenericType()); True(inMimeTypeStr.NotNulle()); True(inMimeTypeStr.EndsWith("/none")); BasicMimeType mtypeFromStr = BasicMimeTypesX.ParseMimeType(inMimeTypeStr); // none; see next line. Tests that MUST have `allowGenericMatchOnNotFound: true`, // and that default is the reverse True(mtypeFromStr == BasicMimeType.none); mtypeFromStr = BasicMimeTypesX.ParseMimeType(inMimeTypeStr, allowGenericMatchOnNotFound: true); True(inMimeType == mtypeFromStr); }
// for now, doesn't look like the following "main" properties were being used??? //public bool IsMain { get; set; } //public BasicMimeType MainMimeStringType { get; set; } public MimeTypeInfo( BasicMimeType mimeType, string mimeTypeString, string extension = null) { MimeType = mimeType; GenericMimeType = mimeType.GetGenericMimeType(); MimeTypeString = mimeTypeString; Extension = extension; // not sure for now about the former "main" idea, so removing for now // --> removed param: BasicMimeType mainMimeType = BasicMimeType.none) //if(mainMimeType != BasicMimeType.none) { // if(mimeTypeString.NotNulle()) // throw new ArgumentException(); // MainMimeStringType = mainMimeType; //} }
/// <summary> /// Gets the most qualified mime type, with the first input (<paramref name="primary"/>) overriding the /// secondary input. /// </summary> /// <param name="primary">The primary value.</param> /// <param name="secondary">A secondary mime type, which will only override the primary if it is more qualified.</param> public static BasicMimeType GetMostQualifiedMimeType(this BasicMimeType primary, BasicMimeType secondary) { if (primary == BasicMimeType.none) { return(secondary); } if (secondary == BasicMimeType.none) { return(primary); } if (primary.IsGenericType()) { return(secondary.IsGenericType() ? primary : secondary); } return(primary); // primary at this point HAS a subtype, so no matter if secondary is fully set, primary wins at this point }
public void TestBunchOfFullValidTypes( BasicMimeType inMimeType, string inMimeTypeStr, string inExt) { // ASSERT: NOT testing None || Generic types || null mstring || extensions True(inMimeTypeStr.NotNulle()); True(inExt.NotNulle()); False(inMimeType.IsGenericTypeOrNone()); BasicMimeType mtypeFromStr = BasicMimeTypesX.ParseMimeType(inMimeTypeStr); True(mtypeFromStr == inMimeType); BasicMimeType extMtype = BasicMimeTypesX.GetMimeTypeFromFileExtension(inExt); True(mtypeFromStr == extMtype); string gottenMTypeStrFromInEnum = inMimeType.MimeTypeString(); True(inMimeTypeStr == gottenMTypeStrFromInEnum); }
/// <summary></summary> /// <param name="mimeType"></param> /// <param name="extension"></param> /// <param name="uri">Input uri to extend some possible detections, currently /// only used when <paramref name="detectYtubeVimeoTypes"/>.</param> /// <param name="detectYtubeVimeoTypes">True to detect a mime-type for youtube / vimeo.</param> public static BasicMimeType GetMimeTypeFromTypeOrExtension( string mimeType, string extension, Uri uri = null, bool detectYtubeVimeoTypes = false) { mimeType = mimeType.NullIfEmptyTrimmed(); BasicMimeType mime = BasicMimeType.none; if (mimeType != null) { mime = BasicMimeTypesX.ParseMimeType(mimeType, allowGenericMatchOnNotFound: true); } if (extension.NotNulle() && mime.IsGenericTypeOrNone()) //.HasNoSubtypeOrNone()) { { BasicMimeType _extMime = BasicMimeType.none.GetMimeTypeFromFileExtension(extension); mime = mime.GetMostQualifiedMimeType(_extMime); } if (detectYtubeVimeoTypes && uri != null) { if (mime == BasicMimeType.none || (extension.IsNulle() && mimeType == null)) // hmmm, i guess let this override if these conditions met { string linkHost = uri.Host; if (linkHost.CountN() > 5) { if (linkHost.Contains("vimeo")) { mime = BasicMimeType.video_vimeo; } else if (linkHost.Contains("youtu")) { mime = BasicMimeType.video_youtube; } } } } return(mime); }
public static bool IsImage(this BasicMimeType mimeType) => (int)mimeType >= 400 && (int)mimeType < 500;
public static bool IsVideo(this BasicMimeType mimeType) => (int)mimeType >= 600 && (int)mimeType < 700;
public static bool IsText(this BasicMimeType mimeType) => (int)mimeType < 200 && (int)mimeType >= 100;
public static bool IsWebPageOrNone(this BasicMimeType mimeType) => mimeType == BasicMimeType.none || mimeType == BasicMimeType.text_html;
/// <summary> /// Returns true if linkType is other than none or html /// (the reverse of linkType.IsWebPageOrNone()). /// </summary> public static bool IsEnclosureType(this BasicMimeType mimeType) => mimeType.IsWebPageOrNone() == false;
/// <summary> /// Is none or is any of the text types (lt 200); /// </summary> public static bool IsTextOrNone(this BasicMimeType mimeType) => (int)mimeType < 200;
public static bool IsFullySet(this BasicMimeType mimeType) => !mimeType.IsGenericTypeOrNone();
/// <summary> /// Gets the extension of the input url or file path by using <see cref="UriPathInfo"/>, /// and then gets the corresponding mimetype, if any. /// Direct indirection call to <see cref="GetMimeTypeFromPathOrUrl(string)"/>, /// this is for simple discoverability sake. /// </summary> /// <param name="path">Path or Url, absolute or relative or simply a single file name.</param> /// <param name="none">Ignore; simply allows easy findability to this call</param> public static BasicMimeType GetMimeTypeFromPathOrUrl(this BasicMimeType none, string path) => GetMimeTypeFromPathOrUrl(path);
public static string GetExtension(this BasicMimeType mimeType) => MTIDict_ByEnums.V(mimeType)?.Extension;
public static bool IsGenericType(this BasicMimeType mimeType) => (int)mimeType % MinBMimeTypeVal == 0;
/// <summary> /// Returns the official mimetype string value for this enum value, e.g. "text/html" for text_html. /// </summary> /// <param name="mimeType"></param> /// <returns></returns> public static string MimeTypeString(this BasicMimeType mimeType) => MTIDict_ByEnums.V(mimeType)?.MimeTypeString;
public void Test_InvalidOrMissing_Extensions(string ext) { BasicMimeType extMtype = BasicMimeTypesX.GetMimeTypeFromFileExtension(ext); True(extMtype == BasicMimeType.none); }
public static MTI GetMimeInfo(this BasicMimeType mimeType) => MTIDict_ByEnums.V(mimeType);
public static bool IsAudioOrVideo(this BasicMimeType mimeType) => (int)mimeType >= 500 && (int)mimeType < 700;
public static bool IsGenericTypeOrNone(this BasicMimeType mimeType) => mimeType == BasicMimeType.none || mimeType.IsGenericType();
public static bool IsApplication(this BasicMimeType mimeType) => (int)mimeType >= 700 && (int)mimeType < 800;
/// <summary> /// Determines the mimetype for this extension (e.g. "jpg" or "pdf"). Is case insensitive. /// Indirection method for discoverability sake. /// </summary> /// <param name="ext">Extension, such as "jpg". Must NOT have the period.</param> /// <param name="none">Ignore; simply allows easy findability to this call</param> public static BasicMimeType GetMimeTypeFromFileExtension(this BasicMimeType none, string ext) => GetMimeTypeFromFileExtension(ext);