User.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Controller.IO;
  3. using MediaBrowser.Controller.Library;
  4. using MediaBrowser.Model.Configuration;
  5. using MediaBrowser.Model.Serialization;
  6. using System;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Runtime.Serialization;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. namespace MediaBrowser.Controller.Entities
  13. {
  14. /// <summary>
  15. /// Class User
  16. /// </summary>
  17. public class User : BaseItem
  18. {
  19. public static IUserManager UserManager { get; set; }
  20. public static IXmlSerializer XmlSerializer { get; set; }
  21. /// <summary>
  22. /// The _root folder path
  23. /// </summary>
  24. private string _rootFolderPath;
  25. /// <summary>
  26. /// Gets the root folder path.
  27. /// </summary>
  28. /// <value>The root folder path.</value>
  29. [IgnoreDataMember]
  30. public string RootFolderPath
  31. {
  32. get
  33. {
  34. if (_rootFolderPath == null)
  35. {
  36. if (Configuration.UseCustomLibrary)
  37. {
  38. _rootFolderPath = GetRootFolderPath(Name);
  39. if (!Directory.Exists(_rootFolderPath))
  40. {
  41. Directory.CreateDirectory(_rootFolderPath);
  42. }
  43. }
  44. else
  45. {
  46. _rootFolderPath = ConfigurationManager.ApplicationPaths.DefaultUserViewsPath;
  47. }
  48. }
  49. return _rootFolderPath;
  50. }
  51. }
  52. /// <summary>
  53. /// Gets the root folder path based on a given username
  54. /// </summary>
  55. /// <param name="username">The username.</param>
  56. /// <returns>System.String.</returns>
  57. private string GetRootFolderPath(string username)
  58. {
  59. var safeFolderName = FileSystem.GetValidFilename(username);
  60. return System.IO.Path.Combine(ConfigurationManager.ApplicationPaths.RootFolderPath, safeFolderName);
  61. }
  62. /// <summary>
  63. /// Gets or sets the password.
  64. /// </summary>
  65. /// <value>The password.</value>
  66. public string Password { get; set; }
  67. /// <summary>
  68. /// Gets or sets the path.
  69. /// </summary>
  70. /// <value>The path.</value>
  71. public override string Path
  72. {
  73. get
  74. {
  75. // Return this so that metadata providers will look in here
  76. return ConfigurationDirectoryPath;
  77. }
  78. set
  79. {
  80. base.Path = value;
  81. }
  82. }
  83. /// <summary>
  84. /// Ensure this has a value
  85. /// </summary>
  86. /// <value>The display type of the media.</value>
  87. public override string DisplayMediaType
  88. {
  89. get
  90. {
  91. return base.DisplayMediaType ?? GetType().Name;
  92. }
  93. set
  94. {
  95. base.DisplayMediaType = value;
  96. }
  97. }
  98. /// <summary>
  99. /// The _root folder
  100. /// </summary>
  101. private UserRootFolder _rootFolder;
  102. /// <summary>
  103. /// The _user root folder initialized
  104. /// </summary>
  105. private bool _userRootFolderInitialized;
  106. /// <summary>
  107. /// The _user root folder sync lock
  108. /// </summary>
  109. private object _userRootFolderSyncLock = new object();
  110. /// <summary>
  111. /// Gets the root folder.
  112. /// </summary>
  113. /// <value>The root folder.</value>
  114. [IgnoreDataMember]
  115. public UserRootFolder RootFolder
  116. {
  117. get
  118. {
  119. LazyInitializer.EnsureInitialized(ref _rootFolder, ref _userRootFolderInitialized, ref _userRootFolderSyncLock, () => (UserRootFolder)LibraryManager.ResolvePath(RootFolderPath));
  120. return _rootFolder;
  121. }
  122. private set
  123. {
  124. _rootFolder = value;
  125. if (_rootFolder == null)
  126. {
  127. _userRootFolderInitialized = false;
  128. }
  129. }
  130. }
  131. /// <summary>
  132. /// Gets or sets the last login date.
  133. /// </summary>
  134. /// <value>The last login date.</value>
  135. public DateTime? LastLoginDate { get; set; }
  136. /// <summary>
  137. /// Gets or sets the last activity date.
  138. /// </summary>
  139. /// <value>The last activity date.</value>
  140. public DateTime? LastActivityDate { get; set; }
  141. /// <summary>
  142. /// The _configuration
  143. /// </summary>
  144. private UserConfiguration _configuration;
  145. /// <summary>
  146. /// The _configuration initialized
  147. /// </summary>
  148. private bool _configurationInitialized;
  149. /// <summary>
  150. /// The _configuration sync lock
  151. /// </summary>
  152. private object _configurationSyncLock = new object();
  153. /// <summary>
  154. /// Gets the user's configuration
  155. /// </summary>
  156. /// <value>The configuration.</value>
  157. [IgnoreDataMember]
  158. public UserConfiguration Configuration
  159. {
  160. get
  161. {
  162. // Lazy load
  163. LazyInitializer.EnsureInitialized(ref _configuration, ref _configurationInitialized, ref _configurationSyncLock, () => (UserConfiguration)ConfigurationHelper.GetXmlConfiguration(typeof(UserConfiguration), ConfigurationFilePath, XmlSerializer));
  164. return _configuration;
  165. }
  166. private set
  167. {
  168. _configuration = value;
  169. if (value == null)
  170. {
  171. _configurationInitialized = false;
  172. }
  173. }
  174. }
  175. /// <summary>
  176. /// Gets the last date modified of the configuration
  177. /// </summary>
  178. /// <value>The configuration date last modified.</value>
  179. [IgnoreDataMember]
  180. public DateTime ConfigurationDateLastModified
  181. {
  182. get
  183. {
  184. // Ensure it's been lazy loaded
  185. var config = Configuration;
  186. return File.GetLastWriteTimeUtc(ConfigurationFilePath);
  187. }
  188. }
  189. /// <summary>
  190. /// Reloads the root media folder
  191. /// </summary>
  192. /// <param name="cancellationToken">The cancellation token.</param>
  193. /// <param name="progress">The progress.</param>
  194. /// <returns>Task.</returns>
  195. public async Task ValidateMediaLibrary(IProgress<double> progress, CancellationToken cancellationToken)
  196. {
  197. Logger.Info("Validating media library for {0}", Name);
  198. await RootFolder.RefreshMetadata(cancellationToken).ConfigureAwait(false);
  199. cancellationToken.ThrowIfCancellationRequested();
  200. await RootFolder.ValidateChildren(progress, cancellationToken).ConfigureAwait(false);
  201. }
  202. /// <summary>
  203. /// Validates only the collection folders for a User and goes no further
  204. /// </summary>
  205. /// <param name="cancellationToken">The cancellation token.</param>
  206. /// <param name="progress">The progress.</param>
  207. /// <returns>Task.</returns>
  208. public async Task ValidateCollectionFolders(IProgress<double> progress, CancellationToken cancellationToken)
  209. {
  210. Logger.Info("Validating collection folders for {0}", Name);
  211. await RootFolder.RefreshMetadata(cancellationToken).ConfigureAwait(false);
  212. cancellationToken.ThrowIfCancellationRequested();
  213. await RootFolder.ValidateChildren(progress, cancellationToken, recursive: false).ConfigureAwait(false);
  214. }
  215. /// <summary>
  216. /// Renames the user.
  217. /// </summary>
  218. /// <param name="newName">The new name.</param>
  219. /// <returns>Task.</returns>
  220. /// <exception cref="System.ArgumentNullException"></exception>
  221. public Task Rename(string newName)
  222. {
  223. if (string.IsNullOrEmpty(newName))
  224. {
  225. throw new ArgumentNullException();
  226. }
  227. // If only the casing is changing, leave the file system alone
  228. if (!newName.Equals(Name, StringComparison.OrdinalIgnoreCase))
  229. {
  230. // Move configuration
  231. var newConfigDirectory = GetConfigurationDirectoryPath(newName);
  232. // Exceptions will be thrown if these paths already exist
  233. if (Directory.Exists(newConfigDirectory))
  234. {
  235. Directory.Delete(newConfigDirectory, true);
  236. }
  237. Directory.Move(ConfigurationDirectoryPath, newConfigDirectory);
  238. var customLibraryPath = GetRootFolderPath(Name);
  239. // Move the root folder path if using a custom library
  240. if (Directory.Exists(customLibraryPath))
  241. {
  242. var newRootFolderPath = GetRootFolderPath(newName);
  243. if (Directory.Exists(newRootFolderPath))
  244. {
  245. Directory.Delete(newRootFolderPath, true);
  246. }
  247. Directory.Move(customLibraryPath, newRootFolderPath);
  248. }
  249. }
  250. Name = newName;
  251. // Force these to be lazy loaded again
  252. _configurationDirectoryPath = null;
  253. _rootFolderPath = null;
  254. RootFolder = null;
  255. // Kick off a task to validate the media library
  256. Task.Run(() => ValidateMediaLibrary(new Progress<double> { }, CancellationToken.None));
  257. return RefreshMetadata(CancellationToken.None, forceSave: true, forceRefresh: true);
  258. }
  259. /// <summary>
  260. /// The _configuration directory path
  261. /// </summary>
  262. private string _configurationDirectoryPath;
  263. /// <summary>
  264. /// Gets the path to the user's configuration directory
  265. /// </summary>
  266. /// <value>The configuration directory path.</value>
  267. private string ConfigurationDirectoryPath
  268. {
  269. get
  270. {
  271. if (_configurationDirectoryPath == null)
  272. {
  273. _configurationDirectoryPath = GetConfigurationDirectoryPath(Name);
  274. if (!Directory.Exists(_configurationDirectoryPath))
  275. {
  276. Directory.CreateDirectory(_configurationDirectoryPath);
  277. }
  278. }
  279. return _configurationDirectoryPath;
  280. }
  281. }
  282. /// <summary>
  283. /// Gets the configuration directory path.
  284. /// </summary>
  285. /// <param name="username">The username.</param>
  286. /// <returns>System.String.</returns>
  287. private string GetConfigurationDirectoryPath(string username)
  288. {
  289. var safeFolderName = FileSystem.GetValidFilename(username);
  290. return System.IO.Path.Combine(ConfigurationManager.ApplicationPaths.UserConfigurationDirectoryPath, safeFolderName);
  291. }
  292. /// <summary>
  293. /// Gets the path to the user's configuration file
  294. /// </summary>
  295. /// <value>The configuration file path.</value>
  296. private string ConfigurationFilePath
  297. {
  298. get
  299. {
  300. return System.IO.Path.Combine(ConfigurationDirectoryPath, "config.xml");
  301. }
  302. }
  303. /// <summary>
  304. /// Saves the current configuration to the file system
  305. /// </summary>
  306. public void SaveConfiguration(IXmlSerializer serializer)
  307. {
  308. serializer.SerializeToFile(Configuration, ConfigurationFilePath);
  309. }
  310. /// <summary>
  311. /// Refresh metadata on us by execution our provider chain
  312. /// The item will be persisted if a change is made by a provider, or if it's new or changed.
  313. /// </summary>
  314. /// <param name="cancellationToken">The cancellation token.</param>
  315. /// <param name="forceSave">if set to <c>true</c> [is new item].</param>
  316. /// <param name="forceRefresh">if set to <c>true</c> [force].</param>
  317. /// <param name="allowSlowProviders">if set to <c>true</c> [allow slow providers].</param>
  318. /// <param name="resetResolveArgs">if set to <c>true</c> [reset resolve args].</param>
  319. /// <returns>true if a provider reports we changed</returns>
  320. public override async Task<bool> RefreshMetadata(CancellationToken cancellationToken, bool forceSave = false, bool forceRefresh = false, bool allowSlowProviders = true, bool resetResolveArgs = true)
  321. {
  322. if (resetResolveArgs)
  323. {
  324. ResolveArgs = null;
  325. }
  326. var changed = await ProviderManager.ExecuteMetadataProviders(this, cancellationToken, forceRefresh, allowSlowProviders).ConfigureAwait(false);
  327. if (changed || forceSave)
  328. {
  329. cancellationToken.ThrowIfCancellationRequested();
  330. await UserManager.UpdateUser(this).ConfigureAwait(false);
  331. }
  332. return changed;
  333. }
  334. /// <summary>
  335. /// Updates the configuration.
  336. /// </summary>
  337. /// <param name="config">The config.</param>
  338. /// <param name="serializer">The serializer.</param>
  339. /// <exception cref="System.ArgumentNullException">config</exception>
  340. public void UpdateConfiguration(UserConfiguration config, IXmlSerializer serializer)
  341. {
  342. if (config == null)
  343. {
  344. throw new ArgumentNullException("config");
  345. }
  346. var customLibraryChanged = config.UseCustomLibrary != Configuration.UseCustomLibrary;
  347. Configuration = config;
  348. SaveConfiguration(serializer);
  349. // Force these to be lazy loaded again
  350. if (customLibraryChanged)
  351. {
  352. _rootFolderPath = null;
  353. RootFolder = null;
  354. }
  355. }
  356. }
  357. }