SharingRepository.cs 3.6 KB

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