2014-01-20 16:09:53 +00:00
using MediaBrowser.Controller ;
using MediaBrowser.Controller.Persistence ;
using MediaBrowser.Model.FileOrganization ;
using MediaBrowser.Model.Logging ;
2014-01-20 18:46:08 +00:00
using MediaBrowser.Model.Querying ;
2014-01-20 16:09:53 +00:00
using System ;
2014-01-21 06:10:58 +00:00
using System.Collections.Generic ;
2014-01-20 16:09:53 +00:00
using System.Data ;
2014-01-21 06:10:58 +00:00
using System.Globalization ;
2014-01-20 16:09:53 +00:00
using System.IO ;
2014-01-23 21:09:00 +00:00
using System.Linq ;
2014-01-20 16:09:53 +00:00
using System.Threading ;
using System.Threading.Tasks ;
namespace MediaBrowser.Server.Implementations.Persistence
{
2015-05-02 22:59:01 +00:00
public class SqliteFileOrganizationRepository : BaseSqliteRepository , IFileOrganizationRepository , IDisposable
2014-01-20 16:09:53 +00:00
{
2014-01-21 06:10:58 +00:00
private readonly CultureInfo _usCulture = new CultureInfo ( "en-US" ) ;
2016-06-11 15:55:05 +00:00
public SqliteFileOrganizationRepository ( ILogManager logManager , IServerApplicationPaths appPaths , IDbConnector connector ) : base ( logManager , connector )
2014-01-20 16:09:53 +00:00
{
2016-06-12 01:25:20 +00:00
DbFilePath = Path . Combine ( appPaths . DataPath , "fileorganization.db" ) ;
2014-01-20 16:09:53 +00:00
}
/// <summary>
/// Opens the connection to the database
/// </summary>
/// <returns>Task.</returns>
2016-06-12 01:25:20 +00:00
public async Task Initialize ( )
2014-01-20 16:09:53 +00:00
{
2016-06-12 01:25:20 +00:00
using ( var connection = await CreateConnection ( ) . ConfigureAwait ( false ) )
{
string [ ] queries = {
2014-01-20 16:09:53 +00:00
2014-02-19 16:24:06 +00:00
"create table if not exists FileOrganizerResults (ResultId GUID PRIMARY KEY, OriginalPath TEXT, TargetPath TEXT, FileLength INT, OrganizationDate datetime, Status TEXT, OrganizationType TEXT, StatusMessage TEXT, ExtractedName TEXT, ExtractedYear int null, ExtractedSeasonNumber int null, ExtractedEpisodeNumber int null, ExtractedEndingEpisodeNumber, DuplicatePaths TEXT int null)" ,
2016-06-11 20:12:01 +00:00
"create index if not exists idx_FileOrganizerResults on FileOrganizerResults(ResultId)"
2014-01-20 16:09:53 +00:00
} ;
2016-06-12 01:25:20 +00:00
connection . RunQueries ( queries , Logger ) ;
}
2014-01-21 06:10:58 +00:00
}
public async Task SaveResult ( FileOrganizationResult result , CancellationToken cancellationToken )
{
if ( result = = null )
{
throw new ArgumentNullException ( "result" ) ;
}
cancellationToken . ThrowIfCancellationRequested ( ) ;
2016-06-12 01:25:20 +00:00
using ( var connection = await CreateConnection ( ) . ConfigureAwait ( false ) )
2014-01-21 06:10:58 +00:00
{
2016-06-12 01:25:20 +00:00
using ( var saveResultCommand = connection . CreateCommand ( ) )
2014-01-21 06:10:58 +00:00
{
2016-06-12 01:25:20 +00:00
saveResultCommand . CommandText = "replace into FileOrganizerResults (ResultId, OriginalPath, TargetPath, FileLength, OrganizationDate, Status, OrganizationType, StatusMessage, ExtractedName, ExtractedYear, ExtractedSeasonNumber, ExtractedEpisodeNumber, ExtractedEndingEpisodeNumber, DuplicatePaths) values (@ResultId, @OriginalPath, @TargetPath, @FileLength, @OrganizationDate, @Status, @OrganizationType, @StatusMessage, @ExtractedName, @ExtractedYear, @ExtractedSeasonNumber, @ExtractedEpisodeNumber, @ExtractedEndingEpisodeNumber, @DuplicatePaths)" ;
saveResultCommand . Parameters . Add ( saveResultCommand , "@ResultId" ) ;
saveResultCommand . Parameters . Add ( saveResultCommand , "@OriginalPath" ) ;
saveResultCommand . Parameters . Add ( saveResultCommand , "@TargetPath" ) ;
saveResultCommand . Parameters . Add ( saveResultCommand , "@FileLength" ) ;
saveResultCommand . Parameters . Add ( saveResultCommand , "@OrganizationDate" ) ;
saveResultCommand . Parameters . Add ( saveResultCommand , "@Status" ) ;
saveResultCommand . Parameters . Add ( saveResultCommand , "@OrganizationType" ) ;
saveResultCommand . Parameters . Add ( saveResultCommand , "@StatusMessage" ) ;
saveResultCommand . Parameters . Add ( saveResultCommand , "@ExtractedName" ) ;
saveResultCommand . Parameters . Add ( saveResultCommand , "@ExtractedYear" ) ;
saveResultCommand . Parameters . Add ( saveResultCommand , "@ExtractedSeasonNumber" ) ;
saveResultCommand . Parameters . Add ( saveResultCommand , "@ExtractedEpisodeNumber" ) ;
saveResultCommand . Parameters . Add ( saveResultCommand , "@ExtractedEndingEpisodeNumber" ) ;
saveResultCommand . Parameters . Add ( saveResultCommand , "@DuplicatePaths" ) ;
IDbTransaction transaction = null ;
try
{
transaction = connection . BeginTransaction ( ) ;
var index = 0 ;
saveResultCommand . GetParameter ( index + + ) . Value = new Guid ( result . Id ) ;
saveResultCommand . GetParameter ( index + + ) . Value = result . OriginalPath ;
saveResultCommand . GetParameter ( index + + ) . Value = result . TargetPath ;
saveResultCommand . GetParameter ( index + + ) . Value = result . FileSize ;
saveResultCommand . GetParameter ( index + + ) . Value = result . Date ;
saveResultCommand . GetParameter ( index + + ) . Value = result . Status . ToString ( ) ;
saveResultCommand . GetParameter ( index + + ) . Value = result . Type . ToString ( ) ;
saveResultCommand . GetParameter ( index + + ) . Value = result . StatusMessage ;
saveResultCommand . GetParameter ( index + + ) . Value = result . ExtractedName ;
saveResultCommand . GetParameter ( index + + ) . Value = result . ExtractedYear ;
saveResultCommand . GetParameter ( index + + ) . Value = result . ExtractedSeasonNumber ;
saveResultCommand . GetParameter ( index + + ) . Value = result . ExtractedEpisodeNumber ;
saveResultCommand . GetParameter ( index + + ) . Value = result . ExtractedEndingEpisodeNumber ;
saveResultCommand . GetParameter ( index ) . Value = string . Join ( "|" , result . DuplicatePaths . ToArray ( ) ) ;
saveResultCommand . Transaction = transaction ;
saveResultCommand . ExecuteNonQuery ( ) ;
transaction . Commit ( ) ;
}
catch ( OperationCanceledException )
{
if ( transaction ! = null )
{
transaction . Rollback ( ) ;
}
2014-01-21 06:10:58 +00:00
2016-06-12 01:25:20 +00:00
throw ;
}
catch ( Exception e )
{
Logger . ErrorException ( "Failed to save FileOrganizationResult:" , e ) ;
2014-01-21 06:10:58 +00:00
2016-06-12 01:25:20 +00:00
if ( transaction ! = null )
{
transaction . Rollback ( ) ;
}
2014-01-21 06:10:58 +00:00
2016-06-12 01:25:20 +00:00
throw ;
}
finally
{
if ( transaction ! = null )
{
transaction . Dispose ( ) ;
}
}
2014-01-21 06:10:58 +00:00
}
}
2014-01-20 16:09:53 +00:00
}
2014-01-21 06:10:58 +00:00
public async Task Delete ( string id )
2014-01-20 16:09:53 +00:00
{
2014-01-21 06:10:58 +00:00
if ( string . IsNullOrEmpty ( id ) )
{
throw new ArgumentNullException ( "id" ) ;
}
2016-06-12 01:25:20 +00:00
using ( var connection = await CreateConnection ( ) . ConfigureAwait ( false ) )
{
using ( var deleteResultCommand = connection . CreateCommand ( ) )
{
deleteResultCommand . CommandText = "delete from FileOrganizerResults where ResultId = @ResultId" ;
2014-01-21 06:10:58 +00:00
2016-06-12 01:25:20 +00:00
deleteResultCommand . Parameters . Add ( deleteResultCommand , "@ResultId" ) ;
2014-01-21 06:10:58 +00:00
2016-06-12 01:25:20 +00:00
IDbTransaction transaction = null ;
2014-01-21 06:10:58 +00:00
2016-06-12 01:25:20 +00:00
try
{
transaction = connection . BeginTransaction ( ) ;
2014-01-21 06:10:58 +00:00
2016-06-12 01:25:20 +00:00
deleteResultCommand . GetParameter ( 0 ) . Value = new Guid ( id ) ;
2014-01-21 06:10:58 +00:00
2016-06-12 01:25:20 +00:00
deleteResultCommand . Transaction = transaction ;
2014-01-21 06:10:58 +00:00
2016-06-12 01:25:20 +00:00
deleteResultCommand . ExecuteNonQuery ( ) ;
2014-01-21 06:10:58 +00:00
2016-06-12 01:25:20 +00:00
transaction . Commit ( ) ;
}
catch ( OperationCanceledException )
{
if ( transaction ! = null )
{
transaction . Rollback ( ) ;
}
2014-01-21 06:10:58 +00:00
2016-06-12 01:25:20 +00:00
throw ;
}
catch ( Exception e )
{
Logger . ErrorException ( "Failed to delete FileOrganizationResult:" , e ) ;
2014-01-21 06:10:58 +00:00
2016-06-12 01:25:20 +00:00
if ( transaction ! = null )
{
transaction . Rollback ( ) ;
}
2014-01-21 06:10:58 +00:00
2016-06-12 01:25:20 +00:00
throw ;
}
finally
{
if ( transaction ! = null )
{
transaction . Dispose ( ) ;
}
}
}
2014-01-21 06:10:58 +00:00
}
2014-01-20 16:09:53 +00:00
}
2014-01-22 17:05:06 +00:00
public async Task DeleteAll ( )
{
2016-06-12 01:25:20 +00:00
using ( var connection = await CreateConnection ( ) . ConfigureAwait ( false ) )
{
using ( var cmd = connection . CreateCommand ( ) )
{
cmd . CommandText = "delete from FileOrganizerResults" ;
2014-01-22 17:05:06 +00:00
2016-06-12 01:25:20 +00:00
IDbTransaction transaction = null ;
2014-01-22 17:05:06 +00:00
2016-06-12 01:25:20 +00:00
try
{
transaction = connection . BeginTransaction ( ) ;
2014-01-22 17:05:06 +00:00
2016-06-12 01:25:20 +00:00
cmd . Transaction = transaction ;
2014-01-22 17:05:06 +00:00
2016-06-12 01:25:20 +00:00
cmd . ExecuteNonQuery ( ) ;
2014-01-22 17:05:06 +00:00
2016-06-12 01:25:20 +00:00
transaction . Commit ( ) ;
}
catch ( OperationCanceledException )
{
if ( transaction ! = null )
{
transaction . Rollback ( ) ;
}
2014-01-22 17:05:06 +00:00
2016-06-12 01:25:20 +00:00
throw ;
}
catch ( Exception e )
{
Logger . ErrorException ( "Failed to delete results" , e ) ;
2014-01-22 17:05:06 +00:00
2016-06-12 01:25:20 +00:00
if ( transaction ! = null )
{
transaction . Rollback ( ) ;
}
2014-01-22 17:05:06 +00:00
2016-06-12 01:25:20 +00:00
throw ;
}
finally
{
if ( transaction ! = null )
{
transaction . Dispose ( ) ;
}
}
}
2014-01-22 17:05:06 +00:00
}
}
2016-06-12 01:25:20 +00:00
2014-01-20 18:46:08 +00:00
public QueryResult < FileOrganizationResult > GetResults ( FileOrganizationResultQuery query )
2014-01-20 16:09:53 +00:00
{
2014-01-21 06:10:58 +00:00
if ( query = = null )
{
throw new ArgumentNullException ( "query" ) ;
}
2016-06-12 01:25:20 +00:00
using ( var connection = CreateConnection ( true ) . Result )
2014-01-21 06:10:58 +00:00
{
2016-06-12 01:25:20 +00:00
using ( var cmd = connection . CreateCommand ( ) )
2014-01-21 06:10:58 +00:00
{
2016-06-12 01:25:20 +00:00
cmd . CommandText = "SELECT ResultId, OriginalPath, TargetPath, FileLength, OrganizationDate, Status, OrganizationType, StatusMessage, ExtractedName, ExtractedYear, ExtractedSeasonNumber, ExtractedEpisodeNumber, ExtractedEndingEpisodeNumber, DuplicatePaths from FileOrganizerResults" ;
2014-01-21 06:10:58 +00:00
2016-06-12 01:25:20 +00:00
if ( query . StartIndex . HasValue & & query . StartIndex . Value > 0 )
{
cmd . CommandText + = string . Format ( " WHERE ResultId NOT IN (SELECT ResultId FROM FileOrganizerResults ORDER BY OrganizationDate desc LIMIT {0})" ,
query . StartIndex . Value . ToString ( _usCulture ) ) ;
}
2014-01-21 06:10:58 +00:00
2016-06-12 01:25:20 +00:00
cmd . CommandText + = " ORDER BY OrganizationDate desc" ;
if ( query . Limit . HasValue )
{
cmd . CommandText + = " LIMIT " + query . Limit . Value . ToString ( _usCulture ) ;
}
2014-01-21 06:10:58 +00:00
2016-06-12 01:25:20 +00:00
cmd . CommandText + = "; select count (ResultId) from FileOrganizerResults" ;
2014-01-21 06:10:58 +00:00
2016-06-12 01:25:20 +00:00
var list = new List < FileOrganizationResult > ( ) ;
var count = 0 ;
2014-01-21 06:10:58 +00:00
2016-06-12 01:25:20 +00:00
using ( var reader = cmd . ExecuteReader ( CommandBehavior . SequentialAccess ) )
2014-01-21 06:10:58 +00:00
{
2016-06-12 01:25:20 +00:00
while ( reader . Read ( ) )
{
list . Add ( GetResult ( reader ) ) ;
}
if ( reader . NextResult ( ) & & reader . Read ( ) )
{
count = reader . GetInt32 ( 0 ) ;
}
2014-01-21 06:10:58 +00:00
}
2016-06-12 01:25:20 +00:00
return new QueryResult < FileOrganizationResult > ( )
2014-01-21 06:10:58 +00:00
{
2016-06-12 01:25:20 +00:00
Items = list . ToArray ( ) ,
TotalRecordCount = count
} ;
2014-01-21 06:10:58 +00:00
}
}
}
public FileOrganizationResult GetResult ( string id )
{
if ( string . IsNullOrEmpty ( id ) )
{
throw new ArgumentNullException ( "id" ) ;
}
2016-06-12 01:25:20 +00:00
using ( var connection = CreateConnection ( true ) . Result )
2014-01-21 06:10:58 +00:00
{
2016-06-12 01:25:20 +00:00
var guid = new Guid ( id ) ;
2014-01-21 06:10:58 +00:00
2016-06-12 01:25:20 +00:00
using ( var cmd = connection . CreateCommand ( ) )
2014-01-21 06:10:58 +00:00
{
2016-06-12 01:25:20 +00:00
cmd . CommandText = "select ResultId, OriginalPath, TargetPath, FileLength, OrganizationDate, Status, OrganizationType, StatusMessage, ExtractedName, ExtractedYear, ExtractedSeasonNumber, ExtractedEpisodeNumber, ExtractedEndingEpisodeNumber, DuplicatePaths from FileOrganizerResults where ResultId=@Id" ;
cmd . Parameters . Add ( cmd , "@Id" , DbType . Guid ) . Value = guid ;
using ( var reader = cmd . ExecuteReader ( CommandBehavior . SequentialAccess | CommandBehavior . SingleResult | CommandBehavior . SingleRow ) )
2014-01-21 06:10:58 +00:00
{
2016-06-12 01:25:20 +00:00
if ( reader . Read ( ) )
{
return GetResult ( reader ) ;
}
2014-01-21 06:10:58 +00:00
}
}
2016-06-12 01:25:20 +00:00
return null ;
}
2014-01-21 06:10:58 +00:00
}
public FileOrganizationResult GetResult ( IDataReader reader )
{
2014-02-19 16:24:06 +00:00
var index = 0 ;
2014-01-21 06:10:58 +00:00
var result = new FileOrganizationResult
{
Id = reader . GetGuid ( 0 ) . ToString ( "N" )
} ;
2014-02-19 16:24:06 +00:00
index + + ;
if ( ! reader . IsDBNull ( index ) )
2014-01-21 06:10:58 +00:00
{
2014-02-19 16:24:06 +00:00
result . OriginalPath = reader . GetString ( index ) ;
2014-01-21 06:10:58 +00:00
}
2014-02-19 16:24:06 +00:00
index + + ;
if ( ! reader . IsDBNull ( index ) )
2014-01-21 06:10:58 +00:00
{
2014-02-19 16:24:06 +00:00
result . TargetPath = reader . GetString ( index ) ;
2014-01-21 06:10:58 +00:00
}
2014-02-19 16:24:06 +00:00
index + + ;
result . FileSize = reader . GetInt64 ( index ) ;
index + + ;
result . Date = reader . GetDateTime ( index ) . ToUniversalTime ( ) ;
index + + ;
result . Status = ( FileSortingStatus ) Enum . Parse ( typeof ( FileSortingStatus ) , reader . GetString ( index ) , true ) ;
index + + ;
result . Type = ( FileOrganizerType ) Enum . Parse ( typeof ( FileOrganizerType ) , reader . GetString ( index ) , true ) ;
2014-01-21 06:10:58 +00:00
2014-02-19 16:24:06 +00:00
index + + ;
if ( ! reader . IsDBNull ( index ) )
2014-01-21 06:10:58 +00:00
{
2014-02-19 16:24:06 +00:00
result . StatusMessage = reader . GetString ( index ) ;
2014-01-21 06:10:58 +00:00
}
result . OriginalFileName = Path . GetFileName ( result . OriginalPath ) ;
2014-02-19 16:24:06 +00:00
index + + ;
if ( ! reader . IsDBNull ( index ) )
2014-01-21 06:10:58 +00:00
{
2014-02-19 16:24:06 +00:00
result . ExtractedName = reader . GetString ( index ) ;
2014-01-21 06:10:58 +00:00
}
2014-02-19 16:24:06 +00:00
index + + ;
if ( ! reader . IsDBNull ( index ) )
2014-01-21 06:10:58 +00:00
{
2014-02-19 16:24:06 +00:00
result . ExtractedYear = reader . GetInt32 ( index ) ;
2014-01-21 06:10:58 +00:00
}
2014-02-19 16:24:06 +00:00
index + + ;
if ( ! reader . IsDBNull ( index ) )
2014-01-21 16:24:12 +00:00
{
2014-02-19 16:24:06 +00:00
result . ExtractedSeasonNumber = reader . GetInt32 ( index ) ;
2014-01-21 16:24:12 +00:00
}
2014-02-19 16:24:06 +00:00
index + + ;
if ( ! reader . IsDBNull ( index ) )
2014-01-21 16:24:12 +00:00
{
2014-02-19 16:24:06 +00:00
result . ExtractedEpisodeNumber = reader . GetInt32 ( index ) ;
2014-01-21 16:24:12 +00:00
}
2014-02-19 16:24:06 +00:00
index + + ;
if ( ! reader . IsDBNull ( index ) )
2014-01-21 16:24:12 +00:00
{
2014-02-19 16:24:06 +00:00
result . ExtractedEndingEpisodeNumber = reader . GetInt32 ( index ) ;
2014-01-21 16:24:12 +00:00
}
2014-02-19 16:24:06 +00:00
index + + ;
if ( ! reader . IsDBNull ( index ) )
2014-01-23 21:09:00 +00:00
{
2014-02-19 16:24:06 +00:00
result . DuplicatePaths = reader . GetString ( index ) . Split ( '|' ) . Where ( i = > ! string . IsNullOrEmpty ( i ) ) . ToList ( ) ;
2014-01-23 21:09:00 +00:00
}
2014-01-21 06:10:58 +00:00
return result ;
2014-01-20 16:09:53 +00:00
}
}
}