SharingRepository.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using Emby.Server.Implementations.Data;
  7. using MediaBrowser.Common.Configuration;
  8. using MediaBrowser.Model.Logging;
  9. using MediaBrowser.Model.Social;
  10. using SQLitePCL.pretty;
  11. using MediaBrowser.Model.Extensions;
  12. namespace Emby.Server.Implementations.Social
  13. {
  14. public class SharingRepository : BaseSqliteRepository, ISharingRepository
  15. {
  16. public SharingRepository(ILogger logger, IApplicationPaths appPaths)
  17. : base(logger)
  18. {
  19. DbFilePath = Path.Combine(appPaths.DataPath, "shares.db");
  20. }
  21. /// <summary>
  22. /// Opens the connection to the database
  23. /// </summary>
  24. /// <returns>Task.</returns>
  25. public void Initialize()
  26. {
  27. using (var connection = CreateConnection())
  28. {
  29. RunDefaultInitialization(connection);
  30. string[] queries = {
  31. "create table if not exists Shares (Id GUID, ItemId TEXT, UserId TEXT, ExpirationDate DateTime, PRIMARY KEY (Id))",
  32. "create index if not exists idx_Shares on Shares(Id)",
  33. "pragma shrink_memory"
  34. };
  35. connection.RunQueries(queries);
  36. }
  37. }
  38. public async Task CreateShare(SocialShareInfo info)
  39. {
  40. if (info == null)
  41. {
  42. throw new ArgumentNullException("info");
  43. }
  44. if (string.IsNullOrWhiteSpace(info.Id))
  45. {
  46. throw new ArgumentNullException("info.Id");
  47. }
  48. using (WriteLock.Write())
  49. {
  50. using (var connection = CreateConnection())
  51. {
  52. connection.RunInTransaction(db =>
  53. {
  54. var commandText = "replace into Shares (Id, ItemId, UserId, ExpirationDate) values (?, ?, ?, ?)";
  55. db.Execute(commandText,
  56. info.Id.ToGuidBlob(),
  57. info.ItemId,
  58. info.UserId,
  59. info.ExpirationDate.ToDateTimeParamValue());
  60. }, TransactionMode);
  61. }
  62. }
  63. }
  64. public SocialShareInfo GetShareInfo(string id)
  65. {
  66. if (string.IsNullOrWhiteSpace(id))
  67. {
  68. throw new ArgumentNullException("id");
  69. }
  70. using (WriteLock.Read())
  71. {
  72. using (var connection = CreateConnection(true))
  73. {
  74. var commandText = "select Id, ItemId, UserId, ExpirationDate from Shares where id = ?";
  75. var paramList = new List<object>();
  76. paramList.Add(id.ToGuidBlob());
  77. foreach (var row in connection.Query(commandText, paramList.ToArray(paramList.Count)))
  78. {
  79. return GetSocialShareInfo(row);
  80. }
  81. }
  82. }
  83. return null;
  84. }
  85. private SocialShareInfo GetSocialShareInfo(IReadOnlyList<IResultSetValue> reader)
  86. {
  87. var info = new SocialShareInfo();
  88. info.Id = reader[0].ReadGuidFromBlob().ToString("N");
  89. info.ItemId = reader[1].ToString();
  90. info.UserId = reader[2].ToString();
  91. info.ExpirationDate = reader[3].ReadDateTime();
  92. return info;
  93. }
  94. public async Task DeleteShare(string id)
  95. {
  96. }
  97. }
  98. }