Add media_library.config.max_file_size option (#3028)

* feat: add media_library.config.max_file_size option
This commit is contained in:
Mauro Bieg 2020-01-09 18:56:11 +01:00 committed by Erez Rokah
parent d2db746b9d
commit 24a81ef9b7
4 changed files with 40 additions and 6 deletions

View File

@ -40,6 +40,7 @@ class MediaLibrary extends React.Component {
hasNextPage: PropTypes.bool, hasNextPage: PropTypes.bool,
isPaginating: PropTypes.bool, isPaginating: PropTypes.bool,
privateUpload: PropTypes.bool, privateUpload: PropTypes.bool,
config: ImmutablePropTypes.map,
loadMedia: PropTypes.func.isRequired, loadMedia: PropTypes.func.isRequired,
dynamicSearchQuery: PropTypes.string, dynamicSearchQuery: PropTypes.string,
page: PropTypes.number, page: PropTypes.number,
@ -159,18 +160,27 @@ class MediaLibrary extends React.Component {
event.persist(); event.persist();
event.stopPropagation(); event.stopPropagation();
event.preventDefault(); event.preventDefault();
const { persistMedia, privateUpload } = this.props; const { persistMedia, privateUpload, config, t } = this.props;
const { files: fileList } = event.dataTransfer || event.target; const { files: fileList } = event.dataTransfer || event.target;
const files = [...fileList]; const files = [...fileList];
const file = files[0]; const file = files[0];
const maxFileSize = config.get('max_file_size');
await persistMedia(file, { privateUpload }); if (maxFileSize && file.size > maxFileSize) {
window.alert(
t('mediaLibrary.mediaLibrary.fileTooLarge', {
size: Math.floor(maxFileSize / 1000),
}),
);
} else {
await persistMedia(file, { privateUpload });
this.setState({ selectedFile: this.props.files[0] }); this.setState({ selectedFile: this.props.files[0] });
this.scrollToTop();
}
event.target.value = null; event.target.value = null;
this.scrollToTop();
}; };
/** /**
@ -318,6 +328,7 @@ const mapStateToProps = state => {
isPersisting: mediaLibrary.get('isPersisting'), isPersisting: mediaLibrary.get('isPersisting'),
isDeleting: mediaLibrary.get('isDeleting'), isDeleting: mediaLibrary.get('isDeleting'),
privateUpload: mediaLibrary.get('privateUpload'), privateUpload: mediaLibrary.get('privateUpload'),
config: mediaLibrary.get('config'),
page: mediaLibrary.get('page'), page: mediaLibrary.get('page'),
hasNextPage: mediaLibrary.get('hasNextPage'), hasNextPage: mediaLibrary.get('hasNextPage'),
isPaginating: mediaLibrary.get('isPaginating'), isPaginating: mediaLibrary.get('isPaginating'),

View File

@ -27,6 +27,7 @@ const defaultState = {
showMediaButton: true, showMediaButton: true,
controlMedia: Map(), controlMedia: Map(),
displayURLs: Map(), displayURLs: Map(),
config: Map(),
}; };
const mediaLibrary = (state = Map(defaultState), action) => { const mediaLibrary = (state = Map(defaultState), action) => {
@ -37,7 +38,8 @@ const mediaLibrary = (state = Map(defaultState), action) => {
map.set('showMediaButton', action.payload.enableStandalone()); map.set('showMediaButton', action.payload.enableStandalone());
}); });
case MEDIA_LIBRARY_OPEN: { case MEDIA_LIBRARY_OPEN: {
const { controlID, forImage, privateUpload } = action.payload; const { controlID, forImage, privateUpload, config } = action.payload;
const libConfig = config || Map();
const privateUploadChanged = state.get('privateUpload') !== privateUpload; const privateUploadChanged = state.get('privateUpload') !== privateUpload;
if (privateUploadChanged) { if (privateUploadChanged) {
return Map({ return Map({
@ -46,6 +48,7 @@ const mediaLibrary = (state = Map(defaultState), action) => {
controlID, controlID,
canInsert: !!controlID, canInsert: !!controlID,
privateUpload, privateUpload,
config: libConfig,
controlMedia: Map(), controlMedia: Map(),
}); });
} }
@ -55,6 +58,7 @@ const mediaLibrary = (state = Map(defaultState), action) => {
map.set('controlID', controlID); map.set('controlID', controlID);
map.set('canInsert', !!controlID); map.set('canInsert', !!controlID);
map.set('privateUpload', privateUpload); map.set('privateUpload', privateUpload);
map.set('config', libConfig);
}); });
} }
case MEDIA_LIBRARY_CLOSE: case MEDIA_LIBRARY_CLOSE:

View File

@ -115,6 +115,7 @@ const en = {
}, },
mediaLibrary: { mediaLibrary: {
onDelete: 'Are you sure you want to delete selected media?', onDelete: 'Are you sure you want to delete selected media?',
fileTooLarge: 'File too large.\nConfigured to not allow files greater than %{size} kB.',
}, },
mediaLibraryModal: { mediaLibraryModal: {
loading: 'Loading...', loading: 'Loading...',

View File

@ -332,3 +332,21 @@ Template tags produce the following output:
- `{{author-login}}`: the login/username of the author - `{{author-login}}`: the login/username of the author
- `{{author-name}}`: the full name of the author (might be empty based on the user's profile) - `{{author-name}}`: the full name of the author (might be empty based on the user's profile)
## Image widget file size limit
You can set a limit to as what the maximum file size of a file is that users can upload directly into a image field.
Example config:
```yaml
- label: "Featured Image"
name: "thumbnail"
widget: "image"
default: "/uploads/chocolate-dogecoin.jpg"
media_library:
config:
max_file_size: 512000 # in bytes, only for default media library
```