Exemplo n.º 1
0
        /// <summary>
        /// Create intersection with another option
        /// </summary>
        /// <param name="option"></param>
        /// <returns>this if <paramref name="option"/> is null or new instance with intersection</returns>
        public virtual AllOptions Intersection(IOption option)
        {
            if (option == null)
            {
                return(this);
            }
            AllOptions result = new AllOptions();

            result.CanBrowse           = this.CanBrowse | option.CanBrowse();
            result.CanGetEntry         = this.CanGetEntry | option.CanGetEntry();
            result.CanObserve          = this.CanObserve | option.CanObserve();
            result.CanOpen             = this.CanOpen | option.CanOpen();
            result.CanRead             = this.CanRead | option.CanRead();
            result.CanWrite            = this.CanWrite | option.CanWrite();
            result.CanCreateFile       = this.CanCreateFile | option.CanCreateFile();
            result.CanDelete           = this.CanDelete | option.CanDelete();
            result.CanSetFileAttribute = this.CanSetFileAttribute | option.CanSetFileAttribute();
            result.CanMount            = this.CanMount | option.CanMount();
            result.CanCreateFile       = this.CanCreateFile | option.CanCreateFile();
            result.CanDelete           = this.CanDelete | option.CanDelete();
            result.CanMove             = this.CanMove | option.CanMove();
            result.CanCreateDirectory  = this.CanCreateDirectory | option.CanCreateDirectory();
            result.CanMount            = this.CanMount | option.CanMount();
            result.CanUnmount          = this.CanUnmount | option.CanUnmount();
            result.CanListMountPoints  = this.CanListMountPoints | option.CanListMountPoints();
            result.SubPath             = this.SubPath ?? option.SubPath();
            return(result);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Read options from <paramref name="option"/> and return flattened object.
 /// </summary>
 /// <param name="option"></param>
 /// <returns></returns>
 public virtual void ReadFrom(IOption option)
 {
     this.CanBrowse           = option.CanBrowse();
     this.CanGetEntry         = option.CanGetEntry();
     this.CanObserve          = option.CanObserve();
     this.CanOpen             = option.CanOpen();
     this.CanRead             = option.CanRead();
     this.CanWrite            = option.CanWrite();
     this.CanCreateFile       = option.CanCreateFile();
     this.CanDelete           = option.CanDelete();
     this.CanMove             = option.CanMove();
     this.CanCreateDirectory  = option.CanCreateDirectory();
     this.CanMount            = option.CanMount();
     this.CanUnmount          = option.CanUnmount();
     this.CanListMountPoints  = option.CanListMountPoints();
     this.SubPath             = option.SubPath();
     this.CanSetFileAttribute = option.CanSetFileAttribute();
 }
        /// <summary>
        /// Tests if document exists without attempting to download it completely.
        ///
        /// Estimates that <paramref name="uri"/> refers to a directory if path ends with '/'.
        /// </summary>
        /// <param name="uri"></param>
        /// <param name="option"></param>
        /// <returns></returns>
        public async Task <IEntry> GetEntryAsync(string uri, IOption option = null)
        {
            // Take reference
            var _httpClient = httpClient;

            // Assert not disposed
            if (_httpClient == null || IsDisposing)
            {
                throw new ObjectDisposedException(nameof(HttpFileSystem));
            }
            // Assert allowed
            if (!options.CanGetEntry || !option.CanGetEntry(true))
            {
                throw new NotSupportedException(nameof(GetEntryAsync));
            }
            // Append subpath
            string _subpath  = this.options.SubPath;
            string __subpath = option.SubPath();
            string uri_      = uri;

            if (_subpath != null || __subpath != null)
            {
                uri_ = _subpath + __subpath + uri;
            }

            // Cancel token
            CancellationToken cancel = default;

            option.TryGetToken(uri_, out cancel);

            try
            {
                // Request object
                using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, uri_))
                {
                    // Read token
                    ReadTokenToHeaders(uri_, option, request.Headers);
                    // Read authentication token
                    AuthenticationHeaderValue authenticationHeader;
                    if (this.token.TryGetToken(uri_, out authenticationHeader) || option.TryGetToken(uri_, out authenticationHeader))
                    {
                        request.Headers.Authorization = authenticationHeader;
                    }

                    // Start GET
                    HttpResponseMessage response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);

                    // Not found
                    if (response.StatusCode == HttpStatusCode.NotFound)
                    {
                        return(null);
                    }
                    // Assert ok
                    if (!response.IsSuccessStatusCode)
                    {
                        throw new FileSystemException(this, uri, response.StatusCode.ToString());
                    }
                    // Length
                    long?length = response.Content.Headers.ContentLength;
                    // Last modified
                    DateTimeOffset?lastModified = response.Content.Headers.LastModified;
                    // Parse uri into parts
                    Uri _uri = new Uri(uri_);
                    // Entry name
                    string name = GetEntryName(_uri.AbsoluteUri);
                    // Is directory
                    if (_uri.AbsolutePath.EndsWith("/"))
                    {
                        return(new DirectoryEntry(this, uri, name, lastModified ?? DateTimeOffset.MinValue, DateTimeOffset.MinValue, null));
                    }
                    // Is file
                    else
                    {
                        return(new FileEntry(this, uri, name, lastModified ?? DateTimeOffset.MinValue, DateTimeOffset.MinValue, length ?? -1L, null));
                    }
                }
            }
            catch (AggregateException e)
            {
                Exception _e = e;
                if (e.InnerExceptions.Count == 1)
                {
                    _e = e.InnerExceptions.First();
                }
                throw new FileSystemException(this, uri, e.Message, e);
            }
            catch (Exception e) when(e is FileSystemException == false)
            {
                throw new FileSystemException(this, uri, e.Message, e);
            }
        }
        /// <summary>
        /// Get entry of a single file or directory.
        /// </summary>
        /// <param name="path">path to a directory or to a single file, "" is root, separator is "/"</param>
        /// <param name="option">(optional) operation specific option; capability constraint, a session, security token or credential. Used for authenticating, authorizing or restricting the operation.</param>
        /// <returns>entry, or null if entry is not found</returns>
        /// <exception cref="IOException">On unexpected IO error</exception>
        /// <exception cref="SecurityException">If caller did not have permission</exception>
        /// <exception cref="ArgumentNullException"><paramref name="path"/> is null</exception>
        /// <exception cref="ArgumentException"><paramref name="path"/> contains only white space, or contains one or more invalid characters</exception>
        /// <exception cref="NotSupportedException">The <see cref="IFileSystem"/> doesn't support exists</exception>
        /// <exception cref="UnauthorizedAccessException">The access requested is not permitted by the operating system for the specified path, such as when access is Write or ReadWrite and the file or directory is set for read-only access.</exception>
        /// <exception cref="PathTooLongException">The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters.</exception>
        /// <exception cref="InvalidOperationException">If <paramref name="path"/> refers to a non-file device, such as "con:", "com1:", "lpt1:", etc.</exception>
        /// <exception cref="ObjectDisposedException"/>
        public IEntry GetEntry(string path, IOption option = null)
        {
            // Assert allowed
            if (!CanGetEntry || !option.CanGetEntry(true))
            {
                throw new NotSupportedException(nameof(GetEntry));
            }
            //
            if (path == "" && rootEntry != null)
            {
                return(rootEntry);
            }
            // Make path
            if (isPhysicalFileProvider && path.Contains(@"\"))
            {
                path = path.Replace(@"\", "/");
            }
            // Is disposed?
            IFileProvider fp = fileProvider;

            if (fp == null)
            {
                throw new ObjectDisposedException(nameof(FileProviderSystem));
            }

            // File
            IFileInfo fi = fp.GetFileInfo(path);

            if (fi.Exists)
            {
                if (fi.IsDirectory)
                {
                    if (path != "" && !path.EndsWith("/") && !path.EndsWith("\\"))
                    {
                        path = path + "/";
                    }
                    return(new DirectoryEntry(this, path, fi.Name, fi.LastModified, DateTimeOffset.MinValue, fi.PhysicalPath));
                }
                else
                {
                    return(new FileEntry(this, path, fi.Name, fi.LastModified, DateTimeOffset.MinValue, fi.Length, fi.PhysicalPath));
                }
            }

            // Directory
            IDirectoryContents contents = fp.GetDirectoryContents(path);

            if (contents.Exists)
            {
                string name;
                if (path == "")
                {
                    name = "";
                    path = "";
                }
                else if (!path.EndsWith("/") && !path.EndsWith("\\"))
                {
                    name = Path.GetFileName(path);
                    path = path + "/";
                }
                else
                {
                    name = Path.GetDirectoryName(path);
                }
                return(new DirectoryEntry(this, path, name, DateTimeOffset.MinValue, DateTimeOffset.MinValue, null));
            }

            // Nothing was found
            return(null);
        }