/// <summary>
            /// Read options from <paramref name="option"/> and return flattened object.
            /// </summary>
            /// <param name="option"></param>
            /// <returns></returns>
            public static Options Read(IOption option)
            {
                Options result = new Options();

                IOpenOption open = option.AsOption <IOpenOption>();

                if (open != null)
                {
                    result.CanCreateFile = open.CanCreateFile;
                    result.CanOpen       = open.CanOpen;
                    result.CanRead       = open.CanRead;
                    result.CanWrite      = open.CanWrite;
                }
                IBrowseOption browse = option.AsOption <IBrowseOption>();

                if (browse != null)
                {
                    result.CanBrowse = browse.CanBrowse;
                }
                IObserveOption observe = option.AsOption <IObserveOption>();

                if (observe != null)
                {
                    result.CanObserve = observe.CanObserve;
                }
                IToken token = option.AsOption <IToken>();

                if (token != null)
                {
                    var enumr = option.ListTokens(false);
                    result.tokens = enumr is IToken[] arr ? arr : enumr.ToArray();
                }
                return(result);
            }
            /// <summary>Read from <paramref name="option"/></summary>
            /// <param name="option"></param>
            /// <returns>this</returns>
            public virtual Options Read(IOption option)
            {
                IOpenOption open = option.AsOption <IOpenOption>();

                if (open != null)
                {
                    CanCreateFile = open.CanCreateFile;
                    CanOpen       = open.CanOpen;
                    CanRead       = open.CanRead;
                    CanWrite      = open.CanWrite;
                }
                IDeleteOption delete = option.AsOption <IDeleteOption>();

                if (delete != null)
                {
                    CanDelete = delete.CanDelete;
                }
                IBrowseOption browse = option.AsOption <IBrowseOption>();

                if (browse != null)
                {
                    CanBrowse   = browse.CanBrowse;
                    CanGetEntry = browse.CanGetEntry;
                }
                ISubPathOption subpath = option.AsOption <ISubPathOption>();

                if (subpath != null)
                {
                    SubPath = subpath.SubPath;
                }
                return(this);
            }
        /// <summary>
        /// Query for a token object at path <paramref name="path"/> as type <paramref name="key"/>.
        /// </summary>
        /// <param name="tokenContainer"></param>
        /// <param name="path">(optional) path to query token at</param>
        /// <param name="key">(optional) key to query, typically <see cref="Type.FullName"/></param>
        /// <param name="token">array of tokens, or null if failed to find matching tokens</param>
        /// <returns>true if tokens were found for the parameters</returns>
        public static bool TryGetToken(this IOption tokenContainer, string path, string key, out object token)
        {
            ITokenProvider tp = tokenContainer.AsOption <ITokenProvider>();

            if (tp != null && tp.TryGetToken(path, key, out token))
            {
                return(true);
            }
            token = null;
            return(false);
        }
        /// <summary>
        /// Query for a token object at path <paramref name="path"/> as type <typeparamref name="T"/>.
        /// </summary>
        /// <param name="tokenContainer"></param>
        /// <param name="path">(optional) path to query token at</param>
        /// <param name="key">(optional) key to query, typically <see cref="Type.FullName"/></param>
        /// <param name="token">array of tokens, or null if failed to find matching tokens</param>
        /// <returns>true if tokens were found for the parameters</returns>
        public static bool TryGetToken <T>(this IOption tokenContainer, string path, string key, out T token)
        {
            object         obj;
            ITokenProvider tp = tokenContainer.AsOption <ITokenProvider>();

            if (tp != null && tp.TryGetToken(path, key, out obj) && obj is T casted)
            {
                token = casted; return(true);
            }
            token = default;
            return(false);
        }
 /// <summary>
 /// Create http:// filesystem.
 ///
 /// HttpFileSystem is intended
 /// </summary>
 /// <param name="httpClient">(optional) httpClient instance to use, or null to have new created</param>
 /// <param name="option">(optional) options</param>
 public HttpFileSystem(HttpClient httpClient = default, IOption option = null) : base()
 {
     this.httpClient = httpClient ?? new HttpClient();
     if (option != null)
     {
         this.options = CreateOptions().Read(option);
         this.token   = option.AsOption <IToken>();
     }
     else
     {
         this.options = Options.Instance;
     }
 }
        /// <summary>
        /// Query for a token object at path <paramref name="path"/> as type <typeparamref name="T"/>.
        /// </summary>
        /// <param name="tokenContainer"></param>
        /// <param name="path">(optional) path to query token at</param>
        /// <param name="key">(optional) key to query, typically <see cref="Type.FullName"/></param>
        /// <param name="tokens">array of tokens, or null if failed to find matching tokens</param>
        /// <returns>true if tokens were found for the parameters</returns>
        public static bool TryGetAllTokens <T>(this IOption tokenContainer, string path, string key, out T[] tokens)
        {
            object[]       objs;
            ITokenProvider tp = tokenContainer.AsOption <ITokenProvider>();

            if (tp != null && tp.TryGetAllTokens(path, key, out objs))
            {
                List <T> list = new List <T>(objs.Length);
                for (int i = 0; i < objs.Length; i++)
                {
                    if (objs[i] is T casted)
                    {
                        list.Add(casted);
                    }
                }
                tokens = list.ToArray();
                return(list.Count > 0);
            }
            tokens = null;
            return(false);
        }
 /// <summary>
 /// Test if <paramref name="filesystemOption"/> has Observe capability.
 /// </summary>
 /// <param name="filesystemOption"></param>
 /// <param name="defaultValue">Returned value if option is unspecified</param>
 /// <returns>true, if has Observe capability</returns>
 public static bool CanObserve(this IOption filesystemOption, bool defaultValue = false)
 => filesystemOption.AsOption <IObserveOption>() is IObserveOption observer ? observer.CanObserve : defaultValue;
