Exemplo n.º 1
0
        public object ToJsonModel(ILocation location)
        {
            FileId id = FileId.FromPhysicalPath(location.Path);

            var obj = new {
                alias  = location.Alias,
                id     = id.Uuid,
                path   = location.GetRawPath(),
                claims = location.Claims
            };

            return(Core.Environment.Hal.Apply(Defines.LocationsResource.Guid, obj));
        }
Exemplo n.º 2
0
        //
        // Accept parent to optimize serialization performance
        private object DirectoryToJsonModel(IFileInfo info, Fields fields = null, bool full = true, IFileInfo parent = null)
        {
            if (fields == null)
            {
                fields = Fields.All;
            }

            dynamic obj    = new ExpandoObject();
            var     fileId = FileId.FromPhysicalPath(info.Path);
            bool?   exists = null;

            //
            // name
            if (fields.Exists("name"))
            {
                obj.name = info.Name;
            }

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

            //
            // alias
            if (fields.Exists("alias"))
            {
                foreach (var location in _fileProvider.Options.Locations)
                {
                    if (location.Path.TrimEnd(PathUtil.SEPARATORS).Equals(info.Path.TrimEnd(PathUtil.SEPARATORS), StringComparison.OrdinalIgnoreCase))
                    {
                        obj.alias = location.Alias;
                        break;
                    }
                }
            }

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

            //
            // physical_path
            if (fields.Exists("physical_path"))
            {
                obj.physical_path = info.Path;
            }

            //
            // exists
            if (fields.Exists("exists"))
            {
                exists     = exists ?? info.Exists;
                obj.exists = exists.Value;
            }

            //
            // created
            if (fields.Exists("created"))
            {
                exists      = exists ?? info.Exists;
                obj.created = exists.Value ? (object)info.Created.ToUniversalTime() : null;
            }

            //
            // last_modified
            if (fields.Exists("last_modified"))
            {
                exists            = exists ?? info.Exists;
                obj.last_modified = exists.Value ? (object)info.LastModified.ToUniversalTime() : null;
            }

            //
            // last_access
            if (fields.Exists("last_access"))
            {
                exists          = exists ?? info.Exists;
                obj.last_access = exists.Value ? (object)info.LastAccessed.ToUniversalTime() : null;
            }

            //
            // total_files
            // We check for the 'full' flag to avoid unauthorized exception when referencing directories
            // Listing a directories content requires extra permissions
            if (fields.Exists("total_files") && full)
            {
                exists = exists ?? info.Exists;
                if (_fileProvider.IsAccessAllowed(info.Path, FileAccess.Read))
                {
                    obj.total_files = exists.Value ? _fileProvider.GetFiles(info, "*").Count() + _fileProvider.GetDirectories(info, "*").Count() : 0;
                }
            }

            //
            // parent
            if (fields.Exists("parent"))
            {
                obj.parent = parent != null?DirectoryToJsonModelRef(parent, fields.Filter("parent")) : GetParentJsonModelRef(info.Path, fields.Filter("parent"));
            }

            //
            // claims
            if (fields.Exists("claims"))
            {
                obj.claims = info.Claims;
            }

            return(Core.Environment.Hal.Apply(Defines.DirectoriesResource.Guid, obj, full));
        }
