def get_files_browse_urls(user=None): """ Recursively walks all dirs under upload dir and generates a list of thumbnail and full image URL's for each file found. """ files = [] for filename in get_image_files(user=user): src = utils.get_media_url(filename) if getattr(settings, 'CKEDITOR_IMAGE_BACKEND', None): if is_image(src): thumb = utils.get_media_url(utils.get_thumb_filename(filename)) else: thumb = utils.get_icon_filename(filename) visible_filename = os.path.split(filename)[1] if len(visible_filename) > 20: visible_filename = visible_filename[0:19] + '...' else: thumb = src visible_filename = os.path.split(filename)[1] files.append({ 'thumb': thumb, 'src': src, 'is_image': is_image(src), 'visible_filename': visible_filename, }) return files
def get_files_browse_urls(user=None): """ Recursively walks all dirs under upload dir and generates a list of thumbnail and full image URL's for each file found. """ files = [] for filename in get_image_files(user=user): src = utils.get_media_url(filename) if getattr(settings, "CKEDITOR_IMAGE_BACKEND", None): if is_valid_image_extension(src): thumb = utils.get_media_url(utils.get_thumb_filename(filename)) else: thumb = utils.get_icon_filename(filename) visible_filename = os.path.split(filename)[1] if len(visible_filename) > 20: visible_filename = visible_filename[0:19] + "..." else: thumb = src visible_filename = os.path.split(filename)[1] files.append( { "thumb": thumb, "src": src, "is_image": is_valid_image_extension(src), "visible_filename": visible_filename, } ) return files
def post(self, request, **kwargs): """ Uploads a file and send back its URL to CKEditor. """ uploaded_file = request.FILES['upload'] backend = image_processing.get_backend() ck_func_num = escape(request.GET['CKEditorFuncNum']) # Throws an error when an non-image file are uploaded. if not getattr(settings, 'CKEDITOR_ALLOW_NONIMAGE_FILES', True): try: backend.image_verify(uploaded_file) except utils.NotAnImageException: return HttpResponse(""" <script type='text/javascript'> window.parent.CKEDITOR.tools.callFunction({0}, '', 'Invalid file type.'); </script>""".format(ck_func_num)) saved_path = self._save_file(request, uploaded_file) self._create_thumbnail_if_needed(backend, saved_path) url = utils.get_media_url(saved_path) # Respond with Javascript sending ckeditor upload url. return HttpResponse(""" <script type='text/javascript'> window.parent.CKEDITOR.tools.callFunction({0}, '{1}'); </script>""".format(ck_func_num, url))
def post(self, request, **kwargs): """ Uploads a file and send back its URL to CKEditor. """ uploaded_file = request.FILES['upload'] ck_func_num = request.GET.get('CKEditorFuncNum') if ck_func_num: ck_func_num = escape(ck_func_num) try: saved_path = self._save_file(request, uploaded_file) except TypeError: return HttpResponse(""" <script type='text/javascript'> window.parent.CKEDITOR.tools.callFunction({0}, '', 'Invalid file type.'); </script>""".format(ck_func_num)) url = utils.get_media_url(saved_path) if ck_func_num: # Respond with Javascript sending ckeditor upload url. return HttpResponse(""" <script type='text/javascript'> window.parent.CKEDITOR.tools.callFunction({0}, '{1}'); </script>""".format(ck_func_num, url)) else: _, filename = os.path.split(saved_path) retdata = {'url': url, 'uploaded': '1', 'fileName': filename} return JsonResponse(retdata)
def post(self, request, **kwargs): """ Uploads a file and send back its URL to CKEditor. """ absolute_path = getattr(settings, 'CKEDITOR_UPLOADER_ABSOLUTE_PATH_PREFIX', '') uploaded_file = request.FILES['upload'] backend = image_processing.get_backend() ck_func_num = escape(request.GET['CKEditorFuncNum']) # Throws an error when an non-image file are uploaded. if not getattr(settings, 'CKEDITOR_ALLOW_NONIMAGE_FILES', True): try: backend.image_verify(uploaded_file) except utils.NotAnImageException: return HttpResponse(""" <script type='text/javascript'> window.parent.CKEDITOR.tools.callFunction({0}, '', 'Invalid file type.'); </script>""".format(ck_func_num)) saved_path = self._save_file(request, uploaded_file) self._create_thumbnail_if_needed(backend, saved_path) url = utils.get_media_url(saved_path) # Respond with Javascript sending ckeditor upload url. return HttpResponse(""" <script type='text/javascript'> window.parent.CKEDITOR.tools.callFunction({0}, '{1}'); </script>""".format(ck_func_num, absolute_path + url))
def post(self, request, **kwargs): """ Uploads a file and send back its URL to CKEditor. Exactly the same as in django-ckeditor 5.0.3 except that creates absolute URLs instead of relative ones. """ uploaded_file = request.FILES['upload'] backend = image_processing.get_backend() ck_func_num = escape(request.GET['CKEditorFuncNum']) # Throws an error when an non-image file are uploaded. if not getattr(settings, 'CKEDITOR_ALLOW_NONIMAGE_FILES', True): try: backend.image_verify(uploaded_file) except utils.NotAnImageException: return HttpResponse(""" <script type='text/javascript'> window.parent.CKEDITOR.tools.callFunction({0}, '', 'Invalid file type.'); </script>""".format(ck_func_num)) saved_path = self._save_file(request, uploaded_file) self._create_thumbnail_if_needed(backend, saved_path) url = utils.get_media_url(saved_path) # this is the only customization to this function url = request.build_absolute_uri(url) # Respond with Javascript sending ckeditor upload url. return HttpResponse(""" <script type='text/javascript'> window.parent.CKEDITOR.tools.callFunction({0}, '{1}'); </script>""".format(ck_func_num, url))
def append_file_images(filename,files,user): src = utils.get_media_url(filename) if getattr(settings, 'CKEDITOR_IMAGE_BACKEND', None): if is_image(src): thumb = utils.get_media_url(utils.get_thumb_filename(filename)) else: thumb = utils.get_icon_filename(filename) visible_filename = os.path.split(filename)[1] if len(visible_filename) > 20: visible_filename = visible_filename[0:19] + '...' else: thumb = src visible_filename = os.path.split(filename)[1] files.append({ 'thumb': thumb, 'src': src, 'is_image': is_image(src), 'visible_filename': visible_filename, })
def _custom_save_file(self, request, uploaded_file, backend): save_file_module = getattr(settings, 'CKEDITOR_SAVE_FILE_MODULE', None) save_file_func = getattr(settings, 'CKEDITOR_SAVE_FILE_FUNC', None) if save_file_module and save_file_func: m = importlib.import_module(save_file_module) save_file = getattr(m, save_file_func) url = save_file(request, uploaded_file) else: saved_path = self._save_file(request, uploaded_file) if (str(saved_path).split('.')[1].lower() != 'gif'): self._create_thumbnail_if_needed(backend, saved_path) url = utils.get_media_url(saved_path) return url
def post(self, request, **kwargs): """ Uploads a file and send back its URL to CKEditor. """ uploaded_file = request.FILES['upload'] backend = image_processing.get_backend() ck_func_num = request.GET.get('CKEditorFuncNum') if ck_func_num: ck_func_num = escape(ck_func_num) # Throws an error when an non-image file are uploaded. if not getattr(settings, 'CKEDITOR_ALLOW_NONIMAGE_FILES', True): try: backend.image_verify(uploaded_file) except utils.NotAnImageException: return HttpResponse(""" <script type='text/javascript'> window.parent.CKEDITOR.tools.callFunction({0}, '', 'Invalid file type.'); </script>""".format(ck_func_num)) if getattr(settings, 'CKEDITOR_UPLOAD_FILE_MAX_SIZE') is not None: if uploaded_file.size > settings.CKEDITOR_UPLOAD_FILE_MAX_SIZE: return HttpResponse(""" <script type='text/javascript'> window.parent.CKEDITOR.tools.callFunction({0}, '', 'File max size is {1}.'); </script>""".format( ck_func_num, filesizeformat(settings.CKEDITOR_UPLOAD_FILE_MAX_SIZE))) saved_path = self._save_file(request, uploaded_file) self._create_thumbnail_if_needed(backend, saved_path) url = utils.get_media_url(saved_path) if ck_func_num: # Respond with Javascript sending ckeditor upload url. return HttpResponse(""" <script type='text/javascript'> window.parent.CKEDITOR.tools.callFunction({0}, '{1}'); </script>""".format(ck_func_num, url)) else: retdata = { 'url': url, 'uploaded': '1', 'fileName': uploaded_file.name } return JsonResponse(retdata)
def post(self, request, **kwargs): """ Uploads a file and send back its URL to CKEditor. """ uploaded_file = request.FILES['upload'] backend = image_processing.get_backend() self._verify_file(backend, uploaded_file) saved_path = self._save_file(request, uploaded_file) self._create_thumbnail_if_needed(backend, saved_path) url = utils.get_media_url(saved_path) # Respond with Javascript sending ckeditor upload url. return HttpResponse(""" <script type='text/javascript'> window.parent.CKEDITOR.tools.callFunction({0}, '{1}'); </script>""".format(request.GET['CKEditorFuncNum'], url))
def post(self, request, **kwargs): """ Uploads a file and send back its URL to CKEditor. """ uploaded_file = request.FILES["upload"] backend = get_backend() ck_func_num = request.GET.get("CKEditorFuncNum") if ck_func_num: ck_func_num = escape(ck_func_num) filewrapper = backend(storage, uploaded_file) allow_nonimages = getattr(settings, "CKEDITOR_ALLOW_NONIMAGE_FILES", True) # Throws an error when an non-image file are uploaded. if not filewrapper.is_image and not allow_nonimages: return HttpResponse( """ <script type='text/javascript'> window.parent.CKEDITOR.tools.callFunction({}, '', 'Invalid file type.'); </script>""".format( ck_func_num ) ) filepath = get_upload_filename(uploaded_file.name, request) saved_path = filewrapper.save_as(filepath) url = utils.get_media_url(saved_path) if ck_func_num: # Respond with Javascript sending ckeditor upload url. return HttpResponse( """ <script type='text/javascript'> window.parent.CKEDITOR.tools.callFunction({}, '{}'); </script>""".format( ck_func_num, url ) ) else: _, filename = os.path.split(saved_path) retdata = {"url": url, "uploaded": "1", "fileName": filename} return JsonResponse(retdata)
def post(self, request, **kwargs): """ Uploads a file and send back its URL to CKEditor. """ uploaded_file = request.FILES['upload'] backend = registry.get_backend() ck_func_num = request.GET.get('CKEditorFuncNum') if ck_func_num: ck_func_num = escape(ck_func_num) filewrapper = backend(storage, uploaded_file) allow_nonimages = getattr(settings, 'CKEDITOR_ALLOW_NONIMAGE_FILES', True) # Throws an error when an non-image file are uploaded. if not filewrapper.is_image and not allow_nonimages: return HttpResponse(""" <script type='text/javascript'> window.parent.CKEDITOR.tools.callFunction({0}, '', 'Invalid file type.'); </script>""".format(ck_func_num)) filepath = get_upload_filename(uploaded_file.name, request.user) saved_path = filewrapper.save_as(filepath) url = utils.get_media_url(saved_path) if ck_func_num: # Respond with Javascript sending ckeditor upload url. return HttpResponse(""" <script type='text/javascript'> window.parent.CKEDITOR.tools.callFunction({0}, '{1}'); </script>""".format(ck_func_num, url)) else: _, filename = os.path.split(saved_path) retdata = {'url': url, 'uploaded': '1', 'fileName': filename} return JsonResponse(retdata)