Update RecordTaskFactory

This commit is contained in:
Genteure 2021-06-01 23:53:38 +08:00
parent 2d44a9ad1e
commit bf8a4481e2
2 changed files with 19 additions and 5 deletions

View File

@ -40,6 +40,8 @@ namespace BililiveRecorder.Core.Recording
};
this.OnRecordFileOpening(this.fileOpeningEventArgs);
this.logger.Information("新建录制文件 {Path}", fullPath);
var file = new FileStream(fullPath, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.Read | FileShare.Delete);
_ = Task.Run(async () => await this.WriteStreamToFileAsync(stream, file).ConfigureAwait(false));

View File

@ -1,23 +1,35 @@
using System;
using BililiveRecorder.Core.Config.V2;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
namespace BililiveRecorder.Core.Recording
{
public class RecordTaskFactory : IRecordTaskFactory
{
private readonly ILogger logger;
private readonly IServiceProvider serviceProvider;
private readonly ObjectFactory factoryRawData;
private readonly ObjectFactory factoryStandard;
public RecordTaskFactory(IServiceProvider serviceProvider)
public RecordTaskFactory(ILogger logger, IServiceProvider serviceProvider)
{
this.logger = logger?.ForContext<RecordTaskFactory>() ?? throw new ArgumentNullException(nameof(logger));
this.serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
this.factoryRawData = ActivatorUtilities.CreateFactory(typeof(RawDataRecordTask), new[] { typeof(IRoom) });
this.factoryStandard = ActivatorUtilities.CreateFactory(typeof(StandardRecordTask), new[] { typeof(IRoom) });
}
public IRecordTask CreateRecordTask(IRoom room) =>
room.RoomConfig.RecordMode switch
public IRecordTask CreateRecordTask(IRoom room)
{
var recordMode = room.RoomConfig.RecordMode;
this.logger.Debug("Create record task with mode {RecordMode} for room {RoomId}", recordMode, room.RoomConfig.RoomId);
return recordMode switch
{
RecordMode.RawData => ActivatorUtilities.CreateInstance<RawDataRecordTask>(this.serviceProvider, room),
_ => ActivatorUtilities.CreateInstance<StandardRecordTask>(this.serviceProvider, room)
RecordMode.RawData => (IRecordTask)this.factoryRawData(this.serviceProvider, new[] { room }),
_ => (IRecordTask)this.factoryStandard(this.serviceProvider, new[] { room })
};
}
}
}