Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languageruby
title./app/controller/upload_file_controller.rb
class UploadFileController < ApplicationController
	skip_before_action :verify_authenticity_token # csrf_token
  
	def upload
		...
	        uploaded = UploadFile.create(file: params[:file]) 


        render json:{
            "uploadPath": uploaded.file.url
        }
    end

 	   def import
		...
	
        uploaded = UploadFile.create(file: params[:file])

        # 1. 문서 변환
        doc_file_path = uploaded.file.path
        output_dir_path = execute_converter(doc_file_path)

        # 2. PB 데이터 직렬화
        pb_file_path = "%s/document.pb" % [output_dir_path]
        aSerialized = serialized_pb(pb_file_path)

        # 3. 불필요한 파일 삭제
        File.delete(doc_file_path) if File.exist?(doc_file_path)
        File.delete(pb_file_path) if File.exist?(pb_file_path)
        
        render json: {
            "importPath": "/output/%s" % [File.basename(output_dir_path)],
            "serializedData": aSerialized
        }
    end

    # 변환 모듈을 실행하여 문서를 변환합니다.
    def execute_converter(input_file_path)
        root_path = Rails.root
        input_file_name = File.basename(input_file_path)
        output_dir_name = File.basename(input_file_name, File.extname(input_file_name))
        converter_path = "%s/sedocConverter/sedocConverter_exe" % [root_path]
        font_dir_path = "%s/fonts" % [root_path]
        output_dir_path = "%s/public/output/%s" % [root_path, output_dir_name] 
        tmp_dir_path = "%s/tmp" % [root_path]

        system("%s -pz -f %s %s %s %s" % [converter_path, font_dir_path, input_file_path, output_dir_path, tmp_dir_path])

        return output_dir_path
    end

    # pb 파일을 읽어 serialize 하여 반환합니다.
    def serialized_pb(pb_file_path) 
        aFile = File.open(pb_file_path, "r")
        aSerialized = Array.new
        
        if aFile
            aFile.sysread(16)
            aFile.each_byte do |byte|
                aSerialized.push(byte & 0xFF)
            end
        end

        return aSerialized
    end
end

1.4 데이터베이스 마이그레이션

Code Block
languagebash
# UploadFile에 파일을 담을 'file' 필드 추가
> rails generate migration AddFileToUploadFile file:string 
> rake db:migrate

...