Example #1
0
        public IFileInfo GetFileInfos(string subpath, IModuleEntry module)
        {
            if (!_fileInfos.TryGetValue(subpath, out var fileInfo))
            {
                //if (!assetPaths.Contains(_root + subpath, StringComparer.Ordinal))
                //{
                //    return new NotFoundFileInfo(subpath);
                //}

                lock (_fileInfos)
                {
                    if (!_fileInfos.TryGetValue(subpath, out fileInfo))
                    {
                        var resourcePath = _baseNamespace + subpath.Replace('/', '>');
                        var fileName     = Path.GetFileName(subpath);

                        if (module.Assembly.GetManifestResourceInfo(resourcePath) == null)
                        {
                            return(new NotFoundFileInfo(fileName));
                        }

                        _fileInfos[subpath] = fileInfo = new EmbeddedResourceFileInfo(
                            module.Assembly, resourcePath, fileName, DateTime.Now);
                    }
                }
            }

            return(fileInfo);
        }
Example #2
0
        public IFileInfo GetFileInfo(string subpath)
        {
            if (string.IsNullOrEmpty(subpath))
            {
                return(new NotFoundFileInfo(subpath));
            }

            var name = Path.GetFileName(subpath);

            var encodedPath  = EncodeAsResourcesPath(subpath);
            var resourcePath = _baseNamespace + encodedPath;

            if (_fileLookupCache.TryGetValue(resourcePath, out EmbeddedResourceFileInfo fileInfo))
            {
                return(fileInfo);
            }

            if (HasInvalidPathChars(resourcePath))
            {
                return(new NotFoundFileInfo(resourcePath));
            }

            if (_assembly.GetManifestResourceInfo(resourcePath) == null)
            {
                return(new NotFoundFileInfo(name));
            }

            fileInfo = new EmbeddedResourceFileInfo(_assembly, resourcePath, name, _lastModified);
            _fileLookupCache.TryAdd(resourcePath, fileInfo);
            return(fileInfo);
        }
Example #3
0
        public IFileInfo GetFileInfo(string subpath)
        {
            if (!_fileInfos.TryGetValue(subpath, out var fileInfo))
            {
                if (!AssetPaths.Contains(Root + subpath, StringComparer.Ordinal))
                {
                    return(new NotFoundFileInfo(subpath));
                }

                lock (_fileInfos)
                {
                    if (!_fileInfos.TryGetValue(subpath, out fileInfo))
                    {
                        var resourcePath = _baseNamespace + subpath.Replace('/', '>');
                        var fileName     = Path.GetFileName(subpath);

                        if (Assembly.GetManifestResourceInfo(resourcePath) == null)
                        {
                            return(new NotFoundFileInfo(fileName));
                        }

                        _fileInfos[subpath] = fileInfo = new EmbeddedResourceFileInfo(
                            Assembly, resourcePath, fileName, _lastModified);
                    }
                }
            }

            return(fileInfo);
        }
Example #4
0
        public async Task Invoke(HttpContext context)
        {
            string sysId = context.Request.Headers["sysid"];

            if (this._userService.IsAuthorizePic(context.Request.PathBase, sysId))
            {
                await this._next.Invoke(context);
            }
            else
            {
                //Microsoft.Extensions.FileProviders.IFileInfo fileInfo;
                // Microsoft.Extensions.FileProviders.NotFoundFileInfo
                //Microsoft.Extensions.FileProviders.Physical.PhysicalFileInfo
                // Microsoft.Extensions.FileProviders.Embedded.EmbeddedResourceFileInfo

                //设置默认授权图片的三种方式:
                // 1 await context.Response.SendFileAsync(Path.Combine(@"wwwroot", "image/Forbidden.png"));
                // 2 await context.Response.SendFileAsync(new PhysicalFileInfo(new FileInfo("wwwroot/image/Forbidden.png")));
                // 3:嵌入式资源方式获取流,对资源的要求,必须属性:生成操作->嵌入的资源 方式
                //      3.1  IFileInfo fileInfo = new EmbeddedResourceFileInfo(Assembly.GetAssembly(typeof(PicCustomMiddleware)), Assembly.GetExecutingAssembly().GetName().Name + ".wwwroot.image.Forbidden.png", "pic", DateTimeOffset.Now);
                //      3.2  await context.Response.SendFileAsync(fileInfo);


                //var bb = Assembly.GetAssembly(typeof(PicCustomMiddleware)).GetManifestResourceStream(Assembly.GetExecutingAssembly().GetName().Name + ".wwwroot.image.Forbidden.png");
                IFileInfo fileInfo = new EmbeddedResourceFileInfo(Assembly.GetAssembly(typeof(PicCustomMiddleware)), Assembly.GetExecutingAssembly().GetName().Name + ".wwwroot.image.Forbidden.png", "pic", DateTimeOffset.Now);
                this._logger.LogInformation($"fileInfo:{fileInfo} fileInfo.PhysicalPath:{fileInfo.PhysicalPath}  fileInfo.Length:{fileInfo.Length}");
                await context.Response.SendFileAsync(fileInfo);
            }
        }
    public void AddFiles(Dictionary <string, IFileInfo> files)
    {
        var lastModificationTime = GetLastModificationTime();

        foreach (var resourcePath in Assembly.GetManifestResourceNames())
        {
            if (!BaseNamespace.IsNullOrEmpty() && !resourcePath.StartsWith(BaseNamespace))
            {
                continue;
            }

            var fullPath = ConvertToRelativePath(resourcePath).EnsureStartsWith('/');

            if (fullPath.Contains("/"))
            {
                AddDirectoriesRecursively(files, fullPath.Substring(0, fullPath.LastIndexOf('/')), lastModificationTime);
            }

            files[fullPath] = new EmbeddedResourceFileInfo(
                Assembly,
                resourcePath,
                fullPath,
                CalculateFileName(fullPath),
                lastModificationTime
                );
        }
    }
