Page tree
Skip to end of metadata
Go to start of metadata

1.  JSON Encoder 설치

다운로드 : JSON.phps

2. 이미지 업로드(동영상, 파일 삽입도 동일한 형태로 진행)

주의사항

가이드로 제공되는 아래 코드 중 파일 업로드 부분은 샘플 코드로서 보안 관련 처리가 미흡합니다.

파일 업로드 부분은 프로젝트 내부에서 사용하시는 부분을 그대로 사용하시고 아래 코드를 참고하셔서 연동 부분을 처리해주세요. 

<?php
    //업로드 디렉토리
    $uploadDir = 'uploads/images';

    //폼 데이터 이름
    $fieldName = 'file';

    //파일 이름
    $fileName = explode('.', $_FILES[$fieldName]['name']);

    //파일 확장자
    $extension = end($fileName);

    //임시 파일 이름
    $tmpName = $_FILES[$fieldName]['tmp_name'];

    //저장될 새로운 파일이름
    $newFileName = sha1(microtime());

    //실제 파일 업로드 경로
    $fileUploadPath = "${uploadDir}/${newFileName}.${extension}";

    //파일을 저장합니다
    move_uploaded_file($tmpName, $fileUploadPath);


	// JSON Encoder를 include 한다.
	include("./JSON.phps");

    //클라이언트로 응답을 보냅니다.
    header('Content-Type: application/json');
    echo json_encode_new(array(
        'uploadPath' => $fileUploadPath,
    ));


	function json_encode_new($data) {
		$json = new Services_JSON();
		return($json->encode($data));
	}
?>

3. HWP, MS Word, LibreOffice 문서 임포트

주의사항

가이드로 제공되는 아래 코드 중 파일 업로드 부분은 샘플 코드로서 보안 관련 처리가 미흡합니다.

파일 업로드 부분은 프로젝트 내부에서 사용하시는 부분을 그대로 사용하시고 아래 코드를 참고하셔서 연동 부분을 처리해주세요. 

<?php
    // upload path
    $uploadDir = 'uploads/docs';

    // form filed name
    $fieldName = 'file';

    // file name
    $fileName = explode('.', $_FILES[$fieldName]['name']);

    //file extension
    $extension = end($fileName);

    // temp file name
    $tmpName = $_FILES[$fieldName]['tmp_name'];

    // new file name to save
    $newFileName = sha1(microtime());

    // file upload path
    $fileUploadPath = "${uploadDir}/${newFileName}.${extension}";

    // save file to disk
    move_uploaded_file($tmpName, $fileUploadPath);

    // directory name to save conversion result
    $wordDir = 'works';

    // execute conversion
    $importPath = "${wordDir}/${newFileName}";
    executeConverter($fileUploadPath, $importPath);

    // serialize document data
	// v2.3.0 부터 파일명이 document.word.pb에서 document.pb로 변경됨
    $pbFilePath = "${importPath}/document.pb";
    $serializedData = readPBData($pbFilePath);

	include("./JSON.phps");

    // send response
    header('Content-Type: application/json');
    echo json_encode_new(array(
        'serializedData' => $serializedData,
        'importPath' => $importPath,
    ));

	function executeConverter($inputFilePath, $outputFilePath)
	{
    	$sedocConverterPath = 'c:/sedocConverter/sedocConverter.exe';
	    $fontsDir = 'c:/sedocConverter/fonts';
	    $tempDir = 'c:/sedocConverter/tmp';

	    $cmd = "${sedocConverterPath} -f ${fontsDir} ${inputFilePath} ${outputFilePath} ${tempDir}";
	    exec($cmd);
	}

	function readPBData($pbFilePath)
	{
		$filesize = filesize($pbFilePath);
		$zd = gzopen($pbFilePath, "r");
		$data = substr( gzread($zd, $filesize), 16 );
		gzclose($zd);


	    $byteArray = unpack('C*', gzuncompress($data));
	    $serializedData = array_values($byteArray);

	    return $serializedData;
	}

	function json_encode_new($data) {
		$json = new Service_JSON();
		return($json->encode($data));
	}
?>
  • No labels