internal object VdirToJsonModelRef(Vdir vdir, Fields fields = null, object parent = null)
 {
     if (fields == null || !fields.HasFields)
     {
         return(VdirToJsonModel(vdir, RefFields, false, parent));
     }
     else
     {
         return(VdirToJsonModel(vdir, fields, false, parent));
     }
 }
        internal FileType GetFileType(Site site, string path, string physicalPath)
        {
            // A virtual directory is a directory who's physical path is not the combination of the sites physical path and the relative path from the sites root
            // and has same virtual path as app + vdir path
            var app  = ResolveApplication(site, path);
            var vdir = ResolveVdir(site, path);

            var differentPhysicalPath = !Path.Combine(GetPhysicalPath(site), path.Replace('/', '\\').TrimStart('\\')).Equals(physicalPath, StringComparison.OrdinalIgnoreCase);

            if (path == "/" || differentPhysicalPath && IsExactVdirPath(site, app, vdir, path))
            {
                return(Vdir.GetVdirType(vdir));
            }
            else if (_fileProvider.GetDirectory(physicalPath).Exists)
            {
                return(FileType.Directory);
            }

            return(FileType.File);
        }
        private object GetParentVdirJsonModelRef(Vdir vdir, Fields fields = null)
        {
            object ret = null;

            if (vdir.VirtualDirectory.Path != "/")
            {
                var rootVdir = vdir.Application.VirtualDirectories["/"];
                ret = rootVdir == null ? null : VdirToJsonModelRef(new Vdir(vdir.Site, vdir.Application, rootVdir));
            }
            else if (vdir.Application.Path != "/")
            {
                var rootApp  = vdir.Site.Applications["/"];
                var rootVdir = rootApp == null ? null : rootApp.VirtualDirectories["/"];
                ret = rootApp == null || rootVdir == null ? null : VdirToJsonModelRef(new Vdir(vdir.Site, rootApp, rootVdir), fields);
            }
            else
            {
                ret = null;
            }

            return(ret);
        }
        internal object VdirToJsonModel(Vdir vdir, Fields fields = null, bool full = true, object parent = null)
        {
            if (vdir == null)
            {
                return(null);
            }

            if (fields == null)
            {
                fields = Fields.All;
            }



            dynamic obj    = new ExpandoObject();
            var     FileId = new FileId(vdir.Site.Id, vdir.Path);

            //
            // name
            if (fields.Exists("name"))
            {
                obj.name = vdir.Path.TrimStart('/');
            }

            //
            // id
            if (fields.Exists("id"))
            {
                obj.id = FileId.Uuid;
            }

            //
            // type
            if (fields.Exists("type"))
            {
                obj.type = Enum.GetName(typeof(FileType), Vdir.GetVdirType(vdir.VirtualDirectory)).ToLower();
            }

            //
            // path
            if (fields.Exists("path"))
            {
                obj.path = vdir.Path.Replace('\\', '/');
            }

            //
            // parent
            if (fields.Exists("parent"))
            {
                obj.parent = parent ?? GetParentVdirJsonModelRef(vdir, fields.Filter("parent"));
            }

            //
            // website
            if (fields.Exists("website"))
            {
                obj.website = SiteHelper.ToJsonModelRef(vdir.Site, fields.Filter("website"));
            }

            //
            // file_info
            if (fields.Exists("file_info"))
            {
                string physicalPath = GetPhysicalPath(vdir.Site, vdir.Path);

                //
                // Virtual directory can be at an arbitrary path from which we can not retrieve the file info
                // Serializing children (parent != null) should not throw
                if (parent != null && !_fileProvider.IsAccessAllowed(physicalPath, FileAccess.Read))
                {
                    obj.file_info = null;
                }
                else
                {
                    obj.file_info = _filesHelper.ToJsonModelRef(_fileProvider.GetDirectory(physicalPath), fields.Filter("file_info"));
                }
            }

            return(Core.Environment.Hal.Apply(Defines.DirectoriesResource.Guid, obj, full));
        }
        internal object VdirToJsonModel(Vdir vdir, Fields fields = null, bool full = true)
        {
            if (vdir == null)
            {
                return(null);
            }

            if (fields == null)
            {
                fields = Fields.All;
            }

            var directory = _fileProvider.GetDirectory(GetPhysicalPath(vdir.Site, vdir.Path));

            dynamic obj    = new ExpandoObject();
            var     FileId = new FileId(vdir.Site.Id, vdir.Path);

            //
            // name
            if (fields.Exists("name"))
            {
                obj.name = vdir.Path.TrimStart('/');
            }

            //
            // id
            if (fields.Exists("id"))
            {
                obj.id = FileId.Uuid;
            }

            //
            // type
            if (fields.Exists("type"))
            {
                obj.type = Enum.GetName(typeof(FileType), Vdir.GetVdirType(vdir.VirtualDirectory)).ToLower();
            }

            //
            // path
            if (fields.Exists("path"))
            {
                obj.path = vdir.Path.Replace('\\', '/');
            }

            //
            // parent
            if (fields.Exists("parent"))
            {
                obj.parent = GetParentVdirJsonModelRef(vdir, fields.Filter("parent"));
            }

            //
            // website
            if (fields.Exists("website"))
            {
                obj.website = SiteHelper.ToJsonModelRef(vdir.Site, fields.Filter("website"));
            }

            //
            // file_info
            if (fields.Exists("file_info"))
            {
                obj.file_info = _filesHelper.ToJsonModelRef(directory, fields.Filter("file_info"));
            }

            return(Core.Environment.Hal.Apply(Defines.DirectoriesResource.Guid, obj, full));
        }