2016-11-18 08:39:20 +00:00
using System ;
using System.Collections.Generic ;
using System.IO ;
using Emby.Server.Implementations.Data ;
using MediaBrowser.Common.Configuration ;
using MediaBrowser.Model.Logging ;
using MediaBrowser.Model.Social ;
using SQLitePCL.pretty ;
2017-08-09 19:56:38 +00:00
using MediaBrowser.Model.Extensions ;
2017-09-22 05:54:57 +00:00
using MediaBrowser.Model.IO ;
2016-11-18 08:39:20 +00:00
namespace Emby.Server.Implementations.Social
{
public class SharingRepository : BaseSqliteRepository , ISharingRepository
{
2017-09-22 05:54:57 +00:00
protected IFileSystem FileSystem { get ; private set ; }
public SharingRepository ( ILogger logger , IApplicationPaths appPaths , IFileSystem fileSystem )
2016-11-18 08:39:20 +00:00
: base ( logger )
{
2017-09-22 05:54:57 +00:00
FileSystem = fileSystem ;
2016-11-18 08:39:20 +00:00
DbFilePath = Path . Combine ( appPaths . DataPath , "shares.db" ) ;
}
2017-09-22 05:54:57 +00:00
public void Initialize ( )
{
try
{
InitializeInternal ( ) ;
}
catch ( Exception ex )
{
Logger . ErrorException ( "Error loading database file. Will reset and retry." , ex ) ;
FileSystem . DeleteFile ( DbFilePath ) ;
InitializeInternal ( ) ;
}
}
2016-11-18 08:39:20 +00:00
/// <summary>
/// Opens the connection to the database
/// </summary>
/// <returns>Task.</returns>
2017-09-22 05:54:57 +00:00
private void InitializeInternal ( )
2016-11-18 08:39:20 +00:00
{
using ( var connection = CreateConnection ( ) )
{
2016-11-29 19:12:37 +00:00
RunDefaultInitialization ( connection ) ;
2016-11-21 03:52:58 +00:00
2016-11-18 08:39:20 +00:00
string [ ] queries = {
2017-10-22 06:22:43 +00:00
"create table if not exists Shares (Id GUID NOT NULL, ItemId TEXT NOT NULL, UserId TEXT NOT NULL, ExpirationDate DateTime NOT NULL, PRIMARY KEY (Id))" ,
2016-11-18 08:39:20 +00:00
"create index if not exists idx_Shares on Shares(Id)" ,
"pragma shrink_memory"
} ;
connection . RunQueries ( queries ) ;
}
}
2017-08-27 00:32:33 +00:00
public void CreateShare ( SocialShareInfo info )
2016-11-18 08:39:20 +00:00
{
if ( info = = null )
{
throw new ArgumentNullException ( "info" ) ;
}
if ( string . IsNullOrWhiteSpace ( info . Id ) )
{
throw new ArgumentNullException ( "info.Id" ) ;
}
2016-12-11 05:12:00 +00:00
using ( WriteLock . Write ( ) )
2016-11-18 08:39:20 +00:00
{
2016-12-11 05:12:00 +00:00
using ( var connection = CreateConnection ( ) )
2016-11-18 08:39:20 +00:00
{
connection . RunInTransaction ( db = >
{
var commandText = "replace into Shares (Id, ItemId, UserId, ExpirationDate) values (?, ?, ?, ?)" ;
db . Execute ( commandText ,
2017-05-07 20:02:32 +00:00
info . Id . ToGuidBlob ( ) ,
2016-11-18 08:39:20 +00:00
info . ItemId ,
info . UserId ,
info . ExpirationDate . ToDateTimeParamValue ( ) ) ;
2016-11-28 19:26:48 +00:00
} , TransactionMode ) ;
2016-11-18 08:39:20 +00:00
}
}
}
public SocialShareInfo GetShareInfo ( string id )
{
if ( string . IsNullOrWhiteSpace ( id ) )
{
throw new ArgumentNullException ( "id" ) ;
}
2016-12-11 05:12:00 +00:00
using ( WriteLock . Read ( ) )
2016-11-18 08:39:20 +00:00
{
2016-12-11 05:12:00 +00:00
using ( var connection = CreateConnection ( true ) )
2016-11-18 08:39:20 +00:00
{
var commandText = "select Id, ItemId, UserId, ExpirationDate from Shares where id = ?" ;
var paramList = new List < object > ( ) ;
2017-05-07 20:02:32 +00:00
paramList . Add ( id . ToGuidBlob ( ) ) ;
2016-11-18 08:39:20 +00:00
2017-08-09 19:56:38 +00:00
foreach ( var row in connection . Query ( commandText , paramList . ToArray ( paramList . Count ) ) )
2016-11-18 08:39:20 +00:00
{
return GetSocialShareInfo ( row ) ;
}
}
}
return null ;
}
private SocialShareInfo GetSocialShareInfo ( IReadOnlyList < IResultSetValue > reader )
{
var info = new SocialShareInfo ( ) ;
2017-05-07 20:02:32 +00:00
info . Id = reader [ 0 ] . ReadGuidFromBlob ( ) . ToString ( "N" ) ;
2016-11-18 08:39:20 +00:00
info . ItemId = reader [ 1 ] . ToString ( ) ;
info . UserId = reader [ 2 ] . ToString ( ) ;
info . ExpirationDate = reader [ 3 ] . ReadDateTime ( ) ;
return info ;
}
2017-08-27 00:32:33 +00:00
public void DeleteShare ( string id )
2016-11-18 08:39:20 +00:00
{
}
}
}