Date: Fri, 29 Mar 2024 05:06:03 +0900 (KST) Message-ID: <538770348.7059.1711656363870@localhost> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_7058_1683036746.1711656363870" ------=_Part_7058_1683036746.1711656363870 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
Caution
Among the following codes provided as the guide, file upload part is a <= strong>sample code and has insufficient security.
As for the file upload, please use the code used within your project and= refer the following code to handle the system link.
In the following example, '/upload' API was= used for uploading images, videos and files and '/import' API was used for= imports.
{=20 'editor.import.api': '/import', 'editor.upload.image.api': '/upload', 'editor.upload.video.api': '/upload', 'editor.upload.file.api': '/upload', }=20
> rails genera= te uploader File=20
Class FileUploade= r < CarrierWave::Uploader::Base # Include RMagick or MiniMagick support: # include CarrierWave::RMagick # include CarrierWave::MiniMagick # Choose what kind of storage to use for this uploader: storage :file # storage :fog # Override the directory where uploaded files will be stored. # This is a sensible default for uploaders that are meant to be mounted: def store_dir "uploads/" end # Provide a default URL as the default if there hasn't been a file upload= ed: # def default_url(*args) # # For Rails 3.1+ asset pipeline compatibility: # # ActionController::Base.helpers.asset_path("fallback/" + [version_na= me, "default.png"].compact.join('_')) # # "/images/fallback/" + [version_name, "default.png"].compact.join('_') # end # Process files as they are uploaded: # process scale: [200, 300] # # def scale(width, height) # # do something # end # Create different versions of your uploaded files: # version :thumb do # process resize_to_fit: [50, 50] # end # Add a white list of extensions which are allowed to be uploaded. # For images you might use something like this: # def extension_whitelist # %w(jpg jpeg gif png) # end # Override the filename of the uploaded files: # Avoid using model.id or version_name here, see uploader/store.rb for de= tails. def filename "#{secure_token}.#{file.extension}" if original_filename.present? end protected def secure_token var =3D :"@#{mounted_as}_secure_token" model.instance_variable_get(var) or model.instance_variable_set(var, Se= cureRandom.uuid) end end=20
> rails genera= te model UploadFile=20
class UploadFile = < ApplicationRecord =09mount_uploader :file, FileUploader end=20
> rails genera= te controller UploadFile=20
class UploadFileC= ontroller < ApplicationController =09skip_before_action :verify_authenticity_token # csrf_token =20 =09def upload uploaded =3D UploadFile.create(file: params[:file])=20 render json:{ "uploadPath": uploaded.file.url } end def import root_path =3D Rails.root uploaded =3D UploadFile.create(file: params[:file]) # 1. Document conversion input_file_path =3D uploaded.file.path input_file_name =3D File.basename(input_file_path) output_dir_name =3D File.basename(input_file_name, File.extname(inp= ut_file_name)) converter_path =3D "%s/sedocConverter/sedocConverter_exe" % [root_p= ath] font_dir_path =3D "%s/fonts" % [root_path] output_dir_path =3D "%s/public/output/%s" % [root_path, output_dir_= name]=20 tmp_dir_path =3D "%s/tmp" % [root_path] system("%s -pz -f %s %s %s %s" % [converter_path, font_dir_path, in= put_file_path, output_dir_path, tmp_dir_path]) # 2. Serializing PB data pb_file_path =3D "%s/document.pb" % [output_dir_path] aFile =3D File.open(pb_file_path, "r") aSerialized =3D Array.new =20 if aFile aFile.sysread(16) aFile.each_byte do |byte| aSerialized.push(byte & 0xFF) end end # 3. Deleting unnecessary files File.delete(input_file_path) if File.exist?(input_file_path) File.delete(pb_file_path) if File.exist?(pb_file_path) =20 render json: { "importPath": "/output/%s" % [File.basename(output_dir_path)], "serializedData": aSerialized } end end=20
# Adding 'file' f= ield to include files in UploadFile > rails generate migration AddFileToUploadFile file:string=20 > rake db:migrate=20
Rails.application= .routes.draw do =09post 'import' =3D> 'upload_file#import' =09post 'upload' =3D> 'upload_file#upload' end=20