<commit_before>import './InputUpload.scss';
import { string, bool, number, func, object, array, any } from 'prop-types';
import React, { Component } from 'react';
import Uppy from 'uppy/lib/core';
import AwsS3 from 'uppy/lib/plugins/AwsS3';
import Webcam from 'uppy/lib/plugins/Webcam';
import Dashboard from 'uppy/lib/plugins/Dashboard';
import StatusBar from 'uppy/lib/plugins/StatusBar';
import prettierBytes from 'prettier-bytes';
import classNames from 'classnames';
import { Label, SubmitStatusIcon, IconSmall } from 'views/components';
import { BaseEntityQuery, GennyBridge } from 'utils/genny';

class InputUpload extends Component {

    static defaultProps = {
        className: '',
        maxNumberOfFiles: 0,
        autoProceed: true,
        icon: 'add_circle',
        defaultValue: [],
        name: '',
        mandatory: false,
        identifier: null,
        validationStatus: null,
        // allowedFileTypes: ['image/jpeg', 'image/png'],
        allowedFileTypes: null,
    }

    static propTypes = {
        className: string,
        maxNumberOfFiles: number,
        icon: string,
        autoProceed: bool,
        onChange: func,
        defaultValue: object,
        value: array,
        label: string,
        validation: func,
        validationList: array,
        identifier: any,
        style: object,
        mandatory: bool,
        isHorizontal: bool,
        hideHeader: bool,
        validationStatus: string,
        name: string,
        allowedFileTypes: array,
    }

    state = {
        error: null,
        files: [],
        completeText: 'Done'
    }

    componentDidMount() {

        let files = [];
        try {
            files = (this.props.value && this.props.value != 'null') ? JSON.parse(this.props.value) : this.props.defaultValue;
        } catch (e) {}

        this.setState({
            files,
        }, () => {
            //console.log( this.state );
        });
    }

    componentDidReceiveProps(nextProps) {
        let files = [];
        try {
            files = (nextProps.value && nextProps.value != 'null') ? JSON.parse(nextProps.value) : nextProps.defaultValue;
        } catch (e) {}

        this.setState({
            files,
        }, () => {
            //console.log( this.state );
        });
    }

    componentWillMount() {

        const { autoProceed } = this.props;

        const hosturlattr = BaseEntityQuery.getBaseEntityAttribute(GennyBridge.getProject(), 'PRI_UPPY_URL');
        if (hosturlattr != null && hosturlattr.value != null) {

            this.uppy = new Uppy({
                    autoProceed,
                    debug: false,
                    restrictions: {
                        maxNumberOfFiles: this.props.maxNumberOfFiles,
                    },
                    onBeforeFileAdded: (currentFile) => this.checkFileType(currentFile)
                })
                .use(Dashboard, {
                    closeModalOnClickOutside: true,
                    // note: '.jpeg, .jpg, and .png file types allowed only',
                    hideProgressAfterFinish: true,
                })
                .use(AwsS3, { host: hosturlattr.value })
                .use(Webcam, { target: Dashboard })
                .run();

            this.uppy.on('complete', this.handleComplete);
        }
    }

    componentWillUnmount() {
        if (this.uppy) {
            this.uppy.close();
        }
        removeEventListener('hashchange', this.handleHashChange, false);
    }

    get modalName() {
        return 'uppy';
    }

    checkFileType = (currentFile) => {

        if (this.props.allowedFileTypes.includes(currentFile.type)) {
            this.uppy.info('Upload successful', 'success', 3000);
            return true;
        }
        this.uppy.info('Invalid file type', 'error', 3000);
        return false;
    }


