SharingRepository.cs 4.1 KB

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