sanity-plugin-mux-input 2.17.0 → 2.18.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sanity-plugin-mux-input",
3
- "version": "2.17.0",
3
+ "version": "2.18.0",
4
4
  "description": "An input component that integrates Sanity Studio with Mux video encoding/hosting service.",
5
5
  "keywords": [
6
6
  "sanity",
@@ -4,6 +4,7 @@ import {catchError, mergeMap, mergeMapTo, switchMap} from 'rxjs/operators'
4
4
  import type {SanityClient} from 'sanity'
5
5
 
6
6
  import {createUpChunkObservable} from '../clients/upChunkObservable'
7
+ import {formatDriveShareLink} from '../util/formatDriveShareLink'
7
8
  import {roundPxString} from '../util/roundPxString'
8
9
  import type {MuxAsset, MuxNewAssetSettings, WatermarkConfig} from '../util/types'
9
10
  import {getAsset} from './assets'
@@ -280,17 +281,18 @@ export function testUrl(url: string): Observable<string> {
280
281
  if (typeof url !== 'string') {
281
282
  return throwError(error)
282
283
  }
283
- const trimmedUrl = url.trim()
284
+ let formattedUrl = url.trim()
285
+ formattedUrl = formatDriveShareLink(formattedUrl)
284
286
  let parsed
285
287
  try {
286
- parsed = new URL(trimmedUrl)
288
+ parsed = new URL(formattedUrl)
287
289
  } catch (err) {
288
290
  return throwError(error)
289
291
  }
290
292
  if (parsed && !parsed.protocol.match(/http:|https:/)) {
291
293
  return throwError(error)
292
294
  }
293
- return of(trimmedUrl)
295
+ return of(formattedUrl)
294
296
  }
295
297
 
296
298
  function optionsFromFile(opts: {preserveFilename?: boolean}, file: File) {
@@ -18,6 +18,7 @@ import {useId, useRef, useState} from 'react'
18
18
 
19
19
  import {addTextTrackFromUrl, generateSubtitles, getAsset} from '../actions/assets'
20
20
  import {useClient} from '../hooks/useClient'
21
+ import {addKeysToMuxData} from '../util/addKeysToMuxData'
21
22
  import {extractErrorMessage, pollTrackStatus} from '../util/textTracks'
22
23
  import {type MuxTextTrack, SUPPORTED_MUX_LANGUAGES, type VideoAssetDocument} from '../util/types'
23
24
 
@@ -60,6 +61,20 @@ export default function AddCaptionDialog({asset, onAdd, onClose}: Props) {
60
61
  return assetDocument.url
61
62
  }
62
63
 
64
+ const refreshAssetData = async () => {
65
+ if (!asset._id || !asset.assetId) return
66
+ try {
67
+ const latestAssetData = await getAsset(client, asset.assetId)
68
+ const dataWithKeys = addKeysToMuxData(latestAssetData.data)
69
+ await client
70
+ .patch(asset._id)
71
+ .set({data: dataWithKeys, status: latestAssetData.data.status})
72
+ .commit({returnDocuments: false})
73
+ } catch (refreshError) {
74
+ console.error('Failed to refresh asset data:', refreshError)
75
+ }
76
+ }
77
+
63
78
  const handleAddTrackFromUrl = async () => {
64
79
  if (!asset.assetId) {
65
80
  throw new Error('Asset ID is required')
@@ -75,9 +90,9 @@ export default function AddCaptionDialog({asset, onAdd, onClose}: Props) {
75
90
  vttUrlToUse = await uploadVttFile(selectedFile)
76
91
  } catch (uploadError) {
77
92
  toast.push({
78
- title: 'Failed to upload VTT file',
93
+ title: 'Failed to upload caption file',
79
94
  status: 'error',
80
- description: 'Could not upload the VTT file to Sanity. Please try again.',
95
+ description: 'Could not upload the caption file to Sanity. Please try again.',
81
96
  })
82
97
  setIsSubmitting(false)
83
98
  throw uploadError
@@ -95,7 +110,7 @@ export default function AddCaptionDialog({asset, onAdd, onClose}: Props) {
95
110
  assetId: asset.assetId,
96
111
  trackName: trimmedName,
97
112
  trackLanguageCode: trimmedLanguageCode,
98
- onTrackErrored: (track) => {
113
+ onTrackErrored: async (track) => {
99
114
  const errorMessage =
100
115
  track.error?.messages?.[0] ||
101
116
  track.error?.type ||
@@ -105,6 +120,7 @@ export default function AddCaptionDialog({asset, onAdd, onClose}: Props) {
105
120
  status: 'error',
106
121
  description: errorMessage,
107
122
  })
123
+ await refreshAssetData()
108
124
  onAdd(track)
109
125
  onClose()
110
126
  },
@@ -132,11 +148,13 @@ export default function AddCaptionDialog({asset, onAdd, onClose}: Props) {
132
148
  description:
133
149
  'The track was created and is being processed. It will appear in the list shortly.',
134
150
  })
151
+ await refreshAssetData()
135
152
  onAdd(result.track)
136
153
  onClose()
137
154
  return
138
155
  }
139
156
 
157
+ await refreshAssetData()
140
158
  toast.push({
141
159
  title: 'Caption track added',
142
160
  status: 'success',
@@ -195,9 +213,9 @@ export default function AddCaptionDialog({asset, onAdd, onClose}: Props) {
195
213
  if (!isAutogenerated) {
196
214
  if (!selectedFile && !vttUrl.trim()) {
197
215
  toast.push({
198
- title: 'VTT file or URL required',
216
+ title: 'Caption file or URL required',
199
217
  status: 'error',
200
- description: 'Please select a VTT file or enter a VTT file URL',
218
+ description: 'Please select a VTT or SRT file or enter a caption file URL',
201
219
  })
202
220
  return
203
221
  }
@@ -209,7 +227,8 @@ export default function AddCaptionDialog({asset, onAdd, onClose}: Props) {
209
227
  toast.push({
210
228
  title: 'Invalid URL',
211
229
  status: 'error',
212
- description: 'Please enter a valid URL (e.g., https://example.com/subtitles.vtt)',
230
+ description:
231
+ 'Please enter a valid URL (e.g., https://example.com/subtitles.vtt or subtitles.srt)',
213
232
  })
214
233
  return
215
234
  }
@@ -303,7 +322,7 @@ export default function AddCaptionDialog({asset, onAdd, onClose}: Props) {
303
322
  <input
304
323
  ref={fileInputRef}
305
324
  type="file"
306
- accept=".vtt,text/vtt"
325
+ accept=".vtt,text/vtt,.srt,application/x-subrip"
307
326
  style={{display: 'none'}}
308
327
  onChange={(e) => {
309
328
  if (e.target.files && e.target.files.length > 0 && !isSubmitting) {
@@ -314,10 +333,10 @@ export default function AddCaptionDialog({asset, onAdd, onClose}: Props) {
314
333
  />
315
334
  </Card>
316
335
  <Text size={1} muted style={{textAlign: 'center'}}>
317
- Or enter the VTT file URL
336
+ Or enter the caption file URL
318
337
  </Text>
319
338
  <Stack space={2}>
320
- <Label htmlFor="vtt-url">VTT File URL</Label>
339
+ <Label htmlFor="vtt-url">Caption File URL (.vtt or .srt)</Label>
321
340
  <TextInput
322
341
  id="vtt-url"
323
342
  placeholder="https://example.com/subtitles.vtt"
@@ -17,6 +17,7 @@ import {useEffect, useId, useRef, useState} from 'react'
17
17
 
18
18
  import {addTextTrackFromUrl, deleteTextTrack, getAsset} from '../actions/assets'
19
19
  import {useClient} from '../hooks/useClient'
20
+ import {addKeysToMuxData} from '../util/addKeysToMuxData'
20
21
  import {generateJwt} from '../util/generateJwt'
21
22
  import {getPlaybackId} from '../util/getPlaybackPolicy'
22
23
  import {getPlaybackPolicy} from '../util/getPlaybackPolicy'
@@ -125,10 +126,11 @@ export default function EditCaptionDialog({asset, track, onUpdate, onClose}: Pro
125
126
  if (!asset._id || !asset.assetId) return
126
127
  try {
127
128
  const latestAssetData = await getAsset(client, asset.assetId)
129
+ const dataWithKeys = addKeysToMuxData(latestAssetData.data)
128
130
  await client
129
131
  .patch(asset._id)
130
- .set({data: latestAssetData.data, status: latestAssetData.data.status})
131
- .commit()
132
+ .set({data: dataWithKeys, status: latestAssetData.data.status})
133
+ .commit({returnDocuments: false})
132
134
  } catch (refreshError) {
133
135
  console.error('Failed to refresh asset data:', refreshError)
134
136
  }
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Format Google Drive share links as Google Drive export links.
3
+ * Supported formats:
4
+ * - https://drive.google.com/uc?id=<ID>...
5
+ * - https://drive.google.com/open?id=<ID>...
6
+ * - https://drive.google.com/file/d/<ID>...
7
+ * - https://drive.google.com/folder/<ID>...
8
+ *
9
+ * @param url Google Drive share link to format.
10
+ * @returns Google Drive export link (URL passthrough if not share link).
11
+ */
12
+ export function formatDriveShareLink(url: string): string {
13
+ // Export link formatter.
14
+ const formatExportLink = (id: string) => {
15
+ return `https://drive.google.com/uc?export=download&id=${id}`
16
+ }
17
+
18
+ // URL formatting.
19
+ try {
20
+ // Parse URL.
21
+ const trimmed = url.trim()
22
+ const parsed = new URL(trimmed)
23
+
24
+ // Enforce strict host name.
25
+ if (parsed.hostname !== 'drive.google.com') {
26
+ throw new Error('URL is not from Google Drive.')
27
+ }
28
+
29
+ // Look for ID in search parameters.
30
+ const id = parsed.searchParams.get('id') || ''
31
+ if (id.length) {
32
+ return formatExportLink(id)
33
+ }
34
+
35
+ // Look for ID in path name.
36
+ const path = parsed.pathname.split('/') || []
37
+
38
+ // Path is /file/d/<ID>...
39
+ if (path.includes('file') && path.includes('d')) {
40
+ const index =
41
+ path.findIndex((value: string) => {
42
+ return value === 'd'
43
+ }) + 1
44
+ const fileId = path.at(index) || ''
45
+ return formatExportLink(fileId)
46
+ }
47
+
48
+ // Path is /folder/<ID>...
49
+ if (path.includes('folders')) {
50
+ const index =
51
+ path.findIndex((value: string) => {
52
+ return value === 'folders'
53
+ }) + 1
54
+ const folderId = path.at(index) || ''
55
+ return formatExportLink(folderId)
56
+ }
57
+
58
+ // URL not recognized.
59
+ throw new Error('URL was not recognized.')
60
+ } catch {
61
+ // URL passthrough by default.
62
+ return url
63
+ }
64
+ }