@maas/payload-plugin-media-cloud 0.0.14 → 0.0.15
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/dist/components/upload-handler/upload-handler.mjs +1 -1
- package/dist/endpoints/muxWebhookHandler.mjs +7 -1
- package/dist/endpoints/muxWebhookHandler.mjs.map +1 -1
- package/dist/types/index.d.mts +4 -0
- package/dist/utils/mux.d.mts +3 -3
- package/dist/utils/tus.d.mts +2 -2
- package/package.json +1 -1
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
import { MediaCloudErrors } from "../../types/errors.mjs";
|
|
4
4
|
import { useErrorHandler } from "../../hooks/useErrorHandler.mjs";
|
|
5
|
-
import { getFileType, isVideo, sanitizeFilename } from "../../utils/file.mjs";
|
|
6
5
|
import { useMediaCloudEmitter } from "../../hooks/useMediaCloudEmitter.mjs";
|
|
6
|
+
import { getFileType, isVideo, sanitizeFilename } from "../../utils/file.mjs";
|
|
7
7
|
import * as upchunk from "@mux/upchunk";
|
|
8
8
|
import * as tus from "tus-js-client";
|
|
9
9
|
import { toast } from "@payloadcms/ui";
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { MediaCloudErrors } from "../types/errors.mjs";
|
|
2
2
|
import { useErrorHandler } from "../hooks/useErrorHandler.mjs";
|
|
3
|
+
import { useMediaCloudEmitter } from "../hooks/useMediaCloudEmitter.mjs";
|
|
3
4
|
|
|
4
5
|
//#region src/endpoints/muxWebhookHandler.ts
|
|
5
6
|
const { logError } = useErrorHandler();
|
|
7
|
+
const { emit } = useMediaCloudEmitter();
|
|
6
8
|
function getMuxWebhookHandler(args) {
|
|
7
9
|
const { getMuxClient } = args;
|
|
8
10
|
return async (req) => {
|
|
@@ -44,7 +46,7 @@ async function updateMuxAsset(args) {
|
|
|
44
46
|
pagination: false
|
|
45
47
|
});
|
|
46
48
|
if (docs.length > 0) {
|
|
47
|
-
const { id } = docs[0];
|
|
49
|
+
const { id, filename } = docs[0];
|
|
48
50
|
const width = Array.from(asset.tracks ?? []).reduce((max, track) => track.type === "video" && track.max_width ? Math.max(max, track.max_width) : max, 0);
|
|
49
51
|
const height = Array.from(asset.tracks ?? []).reduce((max, track) => track.type === "video" && track.max_height ? Math.max(max, track.max_height) : max, 0);
|
|
50
52
|
await payload.update({
|
|
@@ -66,6 +68,10 @@ async function updateMuxAsset(args) {
|
|
|
66
68
|
height
|
|
67
69
|
}
|
|
68
70
|
});
|
|
71
|
+
emit("assetUpdated", {
|
|
72
|
+
id,
|
|
73
|
+
filename
|
|
74
|
+
});
|
|
69
75
|
}
|
|
70
76
|
}
|
|
71
77
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"muxWebhookHandler.mjs","names":[],"sources":["../../src/endpoints/muxWebhookHandler.ts"],"sourcesContent":["import { useErrorHandler } from '../hooks/useErrorHandler'\nimport { MediaCloudErrors } from '../types/errors'\n\nimport type { Mux } from '@mux/mux-node'\nimport type { BasePayload, PayloadHandler } from 'payload'\nimport type { StaticRenditions } from '../types'\n\ninterface GetMuxWebhookHandlerArgs {\n getMuxClient: () => Mux\n}\n\nconst { logError } = useErrorHandler()\n\nexport function getMuxWebhookHandler(\n args: GetMuxWebhookHandlerArgs\n): PayloadHandler {\n const { getMuxClient } = args\n\n return async (req) => {\n const mux = getMuxClient()\n\n try {\n const body = req.text ? await req.text() : ''\n const headers = req.headers\n\n if (!body) {\n logError(MediaCloudErrors.MUX_WEBHOOK_BODY_INVALID.message)\n }\n\n // Verify the webhook signature\n mux.webhooks.verifySignature(body, headers)\n\n // Parse the request body\n const event = JSON.parse(body)\n\n // Handle the event\n switch (event.type) {\n case 'video.asset.created':\n case 'video.asset.errored':\n case 'video.asset.deleted':\n console.log(`Received Mux webhook: ${event.type}, ${event.object.id}`)\n break\n case 'video.asset.ready':\n case 'video.asset.static_renditions.ready':\n case 'video.asset.static_renditions.deleted':\n await updateMuxAsset({ asset: event.object, payload: req.payload })\n break\n }\n\n return Response.json({ message: 'Webhook received' }, { status: 200 })\n } catch (error) {\n return Response.json(\n { message: error instanceof Error ? error.message : String(error) },\n { status: 500 }\n )\n }\n }\n}\n\ninterface UpdateMuxAssetArgs {\n asset: Mux.Video.Asset\n payload: BasePayload\n}\n\nasync function updateMuxAsset(args: UpdateMuxAssetArgs): Promise<void> {\n const { asset, payload } = args\n\n if (asset.status === 'ready') {\n const { docs } = await payload.find({\n collection: 'media',\n where: {\n 'mux.assetId': {\n equals: asset.id,\n },\n },\n limit: 1,\n pagination: false,\n })\n\n if (docs.length > 0) {\n const { id } = docs[0]\n\n const width = Array.from(asset.tracks ?? []).reduce(\n (max, track) =>\n track.type === 'video' && track.max_width\n ? Math.max(max, track.max_width)\n : max,\n 0\n )\n\n const height = Array.from(asset.tracks ?? []).reduce(\n (max, track) =>\n track.type === 'video' && track.max_height\n ? Math.max(max, track.max_height)\n : max,\n 0\n )\n\n await payload.update({\n collection: 'media',\n id,\n data: {\n mux: {\n status: asset.status,\n assetId: asset.id,\n playbackId: asset.playback_ids?.[0]?.id,\n aspectRatio: asset.aspect_ratio,\n duration: asset.duration,\n tracks: asset.tracks,\n maxResolutionTier: asset.max_resolution_tier,\n videoQuality: asset.video_quality,\n staticRenditions: asset.static_renditions as StaticRenditions,\n },\n width,\n height,\n },\n })\n }\n }\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"muxWebhookHandler.mjs","names":[],"sources":["../../src/endpoints/muxWebhookHandler.ts"],"sourcesContent":["import { useErrorHandler } from '../hooks/useErrorHandler'\nimport { MediaCloudErrors } from '../types/errors'\n\nimport type { Mux } from '@mux/mux-node'\nimport type { BasePayload, PayloadHandler } from 'payload'\nimport type { StaticRenditions } from '../types'\nimport { useMediaCloudEmitter } from '../hooks/useMediaCloudEmitter'\n\ninterface GetMuxWebhookHandlerArgs {\n getMuxClient: () => Mux\n}\n\nconst { logError } = useErrorHandler()\nconst { emit } = useMediaCloudEmitter()\n\nexport function getMuxWebhookHandler(\n args: GetMuxWebhookHandlerArgs\n): PayloadHandler {\n const { getMuxClient } = args\n\n return async (req) => {\n const mux = getMuxClient()\n\n try {\n const body = req.text ? await req.text() : ''\n const headers = req.headers\n\n if (!body) {\n logError(MediaCloudErrors.MUX_WEBHOOK_BODY_INVALID.message)\n }\n\n // Verify the webhook signature\n mux.webhooks.verifySignature(body, headers)\n\n // Parse the request body\n const event = JSON.parse(body)\n\n // Handle the event\n switch (event.type) {\n case 'video.asset.created':\n case 'video.asset.errored':\n case 'video.asset.deleted':\n console.log(`Received Mux webhook: ${event.type}, ${event.object.id}`)\n break\n case 'video.asset.ready':\n case 'video.asset.static_renditions.ready':\n case 'video.asset.static_renditions.deleted':\n await updateMuxAsset({ asset: event.object, payload: req.payload })\n break\n }\n\n return Response.json({ message: 'Webhook received' }, { status: 200 })\n } catch (error) {\n return Response.json(\n { message: error instanceof Error ? error.message : String(error) },\n { status: 500 }\n )\n }\n }\n}\n\ninterface UpdateMuxAssetArgs {\n asset: Mux.Video.Asset\n payload: BasePayload\n}\n\nasync function updateMuxAsset(args: UpdateMuxAssetArgs): Promise<void> {\n const { asset, payload } = args\n\n if (asset.status === 'ready') {\n const { docs } = await payload.find({\n collection: 'media',\n where: {\n 'mux.assetId': {\n equals: asset.id,\n },\n },\n limit: 1,\n pagination: false,\n })\n\n if (docs.length > 0) {\n const { id, filename } = docs[0]\n\n const width = Array.from(asset.tracks ?? []).reduce(\n (max, track) =>\n track.type === 'video' && track.max_width\n ? Math.max(max, track.max_width)\n : max,\n 0\n )\n\n const height = Array.from(asset.tracks ?? []).reduce(\n (max, track) =>\n track.type === 'video' && track.max_height\n ? Math.max(max, track.max_height)\n : max,\n 0\n )\n\n await payload.update({\n collection: 'media',\n id,\n data: {\n mux: {\n status: asset.status,\n assetId: asset.id,\n playbackId: asset.playback_ids?.[0]?.id,\n aspectRatio: asset.aspect_ratio,\n duration: asset.duration,\n tracks: asset.tracks,\n maxResolutionTier: asset.max_resolution_tier,\n videoQuality: asset.video_quality,\n staticRenditions: asset.static_renditions as StaticRenditions,\n },\n width,\n height,\n },\n })\n\n emit('assetUpdated', { id, filename })\n }\n }\n}\n"],"mappings":";;;;;AAYA,MAAM,EAAE,aAAa,iBAAiB;AACtC,MAAM,EAAE,SAAS,sBAAsB;AAEvC,SAAgB,qBACd,MACgB;CAChB,MAAM,EAAE,iBAAiB;AAEzB,QAAO,OAAO,QAAQ;EACpB,MAAM,MAAM,cAAc;AAE1B,MAAI;GACF,MAAM,OAAO,IAAI,OAAO,MAAM,IAAI,MAAM,GAAG;GAC3C,MAAM,UAAU,IAAI;AAEpB,OAAI,CAAC,KACH,UAAS,iBAAiB,yBAAyB,QAAQ;AAI7D,OAAI,SAAS,gBAAgB,MAAM,QAAQ;GAG3C,MAAM,QAAQ,KAAK,MAAM,KAAK;AAG9B,WAAQ,MAAM,MAAd;IACE,KAAK;IACL,KAAK;IACL,KAAK;AACH,aAAQ,IAAI,yBAAyB,MAAM,KAAK,IAAI,MAAM,OAAO,KAAK;AACtE;IACF,KAAK;IACL,KAAK;IACL,KAAK;AACH,WAAM,eAAe;MAAE,OAAO,MAAM;MAAQ,SAAS,IAAI;MAAS,CAAC;AACnE;;AAGJ,UAAO,SAAS,KAAK,EAAE,SAAS,oBAAoB,EAAE,EAAE,QAAQ,KAAK,CAAC;WAC/D,OAAO;AACd,UAAO,SAAS,KACd,EAAE,SAAS,iBAAiB,QAAQ,MAAM,UAAU,OAAO,MAAM,EAAE,EACnE,EAAE,QAAQ,KAAK,CAChB;;;;AAUP,eAAe,eAAe,MAAyC;CACrE,MAAM,EAAE,OAAO,YAAY;AAE3B,KAAI,MAAM,WAAW,SAAS;EAC5B,MAAM,EAAE,SAAS,MAAM,QAAQ,KAAK;GAClC,YAAY;GACZ,OAAO,EACL,eAAe,EACb,QAAQ,MAAM,IACf,EACF;GACD,OAAO;GACP,YAAY;GACb,CAAC;AAEF,MAAI,KAAK,SAAS,GAAG;GACnB,MAAM,EAAE,IAAI,aAAa,KAAK;GAE9B,MAAM,QAAQ,MAAM,KAAK,MAAM,UAAU,EAAE,CAAC,CAAC,QAC1C,KAAK,UACJ,MAAM,SAAS,WAAW,MAAM,YAC5B,KAAK,IAAI,KAAK,MAAM,UAAU,GAC9B,KACN,EACD;GAED,MAAM,SAAS,MAAM,KAAK,MAAM,UAAU,EAAE,CAAC,CAAC,QAC3C,KAAK,UACJ,MAAM,SAAS,WAAW,MAAM,aAC5B,KAAK,IAAI,KAAK,MAAM,WAAW,GAC/B,KACN,EACD;AAED,SAAM,QAAQ,OAAO;IACnB,YAAY;IACZ;IACA,MAAM;KACJ,KAAK;MACH,QAAQ,MAAM;MACd,SAAS,MAAM;MACf,YAAY,MAAM,eAAe,IAAI;MACrC,aAAa,MAAM;MACnB,UAAU,MAAM;MAChB,QAAQ,MAAM;MACd,mBAAmB,MAAM;MACzB,cAAc,MAAM;MACpB,kBAAkB,MAAM;MACzB;KACD;KACA;KACD;IACF,CAAC;AAEF,QAAK,gBAAgB;IAAE;IAAI;IAAU,CAAC"}
|
package/dist/types/index.d.mts
CHANGED
package/dist/utils/mux.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { MediaCloudPluginOptions } from "../types/index.mjs";
|
|
2
2
|
import Mux from "@mux/mux-node";
|
|
3
|
-
import * as
|
|
3
|
+
import * as payload0 from "payload";
|
|
4
4
|
|
|
5
5
|
//#region src/utils/mux.d.ts
|
|
6
6
|
|
|
@@ -14,11 +14,11 @@ interface CreateMuxEndpointsArgs {
|
|
|
14
14
|
pluginOptions: MediaCloudPluginOptions;
|
|
15
15
|
}
|
|
16
16
|
declare function createMuxEndpoints(args: CreateMuxEndpointsArgs): ({
|
|
17
|
-
handler:
|
|
17
|
+
handler: payload0.PayloadHandler;
|
|
18
18
|
method: "post";
|
|
19
19
|
path: string;
|
|
20
20
|
} | {
|
|
21
|
-
handler:
|
|
21
|
+
handler: payload0.PayloadHandler;
|
|
22
22
|
method: "get";
|
|
23
23
|
path: string;
|
|
24
24
|
})[];
|
package/dist/utils/tus.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { MediaCloudPluginOptions } from "../types/index.mjs";
|
|
2
2
|
import { S3Store } from "../tus/stores/s3/s3-store.mjs";
|
|
3
|
-
import * as
|
|
3
|
+
import * as payload2 from "payload";
|
|
4
4
|
import { Config, PayloadRequest } from "payload";
|
|
5
5
|
import { DataStore, Server } from "@tus/server";
|
|
6
6
|
|
|
@@ -47,7 +47,7 @@ declare function createTusEndpoints(args: CreateTusEndpointsArgs): ({
|
|
|
47
47
|
method: "delete";
|
|
48
48
|
path: string;
|
|
49
49
|
} | {
|
|
50
|
-
handler:
|
|
50
|
+
handler: payload2.PayloadHandler;
|
|
51
51
|
method: "get";
|
|
52
52
|
path: string;
|
|
53
53
|
})[];
|