Last active
May 2, 2023 04:12
-
-
Save jkup/51a70a4241763157d353ef61b3c03376 to your computer and use it in GitHub Desktop.
Building a video app with Cloudflare
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const cfTeamsAccessAuthMiddleware = async ({request, data, env, next}) => { | |
try { | |
const userEmail = request.headers.get("cf-access-authenticated-user-email") | |
if (!userEmail) { | |
throw new Error("User not found, make sure application is behind Cloudflare Access") | |
} | |
// Pass user info to next handlers | |
data.user = { | |
email: userEmail | |
} | |
return next() | |
} catch (e) { | |
return new Response(e.toString(), {status: 401}) | |
} | |
} | |
export const onRequest = [ | |
cfTeamsAccessAuthMiddleware | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { getSignedStreamId } from "../../src/cfStream" | |
export async function onRequestGet(context) { | |
const { | |
request, | |
env, | |
params, | |
} = context | |
const { id } = params | |
if (id) { | |
const res = await fetch(`https://api.cloudflare.com/client/v4/accounts/${env.CF_ACCOUNT_ID}/stream/${id}`, { | |
method: "GET", | |
headers: { | |
"Authorization": `Bearer ${env.CF_API_TOKEN_STREAM}` | |
} | |
}) | |
const video = (await res.json()).result | |
if (video.meta.visibility !== "public") { | |
return new Response(null, {status: 401}) | |
} | |
const signedId = await getSignedStreamId(id, env.CF_STREAM_SIGNING_KEY) | |
return new Response(JSON.stringify({ | |
signedId: `${signedId}` | |
}), { | |
headers: { | |
"content-type": "application/json" | |
} | |
}) | |
} else { | |
const url = new URL(request.url) | |
const res = await (await fetch(`https://api.cloudflare.com/client/v4/accounts/${env.CF_ACCOUNT_ID}/stream?search=${url.searchParams.get("search") || ""}`, { | |
headers: { | |
"Authorization": `Bearer ${env.CF_API_TOKEN_STREAM}` | |
} | |
})).json() | |
const filteredVideos = res.result.filter(x => x.meta.visibility === "public") | |
const videos = await Promise.all(filteredVideos.map(async x => { | |
const signedId = await getSignedStreamId(x.uid, env.CF_STREAM_SIGNING_KEY) | |
return { | |
uid: x.uid, | |
status: x.status, | |
thumbnail: `https://videodelivery.net/${signedId}/thumbnails/thumbnail.jpg`, | |
meta: { | |
name: x.meta.name | |
}, | |
created: x.created, | |
modified: x.modified, | |
duration: x.duration, | |
} | |
})) | |
return new Response(JSON.stringify(videos), {headers: {"content-type": "application/json"}}) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export async function onRequestPost(context) { | |
// Contents of context object | |
const { | |
env, | |
data, | |
} = context | |
const res = await fetch(`https://api.cloudflare.com/client/v4/accounts/${env.CF_ACCOUNT_ID}/stream/direct_upload`, { | |
method: "POST", | |
headers: { | |
"Authorization": `Bearer ${env.CF_API_TOKEN_STREAM}` | |
}, | |
body: JSON.stringify({ | |
maxDurationSeconds: 21600, | |
requireSignedURLs: true, | |
}) | |
}) | |
const newVideo = await res.json() | |
await fetch(`https://api.cloudflare.com/client/v4/accounts/${env.CF_ACCOUNT_ID}/stream/${newVideo.result.uid}`, { | |
method: "POST", | |
headers: { | |
"Authorization": `Bearer ${env.CF_API_TOKEN_STREAM}` | |
}, | |
body: JSON.stringify({ | |
meta: { | |
modified_by: data.user.email | |
} | |
}) | |
}) | |
return new Response(JSON.stringify(newVideo.result), {headers: {"content-type": "application/json"}}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function getOneTimeUploadUrl() { | |
const res = await fetch('/api/admin/videos', {method: 'POST', headers: {'accept': 'application/json'}}) | |
const upload = await res.json() | |
return upload.uploadURL | |
} | |
async function uploadVideo() { | |
const videoInput = document.getElementById("video"); | |
const oneTimeUploadUrl = await getOneTimeUploadUrl(); | |
const video = videoInput.files[0]; | |
const formData = new FormData(); | |
formData.append("file", video); | |
const uploadResult = await fetch(oneTimeUploadUrl, { | |
method: "POST", | |
body: formData, | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment