[FIX] Empty white list enables all media types upload (#1080)

* Create utils to media (canUpload)

* Fix variable name
This commit is contained in:
Djorkaeff Alexandre 2019-07-29 15:26:18 -03:00 committed by Diego Mello
parent d250f77a5d
commit 8cfdf868ed
4 changed files with 34 additions and 32 deletions

View File

@ -13,6 +13,7 @@ import Button from '../Button';
import I18n from '../../i18n';
import sharedStyles from '../../views/Styles';
import { isIOS } from '../../utils/deviceInfo';
import { canUploadFile } from '../../utils/media';
import {
COLOR_PRIMARY, COLOR_BACKGROUND_CONTAINER, COLOR_WHITE, COLOR_DANGER
} from '../../constants/colors';
@ -291,9 +292,11 @@ export default class UploadModal extends Component {
}
render() {
const { window: { width }, isVisible, close } = this.props;
const {
window: { width }, isVisible, close, file, FileUpload_MediaTypeWhiteList, FileUpload_MaxFileSize
} = this.props;
const { name, description } = this.state;
const showError = !this.canUploadFile();
const showError = !canUploadFile(file, { FileUpload_MediaTypeWhiteList, FileUpload_MaxFileSize });
return (
<Modal
isVisible={isVisible}

23
app/utils/media.js Normal file
View File

@ -0,0 +1,23 @@
export const canUploadFile = (file, serverInfo) => {
const { FileUpload_MediaTypeWhiteList, FileUpload_MaxFileSize } = serverInfo;
if (!(file && file.path)) {
return true;
}
if (file.size > FileUpload_MaxFileSize) {
return false;
}
// if white list is empty, all media types are enabled
if (!FileUpload_MediaTypeWhiteList) {
return true;
}
const allowedMime = FileUpload_MediaTypeWhiteList.split(',');
if (allowedMime.includes(file.mime)) {
return true;
}
const wildCardGlob = '/*';
const wildCards = allowedMime.filter(item => item.indexOf(wildCardGlob) > 0);
if (wildCards.includes(file.mime.replace(/(\/.*)$/, wildCardGlob))) {
return true;
}
return false;
};

View File

@ -16,6 +16,7 @@ import { isIOS, isAndroid } from '../../utils/deviceInfo';
import I18n from '../../i18n';
import { CustomIcon } from '../../lib/Icons';
import log from '../../utils/log';
import { canUploadFile } from '../../utils/media';
import DirectoryItem, { ROW_HEIGHT } from '../../presentation/DirectoryItem';
import ServerItem from '../../presentation/ServerItem';
import { CloseShareExtensionButton, CustomHeaderButtons, Item } from '../../containers/HeaderButton';
@ -130,7 +131,7 @@ export default class ShareListView extends React.Component {
name: data.filename,
description: '',
size: data.size,
type: mime.lookup(data.path),
mime: mime.lookup(data.path),
store: 'Uploads',
path: isIOS ? data.path : `file://${ data.path }`
};
@ -187,6 +188,7 @@ export default class ShareListView extends React.Component {
}
getSubscriptions = (server, fileInfo) => {
const { fileInfo: fileData } = this.state;
const { serversDB } = database.databases;
if (server) {
@ -199,7 +201,7 @@ export default class ShareListView extends React.Component {
chats: this.chats ? this.chats.slice() : [],
servers: this.servers ? this.servers.slice() : [],
loading: false,
showError: !this.canUploadFile(serverInfo, fileInfo),
showError: !canUploadFile(fileInfo || fileData, serverInfo),
serverInfo
});
this.forceUpdate();
@ -227,32 +229,6 @@ export default class ShareListView extends React.Component {
});
}
canUploadFile = (serverInfo, fileInfo) => {
const { fileInfo: fileData } = this.state;
const file = fileInfo || fileData;
const { FileUpload_MediaTypeWhiteList, FileUpload_MaxFileSize } = serverInfo;
if (!(file && file.path)) {
return true;
}
if (file.size > FileUpload_MaxFileSize) {
return false;
}
if (!FileUpload_MediaTypeWhiteList) {
return false;
}
const allowedMime = FileUpload_MediaTypeWhiteList.split(',');
if (allowedMime.includes(file.type)) {
return true;
}
const wildCardGlob = '/*';
const wildCards = allowedMime.filter(item => item.indexOf(wildCardGlob) > 0);
if (wildCards.includes(file.type.replace(/(\/.*)$/, wildCardGlob))) {
return true;
}
return false;
}
search = (text) => {
const result = database.objects('subscriptions').filtered('name CONTAINS[c] $0', text);
this.internalSetState({
@ -423,7 +399,7 @@ export default class ShareListView extends React.Component {
<View style={[styles.container, styles.centered]}>
<Text style={styles.title}>{I18n.t(errorMessage)}</Text>
<CustomIcon name='circle-cross' size={120} style={styles.errorIcon} />
<Text style={styles.fileMime}>{ file.type }</Text>
<Text style={styles.fileMime}>{ file.mime }</Text>
</View>
</View>
);

View File

@ -137,7 +137,7 @@ export default class ShareView extends React.Component {
renderPreview = () => {
const { fileInfo } = this.state;
const icon = fileInfo.type.match(/image/)
const icon = fileInfo.mime.match(/image/)
? <Image source={{ isStatic: true, uri: fileInfo.path }} style={styles.mediaImage} />
: (
<View style={styles.mediaIconContainer}>