@atlaskit/media-client 33.1.0 → 33.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -155,6 +155,90 @@ export class FileFetcherImpl {
155
155
  this.setFileState(id, mapMediaItemToFileState(id, itemDetails));
156
156
  return data;
157
157
  });
158
+ _defineProperty(this, "getOrFetchFileState", async (id, collectionName) => {
159
+ let fileState = this.store.getState().files[id];
160
+ return fileState !== null && fileState !== void 0 ? fileState : await this.getCurrentState(id, {
161
+ collectionName
162
+ });
163
+ });
164
+ _defineProperty(this, "getDurationOfVideo", async (id, collectionName) => {
165
+ const fileState = await this.getOrFetchFileState(id, collectionName);
166
+ if (fileState.status !== "processed" || fileState.mediaType !== "video" || !fileState.artifacts["video.mp4"]) {
167
+ throw new Error("File is not a video");
168
+ }
169
+ const aritfactUrl = await this.getArtifactURL(fileState.artifacts, "video.mp4");
170
+ const artifactPath = new URL(aritfactUrl).pathname;
171
+ const res = await this.mediaApi.request(artifactPath, {
172
+ headers: {
173
+ Range: "bytes=0-199"
174
+ }
175
+ });
176
+
177
+ // Get the response as ArrayBuffer
178
+ const arrayBuffer = await res.arrayBuffer();
179
+ const uint8Array = new Uint8Array(arrayBuffer);
180
+ const mvhdBytes = new Uint8Array([109, 118, 104, 100]); // "mvhd" in ASCII
181
+
182
+ let mvhdIndex = -1;
183
+ for (let i = 0; i <= uint8Array.length - mvhdBytes.length; i++) {
184
+ mvhdIndex = i;
185
+ for (let j = 0; j < mvhdBytes.length; j++) {
186
+ if (uint8Array[i + j] !== mvhdBytes[j]) {
187
+ mvhdIndex = -1;
188
+ break;
189
+ }
190
+ }
191
+ if (mvhdIndex !== -1) {
192
+ break;
193
+ }
194
+ }
195
+ if (mvhdIndex === -1) {
196
+ throw new Error("Unable to find mvhd bytes");
197
+ }
198
+
199
+ // Create DataView for reading big-endian integers
200
+ const dataView = new DataView(arrayBuffer);
201
+ const start = mvhdIndex + 16; // Skip atom header and version/flags
202
+
203
+ // Check if we have enough bytes to read timescale and duration
204
+ if (start + 8 > arrayBuffer.byteLength) {
205
+ return 0;
206
+ }
207
+
208
+ // flase as second parameter indicates big-endian 32-bit integers
209
+ const timeScale = dataView.getUint32(start, false);
210
+ const duration = dataView.getUint32(start + 4, false);
211
+ if (timeScale === 0) {
212
+ throw new Error("Timescale is invalid");
213
+ }
214
+
215
+ // get the video length in seconds
216
+ const videoLength = Math.floor(duration / timeScale);
217
+ return videoLength;
218
+ });
219
+ _defineProperty(this, "getVideoDurations", async files => {
220
+ // get all the duration promises
221
+ const promises = files.map(async ({
222
+ id,
223
+ collectionName
224
+ }) => {
225
+ try {
226
+ return await this.getDurationOfVideo(id, collectionName);
227
+ } catch (_err) {
228
+ return -1;
229
+ }
230
+ });
231
+ const durations = await Promise.all(promises);
232
+
233
+ // only return the durations that are present;
234
+ return durations.reduce((acc, curr, i) => {
235
+ if (curr !== -1) {
236
+ const id = files[i].id;
237
+ acc[id] = curr;
238
+ }
239
+ return acc;
240
+ }, {});
241
+ });
158
242
  this.mediaApi = mediaApi;
159
243
  this.store = store;
160
244
  this.dataloader = createFileDataloader(mediaApi);