<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Hosting;
using Orchard.Caching;
using Orchard.Environment;
using Orchard.FileSystems.VirtualPath;

namespace Orchard.FileSystems.WebSite {
    public class WebSiteFolder : IWebSiteFolder {
        private readonly IVirtualPathProvider _virtualPathProvider;
        private readonly IVirtualPathMonitor _virtualPathMonitor;

        public WebSiteFolder(IVirtualPathMonitor virtualPathMonitor, IVirtualPathProvider virtualPathProvider) {
            _virtualPathMonitor = virtualPathMonitor;
            _virtualPathProvider = virtualPathProvider;
        }

        public IEnumerable<string> ListDirectories(string virtualPath) {
            if (!HostingEnvironment.VirtualPathProvider.DirectoryExists(virtualPath))
                return Enumerable.Empty<string>();

            return HostingEnvironment.VirtualPathProvider
                .GetDirectory(virtualPath)
                .Directories.OfType<VirtualDirectory>()
                .Select(d => d.VirtualPath)
                .ToArray();
        }

        private IEnumerable<string> ListFiles(IEnumerable<string> directories) {
            return from dir in directories
                   from file in ListFiles(dir, true)
                   select file;
        }

        public IEnumerable<string> ListFiles(string virtualPath, bool recursive) {
            if (!recursive) {
                return from VirtualFile file in HostingEnvironment.VirtualPathProvider.GetDirectory(virtualPath).Files
                       select file.VirtualPath;
            }
            return (from VirtualFile file in HostingEnvironment.VirtualPathProvider.GetDirectory(virtualPath).Files
                    select file.VirtualPath).Concat(ListFiles(ListDirectories(virtualPath)));
/*

            var path = HostingEnvironment.MapPath(virtualPath);
            if (path == null || !Directory.Exists(path)) {
                return Enumerable.Empty<string>();
            }

            if (!path.EndsWith("\\")) {
                path += "\\";
            }

            var files = Directory.GetFiles(path, searchPattern, recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
            // strip base path to make it relative and replace '\' with '/'
            return from file in files
                   select "~/" + file.Replace(path, "").Replace("\\", "/");*/
        }
        
        public bool FileExists(string virtualPath) {
            return HostingEnvironment.VirtualPathProvider.FileExists(virtualPath);
        }

        public string ReadFile(string virtualPath) {
            return ReadFile(virtualPath, false);
        }

        public string ReadFile(string virtualPath, bool actualContent) {
            if (!HostingEnvironment.VirtualPathProvider.FileExists(virtualPath))
                return null;

            if (actualContent) {
                var physicalPath = _virtualPathProvider.MapPath(virtualPath);
                using (var stream = File.Open(physicalPath, FileMode.Open, FileAccess.Read)) {
                    using (var reader = new StreamReader(stream)) {
                        return reader.ReadToEnd();
                    }
                }
            }
            else {
                using (var stream = VirtualPathProvider.OpenFile(Normalize(virtualPath))) {
                    using (var reader = new StreamReader(stream)) {<commit_msg>Remove commented code<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Hosting;
using Orchard.Caching;
using Orchard.Environment;
using Orchard.FileSystems.VirtualPath;

namespace Orchard.FileSystems.WebSite {
    public class WebSiteFolder : IWebSiteFolder {
        private readonly IVirtualPathProvider _virtualPathProvider;
        private readonly IVirtualPathMonitor _virtualPathMonitor;

        public WebSiteFolder(IVirtualPathMonitor virtualPathMonitor, IVirtualPathProvider virtualPathProvider) {
            _virtualPathMonitor = virtualPathMonitor;
            _virtualPathProvider = virtualPathProvider;
        }

        public IEnumerable<string> ListDirectories(string virtualPath) {
            if (!HostingEnvironment.VirtualPathProvider.DirectoryExists(virtualPath))
                return Enumerable.Empty<string>();

            return HostingEnvironment.VirtualPathProvider
                .GetDirectory(virtualPath)
                .Directories.OfType<VirtualDirectory>()
                .Select(d => d.VirtualPath)
                .ToArray();
        }

        private IEnumerable<string> ListFiles(IEnumerable<string> directories) {
            return from dir in directories
                   from file in ListFiles(dir, true)
                   select file;
        }

        public IEnumerable<string> ListFiles(string virtualPath, bool recursive) {
            if (!recursive) {
                return from VirtualFile file in HostingEnvironment.VirtualPathProvider.GetDirectory(virtualPath).Files
                       select file.VirtualPath;
            }
            return (from VirtualFile file in HostingEnvironment.VirtualPathProvider.GetDirectory(virtualPath).Files
                    select file.VirtualPath).Concat(ListFiles(ListDirectories(virtualPath)));
        }
        
        public bool FileExists(string virtualPath) {
            return HostingEnvironment.VirtualPathProvider.FileExists(virtualPath);
        }

        public string ReadFile(string virtualPath) {
            return ReadFile(virtualPath, false);
        }

        public string ReadFile(string virtualPath, bool actualContent) {
            if (!HostingEnvironment.VirtualPathProvider.FileExists(virtualPath))
                return null;

            if (actualContent) {
                var physicalPath = _virtualPathProvider.MapPath(virtualPath);
                using (var stream = File.Open(physicalPath, FileMode.Open, FileAccess.Read)) {
                    using (var reader = new StreamReader(stream)) {
                        return reader.ReadToEnd();
                    }
                }
            }
            else {
                using (var stream = VirtualPathProvider.OpenFile(Normalize(virtualPath))) {
                    using (var reader = new StreamReader(stream)) {
{'fraction_non_alphanumeric': 0.0768025078369906, 'fraction_numerical': 0.0, 'mean_word_length': 2.2325227963525838, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2973048', 'n_tokens_mistral': 1513, 'n_tokens_neox': 1431, 'n_words': 329}