Skip to content

Commit

Permalink
docs: add metadata storage hooks docs
Browse files Browse the repository at this point in the history
  • Loading branch information
luwes committed Oct 28, 2024
1 parent 7665b2e commit 25e88f0
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,86 @@ export default function Page() {
```
</details>

### Asset metadata storage hooks (callbacks)

By default the asset metadata is stored in a JSON file in the `/videos` directory.
If you want to store the metadata in a database or elsewhere you can customize
the storage hooks in a separate next-video config file.

The below example config shows the default storage hooks for the JSON file storage.

These hooks can be customized to fit your needs by changing the body of the
`loadAsset`, `saveAsset`, and `updateAsset` functions.

```js
// next-video.mjs
import { NextVideo } from 'next-video/process';
import path from 'node:path';
import { mkdir, readFile, writeFile } from 'node:fs/promises';

export const { GET, POST, handler, withNextVideo } = NextVideo({
// Other next-video config options should be added here if using a next-video config file.
// folder: 'videos',
// path: '/api/video',

loadAsset: async function (assetPath) {
const file = await readFile(assetPath);
const asset = JSON.parse(file.toString());
return asset;
},
saveAsset: async function (assetPath, asset) {
try {
await mkdir(path.dirname(assetPath), { recursive: true });
await writeFile(assetPath, JSON.stringify(asset), {
flag: 'wx',
});
} catch (err) {
if (err.code === 'EEXIST') {
// The file already exists, and that's ok in this case. Ignore the error.
return;
}
throw err;
}
},
updateAsset: async function (assetPath, asset) {
await writeFile(assetPath, JSON.stringify(asset));
}
});
```

Then import the `withNextVideo` function in your `next.config.mjs` file.

```js
// next.config.mjs
import { withNextVideo } from './next-video.mjs';

/** @type {import('next').NextConfig} */
const nextConfig = {};

export default withNextVideo(nextConfig);
```

Lastly import the `GET` and `POST`, or `handler` functions in your API routes as you see fit.
The handlers expect a `url` query or body parameter with the video source URL.

These are the most minimal examples for the handlers, typically you would add
more error handling and validation, authentication and authorization.

**App router (Next.js >=13)**

```js
// app/api/video/route.js
export { GET, POST } from '@/next-video';
```

**Pages router (Next.js)**

```js
// pages/api/video/[[...handler]].js
export { handler as default } from '@/next-video';
```


### Player only ([Demo](https://next-video-demo.vercel.app/player-only))

Since [`v1.1.0`](https://github.com/muxinc/next-video/releases/tag/v1.1.0) you can import the player component directly and use it without any upload and processing features.
Expand Down

0 comments on commit 25e88f0

Please sign in to comment.