in src/Microsoft.IIS.Administration.Files/Files/FilesHelper.cs [84:181]
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);
}