BaseItem.cs 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Controller.Channels;
  3. using MediaBrowser.Controller.Collections;
  4. using MediaBrowser.Controller.Configuration;
  5. using MediaBrowser.Controller.Drawing;
  6. using MediaBrowser.Controller.Library;
  7. using MediaBrowser.Controller.LiveTv;
  8. using MediaBrowser.Controller.Localization;
  9. using MediaBrowser.Controller.Persistence;
  10. using MediaBrowser.Controller.Providers;
  11. using MediaBrowser.Model.Configuration;
  12. using MediaBrowser.Model.Dto;
  13. using MediaBrowser.Model.Entities;
  14. using MediaBrowser.Model.Library;
  15. using MediaBrowser.Model.Logging;
  16. using MediaBrowser.Model.Users;
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Globalization;
  20. using System.IO;
  21. using System.Linq;
  22. using System.Runtime.Serialization;
  23. using System.Text;
  24. using System.Threading;
  25. using System.Threading.Tasks;
  26. using CommonIO;
  27. using MediaBrowser.Controller.Sorting;
  28. using MediaBrowser.Model.LiveTv;
  29. using MediaBrowser.Model.Providers;
  30. namespace MediaBrowser.Controller.Entities
  31. {
  32. /// <summary>
  33. /// Class BaseItem
  34. /// </summary>
  35. public abstract class BaseItem : IHasProviderIds, ILibraryItem, IHasImages, IHasUserData, IHasMetadata, IHasLookupInfo<ItemLookupInfo>
  36. {
  37. protected BaseItem()
  38. {
  39. Keywords = new List<string>();
  40. Tags = new List<string>();
  41. Genres = new List<string>();
  42. Studios = new List<string>();
  43. ProviderIds = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  44. LockedFields = new List<MetadataFields>();
  45. ImageInfos = new List<ItemImageInfo>();
  46. }
  47. public static readonly char[] SlugReplaceChars = { '?', '/', '&' };
  48. public static char SlugChar = '-';
  49. /// <summary>
  50. /// The supported image extensions
  51. /// </summary>
  52. public static readonly string[] SupportedImageExtensions = { ".png", ".jpg", ".jpeg", ".tbn", ".gif" };
  53. public static readonly List<string> SupportedImageExtensionsList = SupportedImageExtensions.ToList();
  54. /// <summary>
  55. /// The trailer folder name
  56. /// </summary>
  57. public static string TrailerFolderName = "trailers";
  58. public static string ThemeSongsFolderName = "theme-music";
  59. public static string ThemeSongFilename = "theme";
  60. public static string ThemeVideosFolderName = "backdrops";
  61. [IgnoreDataMember]
  62. public string PreferredMetadataCountryCode { get; set; }
  63. [IgnoreDataMember]
  64. public string PreferredMetadataLanguage { get; set; }
  65. public List<ItemImageInfo> ImageInfos { get; set; }
  66. [IgnoreDataMember]
  67. public bool IsVirtualItem { get; set; }
  68. /// <summary>
  69. /// Gets or sets the album.
  70. /// </summary>
  71. /// <value>The album.</value>
  72. [IgnoreDataMember]
  73. public string Album { get; set; }
  74. /// <summary>
  75. /// Gets or sets the channel identifier.
  76. /// </summary>
  77. /// <value>The channel identifier.</value>
  78. [IgnoreDataMember]
  79. public string ChannelId { get; set; }
  80. [IgnoreDataMember]
  81. public virtual bool SupportsAddingToPlaylist
  82. {
  83. get
  84. {
  85. return false;
  86. }
  87. }
  88. [IgnoreDataMember]
  89. public virtual bool AlwaysScanInternalMetadataPath
  90. {
  91. get { return false; }
  92. }
  93. /// <summary>
  94. /// Gets a value indicating whether this instance is in mixed folder.
  95. /// </summary>
  96. /// <value><c>true</c> if this instance is in mixed folder; otherwise, <c>false</c>.</value>
  97. [IgnoreDataMember]
  98. public bool IsInMixedFolder { get; set; }
  99. [IgnoreDataMember]
  100. public virtual bool SupportsRemoteImageDownloading
  101. {
  102. get
  103. {
  104. return true;
  105. }
  106. }
  107. private string _name;
  108. /// <summary>
  109. /// Gets or sets the name.
  110. /// </summary>
  111. /// <value>The name.</value>
  112. [IgnoreDataMember]
  113. public virtual string Name
  114. {
  115. get
  116. {
  117. return _name;
  118. }
  119. set
  120. {
  121. _name = value;
  122. // lazy load this again
  123. _sortName = null;
  124. }
  125. }
  126. [IgnoreDataMember]
  127. public string SlugName
  128. {
  129. get
  130. {
  131. var name = Name;
  132. if (string.IsNullOrWhiteSpace(name))
  133. {
  134. return string.Empty;
  135. }
  136. return SlugReplaceChars.Aggregate(name, (current, c) => current.Replace(c, SlugChar));
  137. }
  138. }
  139. [IgnoreDataMember]
  140. public bool IsUnaired
  141. {
  142. get { return PremiereDate.HasValue && PremiereDate.Value.ToLocalTime().Date >= DateTime.Now.Date; }
  143. }
  144. public string OriginalTitle { get; set; }
  145. /// <summary>
  146. /// Gets or sets the id.
  147. /// </summary>
  148. /// <value>The id.</value>
  149. [IgnoreDataMember]
  150. public Guid Id { get; set; }
  151. /// <summary>
  152. /// Gets or sets a value indicating whether this instance is hd.
  153. /// </summary>
  154. /// <value><c>true</c> if this instance is hd; otherwise, <c>false</c>.</value>
  155. [IgnoreDataMember]
  156. public bool? IsHD { get; set; }
  157. /// <summary>
  158. /// Gets or sets the audio.
  159. /// </summary>
  160. /// <value>The audio.</value>
  161. [IgnoreDataMember]
  162. public ProgramAudio? Audio { get; set; }
  163. /// <summary>
  164. /// Return the id that should be used to key display prefs for this item.
  165. /// Default is based on the type for everything except actual generic folders.
  166. /// </summary>
  167. /// <value>The display prefs id.</value>
  168. [IgnoreDataMember]
  169. public virtual Guid DisplayPreferencesId
  170. {
  171. get
  172. {
  173. var thisType = GetType();
  174. return thisType == typeof(Folder) ? Id : thisType.FullName.GetMD5();
  175. }
  176. }
  177. /// <summary>
  178. /// Gets or sets the path.
  179. /// </summary>
  180. /// <value>The path.</value>
  181. [IgnoreDataMember]
  182. public virtual string Path { get; set; }
  183. [IgnoreDataMember]
  184. public bool IsOffline { get; set; }
  185. [IgnoreDataMember]
  186. public virtual SourceType SourceType { get; set; }
  187. /// <summary>
  188. /// Returns the folder containing the item.
  189. /// If the item is a folder, it returns the folder itself
  190. /// </summary>
  191. [IgnoreDataMember]
  192. public virtual string ContainingFolderPath
  193. {
  194. get
  195. {
  196. if (IsFolder)
  197. {
  198. return Path;
  199. }
  200. return System.IO.Path.GetDirectoryName(Path);
  201. }
  202. }
  203. /// <summary>
  204. /// Gets or sets the name of the service.
  205. /// </summary>
  206. /// <value>The name of the service.</value>
  207. [IgnoreDataMember]
  208. public string ServiceName { get; set; }
  209. /// <summary>
  210. /// If this content came from an external service, the id of the content on that service
  211. /// </summary>
  212. [IgnoreDataMember]
  213. public string ExternalId
  214. {
  215. get { return this.GetProviderId("ProviderExternalId"); }
  216. set
  217. {
  218. this.SetProviderId("ProviderExternalId", value);
  219. }
  220. }
  221. /// <summary>
  222. /// Gets or sets the etag.
  223. /// </summary>
  224. /// <value>The etag.</value>
  225. [IgnoreDataMember]
  226. public string ExternalEtag { get; set; }
  227. [IgnoreDataMember]
  228. public virtual bool IsHidden
  229. {
  230. get
  231. {
  232. return false;
  233. }
  234. }
  235. [IgnoreDataMember]
  236. public virtual bool IsOwnedItem
  237. {
  238. get
  239. {
  240. // Local trailer, special feature, theme video, etc.
  241. // An item that belongs to another item but is not part of the Parent-Child tree
  242. return !IsFolder && ParentId == Guid.Empty && LocationType == LocationType.FileSystem;
  243. }
  244. }
  245. /// <summary>
  246. /// Gets or sets the type of the location.
  247. /// </summary>
  248. /// <value>The type of the location.</value>
  249. [IgnoreDataMember]
  250. public virtual LocationType LocationType
  251. {
  252. get
  253. {
  254. if (IsOffline)
  255. {
  256. return LocationType.Offline;
  257. }
  258. if (string.IsNullOrWhiteSpace(Path))
  259. {
  260. if (SourceType == SourceType.Channel)
  261. {
  262. return LocationType.Remote;
  263. }
  264. return LocationType.Virtual;
  265. }
  266. return FileSystem.IsPathFile(Path) ? LocationType.FileSystem : LocationType.Remote;
  267. }
  268. }
  269. [IgnoreDataMember]
  270. public virtual bool SupportsLocalMetadata
  271. {
  272. get
  273. {
  274. if (SourceType == SourceType.Channel)
  275. {
  276. return false;
  277. }
  278. var locationType = LocationType;
  279. return locationType != LocationType.Remote && locationType != LocationType.Virtual;
  280. }
  281. }
  282. [IgnoreDataMember]
  283. public virtual string FileNameWithoutExtension
  284. {
  285. get
  286. {
  287. if (LocationType == LocationType.FileSystem)
  288. {
  289. return System.IO.Path.GetFileNameWithoutExtension(Path);
  290. }
  291. return null;
  292. }
  293. }
  294. [IgnoreDataMember]
  295. public virtual bool EnableAlphaNumericSorting
  296. {
  297. get
  298. {
  299. return true;
  300. }
  301. }
  302. private List<Tuple<StringBuilder, bool>> GetSortChunks(string s1)
  303. {
  304. var list = new List<Tuple<StringBuilder, bool>>();
  305. int thisMarker = 0, thisNumericChunk = 0;
  306. while (thisMarker < s1.Length)
  307. {
  308. if (thisMarker >= s1.Length)
  309. {
  310. break;
  311. }
  312. char thisCh = s1[thisMarker];
  313. StringBuilder thisChunk = new StringBuilder();
  314. while ((thisMarker < s1.Length) && (thisChunk.Length == 0 || SortHelper.InChunk(thisCh, thisChunk[0])))
  315. {
  316. thisChunk.Append(thisCh);
  317. thisMarker++;
  318. if (thisMarker < s1.Length)
  319. {
  320. thisCh = s1[thisMarker];
  321. }
  322. }
  323. var isNumeric = thisChunk.Length > 0 && char.IsDigit(thisChunk[0]);
  324. list.Add(new Tuple<StringBuilder, bool>(thisChunk, isNumeric));
  325. }
  326. return list;
  327. }
  328. /// <summary>
  329. /// This is just a helper for convenience
  330. /// </summary>
  331. /// <value>The primary image path.</value>
  332. [IgnoreDataMember]
  333. public string PrimaryImagePath
  334. {
  335. get { return this.GetImagePath(ImageType.Primary); }
  336. }
  337. public virtual bool IsInternetMetadataEnabled()
  338. {
  339. return ConfigurationManager.Configuration.EnableInternetProviders;
  340. }
  341. public virtual bool CanDelete()
  342. {
  343. if (SourceType == SourceType.Channel)
  344. {
  345. return false;
  346. }
  347. var locationType = LocationType;
  348. return locationType != LocationType.Remote &&
  349. locationType != LocationType.Virtual;
  350. }
  351. public virtual bool IsAuthorizedToDelete(User user)
  352. {
  353. return user.Policy.EnableContentDeletion;
  354. }
  355. public bool CanDelete(User user)
  356. {
  357. return CanDelete() && IsAuthorizedToDelete(user);
  358. }
  359. public virtual bool CanDownload()
  360. {
  361. return false;
  362. }
  363. public virtual bool IsAuthorizedToDownload(User user)
  364. {
  365. return user.Policy.EnableContentDownloading;
  366. }
  367. public bool CanDownload(User user)
  368. {
  369. return CanDownload() && IsAuthorizedToDownload(user);
  370. }
  371. /// <summary>
  372. /// Gets or sets the date created.
  373. /// </summary>
  374. /// <value>The date created.</value>
  375. [IgnoreDataMember]
  376. public DateTime DateCreated { get; set; }
  377. /// <summary>
  378. /// Gets or sets the date modified.
  379. /// </summary>
  380. /// <value>The date modified.</value>
  381. [IgnoreDataMember]
  382. public DateTime DateModified { get; set; }
  383. [IgnoreDataMember]
  384. public DateTime DateLastSaved { get; set; }
  385. [IgnoreDataMember]
  386. public DateTime DateLastRefreshed { get; set; }
  387. [IgnoreDataMember]
  388. public DateTime? DateModifiedDuringLastRefresh { get; set; }
  389. /// <summary>
  390. /// The logger
  391. /// </summary>
  392. public static ILogger Logger { get; set; }
  393. public static ILibraryManager LibraryManager { get; set; }
  394. public static IServerConfigurationManager ConfigurationManager { get; set; }
  395. public static IProviderManager ProviderManager { get; set; }
  396. public static ILocalizationManager LocalizationManager { get; set; }
  397. public static IItemRepository ItemRepository { get; set; }
  398. public static IFileSystem FileSystem { get; set; }
  399. public static IUserDataManager UserDataManager { get; set; }
  400. public static ILiveTvManager LiveTvManager { get; set; }
  401. public static IChannelManager ChannelManager { get; set; }
  402. public static ICollectionManager CollectionManager { get; set; }
  403. public static IImageProcessor ImageProcessor { get; set; }
  404. public static IMediaSourceManager MediaSourceManager { get; set; }
  405. /// <summary>
  406. /// Returns a <see cref="System.String" /> that represents this instance.
  407. /// </summary>
  408. /// <returns>A <see cref="System.String" /> that represents this instance.</returns>
  409. public override string ToString()
  410. {
  411. return Name;
  412. }
  413. [IgnoreDataMember]
  414. public bool IsLocked { get; set; }
  415. /// <summary>
  416. /// Gets or sets the locked fields.
  417. /// </summary>
  418. /// <value>The locked fields.</value>
  419. [IgnoreDataMember]
  420. public List<MetadataFields> LockedFields { get; set; }
  421. /// <summary>
  422. /// Gets the type of the media.
  423. /// </summary>
  424. /// <value>The type of the media.</value>
  425. [IgnoreDataMember]
  426. public virtual string MediaType
  427. {
  428. get
  429. {
  430. return null;
  431. }
  432. }
  433. [IgnoreDataMember]
  434. public virtual IEnumerable<string> PhysicalLocations
  435. {
  436. get
  437. {
  438. var locationType = LocationType;
  439. if (locationType == LocationType.Remote || locationType == LocationType.Virtual)
  440. {
  441. return new string[] { };
  442. }
  443. return new[] { Path };
  444. }
  445. }
  446. private string _forcedSortName;
  447. /// <summary>
  448. /// Gets or sets the name of the forced sort.
  449. /// </summary>
  450. /// <value>The name of the forced sort.</value>
  451. [IgnoreDataMember]
  452. public string ForcedSortName
  453. {
  454. get { return _forcedSortName; }
  455. set { _forcedSortName = value; _sortName = null; }
  456. }
  457. private string _sortName;
  458. /// <summary>
  459. /// Gets the name of the sort.
  460. /// </summary>
  461. /// <value>The name of the sort.</value>
  462. [IgnoreDataMember]
  463. public string SortName
  464. {
  465. get
  466. {
  467. if (_sortName == null)
  468. {
  469. if (!string.IsNullOrWhiteSpace(ForcedSortName))
  470. {
  471. // Need the ToLower because that's what CreateSortName does
  472. _sortName = ModifySortChunks(ForcedSortName).ToLower();
  473. }
  474. else
  475. {
  476. _sortName = CreateSortName();
  477. }
  478. }
  479. return _sortName;
  480. }
  481. set
  482. {
  483. _sortName = value;
  484. }
  485. }
  486. public string GetInternalMetadataPath()
  487. {
  488. var basePath = ConfigurationManager.ApplicationPaths.InternalMetadataPath;
  489. return GetInternalMetadataPath(basePath);
  490. }
  491. protected virtual string GetInternalMetadataPath(string basePath)
  492. {
  493. if (SourceType == SourceType.Channel)
  494. {
  495. return System.IO.Path.Combine(basePath, "channels", ChannelId, Id.ToString("N"));
  496. }
  497. var idString = Id.ToString("N");
  498. basePath = System.IO.Path.Combine(basePath, "library");
  499. return System.IO.Path.Combine(basePath, idString.Substring(0, 2), idString);
  500. }
  501. /// <summary>
  502. /// Creates the name of the sort.
  503. /// </summary>
  504. /// <returns>System.String.</returns>
  505. protected virtual string CreateSortName()
  506. {
  507. if (Name == null) return null; //some items may not have name filled in properly
  508. if (!EnableAlphaNumericSorting)
  509. {
  510. return Name.TrimStart();
  511. }
  512. var sortable = Name.Trim().ToLower();
  513. sortable = ConfigurationManager.Configuration.SortRemoveCharacters.Aggregate(sortable, (current, search) => current.Replace(search.ToLower(), string.Empty));
  514. sortable = ConfigurationManager.Configuration.SortReplaceCharacters.Aggregate(sortable, (current, search) => current.Replace(search.ToLower(), " "));
  515. foreach (var search in ConfigurationManager.Configuration.SortRemoveWords)
  516. {
  517. var searchLower = search.ToLower();
  518. // Remove from beginning if a space follows
  519. if (sortable.StartsWith(searchLower + " "))
  520. {
  521. sortable = sortable.Remove(0, searchLower.Length + 1);
  522. }
  523. // Remove from middle if surrounded by spaces
  524. sortable = sortable.Replace(" " + searchLower + " ", " ");
  525. // Remove from end if followed by a space
  526. if (sortable.EndsWith(" " + searchLower))
  527. {
  528. sortable = sortable.Remove(sortable.Length - (searchLower.Length + 1));
  529. }
  530. }
  531. return ModifySortChunks(sortable);
  532. }
  533. private string ModifySortChunks(string name)
  534. {
  535. var chunks = GetSortChunks(name);
  536. var builder = new StringBuilder();
  537. foreach (var chunk in chunks)
  538. {
  539. var chunkBuilder = chunk.Item1;
  540. // This chunk is numeric
  541. if (chunk.Item2)
  542. {
  543. while (chunkBuilder.Length < 10)
  544. {
  545. chunkBuilder.Insert(0, '0');
  546. }
  547. }
  548. builder.Append(chunkBuilder);
  549. }
  550. //Logger.Debug("ModifySortChunks Start: {0} End: {1}", name, builder.ToString());
  551. return builder.ToString().RemoveDiacritics();
  552. }
  553. [IgnoreDataMember]
  554. public Guid ParentId { get; set; }
  555. /// <summary>
  556. /// Gets or sets the parent.
  557. /// </summary>
  558. /// <value>The parent.</value>
  559. [IgnoreDataMember]
  560. public Folder Parent
  561. {
  562. get { return GetParent() as Folder; }
  563. set
  564. {
  565. }
  566. }
  567. public void SetParent(Folder parent)
  568. {
  569. ParentId = parent == null ? Guid.Empty : parent.Id;
  570. }
  571. [IgnoreDataMember]
  572. public IEnumerable<Folder> Parents
  573. {
  574. get { return GetParents().OfType<Folder>(); }
  575. }
  576. public BaseItem GetParent()
  577. {
  578. if (ParentId != Guid.Empty)
  579. {
  580. return LibraryManager.GetItemById(ParentId);
  581. }
  582. return null;
  583. }
  584. public IEnumerable<BaseItem> GetParents()
  585. {
  586. var parent = GetParent();
  587. while (parent != null)
  588. {
  589. yield return parent;
  590. parent = parent.GetParent();
  591. }
  592. }
  593. /// <summary>
  594. /// Finds a parent of a given type
  595. /// </summary>
  596. /// <typeparam name="T"></typeparam>
  597. /// <returns>``0.</returns>
  598. public T FindParent<T>()
  599. where T : Folder
  600. {
  601. return GetParents().OfType<T>().FirstOrDefault();
  602. }
  603. [IgnoreDataMember]
  604. public virtual Guid? DisplayParentId
  605. {
  606. get
  607. {
  608. if (ParentId == Guid.Empty)
  609. {
  610. return null;
  611. }
  612. return ParentId;
  613. }
  614. }
  615. [IgnoreDataMember]
  616. public BaseItem DisplayParent
  617. {
  618. get
  619. {
  620. var id = DisplayParentId;
  621. if (!id.HasValue || id.Value == Guid.Empty)
  622. {
  623. return null;
  624. }
  625. return LibraryManager.GetItemById(id.Value);
  626. }
  627. }
  628. /// <summary>
  629. /// When the item first debuted. For movies this could be premiere date, episodes would be first aired
  630. /// </summary>
  631. /// <value>The premiere date.</value>
  632. [IgnoreDataMember]
  633. public DateTime? PremiereDate { get; set; }
  634. /// <summary>
  635. /// Gets or sets the end date.
  636. /// </summary>
  637. /// <value>The end date.</value>
  638. [IgnoreDataMember]
  639. public DateTime? EndDate { get; set; }
  640. /// <summary>
  641. /// Gets or sets the display type of the media.
  642. /// </summary>
  643. /// <value>The display type of the media.</value>
  644. [IgnoreDataMember]
  645. public string DisplayMediaType { get; set; }
  646. /// <summary>
  647. /// Gets or sets the official rating.
  648. /// </summary>
  649. /// <value>The official rating.</value>
  650. [IgnoreDataMember]
  651. public string OfficialRating { get; set; }
  652. /// <summary>
  653. /// Gets or sets the critic rating.
  654. /// </summary>
  655. /// <value>The critic rating.</value>
  656. [IgnoreDataMember]
  657. public float? CriticRating { get; set; }
  658. /// <summary>
  659. /// Gets or sets the critic rating summary.
  660. /// </summary>
  661. /// <value>The critic rating summary.</value>
  662. [IgnoreDataMember]
  663. public string CriticRatingSummary { get; set; }
  664. /// <summary>
  665. /// Gets or sets the official rating description.
  666. /// </summary>
  667. /// <value>The official rating description.</value>
  668. [IgnoreDataMember]
  669. public string OfficialRatingDescription { get; set; }
  670. /// <summary>
  671. /// Gets or sets the custom rating.
  672. /// </summary>
  673. /// <value>The custom rating.</value>
  674. [IgnoreDataMember]
  675. public string CustomRating { get; set; }
  676. /// <summary>
  677. /// Gets or sets the overview.
  678. /// </summary>
  679. /// <value>The overview.</value>
  680. [IgnoreDataMember]
  681. public string Overview { get; set; }
  682. /// <summary>
  683. /// Gets or sets the studios.
  684. /// </summary>
  685. /// <value>The studios.</value>
  686. [IgnoreDataMember]
  687. public List<string> Studios { get; set; }
  688. /// <summary>
  689. /// Gets or sets the genres.
  690. /// </summary>
  691. /// <value>The genres.</value>
  692. [IgnoreDataMember]
  693. public List<string> Genres { get; set; }
  694. /// <summary>
  695. /// Gets or sets the tags.
  696. /// </summary>
  697. /// <value>The tags.</value>
  698. [IgnoreDataMember]
  699. public List<string> Tags { get; set; }
  700. public List<string> Keywords { get; set; }
  701. /// <summary>
  702. /// Gets or sets the home page URL.
  703. /// </summary>
  704. /// <value>The home page URL.</value>
  705. [IgnoreDataMember]
  706. public string HomePageUrl { get; set; }
  707. /// <summary>
  708. /// Gets or sets the community rating.
  709. /// </summary>
  710. /// <value>The community rating.</value>
  711. [IgnoreDataMember]
  712. public float? CommunityRating { get; set; }
  713. /// <summary>
  714. /// Gets or sets the community rating vote count.
  715. /// </summary>
  716. /// <value>The community rating vote count.</value>
  717. [IgnoreDataMember]
  718. public int? VoteCount { get; set; }
  719. /// <summary>
  720. /// Gets or sets the run time ticks.
  721. /// </summary>
  722. /// <value>The run time ticks.</value>
  723. [IgnoreDataMember]
  724. public long? RunTimeTicks { get; set; }
  725. /// <summary>
  726. /// Gets or sets the production year.
  727. /// </summary>
  728. /// <value>The production year.</value>
  729. [IgnoreDataMember]
  730. public int? ProductionYear { get; set; }
  731. /// <summary>
  732. /// If the item is part of a series, this is it's number in the series.
  733. /// This could be episode number, album track number, etc.
  734. /// </summary>
  735. /// <value>The index number.</value>
  736. [IgnoreDataMember]
  737. public int? IndexNumber { get; set; }
  738. /// <summary>
  739. /// For an episode this could be the season number, or for a song this could be the disc number.
  740. /// </summary>
  741. /// <value>The parent index number.</value>
  742. [IgnoreDataMember]
  743. public int? ParentIndexNumber { get; set; }
  744. [IgnoreDataMember]
  745. public string OfficialRatingForComparison
  746. {
  747. get
  748. {
  749. if (!string.IsNullOrWhiteSpace(OfficialRating))
  750. {
  751. return OfficialRating;
  752. }
  753. var parent = DisplayParent;
  754. if (parent != null)
  755. {
  756. return parent.OfficialRatingForComparison;
  757. }
  758. return null;
  759. }
  760. }
  761. [IgnoreDataMember]
  762. public string CustomRatingForComparison
  763. {
  764. get
  765. {
  766. if (!string.IsNullOrWhiteSpace(CustomRating))
  767. {
  768. return CustomRating;
  769. }
  770. var parent = DisplayParent;
  771. if (parent != null)
  772. {
  773. return parent.CustomRatingForComparison;
  774. }
  775. return null;
  776. }
  777. }
  778. /// <summary>
  779. /// Gets the play access.
  780. /// </summary>
  781. /// <param name="user">The user.</param>
  782. /// <returns>PlayAccess.</returns>
  783. public PlayAccess GetPlayAccess(User user)
  784. {
  785. if (!user.Policy.EnableMediaPlayback)
  786. {
  787. return PlayAccess.None;
  788. }
  789. //if (!user.IsParentalScheduleAllowed())
  790. //{
  791. // return PlayAccess.None;
  792. //}
  793. return PlayAccess.Full;
  794. }
  795. /// <summary>
  796. /// Loads the theme songs.
  797. /// </summary>
  798. /// <returns>List{Audio.Audio}.</returns>
  799. private IEnumerable<Audio.Audio> LoadThemeSongs(List<FileSystemMetadata> fileSystemChildren, IDirectoryService directoryService)
  800. {
  801. var files = fileSystemChildren.Where(i => i.IsDirectory)
  802. .Where(i => string.Equals(i.Name, ThemeSongsFolderName, StringComparison.OrdinalIgnoreCase))
  803. .SelectMany(i => directoryService.GetFiles(i.FullName))
  804. .ToList();
  805. // Support plex/xbmc convention
  806. files.AddRange(fileSystemChildren
  807. .Where(i => !i.IsDirectory && string.Equals(FileSystem.GetFileNameWithoutExtension(i), ThemeSongFilename, StringComparison.OrdinalIgnoreCase))
  808. );
  809. return LibraryManager.ResolvePaths(files, directoryService, null)
  810. .OfType<Audio.Audio>()
  811. .Select(audio =>
  812. {
  813. // Try to retrieve it from the db. If we don't find it, use the resolved version
  814. var dbItem = LibraryManager.GetItemById(audio.Id) as Audio.Audio;
  815. if (dbItem != null)
  816. {
  817. audio = dbItem;
  818. }
  819. audio.ExtraType = ExtraType.ThemeSong;
  820. return audio;
  821. // Sort them so that the list can be easily compared for changes
  822. }).OrderBy(i => i.Path).ToList();
  823. }
  824. /// <summary>
  825. /// Loads the video backdrops.
  826. /// </summary>
  827. /// <returns>List{Video}.</returns>
  828. private IEnumerable<Video> LoadThemeVideos(IEnumerable<FileSystemMetadata> fileSystemChildren, IDirectoryService directoryService)
  829. {
  830. var files = fileSystemChildren.Where(i => i.IsDirectory)
  831. .Where(i => string.Equals(i.Name, ThemeVideosFolderName, StringComparison.OrdinalIgnoreCase))
  832. .SelectMany(i => directoryService.GetFiles(i.FullName));
  833. return LibraryManager.ResolvePaths(files, directoryService, null)
  834. .OfType<Video>()
  835. .Select(item =>
  836. {
  837. // Try to retrieve it from the db. If we don't find it, use the resolved version
  838. var dbItem = LibraryManager.GetItemById(item.Id) as Video;
  839. if (dbItem != null)
  840. {
  841. item = dbItem;
  842. }
  843. item.ExtraType = ExtraType.ThemeVideo;
  844. return item;
  845. // Sort them so that the list can be easily compared for changes
  846. }).OrderBy(i => i.Path).ToList();
  847. }
  848. public Task RefreshMetadata(CancellationToken cancellationToken)
  849. {
  850. return RefreshMetadata(new MetadataRefreshOptions(new DirectoryService(FileSystem)), cancellationToken);
  851. }
  852. /// <summary>
  853. /// Overrides the base implementation to refresh metadata for local trailers
  854. /// </summary>
  855. /// <param name="options">The options.</param>
  856. /// <param name="cancellationToken">The cancellation token.</param>
  857. /// <returns>true if a provider reports we changed</returns>
  858. public async Task<ItemUpdateType> RefreshMetadata(MetadataRefreshOptions options, CancellationToken cancellationToken)
  859. {
  860. var locationType = LocationType;
  861. var requiresSave = false;
  862. if (SupportsOwnedItems)
  863. {
  864. try
  865. {
  866. var files = locationType != LocationType.Remote && locationType != LocationType.Virtual ?
  867. GetFileSystemChildren(options.DirectoryService).ToList() :
  868. new List<FileSystemMetadata>();
  869. var ownedItemsChanged = await RefreshedOwnedItems(options, files, cancellationToken).ConfigureAwait(false);
  870. if (ownedItemsChanged)
  871. {
  872. requiresSave = true;
  873. }
  874. }
  875. catch (Exception ex)
  876. {
  877. Logger.ErrorException("Error refreshing owned items for {0}", ex, Path ?? Name);
  878. }
  879. }
  880. var refreshOptions = requiresSave
  881. ? new MetadataRefreshOptions(options)
  882. {
  883. ForceSave = true
  884. }
  885. : options;
  886. return await ProviderManager.RefreshSingleItem(this, refreshOptions, cancellationToken).ConfigureAwait(false);
  887. }
  888. [IgnoreDataMember]
  889. protected virtual bool SupportsOwnedItems
  890. {
  891. get { return IsFolder || GetParent() != null; }
  892. }
  893. [IgnoreDataMember]
  894. public virtual bool SupportsPeople
  895. {
  896. get { return true; }
  897. }
  898. /// <summary>
  899. /// Refreshes owned items such as trailers, theme videos, special features, etc.
  900. /// Returns true or false indicating if changes were found.
  901. /// </summary>
  902. /// <param name="options"></param>
  903. /// <param name="fileSystemChildren"></param>
  904. /// <param name="cancellationToken"></param>
  905. /// <returns></returns>
  906. protected virtual async Task<bool> RefreshedOwnedItems(MetadataRefreshOptions options, List<FileSystemMetadata> fileSystemChildren, CancellationToken cancellationToken)
  907. {
  908. var themeSongsChanged = false;
  909. var themeVideosChanged = false;
  910. var localTrailersChanged = false;
  911. if (LocationType == LocationType.FileSystem && GetParent() != null)
  912. {
  913. var hasThemeMedia = this as IHasThemeMedia;
  914. if (hasThemeMedia != null)
  915. {
  916. if (!IsInMixedFolder)
  917. {
  918. themeSongsChanged = await RefreshThemeSongs(hasThemeMedia, options, fileSystemChildren, cancellationToken).ConfigureAwait(false);
  919. themeVideosChanged = await RefreshThemeVideos(hasThemeMedia, options, fileSystemChildren, cancellationToken).ConfigureAwait(false);
  920. }
  921. }
  922. var hasTrailers = this as IHasTrailers;
  923. if (hasTrailers != null)
  924. {
  925. localTrailersChanged = await RefreshLocalTrailers(hasTrailers, options, fileSystemChildren, cancellationToken).ConfigureAwait(false);
  926. }
  927. }
  928. return themeSongsChanged || themeVideosChanged || localTrailersChanged;
  929. }
  930. protected virtual IEnumerable<FileSystemMetadata> GetFileSystemChildren(IDirectoryService directoryService)
  931. {
  932. var path = ContainingFolderPath;
  933. return directoryService.GetFileSystemEntries(path);
  934. }
  935. private async Task<bool> RefreshLocalTrailers(IHasTrailers item, MetadataRefreshOptions options, List<FileSystemMetadata> fileSystemChildren, CancellationToken cancellationToken)
  936. {
  937. var newItems = LibraryManager.FindTrailers(this, fileSystemChildren, options.DirectoryService).ToList();
  938. var newItemIds = newItems.Select(i => i.Id).ToList();
  939. var itemsChanged = !item.LocalTrailerIds.SequenceEqual(newItemIds);
  940. var tasks = newItems.Select(i => i.RefreshMetadata(options, cancellationToken));
  941. await Task.WhenAll(tasks).ConfigureAwait(false);
  942. item.LocalTrailerIds = newItemIds;
  943. return itemsChanged;
  944. }
  945. private async Task<bool> RefreshThemeVideos(IHasThemeMedia item, MetadataRefreshOptions options, IEnumerable<FileSystemMetadata> fileSystemChildren, CancellationToken cancellationToken)
  946. {
  947. var newThemeVideos = LoadThemeVideos(fileSystemChildren, options.DirectoryService).ToList();
  948. var newThemeVideoIds = newThemeVideos.Select(i => i.Id).ToList();
  949. var themeVideosChanged = !item.ThemeVideoIds.SequenceEqual(newThemeVideoIds);
  950. var tasks = newThemeVideos.Select(i =>
  951. {
  952. var subOptions = new MetadataRefreshOptions(options);
  953. if (!i.IsThemeMedia)
  954. {
  955. i.ExtraType = ExtraType.ThemeVideo;
  956. subOptions.ForceSave = true;
  957. }
  958. return i.RefreshMetadata(subOptions, cancellationToken);
  959. });
  960. await Task.WhenAll(tasks).ConfigureAwait(false);
  961. item.ThemeVideoIds = newThemeVideoIds;
  962. return themeVideosChanged;
  963. }
  964. /// <summary>
  965. /// Refreshes the theme songs.
  966. /// </summary>
  967. private async Task<bool> RefreshThemeSongs(IHasThemeMedia item, MetadataRefreshOptions options, List<FileSystemMetadata> fileSystemChildren, CancellationToken cancellationToken)
  968. {
  969. var newThemeSongs = LoadThemeSongs(fileSystemChildren, options.DirectoryService).ToList();
  970. var newThemeSongIds = newThemeSongs.Select(i => i.Id).ToList();
  971. var themeSongsChanged = !item.ThemeSongIds.SequenceEqual(newThemeSongIds);
  972. var tasks = newThemeSongs.Select(i =>
  973. {
  974. var subOptions = new MetadataRefreshOptions(options);
  975. if (!i.IsThemeMedia)
  976. {
  977. i.ExtraType = ExtraType.ThemeSong;
  978. subOptions.ForceSave = true;
  979. }
  980. return i.RefreshMetadata(subOptions, cancellationToken);
  981. });
  982. await Task.WhenAll(tasks).ConfigureAwait(false);
  983. item.ThemeSongIds = newThemeSongIds;
  984. return themeSongsChanged;
  985. }
  986. /// <summary>
  987. /// Gets or sets the provider ids.
  988. /// </summary>
  989. /// <value>The provider ids.</value>
  990. public Dictionary<string, string> ProviderIds { get; set; }
  991. [IgnoreDataMember]
  992. public virtual Folder LatestItemsIndexContainer
  993. {
  994. get { return null; }
  995. }
  996. [IgnoreDataMember]
  997. public virtual string PresentationUniqueKey
  998. {
  999. get { return Id.ToString("N"); }
  1000. }
  1001. public virtual bool RequiresRefresh()
  1002. {
  1003. return false;
  1004. }
  1005. public virtual List<string> GetUserDataKeys()
  1006. {
  1007. var list = new List<string>();
  1008. if (SourceType == SourceType.Channel)
  1009. {
  1010. if (!string.IsNullOrWhiteSpace(ExternalId))
  1011. {
  1012. list.Add(ExternalId);
  1013. }
  1014. }
  1015. list.Add(Id.ToString());
  1016. return list;
  1017. }
  1018. internal virtual bool IsValidFromResolver(BaseItem newItem)
  1019. {
  1020. var current = this;
  1021. return current.IsInMixedFolder == newItem.IsInMixedFolder;
  1022. }
  1023. public void AfterMetadataRefresh()
  1024. {
  1025. _sortName = null;
  1026. }
  1027. /// <summary>
  1028. /// Gets the preferred metadata language.
  1029. /// </summary>
  1030. /// <returns>System.String.</returns>
  1031. public string GetPreferredMetadataLanguage()
  1032. {
  1033. string lang = PreferredMetadataLanguage;
  1034. if (string.IsNullOrWhiteSpace(lang))
  1035. {
  1036. lang = GetParents()
  1037. .Select(i => i.PreferredMetadataLanguage)
  1038. .FirstOrDefault(i => !string.IsNullOrWhiteSpace(i));
  1039. }
  1040. if (string.IsNullOrWhiteSpace(lang))
  1041. {
  1042. lang = LibraryManager.GetCollectionFolders(this)
  1043. .Select(i => i.PreferredMetadataLanguage)
  1044. .FirstOrDefault(i => !string.IsNullOrWhiteSpace(i));
  1045. }
  1046. if (string.IsNullOrWhiteSpace(lang))
  1047. {
  1048. lang = ConfigurationManager.Configuration.PreferredMetadataLanguage;
  1049. }
  1050. return lang;
  1051. }
  1052. /// <summary>
  1053. /// Gets the preferred metadata language.
  1054. /// </summary>
  1055. /// <returns>System.String.</returns>
  1056. public string GetPreferredMetadataCountryCode()
  1057. {
  1058. string lang = PreferredMetadataCountryCode;
  1059. if (string.IsNullOrWhiteSpace(lang))
  1060. {
  1061. lang = GetParents()
  1062. .Select(i => i.PreferredMetadataCountryCode)
  1063. .FirstOrDefault(i => !string.IsNullOrWhiteSpace(i));
  1064. }
  1065. if (string.IsNullOrWhiteSpace(lang))
  1066. {
  1067. lang = LibraryManager.GetCollectionFolders(this)
  1068. .Select(i => i.PreferredMetadataCountryCode)
  1069. .FirstOrDefault(i => !string.IsNullOrWhiteSpace(i));
  1070. }
  1071. if (string.IsNullOrWhiteSpace(lang))
  1072. {
  1073. lang = ConfigurationManager.Configuration.MetadataCountryCode;
  1074. }
  1075. return lang;
  1076. }
  1077. public virtual bool IsSaveLocalMetadataEnabled()
  1078. {
  1079. if (SourceType == SourceType.Channel)
  1080. {
  1081. return false;
  1082. }
  1083. return ConfigurationManager.Configuration.SaveLocalMeta;
  1084. }
  1085. /// <summary>
  1086. /// Determines if a given user has access to this item
  1087. /// </summary>
  1088. /// <param name="user">The user.</param>
  1089. /// <returns><c>true</c> if [is parental allowed] [the specified user]; otherwise, <c>false</c>.</returns>
  1090. /// <exception cref="System.ArgumentNullException">user</exception>
  1091. public bool IsParentalAllowed(User user)
  1092. {
  1093. if (user == null)
  1094. {
  1095. throw new ArgumentNullException("user");
  1096. }
  1097. if (!IsVisibleViaTags(user))
  1098. {
  1099. return false;
  1100. }
  1101. var maxAllowedRating = user.Policy.MaxParentalRating;
  1102. if (maxAllowedRating == null)
  1103. {
  1104. return true;
  1105. }
  1106. var rating = CustomRatingForComparison;
  1107. if (string.IsNullOrWhiteSpace(rating))
  1108. {
  1109. rating = OfficialRatingForComparison;
  1110. }
  1111. if (string.IsNullOrWhiteSpace(rating))
  1112. {
  1113. return !GetBlockUnratedValue(user.Policy);
  1114. }
  1115. var value = LocalizationManager.GetRatingLevel(rating);
  1116. // Could not determine the integer value
  1117. if (!value.HasValue)
  1118. {
  1119. var isAllowed = !GetBlockUnratedValue(user.Policy);
  1120. if (!isAllowed)
  1121. {
  1122. Logger.Debug("{0} has an unrecognized parental rating of {1}.", Name, rating);
  1123. }
  1124. return isAllowed;
  1125. }
  1126. return value.Value <= maxAllowedRating.Value;
  1127. }
  1128. public int? GetParentalRatingValue()
  1129. {
  1130. var rating = CustomRating;
  1131. if (string.IsNullOrWhiteSpace(rating))
  1132. {
  1133. rating = OfficialRating;
  1134. }
  1135. if (string.IsNullOrWhiteSpace(rating))
  1136. {
  1137. return null;
  1138. }
  1139. return LocalizationManager.GetRatingLevel(rating);
  1140. }
  1141. public int? GetInheritedParentalRatingValue()
  1142. {
  1143. var rating = CustomRatingForComparison;
  1144. if (string.IsNullOrWhiteSpace(rating))
  1145. {
  1146. rating = OfficialRatingForComparison;
  1147. }
  1148. if (string.IsNullOrWhiteSpace(rating))
  1149. {
  1150. return null;
  1151. }
  1152. return LocalizationManager.GetRatingLevel(rating);
  1153. }
  1154. public List<string> GetInheritedTags()
  1155. {
  1156. var list = new List<string>();
  1157. list.AddRange(Tags);
  1158. foreach (var parent in GetParents())
  1159. {
  1160. list.AddRange(parent.Tags);
  1161. }
  1162. return list.Distinct(StringComparer.OrdinalIgnoreCase).ToList();
  1163. }
  1164. private bool IsVisibleViaTags(User user)
  1165. {
  1166. var policy = user.Policy;
  1167. if (policy.BlockedTags.Any(i => Tags.Contains(i, StringComparer.OrdinalIgnoreCase)))
  1168. {
  1169. return false;
  1170. }
  1171. return true;
  1172. }
  1173. protected virtual bool IsAllowTagFilterEnforced()
  1174. {
  1175. return true;
  1176. }
  1177. public virtual UnratedItem GetBlockUnratedType()
  1178. {
  1179. if (SourceType == SourceType.Channel)
  1180. {
  1181. return UnratedItem.ChannelContent;
  1182. }
  1183. return UnratedItem.Other;
  1184. }
  1185. /// <summary>
  1186. /// Gets the block unrated value.
  1187. /// </summary>
  1188. /// <param name="config">The configuration.</param>
  1189. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
  1190. protected virtual bool GetBlockUnratedValue(UserPolicy config)
  1191. {
  1192. // Don't block plain folders that are unrated. Let the media underneath get blocked
  1193. // Special folders like series and albums will override this method.
  1194. if (IsFolder)
  1195. {
  1196. return false;
  1197. }
  1198. if (this is IItemByName)
  1199. {
  1200. return false;
  1201. }
  1202. return config.BlockUnratedItems.Contains(GetBlockUnratedType());
  1203. }
  1204. /// <summary>
  1205. /// Determines if this folder should be visible to a given user.
  1206. /// Default is just parental allowed. Can be overridden for more functionality.
  1207. /// </summary>
  1208. /// <param name="user">The user.</param>
  1209. /// <returns><c>true</c> if the specified user is visible; otherwise, <c>false</c>.</returns>
  1210. /// <exception cref="System.ArgumentNullException">user</exception>
  1211. public virtual bool IsVisible(User user)
  1212. {
  1213. if (user == null)
  1214. {
  1215. throw new ArgumentNullException("user");
  1216. }
  1217. return IsParentalAllowed(user);
  1218. }
  1219. public virtual bool IsVisibleStandalone(User user)
  1220. {
  1221. if (SourceType == SourceType.Channel)
  1222. {
  1223. return IsVisibleStandaloneInternal(user, false) && Channel.IsChannelVisible(this, user);
  1224. }
  1225. return IsVisibleStandaloneInternal(user, true);
  1226. }
  1227. protected bool IsVisibleStandaloneInternal(User user, bool checkFolders)
  1228. {
  1229. if (!IsVisible(user))
  1230. {
  1231. return false;
  1232. }
  1233. if (GetParents().Any(i => !i.IsVisible(user)))
  1234. {
  1235. return false;
  1236. }
  1237. if (checkFolders)
  1238. {
  1239. var topParent = GetParents().LastOrDefault() ?? this;
  1240. if (string.IsNullOrWhiteSpace(topParent.Path))
  1241. {
  1242. return true;
  1243. }
  1244. var userCollectionFolders = user.RootFolder.GetChildren(user, true).Select(i => i.Id).ToList();
  1245. var itemCollectionFolders = LibraryManager.GetCollectionFolders(this).Select(i => i.Id);
  1246. if (!itemCollectionFolders.Any(userCollectionFolders.Contains))
  1247. {
  1248. return false;
  1249. }
  1250. }
  1251. return true;
  1252. }
  1253. /// <summary>
  1254. /// Gets a value indicating whether this instance is folder.
  1255. /// </summary>
  1256. /// <value><c>true</c> if this instance is folder; otherwise, <c>false</c>.</value>
  1257. [IgnoreDataMember]
  1258. public virtual bool IsFolder
  1259. {
  1260. get
  1261. {
  1262. return false;
  1263. }
  1264. }
  1265. public virtual string GetClientTypeName()
  1266. {
  1267. if (IsFolder && SourceType == SourceType.Channel && !(this is Channel))
  1268. {
  1269. return "ChannelFolderItem";
  1270. }
  1271. return GetType().Name;
  1272. }
  1273. /// <summary>
  1274. /// Gets the linked child.
  1275. /// </summary>
  1276. /// <param name="info">The info.</param>
  1277. /// <returns>BaseItem.</returns>
  1278. protected BaseItem GetLinkedChild(LinkedChild info)
  1279. {
  1280. // First get using the cached Id
  1281. if (info.ItemId.HasValue)
  1282. {
  1283. if (info.ItemId.Value == Guid.Empty)
  1284. {
  1285. return null;
  1286. }
  1287. var itemById = LibraryManager.GetItemById(info.ItemId.Value);
  1288. if (itemById != null)
  1289. {
  1290. return itemById;
  1291. }
  1292. }
  1293. var item = FindLinkedChild(info);
  1294. // If still null, log
  1295. if (item == null)
  1296. {
  1297. // Don't keep searching over and over
  1298. info.ItemId = Guid.Empty;
  1299. }
  1300. else
  1301. {
  1302. // Cache the id for next time
  1303. info.ItemId = item.Id;
  1304. }
  1305. return item;
  1306. }
  1307. private BaseItem FindLinkedChild(LinkedChild info)
  1308. {
  1309. if (!string.IsNullOrEmpty(info.Path))
  1310. {
  1311. var itemByPath = LibraryManager.FindByPath(info.Path, null);
  1312. if (itemByPath == null)
  1313. {
  1314. //Logger.Warn("Unable to find linked item at path {0}", info.Path);
  1315. }
  1316. return itemByPath;
  1317. }
  1318. return null;
  1319. }
  1320. [IgnoreDataMember]
  1321. public virtual bool EnableRememberingTrackSelections
  1322. {
  1323. get
  1324. {
  1325. return true;
  1326. }
  1327. }
  1328. /// <summary>
  1329. /// Adds a studio to the item
  1330. /// </summary>
  1331. /// <param name="name">The name.</param>
  1332. /// <exception cref="System.ArgumentNullException"></exception>
  1333. public void AddStudio(string name)
  1334. {
  1335. if (string.IsNullOrWhiteSpace(name))
  1336. {
  1337. throw new ArgumentNullException("name");
  1338. }
  1339. if (!Studios.Contains(name, StringComparer.OrdinalIgnoreCase))
  1340. {
  1341. Studios.Add(name);
  1342. }
  1343. }
  1344. /// <summary>
  1345. /// Adds a genre to the item
  1346. /// </summary>
  1347. /// <param name="name">The name.</param>
  1348. /// <exception cref="System.ArgumentNullException"></exception>
  1349. public void AddGenre(string name)
  1350. {
  1351. if (string.IsNullOrWhiteSpace(name))
  1352. {
  1353. throw new ArgumentNullException("name");
  1354. }
  1355. if (!Genres.Contains(name, StringComparer.OrdinalIgnoreCase))
  1356. {
  1357. Genres.Add(name);
  1358. }
  1359. }
  1360. /// <summary>
  1361. /// Marks the played.
  1362. /// </summary>
  1363. /// <param name="user">The user.</param>
  1364. /// <param name="datePlayed">The date played.</param>
  1365. /// <param name="resetPosition">if set to <c>true</c> [reset position].</param>
  1366. /// <returns>Task.</returns>
  1367. /// <exception cref="System.ArgumentNullException"></exception>
  1368. public virtual async Task MarkPlayed(User user,
  1369. DateTime? datePlayed,
  1370. bool resetPosition)
  1371. {
  1372. if (user == null)
  1373. {
  1374. throw new ArgumentNullException();
  1375. }
  1376. var data = UserDataManager.GetUserData(user, this);
  1377. if (datePlayed.HasValue)
  1378. {
  1379. // Incremenet
  1380. data.PlayCount++;
  1381. }
  1382. // Ensure it's at least one
  1383. data.PlayCount = Math.Max(data.PlayCount, 1);
  1384. if (resetPosition)
  1385. {
  1386. data.PlaybackPositionTicks = 0;
  1387. }
  1388. data.LastPlayedDate = datePlayed ?? data.LastPlayedDate;
  1389. data.Played = true;
  1390. await UserDataManager.SaveUserData(user.Id, this, data, UserDataSaveReason.TogglePlayed, CancellationToken.None).ConfigureAwait(false);
  1391. }
  1392. /// <summary>
  1393. /// Marks the unplayed.
  1394. /// </summary>
  1395. /// <param name="user">The user.</param>
  1396. /// <returns>Task.</returns>
  1397. /// <exception cref="System.ArgumentNullException"></exception>
  1398. public virtual async Task MarkUnplayed(User user)
  1399. {
  1400. if (user == null)
  1401. {
  1402. throw new ArgumentNullException();
  1403. }
  1404. var data = UserDataManager.GetUserData(user, this);
  1405. //I think it is okay to do this here.
  1406. // if this is only called when a user is manually forcing something to un-played
  1407. // then it probably is what we want to do...
  1408. data.PlayCount = 0;
  1409. data.PlaybackPositionTicks = 0;
  1410. data.LastPlayedDate = null;
  1411. data.Played = false;
  1412. await UserDataManager.SaveUserData(user.Id, this, data, UserDataSaveReason.TogglePlayed, CancellationToken.None).ConfigureAwait(false);
  1413. }
  1414. /// <summary>
  1415. /// Do whatever refreshing is necessary when the filesystem pertaining to this item has changed.
  1416. /// </summary>
  1417. /// <returns>Task.</returns>
  1418. public virtual Task ChangedExternally()
  1419. {
  1420. ProviderManager.QueueRefresh(Id, new MetadataRefreshOptions(FileSystem));
  1421. return Task.FromResult(true);
  1422. }
  1423. /// <summary>
  1424. /// Gets an image
  1425. /// </summary>
  1426. /// <param name="type">The type.</param>
  1427. /// <param name="imageIndex">Index of the image.</param>
  1428. /// <returns><c>true</c> if the specified type has image; otherwise, <c>false</c>.</returns>
  1429. /// <exception cref="System.ArgumentException">Backdrops should be accessed using Item.Backdrops</exception>
  1430. public bool HasImage(ImageType type, int imageIndex)
  1431. {
  1432. return GetImageInfo(type, imageIndex) != null;
  1433. }
  1434. public void SetImage(ItemImageInfo image, int index)
  1435. {
  1436. if (image.Type == ImageType.Chapter)
  1437. {
  1438. throw new ArgumentException("Cannot set chapter images using SetImagePath");
  1439. }
  1440. var existingImage = GetImageInfo(image.Type, index);
  1441. if (existingImage != null)
  1442. {
  1443. ImageInfos.Remove(existingImage);
  1444. }
  1445. ImageInfos.Add(image);
  1446. }
  1447. public void SetImagePath(ImageType type, int index, FileSystemMetadata file)
  1448. {
  1449. if (type == ImageType.Chapter)
  1450. {
  1451. throw new ArgumentException("Cannot set chapter images using SetImagePath");
  1452. }
  1453. var image = GetImageInfo(type, index);
  1454. if (image == null)
  1455. {
  1456. ImageInfos.Add(GetImageInfo(file, type));
  1457. }
  1458. else
  1459. {
  1460. var imageInfo = GetImageInfo(file, type);
  1461. image.Path = file.FullName;
  1462. image.DateModified = imageInfo.DateModified;
  1463. image.IsPlaceholder = false;
  1464. }
  1465. }
  1466. /// <summary>
  1467. /// Deletes the image.
  1468. /// </summary>
  1469. /// <param name="type">The type.</param>
  1470. /// <param name="index">The index.</param>
  1471. /// <returns>Task.</returns>
  1472. public Task DeleteImage(ImageType type, int index)
  1473. {
  1474. var info = GetImageInfo(type, index);
  1475. if (info == null)
  1476. {
  1477. // Nothing to do
  1478. return Task.FromResult(true);
  1479. }
  1480. // Remove it from the item
  1481. RemoveImage(info);
  1482. if (info.IsLocalFile)
  1483. {
  1484. // Delete the source file
  1485. var currentFile = new FileInfo(info.Path);
  1486. // Deletion will fail if the file is hidden so remove the attribute first
  1487. if (currentFile.Exists)
  1488. {
  1489. if ((currentFile.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
  1490. {
  1491. currentFile.Attributes &= ~FileAttributes.Hidden;
  1492. }
  1493. FileSystem.DeleteFile(currentFile.FullName);
  1494. }
  1495. }
  1496. return UpdateToRepository(ItemUpdateType.ImageUpdate, CancellationToken.None);
  1497. }
  1498. public void RemoveImage(ItemImageInfo image)
  1499. {
  1500. ImageInfos.Remove(image);
  1501. }
  1502. public virtual Task UpdateToRepository(ItemUpdateType updateReason, CancellationToken cancellationToken)
  1503. {
  1504. return LibraryManager.UpdateItem(this, updateReason, cancellationToken);
  1505. }
  1506. /// <summary>
  1507. /// Validates that images within the item are still on the file system
  1508. /// </summary>
  1509. public bool ValidateImages(IDirectoryService directoryService)
  1510. {
  1511. var allFiles = ImageInfos
  1512. .Where(i => i.IsLocalFile)
  1513. .Select(i => System.IO.Path.GetDirectoryName(i.Path))
  1514. .Distinct(StringComparer.OrdinalIgnoreCase)
  1515. .SelectMany(directoryService.GetFiles)
  1516. .Select(i => i.FullName)
  1517. .ToList();
  1518. var deletedImages = ImageInfos
  1519. .Where(image => image.IsLocalFile && !allFiles.Contains(image.Path, StringComparer.OrdinalIgnoreCase))
  1520. .ToList();
  1521. if (deletedImages.Count > 0)
  1522. {
  1523. ImageInfos = ImageInfos.Except(deletedImages).ToList();
  1524. }
  1525. return deletedImages.Count > 0;
  1526. }
  1527. /// <summary>
  1528. /// Gets the image path.
  1529. /// </summary>
  1530. /// <param name="imageType">Type of the image.</param>
  1531. /// <param name="imageIndex">Index of the image.</param>
  1532. /// <returns>System.String.</returns>
  1533. /// <exception cref="System.InvalidOperationException">
  1534. /// </exception>
  1535. /// <exception cref="System.ArgumentNullException">item</exception>
  1536. public string GetImagePath(ImageType imageType, int imageIndex)
  1537. {
  1538. var info = GetImageInfo(imageType, imageIndex);
  1539. return info == null ? null : info.Path;
  1540. }
  1541. /// <summary>
  1542. /// Gets the image information.
  1543. /// </summary>
  1544. /// <param name="imageType">Type of the image.</param>
  1545. /// <param name="imageIndex">Index of the image.</param>
  1546. /// <returns>ItemImageInfo.</returns>
  1547. public ItemImageInfo GetImageInfo(ImageType imageType, int imageIndex)
  1548. {
  1549. if (imageType == ImageType.Chapter)
  1550. {
  1551. var chapter = ItemRepository.GetChapter(Id, imageIndex);
  1552. if (chapter == null)
  1553. {
  1554. return null;
  1555. }
  1556. var path = chapter.ImagePath;
  1557. if (string.IsNullOrWhiteSpace(path))
  1558. {
  1559. return null;
  1560. }
  1561. return new ItemImageInfo
  1562. {
  1563. Path = path,
  1564. DateModified = FileSystem.GetLastWriteTimeUtc(path),
  1565. Type = imageType
  1566. };
  1567. }
  1568. return GetImages(imageType)
  1569. .ElementAtOrDefault(imageIndex);
  1570. }
  1571. public IEnumerable<ItemImageInfo> GetImages(ImageType imageType)
  1572. {
  1573. if (imageType == ImageType.Chapter)
  1574. {
  1575. throw new ArgumentException("No image info for chapter images");
  1576. }
  1577. return ImageInfos.Where(i => i.Type == imageType);
  1578. }
  1579. /// <summary>
  1580. /// Adds the images.
  1581. /// </summary>
  1582. /// <param name="imageType">Type of the image.</param>
  1583. /// <param name="images">The images.</param>
  1584. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
  1585. /// <exception cref="System.ArgumentException">Cannot call AddImages with chapter images</exception>
  1586. public bool AddImages(ImageType imageType, List<FileSystemMetadata> images)
  1587. {
  1588. if (imageType == ImageType.Chapter)
  1589. {
  1590. throw new ArgumentException("Cannot call AddImages with chapter images");
  1591. }
  1592. var existingImages = GetImages(imageType)
  1593. .ToList();
  1594. var newImageList = new List<FileSystemMetadata>();
  1595. var imageAdded = false;
  1596. foreach (var newImage in images)
  1597. {
  1598. if (newImage == null)
  1599. {
  1600. throw new ArgumentException("null image found in list");
  1601. }
  1602. var existing = existingImages
  1603. .FirstOrDefault(i => string.Equals(i.Path, newImage.FullName, StringComparison.OrdinalIgnoreCase));
  1604. if (existing == null)
  1605. {
  1606. newImageList.Add(newImage);
  1607. imageAdded = true;
  1608. }
  1609. else
  1610. {
  1611. if (existing.IsLocalFile)
  1612. {
  1613. existing.DateModified = FileSystem.GetLastWriteTimeUtc(newImage);
  1614. }
  1615. }
  1616. }
  1617. if (imageAdded || images.Count != existingImages.Count)
  1618. {
  1619. var newImagePaths = images.Select(i => i.FullName).ToList();
  1620. var deleted = existingImages
  1621. .Where(i => i.IsLocalFile && !newImagePaths.Contains(i.Path, StringComparer.OrdinalIgnoreCase) && !FileSystem.FileExists(i.Path))
  1622. .ToList();
  1623. ImageInfos = ImageInfos.Except(deleted).ToList();
  1624. }
  1625. ImageInfos.AddRange(newImageList.Select(i => GetImageInfo(i, imageType)));
  1626. return newImageList.Count > 0;
  1627. }
  1628. private ItemImageInfo GetImageInfo(FileSystemMetadata file, ImageType type)
  1629. {
  1630. return new ItemImageInfo
  1631. {
  1632. Path = file.FullName,
  1633. Type = type,
  1634. DateModified = FileSystem.GetLastWriteTimeUtc(file)
  1635. };
  1636. }
  1637. /// <summary>
  1638. /// Gets the file system path to delete when the item is to be deleted
  1639. /// </summary>
  1640. /// <returns></returns>
  1641. public virtual IEnumerable<string> GetDeletePaths()
  1642. {
  1643. return new[] { Path };
  1644. }
  1645. public bool AllowsMultipleImages(ImageType type)
  1646. {
  1647. return type == ImageType.Backdrop || type == ImageType.Screenshot || type == ImageType.Chapter;
  1648. }
  1649. public Task SwapImages(ImageType type, int index1, int index2)
  1650. {
  1651. if (!AllowsMultipleImages(type))
  1652. {
  1653. throw new ArgumentException("The change index operation is only applicable to backdrops and screenshots");
  1654. }
  1655. var info1 = GetImageInfo(type, index1);
  1656. var info2 = GetImageInfo(type, index2);
  1657. if (info1 == null || info2 == null)
  1658. {
  1659. // Nothing to do
  1660. return Task.FromResult(true);
  1661. }
  1662. if (!info1.IsLocalFile || !info2.IsLocalFile)
  1663. {
  1664. // TODO: Not supported yet
  1665. return Task.FromResult(true);
  1666. }
  1667. var path1 = info1.Path;
  1668. var path2 = info2.Path;
  1669. FileSystem.SwapFiles(path1, path2);
  1670. // Refresh these values
  1671. info1.DateModified = FileSystem.GetLastWriteTimeUtc(info1.Path);
  1672. info2.DateModified = FileSystem.GetLastWriteTimeUtc(info2.Path);
  1673. return UpdateToRepository(ItemUpdateType.ImageUpdate, CancellationToken.None);
  1674. }
  1675. public virtual bool IsPlayed(User user)
  1676. {
  1677. var userdata = UserDataManager.GetUserData(user, this);
  1678. return userdata != null && userdata.Played;
  1679. }
  1680. public bool IsFavoriteOrLiked(User user)
  1681. {
  1682. var userdata = UserDataManager.GetUserData(user, this);
  1683. return userdata != null && (userdata.IsFavorite || (userdata.Likes ?? false));
  1684. }
  1685. public virtual bool IsUnplayed(User user)
  1686. {
  1687. if (user == null)
  1688. {
  1689. throw new ArgumentNullException("user");
  1690. }
  1691. var userdata = UserDataManager.GetUserData(user, this);
  1692. return userdata == null || !userdata.Played;
  1693. }
  1694. ItemLookupInfo IHasLookupInfo<ItemLookupInfo>.GetLookupInfo()
  1695. {
  1696. return GetItemLookupInfo<ItemLookupInfo>();
  1697. }
  1698. protected T GetItemLookupInfo<T>()
  1699. where T : ItemLookupInfo, new()
  1700. {
  1701. return new T
  1702. {
  1703. MetadataCountryCode = GetPreferredMetadataCountryCode(),
  1704. MetadataLanguage = GetPreferredMetadataLanguage(),
  1705. Name = Name,
  1706. ProviderIds = ProviderIds,
  1707. IndexNumber = IndexNumber,
  1708. ParentIndexNumber = ParentIndexNumber,
  1709. Year = ProductionYear,
  1710. PremiereDate = PremiereDate
  1711. };
  1712. }
  1713. /// <summary>
  1714. /// This is called before any metadata refresh and returns true or false indicating if changes were made
  1715. /// </summary>
  1716. public virtual bool BeforeMetadataRefresh()
  1717. {
  1718. _sortName = null;
  1719. var hasChanges = false;
  1720. if (string.IsNullOrEmpty(Name) && !string.IsNullOrEmpty(Path))
  1721. {
  1722. Name = FileSystem.GetFileNameWithoutExtension(Path);
  1723. hasChanges = true;
  1724. }
  1725. return hasChanges;
  1726. }
  1727. protected static string GetMappedPath(string path, LocationType locationType)
  1728. {
  1729. if (locationType == LocationType.FileSystem || locationType == LocationType.Offline)
  1730. {
  1731. foreach (var map in ConfigurationManager.Configuration.PathSubstitutions)
  1732. {
  1733. path = LibraryManager.SubstitutePath(path, map.From, map.To);
  1734. }
  1735. }
  1736. return path;
  1737. }
  1738. public virtual void FillUserDataDtoValues(UserItemDataDto dto, UserItemData userData, User user)
  1739. {
  1740. if (RunTimeTicks.HasValue)
  1741. {
  1742. double pct = RunTimeTicks.Value;
  1743. if (pct > 0)
  1744. {
  1745. pct = userData.PlaybackPositionTicks / pct;
  1746. if (pct > 0)
  1747. {
  1748. dto.PlayedPercentage = 100 * pct;
  1749. }
  1750. }
  1751. }
  1752. }
  1753. protected Task RefreshMetadataForOwnedVideo(MetadataRefreshOptions options, string path, CancellationToken cancellationToken)
  1754. {
  1755. var newOptions = new MetadataRefreshOptions(options.DirectoryService)
  1756. {
  1757. ImageRefreshMode = options.ImageRefreshMode,
  1758. MetadataRefreshMode = options.MetadataRefreshMode,
  1759. ReplaceAllMetadata = options.ReplaceAllMetadata
  1760. };
  1761. var id = LibraryManager.GetNewItemId(path, typeof(Video));
  1762. // Try to retrieve it from the db. If we don't find it, use the resolved version
  1763. var video = LibraryManager.GetItemById(id) as Video;
  1764. if (video == null)
  1765. {
  1766. video = LibraryManager.ResolvePath(FileSystem.GetFileSystemInfo(path)) as Video;
  1767. newOptions.ForceSave = true;
  1768. }
  1769. if (video == null)
  1770. {
  1771. return Task.FromResult(true);
  1772. }
  1773. return video.RefreshMetadata(newOptions, cancellationToken);
  1774. }
  1775. public string GetEtag(User user)
  1776. {
  1777. return string.Join("|", GetEtagValues(user).ToArray()).GetMD5().ToString("N");
  1778. }
  1779. protected virtual List<string> GetEtagValues(User user)
  1780. {
  1781. return new List<string>
  1782. {
  1783. DateLastSaved.Ticks.ToString(CultureInfo.InvariantCulture)
  1784. };
  1785. }
  1786. public virtual IEnumerable<Guid> GetAncestorIds()
  1787. {
  1788. return GetParents().Select(i => i.Id).Concat(LibraryManager.GetCollectionFolders(this).Select(i => i.Id));
  1789. }
  1790. public BaseItem GetTopParent()
  1791. {
  1792. if (IsTopParent)
  1793. {
  1794. return this;
  1795. }
  1796. return GetParents().FirstOrDefault(i => i.IsTopParent);
  1797. }
  1798. [IgnoreDataMember]
  1799. public virtual bool IsTopParent
  1800. {
  1801. get
  1802. {
  1803. if (GetParent() is AggregateFolder || this is BasePluginFolder)
  1804. {
  1805. return true;
  1806. }
  1807. var view = this as UserView;
  1808. if (view != null && string.Equals(view.ViewType, CollectionType.LiveTv, StringComparison.OrdinalIgnoreCase))
  1809. {
  1810. return true;
  1811. }
  1812. if (view != null && string.Equals(view.ViewType, CollectionType.Channels, StringComparison.OrdinalIgnoreCase))
  1813. {
  1814. return true;
  1815. }
  1816. return false;
  1817. }
  1818. }
  1819. [IgnoreDataMember]
  1820. public virtual bool SupportsAncestors
  1821. {
  1822. get
  1823. {
  1824. return true;
  1825. }
  1826. }
  1827. public virtual IEnumerable<Guid> GetIdsForAncestorQuery()
  1828. {
  1829. return new[] { Id };
  1830. }
  1831. public virtual Task Delete(DeleteOptions options)
  1832. {
  1833. return LibraryManager.DeleteItem(this, options);
  1834. }
  1835. public virtual Task OnFileDeleted()
  1836. {
  1837. // Remove from database
  1838. return Delete(new DeleteOptions
  1839. {
  1840. DeleteFileLocation = false
  1841. });
  1842. }
  1843. public virtual List<ExternalUrl> GetRelatedUrls()
  1844. {
  1845. return new List<ExternalUrl>();
  1846. }
  1847. }
  1848. }