Esempio n. 8
0
 /// <summary>
 /// Create adapter that adapts <paramref name="sourceFilesystem"/> into <see cref="IFileProvider"/>.
 /// </summary>
 /// <param name="sourceFilesystem"></param>
 /// <param name="option">(optional) decorating options, such as <see cref="IToken"/></param>
 public FileSystemProvider(IFileSystem sourceFilesystem, IOption option = null) : base()
 {
     FileSystem   = sourceFilesystem ?? throw new ArgumentNullException(nameof(sourceFilesystem));
     this.options = AllOptions.Read(Option.Union(sourceFilesystem, option));
     this.token   = option.AsOption <IToken>();
 }
Esempio n. 9
0
 public static Option <T> OfType <T>(this IOption value)
 {
     return(value.AsOption().Where(x => x is T).Transform(x => (T)x));
 }
 /// <summary>
 /// Test if <paramref name="filesystemOption"/> has CreateDirectory capability.
 /// </summary>
 /// <param name="filesystemOption"></param>
 /// <param name="defaultValue">Returned value if option is unspecified</param>
 /// <returns>true, if has CreateDirectory capability</returns>
 public static bool CanCreateDirectory(this IOption filesystemOption, bool defaultValue = false)
 => filesystemOption.AsOption <ICreateDirectoryOption>() is ICreateDirectoryOption directoryConstructor ? directoryConstructor.CanCreateDirectory : defaultValue;
 /// <summary>
 /// Is filesystem allowed to unmount a mount.
 /// </summary>
 /// <param name="filesystemOption"></param>
 /// <param name="defaultValue">Returned value if option is unspecified</param>
 /// <returns></returns>
 public static bool CanUnmount(this IOption filesystemOption, bool defaultValue = false)
 => filesystemOption.AsOption <IMountOption>() is IMountOption mountable ? mountable.CanUnmount : defaultValue;
 /// <summary>
 /// Test if <paramref name="filesystemOption"/> has local move/rename capability.
 /// </summary>
 /// <param name="filesystemOption"></param>
 /// <param name="defaultValue">Returned value if option is unspecified</param>
 /// <returns>true, if has Move capability</returns>
 public static bool CanMove(this IOption filesystemOption, bool defaultValue = false)
 => filesystemOption.AsOption <IMoveOption>() is IMoveOption mover ? mover.CanMove : defaultValue;
 /// <summary>
 /// Test if <paramref name="filesystemOption"/> has Exists capability.
 /// </summary>
 /// <param name="filesystemOption"></param>
 /// <param name="defaultValue">Returned value if option is unspecified</param>
 /// <returns>true if has Exists capability</returns>
 public static bool CanGetEntry(this IOption filesystemOption, bool defaultValue = false)
 => filesystemOption.AsOption <IBrowseOption>() is IBrowseOption browser ? browser.CanGetEntry : defaultValue;
 /// <summary>
 /// Get automounters.
 /// </summary>
 /// <param name="filesystemOption"></param>
 /// <returns></returns>
 public static IPackageLoader[] AutoMounters(this IOption filesystemOption)
 => filesystemOption.AsOption <IAutoMountOption>() is IAutoMountOption automount ? automount.AutoMounters : null;
Esempio n. 15
0
 /// <summary>
 /// Get case sensitivity.
 /// <param name="filesystemOption"></param>
 /// </summary>
 /// <returns>mount path or null</returns>
 public static FileSystemCaseSensitivity CaseSensitivity(this IOption filesystemOption)
 => filesystemOption.AsOption <IPathInfo>() is IPathInfo op ? op.CaseSensitivity : FileSystemCaseSensitivity.Unknown;
 /// <summary>
 /// Test if <paramref name="filesystemOption"/> has SetFileAttribute capability.
 /// </summary>
 /// <param name="filesystemOption"></param>
 /// <param name="defaultValue">Returned value if option is unspecified</param>
 /// <returns>true if has SetFileAttribute capability</returns>
 public static bool CanSetFileAttribute(this IOption filesystemOption, bool defaultValue = false)
 => filesystemOption.AsOption <IFileAttributeOption>() is IFileAttributeOption attributer ? attributer.CanSetFileAttribute : defaultValue;
Esempio n. 17
0
 /// <summary>
 /// Test if <paramref name="filesystemOption"/> has CreateFile capability.
 /// </summary>
 /// <param name="filesystemOption"></param>
 /// <param name="defaultValue">Returned value if option is unspecified</param>
 /// <returns>true, if has CreateFile capability</returns>
 public static bool CanCreateFile(this IOption filesystemOption, bool defaultValue = false)
 => filesystemOption.AsOption <IOpenOption>() is IOpenOption opener ? opener.CanCreateFile : defaultValue;
 /// <summary>
 /// Test if <paramref name="filesystemOption"/> has Delete capability.
 /// </summary>
 /// <param name="filesystemOption"></param>
 /// <param name="defaultValue">Returned value if option is unspecified</param>
 /// <returns>true, if has Delete capability</returns>
 public static bool CanDelete(this IOption filesystemOption, bool defaultValue = false)
 => filesystemOption.AsOption <IDeleteOption>() is IDeleteOption deleter ? deleter.CanDelete : defaultValue;