Example #6
0
        /// <summary>
        /// ISpaModule
        /// This implementation accepts path in Orchard-like format:
        ///
        /// </summary>
        public IFileInfo GetFileInfo(string subpath)
        {
            if (!_fileInfos.TryGetValue(subpath, out var fileInfo))
            {
                lock (_fileInfos)
                {
                    if (!_fileInfos.TryGetValue(subpath, out fileInfo))
                    {
                        var resourcePath = _baseNamespace + subpath.Replace('/', '>');
                        var fileName     = Path.GetFileName(subpath);

                        if (_assembly.GetManifestResourceInfo(resourcePath) == null)
                        {
                            _logger.LogDebug(1, null, $"SpaModuleAssembly.GetFileInfo(): cannot find resource '{subpath}'");
                            fileInfo = new NotFoundFileInfo(fileName);
                        }
                        else
                        {
                            _logger.LogDebug(2, null, $"SpaModuleAssembly.GetFileInfo(): successfully loaded resource '{subpath}'");
                            _fileInfos[subpath] = fileInfo = new EmbeddedResourceFileInfo(_assembly.Object, resourcePath, fileName, _lastModified);
                        }
                    }
                }
            }

            return(fileInfo);
        }
        /// <summary>
        /// Locate a file at the given path
        /// </summary>
        /// <param name="subpath">The path that identifies the file</param>
        /// <param name="fileInfo">The discovered file if any</param>
        /// <returns>True if a file was located at the given path</returns>
        public bool TryGetFileInfo(string subpath, out IFileInfo fileInfo)
        {
            // "/file.txt" expected.
            if (string.IsNullOrEmpty(subpath) || subpath[0] != '/')
            {
                fileInfo = null;
                return(false);
            }

            string fileName     = subpath.Substring(1); // Drop the leading '/'
            string resourcePath = _baseNamespace + fileName.Replace('/', '.');

            if (_assembly.GetManifestResourceInfo(resourcePath) == null)
            {
                fileInfo = null;
                return(false);
            }
            fileInfo = new EmbeddedResourceFileInfo(_assembly, resourcePath, fileName, _lastModified);
            return(true);
        }
        /// <summary>
        /// Locate a file at the given path
        /// </summary>
        /// <param name="subpath">The path that identifies the file</param>
        /// <param name="fileInfo">The discovered file if any</param>
        /// <returns>True if a file was located at the given path</returns>
        public bool TryGetFileInfo(string subpath, out IFileInfo fileInfo)
        {
            // "/file.txt" expected.
            if (string.IsNullOrEmpty(subpath) || subpath[0] != '/')
            {
                fileInfo = null;
                return(false);
            }

            string fileName = subpath.Substring(1);  // Drop the leading '/'

            var parts = fileName.Split('/');
            // replace '/' with '.' and '-' with '_' except last part of path
            string resourcePath = _baseNamespace + string.Join(".", parts.SkipLast().Select(x => x.Replace("-", "_")).Concat(new[] { parts.Last() }));

            if (_assembly.GetManifestResourceInfo(resourcePath) == null)
            {
                fileInfo = null;
                return(false);
            }
            fileInfo = new EmbeddedResourceFileInfo(_assembly, resourcePath, fileName, _lastModified);
            return(true);
        }
