Source: lib/offline/offline_uri.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.offline.OfflineUri');
  7. /**
  8. * The OfflineUri class contains all the components that make up the offline
  9. * uri. The components are:
  10. * TYPE: Used to know what type of data the uri points to. It can either
  11. * be "manifest" or "segment".
  12. * MECHANISM: The name of the mechanism that manages the storage cell that
  13. * holds the data.
  14. * CELL: The name of the cell that holds the data.
  15. * KEY: The key that the data is stored under in the cell.
  16. */
  17. shaka.offline.OfflineUri = class {
  18. /**
  19. * @param {string} type
  20. * @param {string} mechanism
  21. * @param {string} cell
  22. * @param {number} key
  23. */
  24. constructor(type, mechanism, cell, key) {
  25. /**
  26. * @private {string}
  27. * @const
  28. */
  29. this.type_ = type;
  30. /**
  31. * @private {string}
  32. * @const
  33. */
  34. this.mechanism_ = mechanism;
  35. /**
  36. * @private {string}
  37. * @const
  38. */
  39. this.cell_ = cell;
  40. /**
  41. * @private {number}
  42. * @const
  43. */
  44. this.key_ = key;
  45. /**
  46. * @private {string}
  47. * @const
  48. */
  49. this.asString_ = [
  50. 'offline:', type, '/', mechanism, '/', cell, '/', key,
  51. ].join('');
  52. }
  53. /** @return {boolean} */
  54. isManifest() { return this.type_ == 'manifest'; }
  55. /** @return {boolean} */
  56. isSegment() { return this.type_ == 'segment'; }
  57. /** @return {string} */
  58. mechanism() { return this.mechanism_; }
  59. /** @return {string} */
  60. cell() { return this.cell_; }
  61. /** @return {number} */
  62. key() { return this.key_; }
  63. /** @override */
  64. toString() { return this.asString_; }
  65. /**
  66. * @param {string} uri
  67. * @return {?shaka.offline.OfflineUri}
  68. */
  69. static parse(uri) {
  70. const parts = /^offline:([a-z]+)\/([^/]+)\/([^/]+)\/([0-9]+)$/.exec(uri);
  71. if (parts == null) {
  72. return null;
  73. }
  74. const type = parts[1];
  75. if (type != 'manifest' && type != 'segment') {
  76. return null;
  77. }
  78. const mechanism = parts[2];
  79. if (!mechanism) {
  80. return null;
  81. }
  82. const cell = parts[3];
  83. if (!cell) {
  84. return null;
  85. }
  86. const key = Number(parts[4]);
  87. if (type == null) {
  88. return null;
  89. }
  90. return new shaka.offline.OfflineUri(type, mechanism, cell, key);
  91. }
  92. /**
  93. * @param {string} mechanism
  94. * @param {string} cell
  95. * @param {number} key
  96. * @return {!shaka.offline.OfflineUri}
  97. */
  98. static manifest(mechanism, cell, key) {
  99. return new shaka.offline.OfflineUri('manifest', mechanism, cell, key);
  100. }
  101. /**
  102. * @param {string} mechanism
  103. * @param {string} cell
  104. * @param {number} key
  105. * @return {!shaka.offline.OfflineUri}
  106. */
  107. static segment(mechanism, cell, key) {
  108. return new shaka.offline.OfflineUri('segment', mechanism, cell, key);
  109. }
  110. };