<commit_before><?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Upload extends SZone_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     * 		http://example.com/index.php/welcome
     *	- or -
     * 		http://example.com/index.php/welcome/index
     *	- or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.comuploser_guide/general/urls.html
     */


    public function index() {

        $file = array(
            'md5' => $this->input->post('file_md5', TRUE),
            'tmp' => $this->input->post('file_path', TRUE),
            'mimes' => $this->input->post('file_content_type', TRUE),
            'size' => intval($this->input->post('file_size', TRUE))
        );

        $file['path'] = directory_time($this->config->item('path', 'upload'), $err);

        if (!$file['path']) {
            $err = '服务器错误：无法创建文件';
            $ret = array(
                'jsonrpc' => '2.0',
                'error' => array(
                    'code' => 1,
                    'message' => $err
                ),
            );
            $this->json($ret, 503, $err);
            return;
        } else {
            $file['path'] .= $file['md5'];
        }

        if (!$this->skey) {
            $ret = array(
                'jsonrpc' => '2.0',
                'error' => array(
                    'code' => 2,
                    'message' => '登录失效，请重新登录'
                ),
            );
            $this->json($ret, 403, '登录失效，请重新登录');
            return;
        }

        $size = $this->user['real_size'];
        $used = $this->user['real_used'] + $file['size'];

        //判断文件是否存在
        $this->load->model('File_model');
        $file_info = $this->File_model->get_by_md5($file['md5']);

        $file_name = $this->input->post('file_name', TRUE);
        $fdid = $this->input->get('fid');
        $is_media = $this->input->post('media');
        $gid = $this->input->get('gid');

        $file_isnew = FALSE;
        if (empty($file_info)) {

            if ($size < $used + $file['size']) {
                $ret = array(
                    'jsonrpc' => '2.0',
                        'error' => array(
                        'code' => 3,
                        'message' => '空间已经用完!'
                    )
                );
                $this->json($ret, 413, '空间已经用完!');

                unlink($file['tmp']);
                return;
            }
            $file['type'] = format_type($file['mimes']);
            $file['ref'] = 1;

            rename($file['tmp'], $file['path']);
            unset($file['tmp']);
            $file['id'] = $this->File_model->insert_entry($file);
            $file_isnew = TRUE;

        } else {
            $file = $file_info;

            if($gid>0){
                if($this->File_model->check_filename_by_gid($fdid,$gid,$file_name)){
                    $ret = array(
                        'jsonrpc' => '2.0',
                        'error' => array(
                            'code' => 4,
                            'message' => '上传失败,已经有重名文件'
                        )
                    );
                    $this->json($ret, 409, '上传失败,已经有重名文件!');
                    return;
                }else{
                    $file['ref'] += 1;  
                }
            }else{
                if($this->File_model->check_filename_by_uid($fdid,$this->user['id'],$file_name)){
                    $ret = array(
                        'jsonrpc' => '2.0',
                        'error' => array(
                            'code' => 4,
                            'message' => '上传失败,已经有重名文件'
                        )
                    );
                    $this->json($ret, 409, '上传失败,已经有重名文件!');
                    return;
                }else{
                    $file['ref'] += 1;  
                }
            }

            //$user_file = $this->File_model->get_by_uid($file['id'], $this->user['id']);

            // if (!empty($user_file)) {
            //     $ret = array(
            //         'jsonrpc' => '2.0',
            //         'error' => array(
            //             'code' => 4,
            //             'message' => '上传失败,已经有重名文件'
            //         )
            //     );
            //     $this->json($ret, 409, '上传失败,已经有重名文件!');
            //     return;
            // } else {
            //     $file['ref'] += 1;
            // }

        }



        // 判断是否是来自新媒体教学的上传请求
        $fdid = $this->media($is_media, $fdid);

        // 增加用户的文件记录
        $result = $this->File_model->insert_user_entry(array(
            'fid' => $file['id'],
            'name' => $is_media ? $file['md5'].'jpeg' : $file_name,
            'mark' => '',
            'uid' => $this->user['id'],
            'fdid' => $fdid
        ));

        // 增加用户的空间使用
        if (intval($result) > 0) {
            $this->load->model('User_model');
            $this->User_model->update_used($this->user['id'], $used);
        }

        // 如果小组id不为空，则增加小组的文件记录
        
        if ($gid > 0 ) {
            $this->File_model->insert_group_entry(array(
                'fid' => $file['id'],
                'fdid' => $fdid,<commit_msg>add file name support for media<commit_after><?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Upload extends SZone_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     * 		http://example.com/index.php/welcome
     *	- or -
     * 		http://example.com/index.php/welcome/index
     *	- or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.comuploser_guide/general/urls.html
     */


    public function index() {

        $file = array(
            'md5' => $this->input->post('file_md5', TRUE),
            'tmp' => $this->input->post('file_path', TRUE),
            'mimes' => $this->input->post('file_content_type', TRUE),
            'size' => intval($this->input->post('file_size', TRUE))
        );

        $file['path'] = directory_time($this->config->item('path', 'upload'), $err);

        if (!$file['path']) {
            $err = '服务器错误：无法创建文件';
            $ret = array(
                'jsonrpc' => '2.0',
                'error' => array(
                    'code' => 1,
                    'message' => $err
                ),
            );
            $this->json($ret, 503, $err);
            return;
        } else {
            $file['path'] .= $file['md5'];
        }

        if (!$this->skey) {
            $ret = array(
                'jsonrpc' => '2.0',
                'error' => array(
                    'code' => 2,
                    'message' => '登录失效，请重新登录'
                ),
            );
            $this->json($ret, 403, '登录失效，请重新登录');
            return;
        }

        $size = $this->user['real_size'];
        $used = $this->user['real_used'] + $file['size'];

        //判断文件是否存在
        $this->load->model('File_model');
        $file_info = $this->File_model->get_by_md5($file['md5']);

        $file_name = $this->input->post('file_name', TRUE);
        $fdid = $this->input->get('fid');
        $is_media = $this->input->post('media');
        $gid = $this->input->get('gid');

        $file_isnew = FALSE;
        if (empty($file_info)) {

            if ($size < $used + $file['size']) {
                $ret = array(
                    'jsonrpc' => '2.0',
                        'error' => array(
                        'code' => 3,
                        'message' => '空间已经用完!'
                    )
                );
                $this->json($ret, 413, '空间已经用完!');

                unlink($file['tmp']);
                return;
            }
            $file['type'] = format_type($file['mimes']);
            $file['ref'] = 1;

            rename($file['tmp'], $file['path']);
            unset($file['tmp']);
            $file['id'] = $this->File_model->insert_entry($file);
            $file_isnew = TRUE;

        } else {
            $file = $file_info;

            if($gid>0){
                if($this->File_model->check_filename_by_gid($fdid,$gid,$file_name)){
                    $ret = array(
                        'jsonrpc' => '2.0',
                        'error' => array(
                            'code' => 4,
                            'message' => '上传失败,已经有重名文件'
                        )
                    );
                    $this->json($ret, 409, '上传失败,已经有重名文件!');
                    return;
                }else{
                    $file['ref'] += 1;  
                }
            }else{
                if($this->File_model->check_filename_by_uid($fdid,$this->user['id'],$file_name)){
                    $ret = array(
                        'jsonrpc' => '2.0',
                        'error' => array(
                            'code' => 4,
                            'message' => '上传失败,已经有重名文件'
                        )
                    );
                    $this->json($ret, 409, '上传失败,已经有重名文件!');
                    return;
                }else{
                    $file['ref'] += 1;  
                }
            }

            //$user_file = $this->File_model->get_by_uid($file['id'], $this->user['id']);

            // if (!empty($user_file)) {
            //     $ret = array(
            //         'jsonrpc' => '2.0',
            //         'error' => array(
            //             'code' => 4,
            //             'message' => '上传失败,已经有重名文件'
            //         )
            //     );
            //     $this->json($ret, 409, '上传失败,已经有重名文件!');
            //     return;
            // } else {
            //     $file['ref'] += 1;
            // }

        }



        // 判断是否是来自新媒体教学的上传请求
        $fdid = $this->media($is_media, $fdid);

        // 增加用户的文件记录
        $result = $this->File_model->insert_user_entry(array(
            'fid' => $file['id'],
            'name' => $is_media ? $file['md5'].'.jpeg' : $file_name,
            'mark' => '',
            'uid' => $this->user['id'],
            'fdid' => $fdid
        ));

        // 增加用户的空间使用
        if (intval($result) > 0) {
            $this->load->model('User_model');
            $this->User_model->update_used($this->user['id'], $used);
        }

        // 如果小组id不为空，则增加小组的文件记录
        
        if ($gid > 0 ) {
            $this->File_model->insert_group_entry(array(
                'fid' => $file['id'],
                'fdid' => $fdid,
{'fraction_non_alphanumeric': 0.17475107335343015, 'fraction_numerical': 0.008952224353704212, 'mean_word_length': 1.176973553390336, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 185, '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': '7038866', 'n_tokens_mistral': 3583, 'n_tokens_neox': 3445, 'n_words': 532}