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 all commits
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
2 changes: 2 additions & 0 deletions docs/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ 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 `media_folder` and `public_folder` settings can also be applied for individual [Image and File widgets](widgets#imagefile-widget).

### 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
12 changes: 12 additions & 0 deletions docs/widgets.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,15 @@ collections:
searchFields: [name, twitterHandle]
valueField: name
```

### Image/File Widget

If you store your images and files in different folders, you can add `media_folder` and `public_folder` properties to the widget configuration:

```yaml
- label: Image
name: image
widget: image
media_folder: repo/path/to/assets
public_folder: /public/path/to/assets
```
7 changes: 6 additions & 1 deletion src/components/Widgets/FileControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,19 @@ export default class FileControl extends React.Component {
const fileList = e.dataTransfer ? e.dataTransfer.files : e.target.files;
const files = [...fileList];
const imageType = /^image\//;
const assetProxyOpts = {
isPrivate: this.props.field.get('private', false),
mediaFolder: this.props.field.get('media_folder'),
publicFolder: this.props.field.get('public_folder'),
};

// Return the first file on the list
const file = files[0];

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, assetProxyOpts)
.then((assetProxy) => {
this.setState({ processing: false });
this.props.onAddAsset(assetProxy);
Expand Down
6 changes: 5 additions & 1 deletion src/components/Widgets/ImageControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export default class ImageControl extends React.Component {
const fileList = e.dataTransfer ? e.dataTransfer.files : e.target.files;
const files = [...fileList];
const imageType = /^image\//;
const assetProxyOpts = {
mediaFolder: this.props.field.get('media_folder'),
publicFolder: this.props.field.get('public_folder'),
};

// Iterate through the list of files and return the first image on the list
const file = files.find((currentFile) => {
Expand All @@ -57,7 +61,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, assetProxyOpts)
.then((assetProxy) => {
this.setState({ processing: false });
this.props.onAddAsset(assetProxy);
Expand Down
25 changes: 16 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, opts = {}) {
const config = store.getState().config;
const mediaFolder = opts.mediaFolder || config.get('media_folder');
const publicFolder = opts.publicFolder || config.get('public_folder');
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 = mediaFolder && !uploaded ? resolvePath(value, mediaFolder) : value;
this.public_path = !uploaded ? resolvePath(value, publicFolder) : value;
}

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

export function createAssetProxy(value, fileObj, uploaded = false, privateUpload = false) {
export function createAssetProxy(value, fileObj, uploaded = false, opts = {}) {
const state = store.getState();
const privateUpload = opts.isPrivate || false;
const assetProxyOpts = {
mediaFolder: opts.mediaFolder,
publicFolder: opts.publicFolder
};
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, assetProxyOpts)
),
error => new AssetProxy(value, fileObj, false)
);
error => new AssetProxy(value, fileObj, false, assetProxyOpts)
);
} 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, assetProxyOpts));
}