コード例 #1
0
ファイル: CdnUtil.cs プロジェクト: terryzamal/DotStd
        public CdnResource(string _name, string _tagname, string outDir)
        {
            // create an element on the fly. Assume _name is a local minified file. NOT CDN.
            // <IncludeRef name="/js/grid_common.min.js" tagname="script" /> // use minified name to indicate it has one.

            ValidState.ThrowIfWhiteSpace(_name, "CdnResource name");
            name         = _name;
            fallback_src = name;

            if (_tagname == null)
            {
                // derive from the file extension.
                if (_name.EndsWith(Minifier.kExtJs))
                {
                    TagName = CdnTagName.script;
                }
                else if (_name.EndsWith(Minifier.kExtCss))
                {
                    TagName = CdnTagName.link;
                }
                else
                {
                    TagName = CdnTagName.a;
                }
            }
            else
            {
                TagName = EnumUtil.ParseEnum <CdnTagName>(_tagname);
            }

            // Does it have a minified file? assume _name is the minified version.
            string path1   = GetPhysPathFromWeb(outDir, name);
            string name2   = Minifier.GetNonMinName(name);
            string path2   = GetPhysPathFromWeb(outDir, name2);
            bool   exists1 = File.Exists(path1);
            bool   exists2 = File.Exists(path2);

            if (exists1)
            {
                minonly = !exists2; // min exists. but does non-min ?
            }
            else if (exists2)
            {
                name    = name2; // only non-min version exists.
                path1   = path2;
                minonly = true;
            }
            else
            {
                // neither exists. this is bad.
                TagName = CdnTagName.ERROR;
                minonly = false;
                return;
            }

            // cache breaking version For local files. Equiv to "asp-append-version" (would use SHA256)

            var hasher = new HashUtil(HashUtil.GetMD5());       // GetSHA256()

            byte[] hashCode2 = hasher.GetHashFile(path1);
            Debug.Assert(hashCode2 != null);
            version = Convert.ToBase64String(hashCode2);
        }