From a49b10ed2df48eabdb1e12b3fcaca0e711a7ef63 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Sun, 9 Apr 2023 14:10:21 +0200 Subject: [PATCH] allow to upload files from the ckeditor while here: - update ckeditor to 4.21.0 - add a Sourcedialog plugin to the ckeditor --- winixd/functions/upload.cpp | 102 +++++++++++++++++++++--------- winixd/functions/upload.h | 6 +- winixd/html/ckeditor.html | 14 ++-- winixd/html/fun_upload.html | 2 +- winixd/html/index_head_adder.html | 2 +- 5 files changed, 87 insertions(+), 39 deletions(-) diff --git a/winixd/functions/upload.cpp b/winixd/functions/upload.cpp index 0d87e82..b78e4f8 100644 --- a/winixd/functions/upload.cpp +++ b/winixd/functions/upload.cpp @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2008-2022, Tomasz Sowa + * Copyright (c) 2008-2023, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -244,7 +244,7 @@ void Upload::UploadMulti() } if( is_jquery_upload ) - CreateAnswer(); + CreateJqueryUploadAnswer(); else system->RedirectToLastDir(); } @@ -283,7 +283,10 @@ void Upload::UploadSingle() post_file.tmp_filename.clear(); if( is_jquery_upload ) - CreateAnswer(); + CreateJqueryUploadAnswer(); + else + if( is_ckeditor_upload ) + CreateCkeditorUploadAnswer(); else if( cur->request->status == WINIX_ERR_OK ) system->RedirectTo(cur->request->item, L"/cat"); @@ -369,32 +372,10 @@ void Upload::AnalizeFileType(const std::wstring & file_path, std::wstring & file } - -void Upload::MakePost() -{ - InitMagicLibIfNeeded(); - cur->request->item_tab.clear(); - is_jquery_upload = cur->request->IsParam(L"jquery_upload"); - - if( cur->request->post_file_tab.empty() ) - { - cur->request->status = WINIX_ERR_PERMISSION_DENIED; - return; - } - - if( !FunUploadCheckAbuse() ) - return; - - if( cur->request->post_file_tab.size() > 1 ) - UploadMulti(); - else - UploadSingle(); - -} - - - -void Upload::CreateAnswer() +/* + * an answer for the 'upload' function + */ +void Upload::CreateJqueryUploadAnswer() { Request & req = *cur->request; @@ -435,6 +416,67 @@ void Upload::CreateAnswer() } +/* + * an answer for the ckeditor function + * https://ckeditor.com/docs/ckeditor4/latest/guide/dev_file_upload.html + */ +void Upload::CreateCkeditorUploadAnswer() +{ + Request & req = *cur->request; + + files.add(L"uploaded", 1); + files.add(L"fileName", req.item.url); + + std::wstring link; + system->CreateItemLink(req.item, link); + files.add(L"url", link); + + /* + * if uploaded directly from the ckeditor (by drag and drop) then the placed image + * is very small, add "width" and "height" in such a case + */ + //files.add(L"width", "600"); + //files.add(L"height", "500"); + + /* + * if there was an error add "error" item e.g. + * "error": { + * "message": "The file is too big." + * } + */ + + files.serialize_to_json_stream(cur->request->out_bin_stream, false); + cur->request->send_bin_stream = true; + cur->request->use_ezc_engine = false; + cur->request->container_type = Request::container_raw; + cur->request->out_headers.add(Header::content_type, Header::application_json); +} + + +void Upload::MakePost() +{ + InitMagicLibIfNeeded(); + cur->request->item_tab.clear(); + is_jquery_upload = cur->request->IsParam(L"jquery_upload"); + is_ckeditor_upload = cur->request->IsParam(L"ckeditor_upload"); + + if( cur->request->post_file_tab.empty() ) + { + cur->request->status = WINIX_ERR_PERMISSION_DENIED; + return; + } + + if( !FunUploadCheckAbuse() ) + return; + + if( cur->request->post_file_tab.size() > 1 ) + UploadMulti(); + else + UploadSingle(); + +} + + void Upload::MakeGet() { if( cur->request->IsParam(L"jquery_upload") ) @@ -450,7 +492,7 @@ void Upload::MakeGet() raw("order by item.sort_index asc, item.url asc, item.id asc"). get_vector(cur->request->item_tab); - CreateAnswer(); + CreateJqueryUploadAnswer(); } } diff --git a/winixd/functions/upload.h b/winixd/functions/upload.h index 60e0893..7246c12 100644 --- a/winixd/functions/upload.h +++ b/winixd/functions/upload.h @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2010-2022, Tomasz Sowa + * Copyright (c) 2010-2023, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -61,6 +61,7 @@ private: std::wstring path; bool is_jquery_upload; + bool is_ckeditor_upload; magic_t magic_cookie; pt::Space files; @@ -75,7 +76,8 @@ private: void UploadSingle(); void ResizeImage(Item & item); void CreateThumb(Item & item); - void CreateAnswer(); + void CreateJqueryUploadAnswer(); + void CreateCkeditorUploadAnswer(); void InitMagicLibIfNeeded(); void CloseMagicLib(); diff --git a/winixd/html/ckeditor.html b/winixd/html/ckeditor.html index 9e6f7ef..45b7157 100644 --- a/winixd/html/ckeditor.html +++ b/winixd/html/ckeditor.html @@ -34,22 +34,26 @@ editorElement.setAttribute( 'contenteditable', 'true' ); CKEDITOR.inline( '[0]', { +// https://ckeditor.com/docs/ckeditor4/latest/guide/dev_file_browse_upload.html +// config.filebrowserBrowseUrl - setting contains the location of an external file browser +// that should be launched when the Browse Server button is pressed. +// config.filebrowserUploadUrl - setting contains the location of a script that handles file uploads. +// If set, the Upload tab will appear in some dialog windows — the ones +// where such functionality is available, i.e. Link, Image. filebrowserBrowseUrl: '[doc_base_url][dir]ls?ckeditor_browse&fullscreen', +filebrowserUploadUrl: '[doc_base_url][dir]upload?ckeditor_upload', customConfig : '[doc_base_url]/var/ckeditor_winix.js', - -//extraPlugins : 'sourcedialog', +extraPlugins : 'sourcedialog', //removePlugins : 'sourcearea', - toolbar : 'winix', height : '350px', scayt_autoStartup : false, // spellchecker disabled -//docType : '', docType : '', entities : false, // when true then ó becames ó, toolbar_winix : \[ - \['Save','Preview','-'\], + \['Sourcedialog', 'Save','Preview','-'\], \['Cut','Copy','Paste','PasteText','PasteFromWord','-'\], \['Undo','Redo','-','Find','Replace','-','SelectAll','RemoveFormat'\], diff --git a/winixd/html/fun_upload.html b/winixd/html/fun_upload.html index a5e33ee..3859020 100644 --- a/winixd/html/fun_upload.html +++ b/winixd/html/fun_upload.html @@ -160,7 +160,7 @@ \{% if (file.thumbnailUrl) \{ %\} - + \{% \} %\} diff --git a/winixd/html/index_head_adder.html b/winixd/html/index_head_adder.html index c7d24a9..f9a1ff2 100644 --- a/winixd/html/index_head_adder.html +++ b/winixd/html/index_head_adder.html @@ -256,7 +256,7 @@ cm.save() [if winix_function_is "ckeditor"] - + [if not winix_has_jquery] [def winix_has_jquery true]