Skip to content
This repository has been archived by the owner on Jan 19, 2019. It is now read-only.

Commit

Permalink
replace file system calls with IFileSystem when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
LukePulverenti committed Oct 31, 2013
1 parent 3937470 commit a690bc5
Show file tree
Hide file tree
Showing 80 changed files with 570 additions and 302 deletions.
3 changes: 2 additions & 1 deletion MediaBrowser.Api/Library/LibraryHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MediaBrowser.Controller;
using MediaBrowser.Common.IO;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.IO;
using System;
Expand Down
3 changes: 2 additions & 1 deletion MediaBrowser.Api/Library/LibraryStructureService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MediaBrowser.Controller;
using MediaBrowser.Common.IO;
using MediaBrowser.Controller;
using MediaBrowser.Controller.IO;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Entities;
Expand Down
7 changes: 5 additions & 2 deletions MediaBrowser.Api/Playback/BaseStreamingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public abstract class BaseStreamingService : BaseApiService
protected IMediaEncoder MediaEncoder { get; private set; }
protected IDtoService DtoService { get; private set; }

protected IFileSystem FileSystem { get; private set; }

/// <summary>
/// Initializes a new instance of the <see cref="BaseStreamingService" /> class.
/// </summary>
Expand All @@ -67,8 +69,9 @@ public abstract class BaseStreamingService : BaseApiService
/// <param name="libraryManager">The library manager.</param>
/// <param name="isoManager">The iso manager.</param>
/// <param name="mediaEncoder">The media encoder.</param>
protected BaseStreamingService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IDtoService dtoService)
protected BaseStreamingService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IDtoService dtoService, IFileSystem fileSystem)
{
FileSystem = fileSystem;
DtoService = dtoService;
ApplicationPaths = appPaths;
UserManager = userManager;
Expand Down Expand Up @@ -653,7 +656,7 @@ protected async Task StartFfMpeg(StreamState state, string outputPath)
var logFilePath = Path.Combine(ApplicationPaths.LogDirectoryPath, "ffmpeg-" + Guid.NewGuid() + ".txt");

// FFMpeg writes debug/error info to stderr. This is useful when debugging so let's put it in the log directory.
state.LogFileStream = new FileStream(logFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous);
state.LogFileStream = FileSystem.GetFileStream(logFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, true);

process.Exited += (sender, args) => OnFfMpegProcessExited(process, state);

Expand Down
7 changes: 4 additions & 3 deletions MediaBrowser.Api/Playback/Hls/AudioHlsService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MediaBrowser.Common.MediaInfo;
using MediaBrowser.Common.IO;
using MediaBrowser.Common.MediaInfo;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Library;
Expand Down Expand Up @@ -32,8 +33,8 @@ public class AudioHlsService : BaseHlsService
/// <param name="libraryManager">The library manager.</param>
/// <param name="isoManager">The iso manager.</param>
/// <param name="mediaEncoder">The media encoder.</param>
public AudioHlsService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IDtoService dtoService)
: base(appPaths, userManager, libraryManager, isoManager, mediaEncoder, dtoService)
public AudioHlsService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IDtoService dtoService, IFileSystem fileSystem)
: base(appPaths, userManager, libraryManager, isoManager, mediaEncoder, dtoService, fileSystem)
{
}

Expand Down
6 changes: 3 additions & 3 deletions MediaBrowser.Api/Playback/Hls/BaseHlsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ protected override string GetOutputFilePath(StreamState state)
/// <param name="libraryManager">The library manager.</param>
/// <param name="isoManager">The iso manager.</param>
/// <param name="mediaEncoder">The media encoder.</param>
protected BaseHlsService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IDtoService dtoService)
: base(appPaths, userManager, libraryManager, isoManager, mediaEncoder, dtoService)
protected BaseHlsService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IDtoService dtoService, IFileSystem fileSystem)
: base(appPaths, userManager, libraryManager, isoManager, mediaEncoder, dtoService, fileSystem)
{
}

Expand Down Expand Up @@ -209,7 +209,7 @@ private async Task WaitForMinimumSegmentCount(string playlist, int segmentCount)
string fileText;

