Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

media_folder on widged level #492

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ This configuration adds a new setting, `public_folder`. While `media_folder` spe

>If `public_folder` is not set, Netlify CMS will default to the same value as `media_folder`, adding an opening `/` if one is not included.

The same settings can be applied on Image/File widget if you store your file in different folders.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to keep things minimal in the quick start docs, but this information would be good in docs/widgets.md. Extra sections for the image and file widgets can be added, similar to how the relation widget has a separate section.

E.g. you have cover images for your posts in assets/postcovers folder so you just add __media_folder__ and __public_folder__ to your post image widged conf.
It would then look like this:

```
...
- {label: "Cover Image", name: "image", widget: "image", media_folder: "assets/postcovers", public_folder: "/assets/postcovers"}
...
```

### Collections
Collections define the structure for the different content types on your static site. Since every site is different, the `collections` settings will be very different from one site to the next. Let's say your site has a blog, with the posts stored in `_posts/blog`, and files saved in a date-title format, like `1999-12-31-lets-party.md`. Each post
begins with settings in yaml-formatted front matter, like so:
Expand Down
2 changes: 1 addition & 1 deletion src/components/Widgets/FileControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default class FileControl extends React.Component {
this.props.onRemoveAsset(this.props.value);
if (file) {
this.setState({ processing: true });
this.promise = createAssetProxy(file.name, file, false, this.props.field.get('private', false))
this.promise = createAssetProxy(file.name, file, false, this.props.field)
.then((assetProxy) => {
this.setState({ processing: false });
this.props.onAddAsset(assetProxy);
Expand Down
2 changes: 1 addition & 1 deletion src/components/Widgets/ImageControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default class ImageControl extends React.Component {
this.props.onRemoveAsset(this.props.value);
if (file) {
this.setState({ processing: true });
this.promise = createAssetProxy(file.name, file)
this.promise = createAssetProxy(file.name, file, false, this.props.field)
.then((assetProxy) => {
this.setState({ processing: false });
this.props.onAddAsset(assetProxy);
Expand Down
21 changes: 12 additions & 9 deletions src/valueObjects/AssetProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ export const setStore = (storeObj) => {
store = storeObj;
};

export default function AssetProxy(value, fileObj, uploaded = false) {
export default function AssetProxy(value, fileObj, uploaded = false, field_config = null) {
const config = store.getState().config;
const media_folder = field_config && field_config.has('media_folder') ? field_config.get('media_folder') : config.get('media_folder');
const public_folder = field_config && field_config.has('public_folder') ? field_config.get('public_folder') : '/' + media_folder;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These can shorten up a bit, e.g.:

const media_folder = field_config && field_config.get('media_folder') || config.get('media_folder');

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot see if your version has any sideeffects. I prefer longer but self-explaining version

this.value = value;
this.fileObj = fileObj;
this.uploaded = uploaded;
this.sha = null;
this.path = config.get('media_folder') && !uploaded ? resolvePath(value, config.get('media_folder')) : value;
this.public_path = !uploaded ? resolvePath(value, config.get('public_folder')) : value;
this.path = media_folder && !uploaded ? resolvePath(value, media_folder) : value;
this.public_path = !uploaded ? resolvePath(value, public_folder) : value;
}

AssetProxy.prototype.toString = function () {
Expand All @@ -39,20 +41,21 @@ AssetProxy.prototype.toBase64 = function () {
});
};

export function createAssetProxy(value, fileObj, uploaded = false, privateUpload = false) {
export function createAssetProxy(value, fileObj, uploaded = false, field_config = null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

field_config should be field for consistency with other parts of the codebase.

const state = store.getState();
const privateUpload = field_config ? field_config.get('private', false) : false;
const integration = selectIntegration(state, null, 'assetStore');
if (integration && !uploaded) {
const provider = integration && getIntegrationProvider(state.integrations, currentBackend(state.config).getToken, integration);
return provider.upload(fileObj, privateUpload).then(
response => (
new AssetProxy(response.assetURL.replace(/^(https?):/, ''), null, true)
new AssetProxy(response.assetURL.replace(/^(https?):/, ''), null, true, field_config)
),
error => new AssetProxy(value, fileObj, false)
);
error => new AssetProxy(value, fileObj, false, field_config)
);
} else if (privateUpload) {
throw new Error('The Private Upload option is only avaible for Asset Store Integration');
}
return Promise.resolve(new AssetProxy(value, fileObj, uploaded));

return Promise.resolve(new AssetProxy(value, fileObj, uploaded, field_config));
}