Skip to content

Commit 01b6cfd

Browse files
committed
make it easier to set up
1 parent 4a138fa commit 01b6cfd

File tree

9 files changed

+75
-1157
lines changed

9 files changed

+75
-1157
lines changed

contentful-uploader/.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
MUX_ACCESS_TOKEN_ID=your-access-token-id
2+
MUX_ACCESS_TOKEN_SECRET=your-access-token-secret

contentful-uploader/README.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
A Contentful UI extension that makes it simple to add beautiful streaming via [Mux](https://https://mux.com) to your [Contentful](https://contentful.com) project. Just install the extension, add the component to your content model, and you're good to go! 🙌🏾
66

7+
Before getting started, make sure to generate a new [Access Token](https://dashboard.mux.com/settings/access-tokens). To make installation a little easier later, you can save the generated environment file as `.env` in this directory. If you're going to install the plugin [via the interface](#install-via-the-contentful-interface), just keep the token ID and secret handy.
8+
79
## Install via the CLI
810

911
Make sure you have the Contentful CLI installed:
@@ -27,7 +29,7 @@ contentful space use
2729
To install the UI Extension:
2830

2931
```
30-
contentful extension create
32+
npm run install-extension
3133
```
3234

3335
To update the UI Extension:
@@ -37,3 +39,40 @@ contentful extension update --force
3739
```
3840

3941
Note: by default this installs the hosted version of our extension. If you'd like to self-host this extension, you may update `extension.json` to use your own `src`, but keep in mind you'll need to have your own solution for handling CORS requests.
42+
43+
## Install via the Contentful interface
44+
45+
Navigation to Space settings > Extensions, then click "Add new".
46+
47+
Make sure `src` is checked, then set the value to `https://contentful.mux.dev/extension`.
48+
49+
## Setting up your content model
50+
51+
Create a new JSON field in the model you'd like to add Video to. Name the field something useful and descriptive like, for example "Mux Asset".
52+
53+
Click on the "Appearance" tab.
54+
55+
Select "Mux Contentful Uploader"
56+
57+
![Uploader Appearance](screenshots/contentful-appearance.png)
58+
59+
Congratulations, now you've got your very own best-in-class video platform! 🤘🏻
60+
61+
## Using your new Video field in clients
62+
63+
The Mux Asset JSON object you'll get in your clients will look like this:
64+
65+
```
66+
{
67+
"uploadId": "some-upload-id",
68+
"assetId": "some-asset-id",
69+
"playbackId": "a-public-playback-id"
70+
"ready": true
71+
}
72+
```
73+
74+
Both `uploadId` and `assetId` are things we need to manage the asset in the Mux API. `playbackId` is what you'll use in your player. If you want help setting up a player, we have a set of [guides](https://docs.mux.com/docs/playback) to help you get started.
75+
76+
## Talk to us!
77+
78+
This extension was made with 💖 by the folks from [Mux](https://mux.com). If you have any questions or feedback, [let us know](mailto:[email protected])!

contentful-uploader/extension.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "mux-contentful-uploader",
33
"name": "Mux Contentful Uploader",
4-
"src": "https://contentful.mux.dev",
4+
"src": "https://contentful.mux.dev/extension",
55
"fieldTypes": ["Object"],
66
"parameters": {
77
"installation": [

contentful-uploader/now.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
"version": 2,
33
"alias": "contentful.mux.dev",
44
"builds": [
5-
{
6-
"src": "/static-styles/*.css",
7-
"use": "@now/static"
8-
},
95
{
106
"src": "*.md",
117
"use": "@now/md",
@@ -18,7 +14,7 @@
1814
"content": "Add beautiful video streaming to your Contentful dashboard! Mux Video extension that's installed in minutes"
1915
}
2016
],
21-
"css": ["/static-styles/base.css", "/static-styles/mux.css"]
17+
"css": ["https://static.mux.com/css/mux-markdown.css"]
2218
}
2319
},
2420
{

contentful-uploader/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
"login": "contentful login",
3838
"logout": "contentful logout",
3939
"help": "contentful-extension-scripts help",
40-
"now-build": "npm run build"
40+
"now-build": "npm run build",
41+
"install-extension": "eval `cat .env` && contentful extension create --installation-parameters '{\"muxAccessTokenId\": \"'$MUX_TOKEN_ID'\", \"muxAccessTokenSecret\": \"'$MUX_TOKEN_SECRET'\"}'"
4142
},
4243
"browserslist": [
4344
"last 5 Chrome version",
Loading

contentful-uploader/src/index.tsx

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ interface MuxContentfulObject {
2828
interface AppState {
2929
value?: MuxContentfulObject;
3030
uploadProgress?: number;
31-
uploadError?: string;
31+
error: string | false;
3232
}
3333

3434
class App extends React.Component<AppProps, AppState> {
@@ -37,8 +37,6 @@ class App extends React.Component<AppProps, AppState> {
3737
headers: Headers;
3838
};
3939

40-
configured: boolean;
41-
4240
constructor(props: AppProps) {
4341
super(props);
4442

@@ -48,15 +46,16 @@ class App extends React.Component<AppProps, AppState> {
4846
muxAccessTokenSecret: string;
4947
};
5048

51-
this.configured = (!!muxAccessTokenId && !!muxAccessTokenSecret);
52-
5349
this.muxBaseReqOptions = {
5450
mode: 'cors',
5551
headers: this.requestHeaders(muxAccessTokenId, muxAccessTokenSecret),
5652
};
5753

5854
this.state = {
5955
value: props.sdk.field.getValue(),
56+
error:
57+
(!muxAccessTokenId || !muxAccessTokenSecret) &&
58+
"It doesn't look like you've specified your Mux Access Token ID or Secret in the extension configuration.",
6059
};
6160
}
6261

@@ -93,10 +92,7 @@ class App extends React.Component<AppProps, AppState> {
9392

9493
requestHeaders = (tokenId: string, tokenSecret: string) => {
9594
let headers = new Headers();
96-
headers.set(
97-
'Authorization',
98-
'Basic ' + btoa(`${tokenId}:${tokenSecret}`)
99-
);
95+
headers.set('Authorization', 'Basic ' + btoa(`${tokenId}:${tokenSecret}`));
10096
headers.set('Content-Type', 'application/json');
10197

10298
return headers;
@@ -117,6 +113,12 @@ class App extends React.Component<AppProps, AppState> {
117113
method: 'POST',
118114
});
119115

116+
if (res.status === 401) {
117+
throw Error(
118+
'Looks like something is wrong with the Mux Access Token specified in the config. Are you sure the token ID and secret in the extension settings match the access token you created?'
119+
);
120+
}
121+
120122
const { data: muxUpload } = await res.json();
121123

122124
await this.props.sdk.field.setValue({
@@ -126,27 +128,33 @@ class App extends React.Component<AppProps, AppState> {
126128
return muxUpload.url;
127129
};
128130

129-
onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
131+
onChange = async (e: React.ChangeEvent<HTMLInputElement>) => {
130132
const file = e.currentTarget.files && e.currentTarget.files[0];
131133
this.setState({ uploadProgress: 1 });
132134

133135
if (!file) {
134136
throw Error("Looks like a file wasn't selected");
135137
}
136138

137-
const upload = createUpload({
138-
file,
139-
endpoint: this.getUploadUrl,
140-
chunkSize: 5120, // Uploads the file in ~5mb chunks
141-
});
139+
try {
140+
const endpoint = await this.getUploadUrl();
142141

143-
upload.on('error', this.onUploadError);
144-
upload.on('progress', this.onUploadProgress);
145-
upload.on('success', this.onUploadSuccess);
142+
const upload = createUpload({
143+
file,
144+
endpoint,
145+
chunkSize: 5120, // Uploads the file in ~5mb chunks
146+
});
147+
148+
upload.on('error', this.onUploadError);
149+
upload.on('progress', this.onUploadProgress);
150+
upload.on('success', this.onUploadSuccess);
151+
} catch (error) {
152+
this.setState({ error: error.message });
153+
}
146154
};
147155

148156
onUploadError = (progress: CustomEvent) => {
149-
this.setState({ uploadError: progress.detail });
157+
this.setState({ error: progress.detail });
150158
};
151159

152160
onUploadProgress = (progress: CustomEvent) => {
@@ -216,15 +224,8 @@ class App extends React.Component<AppProps, AppState> {
216224
};
217225

218226
render = () => {
219-
if (!this.configured) {
220-
return (
221-
<Note noteType={Note.Type.NEGATIVE}>It doesn't look like you've specified your Mux Access Token ID or Secret in the extension configuration.</Note>
222-
);
223-
}
224-
if (this.state.uploadError) {
225-
return (
226-
<Note noteType={Note.Type.NEGATIVE}>{this.state.uploadError}</Note>
227-
);
227+
if (this.state.error) {
228+
return <Note noteType={Note.Type.NEGATIVE}>{this.state.error}</Note>;
228229
}
229230

230231
if (this.state.value) {

0 commit comments

Comments
 (0)