티스토리 뷰

xe 마이그레이션 시 첨부파일을 base64 인코딩하여 마이그레이션을 한다.


과연 어떤과정을 거칠까 궁금하나 하나씩 뜯어보기로 했다.


importer.admin.controller.php 파일의 importAttaches 파일은 xe로 import 하는 부분이다. 과연 어떻게 하나 살펴보자.


/**
     * Import attachment
     * @param resource $fp
     * @param int $module_srl
     * @param int $upload_target_srl
     * @param array $files
     * @return int
     */
    function importAttaches($fp, $module_srl, $upload_target_srl, &$files)
    {
        $uploaded_count = 0;

        $started = false;
        $buff = null;

        $file_obj = new stdClass;
        while(!feof($fp))
        {
            $str = trim(fgets($fp, 1024));
            // If it ends with </attaches>, break
            if(trim($str) == '</attaches>') break;
            // If it starts with <attach>, collect attachments
            if(trim($str) == '<attach>')
            {
                $file_obj->file_srl = getNextSequence();
                $file_obj->upload_target_srl = $upload_target_srl;
                $file_obj->module_srl = $module_srl;

                $started = true;
                $buff = null;
                // If it starts with <file>, handle the attachement in xml file
            }
            else if(trim($str) == '<file>')
            {
                $file_obj->file = $this->saveTemporaryFile($fp);
                continue;
            }

            if($started) $buff .= $str;
            // If it ends with </attach>, handle attachements
            if(trim($str) == '</attach>')
            {
                $xmlDoc = $this->oXmlParser->parse($buff.$str);

                $file_obj->source_filename = base64_decode($xmlDoc->attach->filename->body);
                $file_obj->download_count = base64_decode($xmlDoc->attach->download_count->body);

                if(!$file_obj->file)
                {
                    $url = base64_decode($xmlDoc->attach->url->body);
                    $path = base64_decode($xmlDoc->attach->path->body);
                    if($path && file_exists($path)) $file_obj->file = $path;
                    else
                    {
                        $file_obj->file = $this->getTmpFilename();
                        FileHandler::getRemoteFile($url, $file_obj->file);
                    }
                }

                if(file_exists($file_obj->file))
                {
                    $random = new Password();
                    // Set upload path by checking if the attachement is an image or other kind of file
                    if(preg_match("/\.(jpe?g|gif|png|wm[va]|mpe?g|avi|swf|flv|mp[1-4]|as[fx]|wav|midi?|moo?v|qt|r[am]{1,2}|m4v)$/i", $file_obj->source_filename))
                    {
                        // Immediately remove the direct file if it has any kind of extensions for hacking
                        $file_obj->source_filename = preg_replace('/\.(php|phtm|phar|html?|cgi|pl|exe|jsp|asp|inc)/i', '$0-x', $file_obj->source_filename);
                        $file_obj->source_filename = str_replace(array('<', '>'), array('%3C', '%3E'), $file_obj->source_filename);

                        $path = sprintf("./files/attach/images/%s/%s", $module_srl, getNumberingPath($upload_target_srl, 3));

                        $ext = substr(strrchr($file_obj->source_filename,'.'),1);
                        $_filename = $random->createSecureSalt(32, 'hex').'.'.$ext;
                        $filename = $path.$_filename;

                        $idx = 1;
                        while(file_exists($filename))
                        {
                            $filename = $path.preg_replace('/\.([a-z0-9]+)$/i','_'.$idx.'.$1', $_filename);
                            $idx++;
                        }

                        $file_obj->direct_download = 'Y';
                    }
                    else
                    {
                        $path = sprintf("./files/attach/binaries/%s/%s", $module_srl, getNumberingPath($upload_target_srl,3));
                        $filename = $path.$random->createSecureSalt(32, 'hex');
                        $file_obj->direct_download = 'N';
                    }
                    // Create a directory
                    if(!FileHandler::makeDir($path)) continue;

                    if(strncmp('./files/cache/importer/', $file_obj->file, 23) === 0)
                    {
                        FileHandler::rename($file_obj->file, $filename);
                    }
                    else
                    {
                        copy($file_obj->file, $filename);
                    }

                    // Insert the file to the DB
                    unset($file_obj->file);
                    if(file_exists($filename))
                    {
                        $file_obj->uploaded_filename = $filename;
                        $file_obj->file_size = filesize($filename);
                        $file_obj->comment = NULL;
                        $file_obj->member_srl = 0;
                        $file_obj->sid = $random->createSecureSalt(32, 'hex');
                        $file_obj->isvalid = 'Y';
                        $output = executeQuery('file.insertFile', $file_obj);

                        if($output->toBool())
                        {
                            $uploaded_count++;
                            $tmp_obj = null;
                            $tmp_obj->source_filename = $file_obj->source_filename;
                            if($file_obj->direct_download == 'Y') $files[$file_obj->source_filename] = $file_obj->uploaded_filename;
                            else $files[$file_obj->source_filename] = getUrl('','module','file','act','procFileDownload','file_srl',$file_obj->file_srl,'sid',$file_obj->sid);
                        }
                    }
                }
            }
        }
        return $uploaded_count;
    }


댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함