sanity-plugin-mux-input 2.15.0 → 2.17.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.15.0",
3
+ "version": "2.17.0",
4
4
  "description": "An input component that integrates Sanity Studio with Mux video encoding/hosting service.",
5
5
  "keywords": [
6
6
  "sanity",
@@ -4,10 +4,38 @@ 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 type {MuxAsset, MuxNewAssetSettings} from '../util/types'
7
+ import {roundPxString} from '../util/roundPxString'
8
+ import type {MuxAsset, MuxNewAssetSettings, WatermarkConfig} from '../util/types'
8
9
  import {getAsset} from './assets'
9
10
  import {testSecretsObservable} from './secrets'
10
11
 
12
+ function sanitizeOverlaySettingsInPlace(settings: MuxNewAssetSettings) {
13
+ const inputs = settings.input
14
+ if (!inputs) return
15
+ for (const input of inputs) {
16
+ const overlay = (input as {overlay_settings?: Record<string, unknown>}).overlay_settings
17
+ if (!overlay) continue
18
+
19
+ const hm = roundPxString(overlay.horizontal_margin)
20
+ const vm = roundPxString(overlay.vertical_margin)
21
+ const w = roundPxString(overlay.width)
22
+
23
+ if (hm) overlay.horizontal_margin = hm
24
+ if (vm) overlay.vertical_margin = vm
25
+ if (w) overlay.width = w
26
+ }
27
+ }
28
+
29
+ function sanitizePxStringsInJson(json: string): string {
30
+ return json.replace(/"(-?\d+(?:\.\d+)?)px"/g, (_match, num) => {
31
+ const n = Number(num)
32
+ if (!Number.isFinite(n)) return _match
33
+ let rounded = Math.round(n)
34
+ if (rounded === 0) rounded = n < 0 ? -1 : 1
35
+ return `"${rounded}px"`
36
+ })
37
+ }
38
+
11
39
  export function cancelUpload(client: SanityClient, uuid: string) {
12
40
  return client.observable.request({
13
41
  url: `/addons/mux/uploads/${client.config().dataset}/${uuid}`,
@@ -20,10 +48,12 @@ export function uploadUrl({
20
48
  url,
21
49
  settings,
22
50
  client,
51
+ watermark,
23
52
  }: {
24
53
  url: string
25
54
  settings: MuxNewAssetSettings
26
55
  client: SanityClient
56
+ watermark?: WatermarkConfig
27
57
  }) {
28
58
  return testUrl(url).pipe(
29
59
  switchMap((validUrl) => {
@@ -38,9 +68,10 @@ export function uploadUrl({
38
68
  const muxBody = settings
39
69
  if (!muxBody.input) muxBody.input = [{type: 'video'}]
40
70
  muxBody.input[0].url = validUrl
71
+ sanitizeOverlaySettingsInPlace(muxBody)
41
72
 
42
73
  const query = {
43
- muxBody: JSON.stringify(muxBody),
74
+ muxBody: sanitizePxStringsInJson(JSON.stringify(muxBody)),
44
75
  filename: validUrl.split('/').slice(-1)[0],
45
76
  }
46
77
 
@@ -79,10 +110,12 @@ export function uploadFile({
79
110
  settings,
80
111
  client,
81
112
  file,
113
+ watermark,
82
114
  }: {
83
115
  settings: MuxNewAssetSettings
84
116
  client: SanityClient
85
117
  file: File
118
+ watermark?: WatermarkConfig
86
119
  }) {
87
120
  return testFile(file).pipe(
88
121
  switchMap((fileOptions) => {
@@ -95,6 +128,7 @@ export function uploadFile({
95
128
  }
96
129
  const uuid = generateUuid()
97
130
  const body = settings
131
+ sanitizeOverlaySettingsInPlace(body)
98
132
 
99
133
  return concat(
100
134
  of({type: 'uuid' as const, uuid}),
@@ -129,7 +163,7 @@ export function uploadFile({
129
163
  if (event.type !== 'success') {
130
164
  return of(event)
131
165
  }
132
- return from(updateAssetDocumentFromUpload(client, uuid)).pipe(
166
+ return from(updateAssetDocumentFromUpload(client, uuid, watermark)).pipe(
133
167
  // eslint-disable-next-line max-nested-callbacks
134
168
  mergeMap((doc) => of({...event, asset: doc}))
135
169
  )
@@ -201,7 +235,11 @@ function pollUpload(client: SanityClient, uuid: string): Promise<UploadResponse>
201
235
  })
202
236
  }
203
237
 
204
- async function updateAssetDocumentFromUpload(client: SanityClient, uuid: string) {
238
+ async function updateAssetDocumentFromUpload(
239
+ client: SanityClient,
240
+ uuid: string,
241
+ _watermark?: WatermarkConfig
242
+ ) {
205
243
  let upload: UploadResponse
206
244
  let asset: {data: MuxAsset}
207
245
  try {