// Need to use FileShare.ReadWrite because we're reading the file at the same time it's being written
using (var fileStream = new FileStream(playlist, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous))
using (var fileStream = FileSystem.GetFileStream(playlist, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true))
{
using (var reader = new StreamReader(fileStream))
{
Expand Down
7 changes: 4 additions & 3 deletions MediaBrowser.Api/Playback/Hls/VideoHlsService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MediaBrowser.Common.MediaInfo;
using MediaBrowser.Common.IO;
using MediaBrowser.Common.MediaInfo;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Library;
Expand Down Expand Up @@ -39,8 +40,8 @@ public class VideoHlsService : BaseHlsService
/// <param name="isoManager">The iso manager.</param>
/// <param name="mediaEncoder">The media encoder.</param>
/// <param name="dtoService">The dto service.</param>
public VideoHlsService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IDtoService dtoService)
: base(appPaths, userManager, libraryManager, isoManager, mediaEncoder, dtoService)
public VideoHlsService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IDtoService dtoService, IFileSystem fileSystem)
: base(appPaths, userManager, libraryManager, isoManager, mediaEncoder, dtoService, fileSystem)
{
}

Expand Down
7 changes: 4 additions & 3 deletions MediaBrowser.Api/Playback/Progressive/AudioService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MediaBrowser.Common.MediaInfo;
using MediaBrowser.Common.IO;
using MediaBrowser.Common.MediaInfo;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Drawing;
using MediaBrowser.Controller.Dto;
Expand Down Expand Up @@ -40,8 +41,8 @@ public class GetAudioStream : StreamRequest
/// </summary>
public class AudioService : BaseProgressiveStreamingService
{
public AudioService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IItemRepository itemRepo, IDtoService dtoService, IImageProcessor imageProcessor)
: base(appPaths, userManager, libraryManager, isoManager, mediaEncoder, itemRepo, dtoService, imageProcessor)
public AudioService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IItemRepository itemRepo, IDtoService dtoService, IImageProcessor imageProcessor, IFileSystem fileSystem)
: base(appPaths, userManager, libraryManager, isoManager, mediaEncoder, itemRepo, dtoService, imageProcessor, fileSystem)
{
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MediaBrowser.Api.Images;
using MediaBrowser.Common.IO;
using MediaBrowser.Common.MediaInfo;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller;
Expand Down Expand Up @@ -27,8 +28,8 @@ public abstract class BaseProgressiveStreamingService : BaseStreamingService
protected readonly IItemRepository ItemRepository;
protected readonly IImageProcessor ImageProcessor;

protected BaseProgressiveStreamingService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IItemRepository itemRepository, IDtoService dtoService, IImageProcessor imageProcessor) :
base(appPaths, userManager, libraryManager, isoManager, mediaEncoder, dtoService)
protected BaseProgressiveStreamingService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IItemRepository itemRepository, IDtoService dtoService, IImageProcessor imageProcessor, IFileSystem fileSystem) :
base(appPaths, userManager, libraryManager, isoManager, mediaEncoder, dtoService, fileSystem)
{
ItemRepository = itemRepository;
ImageProcessor = imageProcessor;
Expand Down Expand Up @@ -346,7 +347,7 @@ private async Task<object> GetStreamResult(StreamState state, IDictionary<string
ApiEntryPoint.Instance.OnTranscodeBeginRequest(outputPath, TranscodingJobType.Progressive);
}

var result = new ProgressiveStreamWriter(outputPath, Logger);
var result = new ProgressiveStreamWriter(outputPath, Logger, FileSystem);

result.Options["Accept-Ranges"] = "none";
result.Options["Content-Type"] = contentType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class ProgressiveStreamWriter : IStreamWriter, IHasOptions
{
private string Path { get; set; }
private ILogger Logger { get; set; }
private readonly IFileSystem _fileSystem;

/// <summary>
/// The _options
Expand All @@ -32,10 +33,11 @@ public IDictionary<string, string> Options
/// </summary>
/// <param name="path">The path.</param>
/// <param name="logger">The logger.</param>
public ProgressiveStreamWriter(string path, ILogger logger)
public ProgressiveStreamWriter(string path, ILogger logger, IFileSystem fileSystem)
{
Path = path;
Logger = logger;
_fileSystem = fileSystem;
}

/// <summary>
Expand Down Expand Up @@ -83,7 +85,7 @@ private async Task StreamFile(string path, Stream outputStream)
var eofCount = 0;
long position = 0;

using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous))
using (var fs = _fileSystem.GetFileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true))
{
while (eofCount < 15)
{
Expand Down
7 changes: 4 additions & 3 deletions MediaBrowser.Api/Playback/Progressive/VideoService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MediaBrowser.Common.MediaInfo;
using MediaBrowser.Common.IO;
using MediaBrowser.Common.MediaInfo;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Drawing;
using MediaBrowser.Controller.Dto;
Expand Down Expand Up @@ -54,8 +55,8 @@ public class GetVideoStream : VideoStreamRequest
/// </summary>
public class VideoService : BaseProgressiveStreamingService
{
public VideoService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IItemRepository itemRepo, IDtoService dtoService, IImageProcessor imageProcessor)
: base(appPaths, userManager, libraryManager, isoManager, mediaEncoder, itemRepo, dtoService, imageProcessor)
public VideoService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IItemRepository itemRepo, IDtoService dtoService, IImageProcessor imageProcessor, IFileSystem fileSystem)
: base(appPaths, userManager, libraryManager, isoManager, mediaEncoder, itemRepo, dtoService, imageProcessor, fileSystem)
{
}

Expand Down
10 changes: 8 additions & 2 deletions MediaBrowser.Api/SystemService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using MediaBrowser.Common.Extensions;
using MediaBrowser.Common.IO;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.IO;
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.Serialization;
using MediaBrowser.Model.System;
Expand Down Expand Up @@ -75,14 +77,17 @@ public class SystemService : BaseApiService
/// </summary>
private readonly IServerConfigurationManager _configurationManager;

private readonly IFileSystem _fileSystem;


/// <summary>
/// Initializes a new instance of the <see cref="SystemService" /> class.
/// </summary>
/// <param name="jsonSerializer">The json serializer.</param>
/// <param name="appHost">The app host.</param>
/// <param name="configurationManager">The configuration manager.</param>
/// <exception cref="System.ArgumentNullException">jsonSerializer</exception>
public SystemService(IJsonSerializer jsonSerializer, IServerApplicationHost appHost, IServerConfigurationManager configurationManager)
public SystemService(IJsonSerializer jsonSerializer, IServerApplicationHost appHost, IServerConfigurationManager configurationManager, IFileSystem fileSystem)
: base()
{
if (jsonSerializer == null)
Expand All @@ -96,6 +101,7 @@ public SystemService(IJsonSerializer jsonSerializer, IServerApplicationHost appH

_appHost = appHost;
_configurationManager = configurationManager;
_fileSystem = fileSystem;
_jsonSerializer = jsonSerializer;
}

Expand All @@ -118,7 +124,7 @@ public object Get(GetSystemInfo request)
/// <returns>System.Object.</returns>
public object Get(GetConfiguration request)
{
var dateModified = File.GetLastWriteTimeUtc(_configurationManager.ApplicationPaths.SystemConfigurationFilePath);
var dateModified = _fileSystem.GetLastWriteTimeUtc(_configurationManager.ApplicationPaths.SystemConfigurationFilePath);

var cacheKey = (_configurationManager.ApplicationPaths.SystemConfigurationFilePath + dateModified.Ticks).GetMD5();

Expand Down
11 changes: 7 additions & 4 deletions MediaBrowser.Api/WebSocket/LogFileWebSocketListener.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using MediaBrowser.Common.IO;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.IO;
using MediaBrowser.Model.Logging;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -27,16 +28,18 @@ protected override string Name
/// The _kernel
/// </summary>
private readonly ILogManager _logManager;
private readonly IFileSystem _fileSystem;

/// <summary>
/// Initializes a new instance of the <see cref="LogFileWebSocketListener" /> class.
/// </summary>
/// <param name="logger">The logger.</param>
/// <param name="logManager">The log manager.</param>
public LogFileWebSocketListener(ILogger logger, ILogManager logManager)
public LogFileWebSocketListener(ILogger logger, ILogManager logManager, IFileSystem fileSystem)
: base(logger)
{
_logManager = logManager;
_fileSystem = fileSystem;
_logManager.LoggerLoaded += kernel_LoggerLoaded;
}

Expand All @@ -53,7 +56,7 @@ protected override async Task<IEnumerable<string>> GetDataToSend(LogFileWebSocke
state.StartLine = 0;
}

var lines = await GetLogLines(state.LastLogFilePath, state.StartLine).ConfigureAwait(false);
var lines = await GetLogLines(state.LastLogFilePath, state.StartLine, _fileSystem).ConfigureAwait(false);

state.StartLine += lines.Count;

Expand Down Expand Up @@ -96,11 +99,11 @@ void kernel_LoggerLoaded(object sender, EventArgs e)
/// <param name="logFilePath">The log file path.</param>
/// <param name="startLine">The start line.</param>
/// <returns>Task{IEnumerable{System.String}}.</returns>
internal static async Task<List<string>> GetLogLines(string logFilePath, int startLine)
internal static async Task<List<string>> GetLogLines(string logFilePath, int startLine, IFileSystem fileSystem)
{
var lines = new List<string>();

using (var fs = new FileStream(logFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, StreamDefaults.DefaultFileStreamBufferSize, true))
using (var fs = fileSystem.GetFileStream(logFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true))
{
using (var reader = new StreamReader(fs))
{
Expand Down
23 changes: 17 additions & 6 deletions MediaBrowser.Common.Implementations/BaseApplicationHost.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System.Net;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Events;
using MediaBrowser.Common.Implementations.Archiving;
using MediaBrowser.Common.Implementations.IO;
using MediaBrowser.Common.Implementations.ScheduledTasks;
using MediaBrowser.Common.Implementations.Security;
using MediaBrowser.Common.Implementations.Serialization;
using MediaBrowser.Common.Implementations.Updates;
using MediaBrowser.Common.IO;
using MediaBrowser.Common.Net;
using MediaBrowser.Common.Plugins;
using MediaBrowser.Common.ScheduledTasks;
Expand All @@ -21,6 +21,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Threading;
Expand Down Expand Up @@ -150,15 +151,17 @@ public abstract class BaseApplicationHost<TApplicationPathsType> : IApplicationH
/// Gets or sets the installation manager.
/// </summary>
/// <value>The installation manager.</value>
protected IInstallationManager InstallationManager { get; set; }
protected IInstallationManager InstallationManager { get; private set; }

protected IFileSystem FileSystemManager { get; private set; }

/// <summary>
/// Gets or sets the zip client.
/// </summary>
/// <value>The zip client.</value>
protected IZipClient ZipClient { get; set; }
protected IZipClient ZipClient { get; private set; }

protected IIsoManager IsoManager { get; set; }
protected IIsoManager IsoManager { get; private set; }

/// <summary>
/// Initializes a new instance of the <see cref="BaseApplicationHost{TApplicationPathsType}"/> class.
Expand Down Expand Up @@ -347,7 +350,10 @@ protected virtual Task RegisterResources()

RegisterSingleInstance(TaskManager);

HttpClient = new HttpClientManager.HttpClientManager(ApplicationPaths, Logger, CreateHttpClient);
FileSystemManager = CreateFileSystemManager();
RegisterSingleInstance(FileSystemManager);

HttpClient = new HttpClientManager.HttpClientManager(ApplicationPaths, Logger, CreateHttpClient, FileSystemManager);
RegisterSingleInstance(HttpClient);

NetworkManager = CreateNetworkManager();
Expand All @@ -367,6 +373,11 @@ protected virtual Task RegisterResources()
});
}

protected virtual IFileSystem CreateFileSystemManager()
{
return new CommonFileSystem(Logger, true);
}

protected abstract HttpClient CreateHttpClient(bool enableHttpCompression);

/// <summary>
Expand Down
Loading

0 comments on commit a690bc5

Please sign in to comment.