Пример #1
0
        private static string SanitizePathAndContent(string path, FileContentsDto content)
        {
            var name   = Path.GetFileName(path);
            var folder = Path.GetDirectoryName(path);
            var ext    = Path.GetExtension(path);

            // not sure what this is for, since I believe code should only get here if there was an ext and it's cshtml
            // probably just to prevent some very unexpected create
            if (name == null)
            {
                name = "missing-name.txt";
            }

            switch (ext?.ToLowerInvariant())
            {
            // .cs files - usually API controllers
            case AssetEditor.CsExtension:
                if ((folder?.ToLower().IndexOf(AssetEditor.CsApiFolder, StringComparison.Ordinal) ?? -1) > -1)
                {
                    var nameWithoutExt = name.Substring(0, name.Length - ext.Length);
                    content.Content =
                        AssetEditor.DefaultCsBody.Replace(AssetEditor.CsApiTemplateControllerName, nameWithoutExt);
                }
                break;

            // .cshtml files (razor) or .code.cshtml (razor code-behind)
            case AssetEditor.CshtmlExtension:
            {
                // ensure all .cshtml start with "_"
                if (!name.StartsWith(AssetEditor.CshtmlPrefix))
                {
                    name = AssetEditor.CshtmlPrefix + name;
                    path = (string.IsNullOrWhiteSpace(folder) ? "" : folder + "\\") + name;
                }

                // first check the code-extension, because it's longer but also would contain the non-code extension
                if (name.EndsWith(AssetEditor.CodeCshtmlExtension))
                {
                    content.Content = AssetEditor.DefaultCodeCshtmlBody;
                }
                else if (name.EndsWith(AssetEditor.CshtmlExtension))
                {
                    content.Content = AssetEditor.DefaultCshtmlBody;
                }
                break;
            }

            // .html files (Tokens)
            case AssetEditor.TokenHtmlExtension:
                content.Content = AssetEditor.DefaultTokenHtmlBody;
                break;
            }

            return(path);
        }
Пример #2
0
        public bool Create(int appId, string path, FileContentsDto content, bool global = false)
        {
            Log.Add($"create a#{appId}, path:{path}, global:{global}, cont-length:{content.Content?.Length}");
            path = path.Replace("/", "\\");

            var thisApp = Factory.Resolve <Apps.App>().InitNoData(new AppIdentity(Eav.Apps.App.AutoLookupZone, appId), Log);

            if (content.Content == null)
            {
                content.Content = "";
            }

            path = SanitizePathAndContent(path, content);

            var isAdmin     = _user.IsAdmin; // UserInfo.IsInRole(PortalSettings.AdministratorRoleName);
            var assetEditor = new AssetEditor(thisApp, path, _user.IsSuperUser, isAdmin, global, Log);

            assetEditor.EnsureUserMayEditAssetOrThrow(path);
            return(assetEditor.Create(content.Content));
        }
Пример #3
0
        public bool Create(int appId, string path, FileContentsDto content, bool global = false)
        {
            Log.Add($"create a#{appId}, path:{path}, global:{global}, cont-length:{content.Content?.Length}");
            path = path.Replace("/", "\\");

            var thisApp = _serviceProvider.Build <Apps.App>().InitNoData(new AppIdentity(Eav.Apps.App.AutoLookupZone, appId), Log);

            if (content.Content == null)
            {
                content.Content = "";
            }

            path = SanitizePathAndContent(path, content);

            var isAdmin     = _user.IsAdmin;
            var assetEditor = _assetEditorLazy.Value.Init(thisApp, path, _user.IsSuperUser, isAdmin, global, Log);

            assetEditor.EnsureUserMayEditAssetOrThrow(path);
            return(assetEditor.Create(content.Content));
        }