Exemplo n.º 3
0
        private object InfoToJsonModel(IFileInfo info, Fields fields = null, bool full = true)
        {
            if (fields == null)
            {
                fields = Fields.All;
            }

            dynamic obj    = new ExpandoObject();
            var     fileId = FileId.FromPhysicalPath(info.Path);
            bool?   exists = null;

            //
            // name
            if (fields.Exists("name"))
            {
                obj.name = info.Name;
            }

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

            //
            // type
            if (fields.Exists("type"))
            {
                obj.type = null;
            }

            //
            // physical_path
            if (fields.Exists("physical_path"))
            {
                obj.physical_path = info.Path;
            }

            //
            // exists
            if (fields.Exists("exists"))
            {
                exists     = exists ?? info.Exists;
                obj.exists = exists.Value;
            }

            //
            // created
            if (fields.Exists("created"))
            {
                exists      = exists ?? info.Exists;
                obj.created = exists.Value ? (object)info.Created.ToUniversalTime() : null;
            }

            //
            // last_modified
            if (fields.Exists("last_modified"))
            {
                exists            = exists ?? info.Exists;
                obj.last_modified = exists.Value ? (object)info.LastModified.ToUniversalTime() : null;
            }

            //
            // last_access
            if (fields.Exists("last_access"))
            {
                exists          = exists ?? info.Exists;
                obj.last_access = exists.Value ? (object)info.LastAccessed.ToUniversalTime() : null;
            }

            //
            // parent
            if (fields.Exists("parent"))
            {
                obj.parent = GetParentJsonModelRef(info.Path, fields.Filter("parent"));
            }

            //
            // claims
            if (fields.Exists("claims"))
            {
                obj.claims = info.Claims;
            }


            return(Core.Environment.Hal.Apply(Defines.FilesResource.Guid, obj, full));
        }
Exemplo n.º 4
0
        //
        // Accept parent to optimize serialization performance
        private object FileToJsonModel(IFileInfo info, Fields fields = null, bool full = true, IFileInfo parent = null)
        {
            if (fields == null)
            {
                fields = Fields.All;
            }

            dynamic obj    = new ExpandoObject();
            var     fileId = FileId.FromPhysicalPath(info.Path);
            bool?   exists = null;

            //
            // name
            if (fields.Exists("name"))
            {
                obj.name = info.Name;
            }

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

            //
            // alias
            if (fields.Exists("alias"))
            {
                foreach (var location in _fileProvider.Options.Locations)
                {
                    if (location.Path.TrimEnd(PathUtil.SEPARATORS).Equals(info.Path.TrimEnd(PathUtil.SEPARATORS), StringComparison.OrdinalIgnoreCase))
                    {
                        obj.alias = location.Alias;
                        break;
                    }
                }
            }

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

            //
            // physical_path
            if (fields.Exists("physical_path"))
            {
                obj.physical_path = info.Path;
            }

            //
            // exists
            if (fields.Exists("exists"))
            {
                exists     = exists ?? info.Exists;
                obj.exists = exists.Value;
            }

            //
            // size
            if (fields.Exists("size"))
            {
                exists   = exists ?? info.Exists;
                obj.size = exists.Value ? info.Size : 0;
            }

            //
            // created
            if (fields.Exists("created"))
            {
                exists      = exists ?? info.Exists;
                obj.created = exists.Value ? (object)info.Created.ToUniversalTime() : null;
            }

            //
            // last_modified
            if (fields.Exists("last_modified"))
            {
                exists            = exists ?? info.Exists;
                obj.last_modified = exists.Value ? (object)info.LastModified.ToUniversalTime() : null;
            }

            //
            // last_access
            if (fields.Exists("last_access"))
            {
                exists          = exists ?? info.Exists;
                obj.last_access = exists.Value ? (object)info.LastAccessed.ToUniversalTime() : null;
            }

            //
            // mime_type
            if (fields.Exists("mime_type"))
            {
                string type = null;
                HttpFileHandler.MimeMaps.TryGetContentType(info.Path, out type);
                obj.mime_type = type;
            }

            //
            // e_tag
            if (fields.Exists("e_tag"))
            {
                exists    = exists ?? info.Exists;
                obj.e_tag = exists.Value ? ETag.Create(info).Value : null;
            }

            //
            // parent
            if (fields.Exists("parent"))
            {
                obj.parent = parent != null?DirectoryToJsonModelRef(parent, fields.Filter("parent")) : GetParentJsonModelRef(info.Path, fields.Filter("parent"));
            }

            //
            // claims
            if (fields.Exists("claims"))
            {
                obj.claims = info.Claims;
            }


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