2016-11-18 01:39:20 -07:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Emby.Server.Implementations.Data;
|
|
|
|
|
using MediaBrowser.Common.Configuration;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using MediaBrowser.Model.Social;
|
|
|
|
|
using SQLitePCL.pretty;
|
|
|
|
|
|
|
|
|
|
namespace Emby.Server.Implementations.Social
|
|
|
|
|
{
|
|
|
|
|
public class SharingRepository : BaseSqliteRepository, ISharingRepository
|
|
|
|
|
{
|
|
|
|
|
public SharingRepository(ILogger logger, IApplicationPaths appPaths)
|
|
|
|
|
: base(logger)
|
|
|
|
|
{
|
|
|
|
|
DbFilePath = Path.Combine(appPaths.DataPath, "shares.db");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Opens the connection to the database
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
public void Initialize()
|
|
|
|
|
{
|
|
|
|
|
using (var connection = CreateConnection())
|
|
|
|
|
{
|
2016-11-29 12:12:37 -07:00
|
|
|
|
RunDefaultInitialization(connection);
|
2016-11-20 20:52:58 -07:00
|
|
|
|
|
2016-11-18 01:39:20 -07:00
|
|
|
|
string[] queries = {
|
|
|
|
|
|
|
|
|
|
"create table if not exists Shares (Id GUID, ItemId TEXT, UserId TEXT, ExpirationDate DateTime, PRIMARY KEY (Id))",
|
|
|
|
|
"create index if not exists idx_Shares on Shares(Id)",
|
|
|
|
|
|
|
|
|
|
"pragma shrink_memory"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
connection.RunQueries(queries);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task CreateShare(SocialShareInfo info)
|
|
|
|
|
{
|
|
|
|
|
if (info == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("info");
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrWhiteSpace(info.Id))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("info.Id");
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-10 22:12:00 -07:00
|
|
|
|
using (WriteLock.Write())
|
2016-11-18 01:39:20 -07:00
|
|
|
|
{
|
2016-12-10 22:12:00 -07:00
|
|
|
|
using (var connection = CreateConnection())
|
2016-11-18 01:39:20 -07:00
|
|
|
|
{
|
|
|
|
|
connection.RunInTransaction(db =>
|
|
|
|
|
{
|
|
|
|
|
var commandText = "replace into Shares (Id, ItemId, UserId, ExpirationDate) values (?, ?, ?, ?)";
|
|
|
|
|
|
|
|
|
|
db.Execute(commandText,
|
2017-05-07 13:02:32 -07:00
|
|
|
|
info.Id.ToGuidBlob(),
|
2016-11-18 01:39:20 -07:00
|
|
|
|
info.ItemId,
|
|
|
|
|
info.UserId,
|
|
|
|
|
info.ExpirationDate.ToDateTimeParamValue());
|
2016-11-28 12:26:48 -07:00
|
|
|
|
}, TransactionMode);
|
2016-11-18 01:39:20 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SocialShareInfo GetShareInfo(string id)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(id))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("id");
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-10 22:12:00 -07:00
|
|
|
|
using (WriteLock.Read())
|
2016-11-18 01:39:20 -07:00
|
|
|
|
{
|
2016-12-10 22:12:00 -07:00
|
|
|
|
using (var connection = CreateConnection(true))
|
2016-11-18 01:39:20 -07:00
|
|
|
|
{
|
|
|
|
|
var commandText = "select Id, ItemId, UserId, ExpirationDate from Shares where id = ?";
|
|
|
|
|
|
|
|
|
|
var paramList = new List<object>();
|
2017-05-07 13:02:32 -07:00
|
|
|
|
paramList.Add(id.ToGuidBlob());
|
2016-11-18 01:39:20 -07:00
|
|
|
|
|
|
|
|
|
foreach (var row in connection.Query(commandText, paramList.ToArray()))
|
|
|
|
|
{
|
|
|
|
|
return GetSocialShareInfo(row);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private SocialShareInfo GetSocialShareInfo(IReadOnlyList<IResultSetValue> reader)
|
|
|
|
|
{
|
|
|
|
|
var info = new SocialShareInfo();
|
|
|
|
|
|
2017-05-07 13:02:32 -07:00
|
|
|
|
info.Id = reader[0].ReadGuidFromBlob().ToString("N");
|
2016-11-18 01:39:20 -07:00
|
|
|
|
info.ItemId = reader[1].ToString();
|
|
|
|
|
info.UserId = reader[2].ToString();
|
|
|
|
|
info.ExpirationDate = reader[3].ReadDateTime();
|
|
|
|
|
|
|
|
|
|
return info;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task DeleteShare(string id)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|