protected override Task<HttpResponseMessage> CreateDirectoryGetResponse(DirectoryInfoBase info, string localFilePath) { HttpResponseMessage response = Request.CreateResponse(); var ms = new MemoryStream(); using (var zip = new ZipArchive(ms, ZipArchiveMode.Create, leaveOpen: true)) { foreach (FileSystemInfoBase fileSysInfo in info.GetFileSystemInfos()) { var directoryInfo = fileSysInfo as DirectoryInfoBase; if (directoryInfo != null) { zip.AddDirectory(directoryInfo, fileSysInfo.Name); } else { // Add it at the root of the zip zip.AddFile(fileSysInfo.FullName, String.Empty); } } } ms.Seek(0, SeekOrigin.Begin); response.Content = new StreamContent(ms); response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/zip"); response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); // Name the zip after the folder. e.g. "c:\foo\bar\" --> "bar" response.Content.Headers.ContentDisposition.FileName = Path.GetFileName(Path.GetDirectoryName(localFilePath)) + ".zip"; return Task.FromResult(response); }
public static IEnumerable<int> Search(this TvdbHandler tvdbHandler, IFileSystem fileSystem, DirectoryInfoBase cacheDirectory, string term) { // Initalise cache if it does not exist. if (_searchCache == null) { _searchCache = new SearchCache(fileSystem, cacheDirectory); } // If not in cache then search online. if(!_searchCache.Contains(term)) { // HACK: TVDB fails on concurrent search, needs a lock to only allow one search. lock(_lock) { // Search. var results = tvdbHandler.SearchSeries(term); // Return null if no results found. if(results.Count==0) { return null; } // Set cache. _searchCache.Insert(term, results.Select(result => result.Id).ToList()); } } // Return series id. return _searchCache.Get(term); }
public static string MakeRelativePath(DirectoryInfoBase fromPath, FileInfoBase toPath) { if (fromPath == null) throw new ArgumentNullException("fromPath"); if (toPath == null) throw new ArgumentNullException("toPath"); string root = fromPath.FullName; if (!(root.EndsWith("\\") || root.EndsWith("/"))) root += "\\"; root += "a.txt"; Uri fromUri = new Uri(root); Uri toUri = new Uri(toPath.FullName); if (fromUri.Scheme != toUri.Scheme) { return toPath.FullName; } // path can't be made relative. Uri relativeUri = fromUri.MakeRelativeUri(toUri); string relativePath = Uri.UnescapeDataString(relativeUri.ToString()); if (toUri.Scheme.ToUpperInvariant() == "FILE") { relativePath = relativePath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar); } return relativePath; }
static void ListDirectory(DirectoryInfoBase dir) { FileSystemInfoBase[] children = dir.GetFileSystemInfos(); Console.WriteLine(); Console.WriteLine(" Directory of {0}", dir.FullName.TrimEnd('\\')); Console.WriteLine(); foreach (DirectoryInfoBase info in children.Where(d => d is DirectoryInfoBase)) { Console.WriteLine(String.Format("{0} <DIR> {1}", ToDisplayString(info.LastWriteTime), info.Name)); } int count = 0; long total = 0; foreach (FileInfoBase info in children.Where(d => !(d is DirectoryInfoBase))) { FileInfoBase file = (FileInfoBase)info; Console.WriteLine(String.Format("{0} {1,17} {2}", ToDisplayString(info.LastWriteTime), file.Length.ToString("#,##0"), info.Name)); total += file.Length; ++count; } Console.WriteLine(String.Format("{0,16} File(s) {1,14} bytes", count.ToString("#,##0"), total.ToString("#,##0"))); }
internal static IDictionary<string, FileInfoBase> GetFiles(DirectoryInfoBase info) { if (info == null) { return null; } return info.GetFilesWithRetry().ToDictionary(f => f.Name, StringComparer.OrdinalIgnoreCase); }
internal static IDictionary<string, DirectoryInfoBase> GetDirectories(DirectoryInfoBase info) { if (info == null) { return null; } return info.GetDirectories().ToDictionary(d => d.Name, StringComparer.OrdinalIgnoreCase); }
public void Organise(IMedia media, DirectoryInfoBase outputDirectory, OrganiserConversionOptions conversionOption, bool strictSeason) { // Create working directory. WorkingDirectory = _fileSystem.DirectoryInfo.FromDirectoryName(_fileSystem.Path.Combine(_fileSystem.Path.GetTempPath(), "WorkingArea")); // Create working directory if it does not exist. if(!WorkingDirectory.Exists) { WorkingDirectory.Create(); } // Copy to working area. CopyMediaToWorkingArea(media); // Convert if required. if(conversionOption == OrganiserConversionOptions.Force) { Logger.Log("Organiser").StdOut.WriteLine("Conversion set to \"force\". Will convert. {0}", media.MediaFile.FullName); ConvertMedia(media); } else if(media.RequiresConversion) { if(conversionOption == OrganiserConversionOptions.Skip) { Logger.Log("Organiser").StdOut.WriteLine("Media requires conversion. Conversion set to \"skip\", skipping conversion. {0}", media.MediaFile.FullName); } else { Logger.Log("Organiser").StdOut.WriteLine("Media requires conversion. Will convert. {0}", media.MediaFile.FullName); ConvertMedia(media); } } // Extract media details exhaustivly. ExtractExhaustiveMediaDetails(media, strictSeason); // Save media meta data. var saveResponse = SaveMediaMetaData(media); if(!saveResponse) { if(conversionOption == OrganiserConversionOptions.Skip) { Logger.Log("Organiser").StdOut.WriteLine("Unable to save metadata. Conversion set to \"skip\", skipping conversion. {0}", media.MediaFile.FullName); } else { Logger.Log("Organiser").StdOut.WriteLine("Unable to save metadata. Will convert. {0}", media.MediaFile.FullName); ConvertMedia(media); SaveMediaMetaData(media); } } // Rename media. RenameMediaToCleanFileName(media); // If output directory not provided, delete file. Otherwise move to output directory. MoveMediaToOutputDirectory(media, outputDirectory); }
public RunCommand() { IsCommand("run", "Run MediaPod."); HasRequiredOption("p|port=", "The port on which the MediaPod's webserver will list.", a => _webserverPort = int.Parse(a)); HasRequiredOption("t|tvShowDictionary=", "The directory for the TV Show library.", a => _tvShowDirectory = _fileSystem.DirectoryInfo.FromDirectoryName(a)); HasRequiredOption("u|unorganisedMediaDictionary=", "The directory where unorganised media can be found.", a => _unorganisedMediaDirectory = _fileSystem.DirectoryInfo.FromDirectoryName(a)); HasRequiredOption("k|tvdbApiKey=", "The ApiKey to use when connecting to the TVDB.", a => _tvdbApiKey = a); HasOption("l|logDirectory=", "The directory to store the logs.", a => _logDirectory = _fileSystem.DirectoryInfo.FromDirectoryName(a)); }
/// <summary> /// Initializes a new instance of <see cref="SnippetExtractorFactory"/>. /// </summary> /// <param name="extensionDirectory">The extension directory.</param> public SnippetExtractorFactory(DirectoryInfoBase extensionDirectory) { // Initialize this.defaultExtractorFactory = () => new DefaultSnippetExtractor(); this.extensionDirectory = extensionDirectory; // Load extensions this.LoadExtensions(); }
public static bool IsSameDirectory(this DirectoryInfoBase item1, DirectoryInfoBase item2) { if (item1 == null) throw new ArgumentNullException(nameof(item1)); if (item2 == null) throw new ArgumentNullException(nameof(item2)); string dir1Path = GetNormalisedFullPath(item1); string dir2Path = GetNormalisedFullPath(item2); return dir1Path.Equals(dir2Path, StringComparison.OrdinalIgnoreCase); }
private static string TryReadNpmVersion(DirectoryInfoBase nodeDir) { var npmRedirectionFile = nodeDir.GetFiles("npm.txt").FirstOrDefault(); if (npmRedirectionFile == null) { return null; } using (StreamReader reader = new StreamReader(npmRedirectionFile.OpenRead())) { return reader.ReadLine(); } }
public XElement Format(Uri file, GeneralTree<INode> features, DirectoryInfoBase outputFolder) { XNamespace xmlns = HtmlNamespace.Xhtml; XElement ul = this.BuildListItems(xmlns, file, features); ul.AddFirst(AddNodeForHome(xmlns, file, outputFolder)); return new XElement( xmlns + "div", new XAttribute("id", "toc"), this.BuildCollapser(xmlns), ul); }
/// <summary> /// Instantiates a file system repository for the given database at the specified directory location. /// </summary> /// <param name="scriptDirectory">The directory where build scripts are located.</param> /// <param name="serverName">The name of the database server.</param> /// <param name="databaseName">The name of the database.</param> /// <param name="fileSystem">An object that provides access to the file system.</param> /// <param name="sqlParser">The sql script parser for reading the SQL file contents.</param> /// <param name="logger">A Logger</param> /// <param name="ignoreUnsupportedSubdirectories">A flag indicating whether to ignore subdirectories that don't conform to the expected naming convention.</param> public FileSystemScriptRepository(DirectoryInfoBase scriptDirectory, string serverName, string databaseName, IFileSystem fileSystem, IParser sqlParser, ILogger logger, bool ignoreUnsupportedSubdirectories) { Logger = logger; ScriptDirectory = scriptDirectory; ServerName = serverName.TrimObjectName(); DatabaseName = databaseName.TrimObjectName(); IgnoreUnsupportedSubdirectories = ignoreUnsupportedSubdirectories; _objectTypes = Enum.GetValues(typeof(DatabaseObjectType)).Cast<DatabaseObjectType>() .ToDictionary(x => x.ToString(), y => y, StringComparer.InvariantCultureIgnoreCase); this.FileSystem = fileSystem; this._sqlParser = sqlParser; this.IsFileInSupportedDirectory = f => _objectTypes.ContainsKey(f.Directory.Name); }
protected override async Task<HttpResponseMessage> CreateDirectoryPutResponse(DirectoryInfoBase info, string localFilePath) { using (var stream = await Request.Content.ReadAsStreamAsync()) { // The unzipping is done over the existing folder, without first removing existing files. // Hence it's more of a PATCH than a PUT. We should consider supporting both with the right semantic. // Though a true PUT at the root would be scary as it would wipe all existing files! var zipArchive = new ZipArchive(stream, ZipArchiveMode.Read); zipArchive.Extract(localFilePath); } return Request.CreateResponse(HttpStatusCode.OK); }
public static void Initialise(IFileSystem fileSystem, int webserverPort, DirectoryInfoBase tvShowDirectory, DirectoryInfoBase unorganisedDirectory, string tvdbApiKey, DirectoryInfoBase logDirectory=null) { // Initalise. if (logDirectory == null) { LogManager = new LogManager(Console.Out, Console.Error); } else { LogManager = new LogManager (fileSystem, logDirectory); } NotificationManager = new NotificationManager(); MetadataSource = new TVDBTVShowMetadataSource(fileSystem, tvdbApiKey); QueuedTaskManager = new QueuedTaskManager(); TVShowLibrary = new TVShowLibrary(tvShowDirectory, new List<ITVShowMetadataSource>() { MetadataSource }); UnorganisedLibrary = new UnorganisedLibrary(unorganisedDirectory); WebserverManager = new WebserverManager(webserverPort); // Sta QueuedTaskManager.Start(); // Setup keep alive thread.. _keepAliveThread = CreateIntervalThread(() => { // Create threads if not alive. if(_fileSystemReloaderThread==null || !_fileSystemReloaderThread.IsAlive) { _fileSystemReloaderThread = CreateIntervalThread(() => { TVShowLibrary.Load(); }, _fileSystemReloaderSleepTime); _fileSystemReloaderThread.Priority = ThreadPriority.Lowest; _fileSystemReloaderThread.Start(); } if(_webserverThread==null || !_webserverThread.IsAlive) { _webserverThread = new Thread(() => { // Run webserver and block from terminating. WebserverManager.Run(); System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite); }); _webserverThread.Priority = ThreadPriority.Normal; _webserverThread.Start(); } }, _keepAliveSleepTime); // Set priority and start. _keepAliveThread.Priority = ThreadPriority.Lowest; _keepAliveThread.Start(); }
public static bool IsChildOf(this DirectoryInfoBase subDir, DirectoryInfoBase baseDir) { var isChild = false; while (subDir?.Parent != null) { if (subDir.Parent.IsSameDirectory(baseDir)) { isChild = true; break; } else { subDir = subDir.Parent; } } return isChild; }
public Organise() { IsCommand("organise", "Organises the media (TV Shows) into a uniform ouput which can be consumed."); HasRequiredOption("i|input=", "The Input path from which media will be found.", v => { // Check inputPath exists. if(!_fileSystem.DirectoryInfo.FromDirectoryName(v).Exists && !_fileSystem.FileInfo.FromFileName(v).Exists) { throw new ApplicationException("Invalid input path provided."); } // Set. _inputPath = v; }); HasRequiredOption("o|output=", "The Output directory from which organised media will be put.", v => { // Check output folder exists. if(!_fileSystem.DirectoryInfo.FromDirectoryName(v).Exists) { throw new ApplicationException("Invalid ouput directory provided."); } // Set. _outputDirectory = _fileSystem.DirectoryInfo.FromDirectoryName(v); }); HasOption("x|exclude", "Excludes organised shows in output directory when searching the input path.", v => _exclude = v != null); HasOption("s|strictSeason", "Enforces a strict season number requirement. This will guarantee the output media contains season details.", v => _strictSeason = v != null); HasOption("c|conversion=", "Specifies if/when input media should be converted. Possibly values: \"default\", \"force\", or \"skip\". \"default\" only converts if required, \"force\" converts everything, and \"skip\" will skip conversion for anything that requires conversion.", v => { if(v == null || v.ToLower() == "default") { _conversionOption = OrganiserConversionOptions.Default; } else if(v.ToLower() == "force") { _conversionOption = OrganiserConversionOptions.Force; } else if(v.ToLower() == "skip") { _conversionOption = OrganiserConversionOptions.Skip; } else { throw new ApplicationException("Invalid conversion option provided."); } }); }
static void CopyInternal(IFileSystem srcFileSystem, DirectoryInfoBase srcFileInfo, IFileSystem dstFileSystem, DirectoryInfoBase dstFileInfo) { foreach (FileSystemInfoBase src in srcFileInfo.GetFileSystemInfos()) { if (src is FileInfoBase) { var dst = dstFileSystem.FileInfo.FromFileName(Path.Combine(dstFileInfo.FullName, src.Name)); _semaphore.WaitOne(); _tasks.Add(CopyInternalAsync(srcFileSystem, (FileInfoBase)src, dstFileSystem, (FileInfoBase)dst).Finally(() => _semaphore.Release())); } else { // recursive var dst = dstFileSystem.DirectoryInfo.FromDirectoryName(Path.Combine(dstFileInfo.FullName, src.Name)); CopyInternal(srcFileSystem, (DirectoryInfoBase)src, dstFileSystem, (DirectoryInfoBase)dst); } } }
private bool CollectDirectories(DirectoryInfoBase directory, INode rootNode, GeneralTree<INode> tree) { List<GeneralTree<INode>> collectedNodes = new List<GeneralTree<INode>>(); foreach (DirectoryInfoBase subDirectory in directory.GetDirectories().OrderBy(di => di.Name)) { GeneralTree<INode> subTree = this.Crawl(subDirectory, rootNode); if (subTree != null) { collectedNodes.Add(subTree); } } foreach (var node in collectedNodes) { tree.Add(node); } return collectedNodes.Count > 0; }
public TVDBTVShowMetadataSource(IFileSystem fileSystem, string apiKey) { // Set fileSystem. _fileSystem = fileSystem; // Make cache directory. var assemblyName = Assembly.GetExecutingAssembly().GetName().Name; _tvdbCacheDirectory = _fileSystem.DirectoryInfo.FromDirectoryName(_fileSystem.Path.Combine(_fileSystem.Path.GetTempPath(), assemblyName + _fileSystem.Path.DirectorySeparatorChar +"TVDBCache")); if(!_tvdbCacheDirectory.Exists) { _tvdbCacheDirectory.Create(); } // Initalise handler with cache. _tvdbHandler = new TvdbHandler(new XmlCacheProvider(_tvdbCacheDirectory.FullName), apiKey); if(!_tvdbHandler.IsCacheInitialised) { _tvdbHandler.InitCache(); } }
protected override Task<HttpResponseMessage> CreateDirectoryGetResponse(DirectoryInfoBase info, string localFilePath) { HttpResponseMessage response = Request.CreateResponse(); response.Content = ZipStreamContent.Create(Path.GetFileName(Path.GetDirectoryName(localFilePath)) + ".zip", Tracer, zip => { foreach (FileSystemInfoBase fileSysInfo in info.GetFileSystemInfos()) { var directoryInfo = fileSysInfo as DirectoryInfoBase; if (directoryInfo != null) { zip.AddDirectory(directoryInfo, Tracer, fileSysInfo.Name); } else { // Add it at the root of the zip zip.AddFile(fileSysInfo.FullName, Tracer, String.Empty); } } }); return Task.FromResult(response); }
public static FileInfoBase GetBannerCacheFile(this TvdbBannerWithThumb banner, IFileSystem fileSystem, DirectoryInfoBase cacheDirectory, bool thumbnail) { // Initalise empty filename. string fileName = ""; // Load Banner if not loaded. if(!thumbnail && !banner.IsLoaded) { banner.LoadBanner(); } else if(thumbnail && !banner.IsThumbLoaded) { banner.LoadThumb(); } // Add pre-fix based on if thumb or not. if(thumbnail) { fileName += "thumb_"; } else { fileName += "img_"; } // Handle different BannerPath conversion for different banner types. if(banner.GetType() == typeof(TvdbFanartBanner)) { fileName += "fan_" + fileSystem.FileInfo.FromFileName(banner.BannerPath).Name; } else { fileName += banner.BannerPath.Replace("/", "_"); } // Return file. var firstCombine = fileSystem.Path.Combine(cacheDirectory.FullName, banner.SeriesId.ToString()); return fileSystem.FileInfo.FromFileName(fileSystem.Path.Combine(firstCombine, fileName)); }
private XElement AddNodeForHome(XNamespace xmlns, Uri file, DirectoryInfoBase rootFolder) { var rootfile = this.fileSystem.FileInfo.FromFileName(this.fileSystem.Path.Combine(rootFolder.FullName, "index.html")); var xElement = new XElement(xmlns + "li", new XAttribute("class", "file"), new XAttribute("id", "root")); string nodeText = "Home"; bool fileIsActuallyTheRoot = DetermineWhetherFileIsTheRootFile(file, rootfile); if (fileIsActuallyTheRoot) { xElement.Add(new XElement(xmlns + "span", new XAttribute("class", "current"), nodeText)); } else { xElement.Add(new XElement(xmlns + "a", new XAttribute("href", file.GetUriForTargetRelativeToMe(rootfile, ".html")), nodeText)); } return xElement; }
internal static void Copy(string sourcePath, string destinationPath, DirectoryInfoBase sourceDirectory, DirectoryInfoBase destinationDirectory, Func<string, DirectoryInfoBase> createDirectoryInfo, bool skipScmFolder) { // Skip hidden directories and directories that begin with . if (skipScmFolder && IsSourceControlFolder(sourceDirectory)) { return; } if (!destinationDirectory.Exists) { destinationDirectory.Create(); } foreach (var sourceFile in sourceDirectory.GetFiles()) { string path = GetDestinationPath(sourcePath, destinationPath, sourceFile); sourceFile.CopyTo(path, overwrite: true); } var destDirectoryLookup = GetDirectories(destinationDirectory); foreach (var sourceSubDirectory in sourceDirectory.GetDirectories()) { DirectoryInfoBase targetSubDirectory; if (!destDirectoryLookup.TryGetValue(sourceSubDirectory.Name, out targetSubDirectory)) { string path = GetDestinationPath(sourcePath, destinationPath, sourceSubDirectory); targetSubDirectory = createDirectoryInfo(path); } Copy(sourcePath, destinationPath, sourceSubDirectory, targetSubDirectory, createDirectoryInfo, skipScmFolder); } }
private GeneralTree<INode> Crawl(DirectoryInfoBase directory, INode rootNode) { INode currentNode = this.featureNodeFactory.Create(rootNode != null ? rootNode.OriginalLocation : null, directory); if (rootNode == null) { rootNode = currentNode; } var tree = new GeneralTree<INode>(currentNode); var filesAreFound = this.CollectFiles(directory, rootNode, tree); var directoriesAreFound = this.CollectDirectories(directory, rootNode, tree); if (!filesAreFound && !directoriesAreFound) { return null; } return tree; }
public static void AddDirectory(this ZipArchive zipArchive, DirectoryInfoBase directory, string directoryNameInArchive) { bool any = false; foreach (var info in directory.GetFileSystemInfos()) { any = true; var subDirectoryInfo = info as DirectoryInfoBase; if (subDirectoryInfo != null) { string childName = Path.Combine(directoryNameInArchive, subDirectoryInfo.Name); zipArchive.AddDirectory(subDirectoryInfo, childName); } else { zipArchive.AddFile((FileInfoBase)info, directoryNameInArchive); } } if (!any) { // If the directory did not have any files or folders, add a entry for it zipArchive.CreateEntry(EnsureTrailingSlash(directoryNameInArchive)); } }
protected override Task<HttpResponseMessage> CreateDirectoryPutResponse(DirectoryInfoBase info, string localFilePath) { if (info != null && info.Exists) { // Return a conflict result return base.CreateDirectoryPutResponse(info, localFilePath); } try { info.Create(); } catch (IOException ex) { Tracer.TraceError(ex); HttpResponseMessage conflictDirectoryResponse = Request.CreateErrorResponse( HttpStatusCode.Conflict, Resources.VfsControllerBase_CannotDeleteDirectory); return Task.FromResult(conflictDirectoryResponse); } // Return 201 Created response HttpResponseMessage successFileResponse = Request.CreateResponse(HttpStatusCode.Created); return Task.FromResult(successFileResponse); }
private static IFileSystem CreateFileSystem(string path, DirectoryInfoBase dir, FileInfoBase file) { var directoryFactory = new Mock<IDirectoryInfoFactory>(); directoryFactory.Setup(d => d.FromDirectoryName(path)) .Returns(dir); var fileInfoFactory = new Mock<IFileInfoFactory>(); fileInfoFactory.Setup(f => f.FromFileName(path)) .Returns(file); var pathBase = new Mock<PathBase>(); pathBase.Setup(p => p.GetFullPath(It.IsAny<string>())) .Returns<string>(s => s); var fileSystem = new Mock<IFileSystem>(); fileSystem.SetupGet(f => f.DirectoryInfo).Returns(directoryFactory.Object); fileSystem.SetupGet(f => f.FileInfo).Returns(fileInfoFactory.Object); fileSystem.SetupGet(f => f.Path).Returns(pathBase.Object); FileSystemHelpers.Instance = fileSystem.Object; return fileSystem.Object; }
public DiFactoryStub(DirectoryInfoBase returnThis) { _returnThis = returnThis; }
private static void DeleteDirectoryContentsSafe(DirectoryInfoBase directoryInfo, bool ignoreErrors) { try { if (directoryInfo.Exists) { foreach (var fsi in directoryInfo.GetFileSystemInfos()) { DeleteFileSystemInfo(fsi, ignoreErrors); } } } catch { if (!ignoreErrors) throw; } }