-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add remote url support as string
- Loading branch information
Showing
11 changed files
with
533 additions
and
23 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import path from 'node:path'; | ||
|
||
export const VIDEOS_PATH = path.join(process.cwd(), '/videos'); | ||
export const VIDEOS_DIR = 'videos'; | ||
export const VIDEOS_PATH = path.join(process.cwd(), VIDEOS_DIR); | ||
export const PACKAGE_NAME = '@mux/next-video'; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import chalk from 'chalk'; | ||
import Mux from '@mux/mux-node'; | ||
import { fetch as uFetch } from 'undici'; | ||
import { updateAsset, Asset } from '../assets.js'; | ||
import log from '../logger.js'; | ||
import { pollForAssetReady } from './local-upload.js'; | ||
|
||
let mux: Mux; | ||
|
||
// We don't want to blow things up immediately if Mux isn't configured, but we also don't want to | ||
// need to initialize it every time in situations like polling. So we'll initialize it lazily but cache | ||
// the instance. | ||
function initMux() { | ||
mux = new Mux(); | ||
} | ||
|
||
function sleep(ms: number) { | ||
return new Promise((resolve) => setTimeout(resolve, ms)); | ||
} | ||
|
||
export default async function handleRemoteVideoAdded(asset: Asset) { | ||
if (!asset.originalFilePath) { | ||
log.error('No URL provided for asset.'); | ||
console.error(asset); | ||
return; | ||
} | ||
|
||
initMux(); | ||
|
||
if (asset.status === 'ready') { | ||
return; | ||
} else if (asset.status === 'processing') { | ||
log.info(log.label('Asset is already processing. Polling for completion:'), asset.originalFilePath); | ||
return pollForAssetReady(asset.originalFilePath, asset); | ||
} | ||
|
||
const src = asset.originalFilePath; | ||
|
||
const assetObj = await mux.video.assets.create({ | ||
// @ts-ignore | ||
input: [{ | ||
url: asset.originalFilePath | ||
}], | ||
playback_policy: ['public'] | ||
}); | ||
|
||
log.info(log.label('Asset is processing:'), src); | ||
log.space(chalk.gray('>'), log.label('Mux Asset ID:'), assetObj.id); | ||
|
||
const processingAsset = await updateAsset(src, { | ||
status: 'processing', | ||
externalIds: { | ||
assetId: assetObj.id, | ||
}, | ||
}); | ||
|
||
return pollForAssetReady(src, processingAsset); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,20 @@ | ||
import videoHandler, { callHandler } from './video-handler.js'; | ||
import localUploadHandler from './handlers/local-upload.js'; | ||
import remoteRequestHandler from './handlers/remote-request.js'; | ||
import log from './logger.js'; | ||
import withNextVideo from './with-next-video.js'; | ||
|
||
try { | ||
// Don't love this little race condition... we gotta figure that one out. | ||
// Basically we need to make sure all the handlers are registered before we start watching for files. | ||
videoHandler('local.video.added', localUploadHandler); | ||
videoHandler('remote.video.added', remoteRequestHandler); | ||
|
||
} catch (err) { | ||
// We'd much prefer to log an error here than crash since it can put | ||
// the main Next process in a weird state. | ||
log.error('An exception occurred within next-video. You may need to restart your dev server.'); | ||
console.error(err); | ||
} | ||
|
||
import withNextVideo from './with-next-video.js'; | ||
|
||
export { videoHandler, withNextVideo, callHandler }; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import type { NextApiRequest, NextApiResponse } from 'next'; | ||
|
||
import { callHandler } from './main.js'; | ||
import { createAsset, getAsset } from './assets.js'; | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
|
||
const url = String(req.query.url); | ||
if (!url) { | ||
res.status(400).json({ error: 'url parameter is required' }); | ||
return; | ||
} | ||
|
||
const remoteRegex = /^https?:\/\//; | ||
const isRemote = remoteRegex.test(url); | ||
|
||
if (!isRemote) { | ||
// todo: handle local files via string src | ||
res.status(400).json({ error: 'local files should be imported as a module' }); | ||
return; | ||
} | ||
|
||
let asset; | ||
try { | ||
asset = await getAsset(url); | ||
} catch { | ||
// todo: does this require auth? | ||
asset = await createAsset(url); | ||
await callHandler('remote.video.added', asset); | ||
|
||
res.status(200).json(asset); | ||
return; | ||
} | ||
|
||
res.status(200).json(asset); | ||
} |
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