Skip to content

Commit

Permalink
add plugin to upload files as links
Browse files Browse the repository at this point in the history
  • Loading branch information
goapunk committed Apr 3, 2024
1 parent 11b1690 commit 78b13b2
Show file tree
Hide file tree
Showing 9 changed files with 1,187 additions and 1,357 deletions.
25 changes: 24 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ Quick start
CKEDITOR_5_CUSTOM_CSS = 'path_to.css' # optional
CKEDITOR_5_FILE_STORAGE = "path_to_storage.CustomStorage" # optional
CKEDITOR_5_UPLOAD_FILE_TYPES = ['jpeg', 'pdf', 'png'] # optional
CKEDITOR_5_CONFIGS = {
'default': {
'toolbar': ['heading', '|', 'bold', 'italic', 'link',
Expand Down Expand Up @@ -271,6 +270,30 @@ Additionally you will have to list all available languages in the ckeditor
config as shown above.
Allow file uploading as link:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
By default only images can be uploaded and embedded in the content. To allow
uploading and embedding files as downloadable links you can add the following
to your config:
.. code-block:: python
CKEDITOR_5_ALLOW_ALL_FILE_TYPES = True
CKEDITOR_5_UPLOAD_FILE_TYPES = ['jpeg', 'pdf', 'png'] # optional
CKEDITOR_5_CONFIGS = {
'default': {
'toolbar': ['heading', '|', 'bold', 'italic', 'link',
'bulletedList', 'numberedList', 'blockQuote', 'imageUpload', 'fileUpload' ], # include fileUpload here
'language': 'de',
},
**Warning**: Uploaded files are not validated and users could upload malicious
content (e.g. a pdf which actually is an executable). Furthermore allowing file
uploads disables any validation for the image upload as the backend can't
distinguish between image and file upload. Exposing the file upload to
all/untrusted users poses a risk!
Installing from GitHub:
^^^^^^^^^^^^^^^^^^^^^^^
.. code-block:: bash
Expand Down
2,480 changes: 1,135 additions & 1,345 deletions django_ckeditor_5/package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion django_ckeditor_5/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"@ckeditor/ckeditor5-theme-lark": "^41.1.0",
"@ckeditor/ckeditor5-ui": "^41.1.0",
"@ckeditor/ckeditor5-upload": "^41.1.0",
"@ckeditor/ckeditor5-word-count": "^41.1.0"
"@ckeditor/ckeditor5-word-count": "^41.1.0",
"@liqd/ckeditor5-file-uploader": "github:liqd/ckeditor5-file-uploader#v0.0.2"
},
"devDependencies": {
"@ckeditor/ckeditor5-core": "^41.1.0",
Expand Down
13 changes: 11 additions & 2 deletions django_ckeditor_5/static/django_ckeditor_5/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ function createEditors(element = document.body) {
const upload_url = element.querySelector(
`#${script_id}-ck-editor-5-upload-url`
).getAttribute('data-upload-url');
const upload_file_types = JSON.parse(element.querySelector(
`#${script_id}-ck-editor-5-upload-url`
).getAttribute('data-upload-file-types'));
const csrf_cookie_name = element.querySelector(
`#${script_id}-ck-editor-5-upload-url`
).getAttribute('data-csrf_cookie_name');
Expand All @@ -75,10 +78,16 @@ function createEditors(element = document.body) {
}
);
config.simpleUpload = {
'uploadUrl': upload_url, 'headers': {
'uploadUrl': upload_url,
'headers': {
'X-CSRFToken': getCookie(csrf_cookie_name),
}
},
};

config.fileUploader = {
'fileTypes': upload_file_types
};

ClassicEditor.create(
editorEl,
config
Expand Down
6 changes: 4 additions & 2 deletions django_ckeditor_5/static/django_ckeditor_5/src/ckeditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ import ListProperties from '@ckeditor/ckeditor5-list/src/listproperties';
import SourceEditing from '@ckeditor/ckeditor5-source-editing/src/sourceediting';
import GeneralHtmlSupport from '@ckeditor/ckeditor5-html-support/src/generalhtmlsupport';
import ImageInsert from '@ckeditor/ckeditor5-image/src/imageinsert';
import {TableCaption} from "@ckeditor/ckeditor5-table";
import { TableCaption } from '@ckeditor/ckeditor5-table';
import WordCount from '@ckeditor/ckeditor5-word-count/src/wordcount';
import Mention from '@ckeditor/ckeditor5-mention/src/mention';
import { Style } from '@ckeditor/ckeditor5-style';
import { HorizontalLine } from '@ckeditor/ckeditor5-horizontal-line';
import {LinkImage} from "@ckeditor/ckeditor5-link";
import {HtmlEmbed} from "@ckeditor/ckeditor5-html-embed";
import { FileUploader } from '@liqd/ckeditor5-file-uploader';

export default class ClassicEditor extends ClassicEditorBase {
}
Expand Down Expand Up @@ -94,5 +95,6 @@ ClassicEditor.builtinPlugins = [
Style,
HorizontalLine,
LinkImage,
HtmlEmbed
HtmlEmbed,
FileUploader,
];
2 changes: 1 addition & 1 deletion django_ckeditor_5/templates/django_ckeditor_5/widget.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
{{ errors }}
{% endif %}
</div>
<input type="hidden" id="{{script_id}}-ck-editor-5-upload-url" data-upload-url="{{upload_url}}" data-csrf_cookie_name="{{ csrf_cookie_name }}">
<input type="hidden" id="{{script_id}}-ck-editor-5-upload-url" data-upload-url="{{upload_url}}" data-upload-file-types="{{upload_file_types}}" data-csrf_cookie_name="{{ csrf_cookie_name }}">

<span id="{{script_id}}-span">{{ config|json_script:script_id }}</span>
11 changes: 7 additions & 4 deletions django_ckeditor_5/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,13 @@ def handle_uploaded_file(f):
def upload_file(request):
if request.method == "POST" and request.user.is_staff:
form = UploadFileForm(request.POST, request.FILES)
try:
image_verify(request.FILES["upload"])
except NoImageException as ex:
return JsonResponse({"error": {"message": f"{ex}"}})
allow_all_file_types = getattr(settings, "CKEDITOR_5_ALLOW_ALL_FILE_TYPES", False)

if not allow_all_file_types:
try:
image_verify(request.FILES['upload'])
except NoImageException as ex:
return JsonResponse({"error": {"message": f"{ex}"}})
if form.is_valid():
url = handle_uploaded_file(request.FILES["upload"])
return JsonResponse({"url": url})
Expand Down
1 change: 1 addition & 0 deletions django_ckeditor_5/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.urls import reverse
from django.utils.safestring import mark_safe
from django.utils.translation import get_language
import json

if get_version() >= "4.0":
from django.utils.translation import gettext_lazy as _
Expand Down
3 changes: 2 additions & 1 deletion example/blog/blog/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
{"color": "hsl(231, 48%, 48%)", "label": "Indigo"},
{"color": "hsl(207, 90%, 54%)", "label": "Blue"},
]

CKEDITOR_5_ALLOW_ALL_FILE_TYPES = True
CKEDITOR_5_CONFIGS = {
"default": {
"removePlugins": ["WordCount"],
Expand Down Expand Up @@ -209,6 +209,7 @@
"insertTable",
"sourceEditing",
"style",
"fileUpload",
],
"shouldNotGroupWhenFull": True,
},
Expand Down

0 comments on commit 78b13b2

Please sign in to comment.