These are static functions available globally. All are fully commented below.

The Keywords helper is not perfect but does a good job of returning a string of unique words useful for searches from plain text. Ideally it would remove many more verbs and add pluralised nouns but that is way beyond the scope of this version. You can add your own words that you don’t want to appear to the ‘illegals’ list.

You could always edit the IsImage helper should you wish to restrict or include other file extensions. You would need to duplicate any edit in the Create view javascript too.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;

namespace Ben.Classes
{
    public static class ExtensionHelpers
    {
        /// 
        /// get the full physical path to a file from the root virtual path
        /// 
        /// e.g. "~/images/ben.jpg"
        /// _path = ExtensionHelpers.PhysicalPathFromRootPath(rootpath);
        /// "C:\Users\Benny Sutton\source\repos\Ben\Ben\Images\ben.jpg"
        public static string PhysicalPathFromRootPath(this string rootpath)
        {
            HttpContext _ctx = HttpContext.Current;
            // TO DO validate is a rootpath?
            return _ctx.Server.MapPath(rootpath);
        }

        /// 
        /// helper for System.IO.Path.GetFileName
        /// 
        /// e.g. "/images/ben.jpg"
        /// "ben.jpg"
        public static string GetFileName(this string filepath)
        {
            return System.IO.Path.GetFileName(filepath);
        }

        /// 
        /// create a list of unique words suitable for a keywords field
        /// 
        /// the string to convert into keywords
        public static string Keywords(this string text)
        {
            // split into lower case words with no trailing/leading punctuation and remove duplicate words
            var uniqueWords = Regex.Matches(text.ToLower(), "\\w+('(s|d|t|ve|m))?")
                .Cast().Select(x => x.Value).Distinct().ToList();

            // remove Contractions (like I'm and don't) and two letter words
            var result = from s in uniqueWords
                         where s.Length > 2 && !s.Contains("'")
                         select s;
            // fill 'illegals' list with words to drop
            string dropwords = "which,than,that,have,seem,the,with,and,all,only,not,out,into,buy,probably,for,over,from,too,not,like,who,what,where,when,how,why,here";
            List filter = dropwords.Split(',').ToList();
            var filtered = result.Except(filter);

            return String.Join(" ", result).Truncate(4000);
        }

        /// 
        /// limit string length
        /// 
        /// string to assess
        /// the number of characters to limit the string to
        /// 
        public static string Truncate(this string value, int maxLength)
        {
            if (string.IsNullOrEmpty(value)) return value;
            return value.Length <= maxLength ? value : value.Substring(0, maxLength);
        }


        /// 
        ///  Is this an image file? This is a weak check for jpeg/gif/png file extension - but the file is on your hard disk already it should be OK! see www.regexr.com/4t9nr
        /// 
        /// FIlename, Rootpath or full PhysicalPath
        public static bool IsImage(this string FileName)
        {
            return Regex.Match(FileName, @".*(\.[Jj][Pp][Gg]|\.[Gg][Ii][Ff]|\.[Jj][Pp][Ee][Gg]|\.[Pp][Nn][Gg])\b").Success;
        }

    }
}
    
Next >>>