Example #9
0
    /// <summary>
    /// Locate the path in the virtual path provider
    /// </summary>
    /// <param name="subpath">The path that identifies the file</param>
    /// <param name="fileInfo">The discovered file if any</param>
    /// <returns>
    /// True if a file was located at the given path
    /// </returns>
    public bool TryGetFileInfo(string subpath, out IFileInfo fileInfo)
    {
        MyCustomVirtualPathProvider virtualPathProvider =
            (MyCustomVirtualPathProvider)HostingEnvironment.VirtualPathProvider;

        if (!virtualPathProvider.FileExists(subpath))
        {
            fileInfo = null;
            return(false);
        }
        try
        {
            EmbeddedResourceVirtualFile virtualFile =
                (EmbeddedResourceVirtualFile)virtualPathProvider.GetFile(subpath);
            fileInfo = new EmbeddedResourceFileInfo(virtualFile);
            return(true);
        }
        catch (InvalidCastException)
        {
            fileInfo = null;
            return(false);
        }
    }
        /// <summary>
        /// Locate a file at the given path
        /// </summary>
        /// <param name="subpath">The path that identifies the file</param>
        /// <param name="fileInfo">The discovered file if any</param>
        /// <returns>True if a file was located at the given path</returns>
        public bool TryGetFileInfo(string subpath, out IFileInfo fileInfo)
        {
            // "/file.txt" expected.
            if (string.IsNullOrEmpty(subpath) || subpath[0] != '/')
            {
                fileInfo = null;
                return false;
            }

            string fileName = subpath.Substring(1);  // Drop the leading '/'
            string resourcePath = _baseNamespace + fileName.Replace('/', '.');

            if (_assembly.GetManifestResourceInfo(resourcePath) == null)
            {
                fileInfo = null;
                return false;
            }
            fileInfo = new EmbeddedResourceFileInfo(_assembly, resourcePath, fileName, _lastModified);
            return true;
        }
Example #11
0
        public IFileInfo GetFileInfo(string subpath)
        {
            if (string.IsNullOrEmpty(subpath))
            {
                return(new NotFoundFileInfo(subpath));
            }

            IFileInfo fileInfo = null;

            if (_resolvedFileInfoDict.TryGetValue(subpath, out fileInfo))
            {
                return(fileInfo);
            }

            var resolvedPath = subpath;
            var name         = Path.GetFileName(subpath);
            var directory    = Path.GetDirectoryName(subpath);

            if (directory != "\\")
            {
                // embed directory dash - underscore checker;
                var directoryBuilder = new StringBuilder(directory);
                for (int i = 0; i < directoryBuilder.Length; i++)
                {
                    if (directoryBuilder[i] == '-')
                    {
                        directoryBuilder[i] = '_';
                    }
                }

                resolvedPath = directoryBuilder.ToString().Replace("\\", "/") + "/" + name;
            }

            var builder = new StringBuilder(_baseNamespace.Length + resolvedPath.Length);

            builder.Append(_baseNamespace);

            // Relative paths starting with a leading slash okay
            if (resolvedPath.StartsWith("/", StringComparison.Ordinal))
            {
                builder.Append(resolvedPath, 1, resolvedPath.Length - 1);
            }
            else
            {
                builder.Append(resolvedPath);
            }

            for (var i = _baseNamespace.Length; i < builder.Length; i++)
            {
                if (builder[i] == '/' || builder[i] == '\\')
                {
                    builder[i] = '.';
                }
            }

            var resourcePath = builder.ToString();

            if (HasInvalidPathChars(resourcePath))
            {
                _resolvedFileInfoDict.Add(subpath, new NotFoundFileInfo(resourcePath));
                return(new NotFoundFileInfo(resourcePath));
            }

            if (_assembly.GetManifestResourceInfo(resourcePath) == null)
            {
                _resolvedFileInfoDict.Add(subpath, new NotFoundFileInfo(name));
                return(new NotFoundFileInfo(name));
            }

            fileInfo = new EmbeddedResourceFileInfo(_assembly, resourcePath, name, _lastModified);
            _resolvedFileInfoDict.Add(subpath, fileInfo);
            return(fileInfo);
        }