    getIconByFileType = fileType => {
        if (fileType.includes('image'))
            return 'image';

        if (fileType.includes('video'))
            return 'videocam';

        if (fileType.includes('audio'))
            return 'audiotrack';

        if (fileType.includes('pdf'))
            return 'picture_as_pdf';

        return 'insert_drive_file';
    }<commit_msg>upload<commit_after>import './InputUpload.scss';
import { string, bool, number, func, object, array, any } from 'prop-types';
import React, { Component } from 'react';
import Uppy from 'uppy/lib/core';
import AwsS3 from 'uppy/lib/plugins/AwsS3';
import Webcam from 'uppy/lib/plugins/Webcam';
import Dashboard from 'uppy/lib/plugins/Dashboard';
import StatusBar from 'uppy/lib/plugins/StatusBar';
import prettierBytes from 'prettier-bytes';
import classNames from 'classnames';
import { Label, SubmitStatusIcon, IconSmall } from 'views/components';
import { BaseEntityQuery, GennyBridge } from 'utils/genny';

class InputUpload extends Component {

    static defaultProps = {
        className: '',
        maxNumberOfFiles: 0,
        autoProceed: true,
        icon: 'add_circle',
        defaultValue: [],
        name: '',
        mandatory: false,
        identifier: null,
        validationStatus: null,
        // allowedFileTypes: ['image/jpeg', 'image/png'],
        allowedFileTypes: null,
    }

    static propTypes = {
        className: string,
        maxNumberOfFiles: number,
        icon: string,
        autoProceed: bool,
        onChange: func,
        defaultValue: object,
        value: array,
        label: string,
        validation: func,
        validationList: array,
        identifier: any,
        style: object,
        mandatory: bool,
        isHorizontal: bool,
        hideHeader: bool,
        validationStatus: string,
        name: string,
        allowedFileTypes: array,
    }

    state = {
        error: null,
        files: [],
        completeText: 'Done'
    }

    componentDidMount() {

        let files = [];
        try {
            files = (this.props.value && this.props.value != 'null') ? JSON.parse(this.props.value) : this.props.defaultValue;
        } catch (e) {}

        this.setState({
            files,
        }, () => {
            //console.log( this.state );
        });
    }

    componentDidReceiveProps(nextProps) {
        let files = [];
        try {
            files = (nextProps.value && nextProps.value != 'null') ? JSON.parse(nextProps.value) : nextProps.defaultValue;
        } catch (e) {}

        this.setState({
            files,
        }, () => {
            //console.log( this.state );
        });
    }

    componentWillMount() {

        const { autoProceed } = this.props;

        const hosturlattr = BaseEntityQuery.getBaseEntityAttribute(GennyBridge.getProject(), 'PRI_UPPY_URL');
        if (hosturlattr != null && hosturlattr.value != null) {

            this.uppy = new Uppy({
                    autoProceed,
                    debug: false,
                    restrictions: {
                        maxNumberOfFiles: this.props.maxNumberOfFiles,
                    },
                    onBeforeFileAdded: (currentFile) => this.checkFileType(currentFile)
                })
                .use(Dashboard, {
                    closeModalOnClickOutside: true,
                    // note: '.jpeg, .jpg, and .png file types allowed only',
                    hideProgressAfterFinish: true,
                })
                .use(AwsS3, { host: hosturlattr.value })
                .use(Webcam, { target: Dashboard })
                .run();

            this.uppy.on('complete', this.handleComplete);
        }
    }

    componentWillUnmount() {
        if (this.uppy) {
            this.uppy.close();
        }
        removeEventListener('hashchange', this.handleHashChange, false);
    }

    get modalName() {
        return 'uppy';
    }

    checkFileType = (currentFile) => {

        if (this.props.allowedFileTypes && this.props.allowedFileTypes.includes(currentFile.type)) {
            this.uppy.info('Upload successful', 'success', 3000);
            return true;
        }
        this.uppy.info('Invalid file type', 'error', 3000);
        return false;
    }


    getIconByFileType = fileType => {
        if (fileType.includes('image'))
            return 'image';

        if (fileType.includes('video'))
            return 'videocam';

        if (fileType.includes('audio'))
            return 'audiotrack';

        if (fileType.includes('pdf'))
            return 'picture_as_pdf';

        return 'insert_drive_file';
    }
{'fraction_non_alphanumeric': 0.12032022604191193, 'fraction_numerical': 0.0028255238992229807, 'mean_word_length': 1.8478042239356354, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2494537', 'n_tokens_mistral': 2419, 'n_tokens_neox': 2317, 'n_words': 509}