Django Example
Caution
For server security, please refer to the link below when handling this work.
[Software Security Weakness Diagnosis Guide]
1. Preliminary Information
The integration guide assumes the project is structured as follows:
- Project name:
synapeditor_django - App name:
edit
1.1 Register the app
# project settings.py
INSTALLED_APPS = [
'edit.apps.EditConfig', # register the app
# ...
]
# project urls.py
urlpatterns = [
path('edit/', include('edit.urls')), # mount the app's URLs
# ...
]
2. Image / Video / File Upload
Caution
The file-upload portion of the sample code below is intentionally minimal and lacks proper security handling.
For the file-upload portion, use what is already in place inside your project, and refer to the code below for the integration portion only.
The upload settings used in this guide are as follows. Change the paths and API names as needed.
- Image upload directory:
media - Image upload API:
/edit/uploadFile
2.1 Editor configuration
var SynapEditorConfig = {
// ...
'editor.upload.image.param': { 'csrfmiddlewaretoken': '{{ csrf_token }}' },
'editor.upload.image.api': '/edit/uploadFile',
// ...
};
2.2 Media path settings
# project settings.py
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
# project urls.py
urlpatterns = [
# ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
2.3 URL → View wiring
# app urls.py
from django.urls import path
from . import views
urlpatterns = [
# ...
path('uploadFile/', views.upload_file, name='upload_file'),
]
2.4 View
The response must include uploadPath.
# app views.py
from django.http import JsonResponse
from .forms import UploadFileForm
def upload_file(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
uploaded_file = form.save()
return JsonResponse({'uploadPath': uploaded_file.file.url})
2.5 Form
# app forms.py
from .models import UploadFile
from django import forms
class UploadFileForm(forms.ModelForm):
class Meta:
model = UploadFile
fields = ('file',)
2.6 Model
# app models.py
import uuid
from django.db import models
def get_file_name(instance, filename):
ext = filename.split('.')[-1]
return "%s.%s" % (uuid.uuid4(), ext)
class UploadFile(models.Model):
file = models.FileField(upload_to=get_file_name)
3. HWP / Word / Excel Document Import
Caution
The file-upload portion of the sample code below is intentionally minimal and lacks proper security handling.
For the file-upload portion, use what is already in place inside your project, and refer to the code below for the integration portion only.
The document import is configured as follows. Adjust the paths and API names as needed.
- Documents are uploaded into the
mediadirectory. - Conversion output is stored under
media/output. - The document import API is
/edit/importDoc.
3.1 Editor configuration
var SynapEditorConfig = {
// ...
'editor.import.param': { 'csrfmiddlewaretoken': '{{ csrf_token }}' },
'editor.import.api': '/edit/importDoc/',
// ...
};
3.2 Media path settings
Same as the upload section (see 2.2).
3.3 URL → View wiring
# app urls.py
from django.urls import path
from . import views
urlpatterns = [
# ...
path('importDoc/', views.import_doc, name='import_doc'),
]
3.4 View
Response must include serializedData and importPath.
# app views.py
import os
import subprocess
import zipfile
import zlib
from django.conf import settings
from django.http import JsonResponse
from .forms import UploadFileForm
PROJECT_PATH = os.path.abspath(os.path.dirname(__name__))
MEDIA_ROOT = settings.MEDIA_ROOT
def import_doc(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
uploaded_file = form.save()
# 1. Convert document
result = execute_converter(uploaded_file.file)
if result['resultCode'] == 0:
output_path = result['outputPath']
# 2. Unzip conversion output
unzip_path = unzip(output_path)
pb_path = unzip_path + '/document.pb'
# 3. Serialize the .pb data
serialized_data = serialize_pb(pb_path)
common_prefix = os.path.commonprefix([output_path, PROJECT_PATH])
import_path = '/' + os.path.relpath(unzip_path, common_prefix)
return JsonResponse({
'serializedData': serialized_data,
'importPath': import_path,
})
def execute_converter(file):
fname, ext = os.path.splitext(file.name)
module_path = '{0}/sedocConverter/sedocConverter_exe'.format(PROJECT_PATH)
font_path = '{0}/sedocConverter/fonts'.format(PROJECT_PATH)
input_path = file.path
output_path = '{0}/output/{1}.zip'.format(MEDIA_ROOT, os.path.basename(fname))
temp_path = '{0}/temp'.format(PROJECT_PATH)
args = [module_path, '-z', '-f', font_path, input_path, output_path, temp_path]
result_code = None
process = subprocess.Popen(args)
try:
result_code = process.wait(timeout=20)
except subprocess.TimeoutExpired:
process.kill()
return {'resultCode': result_code, 'outputPath': output_path}
def unzip(zipFilePath):
unzip_path, _ext = os.path.splitext(zipFilePath)
zip = zipfile.ZipFile(zipFilePath)
zip.extractall(unzip_path)
return unzip_path
def serialize_pb(pbFilePath):
serialized_data = []
with open(pbFilePath, 'rb') as pb_file:
pb_file.seek(16)
pb_contents = pb_file.read()
decompressed = zlib.decompress(pb_contents)
for byte in decompressed:
serialized_data.append(byte & 0xFF)
return serialized_data
3.5 Form
Same as 2.5.
3.6 Model
Same as 2.6.