Example #1
0
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)
        thumb = utils.get_icon_filename(filename)
        visible_filename = os.path.split(filename)[1]
        if len(visible_filename) > 20:
            visible_filename = visible_filename[0:19] + '...'
        if is_image(src):
            if getattr(settings, 'CKEDITOR_IMAGE_BACKEND', None):
                thumb = utils.get_media_url(utils.get_thumb_filename(filename))
            else:
                thumb = src
        files.append({
            'thumb': thumb,
            'src': src,
            'is_image': is_image(src),
            'visible_filename': visible_filename,
        })

    return files
Example #2
0
def upload(request, image=False):
    """
    Uploads a file and send back its URL to CKEditor.

    TODO:
        Validate uploads
    """
    # Get the uploaded file from request.
    upload = request.FILES['upload']

    # check for image
    if image and is_image(upload.name):

        #Verify that file is a valid image
        backend = image_processing.get_backend()

        try:
            backend.image_verify(upload)
        except utils.NotAnImageException:
            return HttpResponse("""
                <script type='text/javascript'>
                    alert('Invalid image file.')
                    window.parent.CKEDITOR.tools.callFunction({0});
                </script>""".format(request.GET['CKEditorFuncNum']))

        # Open output file in which to store upload.
        upload_filename = get_upload_filename(upload.name, request.user, prefix='images')
        saved_path = default_storage.save(upload_filename, upload)

        if backend.should_create_thumbnail(saved_path):
            backend.create_thumbnail(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))

    # check for document
    elif image != True and is_document(upload.name):
        # Open output file in which to store upload.
        upload_filename = get_upload_filename(upload.name, request.user, prefix='documents')
        saved_path = default_storage.save(upload_filename, upload)
        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))

    # bad file format
    else:
        return HttpResponse("""
            <script type='text/javascript'>
                alert('Invalid file format.')
                window.parent.CKEDITOR.tools.callFunction({0});
            </script>""".format(request.GET['CKEditorFuncNum']))
Example #3
0
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))
                visible_filename = None
            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:
            visible_filename = None
            thumb = src
        files.append({
            'thumb': thumb,
            'src': src,
            'is_image': is_image(src),
            'visible_filename': visible_filename,
        })

    return files
Example #4
0
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):
            thumb = utils.get_media_url(utils.get_thumb_filename(filename))
        else:
            thumb = src
        files.append({'thumb': thumb, 'src': src, 'is_image': is_image(src)})

    return files
Example #5
0
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):
            thumb = utils.get_media_url(utils.get_thumb_filename(filename))
        else:
            thumb = src
        files.append({"thumb": thumb, "src": src, "is_image": is_image(src)})

    return files
Example #6
0
    def post(self, request, **kwargs):
        """
        Uploads a file and send back its URL to CKEditor.
        """
        # Get the uploaded file from request.
        upload = request.FILES['upload']

        #Verify that file is a valid image
        backend = image_processing.get_backend()
        try:
            backend.image_verify(upload)
        except utils.NotAnImageException:
            pass

        # Open output file in which to store upload.
        upload_filename = get_upload_filename(upload.name, request.user)
        saved_path = default_storage.save(upload_filename, upload)

        if backend.should_create_thumbnail(saved_path):
            backend.create_thumbnail(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))
Example #7
0
    def post(self, request, **kwargs):
        """
        Uploads a file and send back its URL to CKEditor.
        """
        # Get the uploaded file from request.
        upload = request.FILES['upload']

        #Verify that file is a valid image
        backend = image_processing.get_backend()
        try:
            backend.image_verify(upload)
        except utils.NotAnImageException:
            pass

        # Open output file in which to store upload.
        upload_filename = get_upload_filename(upload.name, request.user)
        saved_path = default_storage.save(upload_filename, upload)

        if backend.should_create_thumbnail(saved_path):
            backend.create_thumbnail(saved_path)

        if saved_path.startswith("/"): #is an absolute path!
            saved_path = os.path.relpath(saved_path, settings.MEDIA_ROOT)

        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))
Example #8
0
def upload(request):
    """
    Uploads a file and send back its URL to CKEditor.

    TODO:
        Validate uploads
    """
    # Get the uploaded file from request.
    upload = request.FILES['upload']

    # Open output file in which to store upload.
    upload_filename = get_upload_filename(upload.name, request.user)
    saved_path = default_storage.save(upload_filename, upload)

    backend = image_processing.get_backend()
    if backend.should_create_thumbnail(saved_path):
        backend.create_thumbnail(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))
Example #9
0
def upload(request):
    """
    Uploads a file and send back its URL to CKEditor.

    TODO:
        Validate uploads
    """
    # Get the uploaded file from request.
    upload = request.FILES['upload']

    #Verify that file is a valid image
    backend = image_processing.get_backend()
    try:
        backend.image_verify(upload)
    except IOError:
        return HttpResponse("""
                   <script type='text/javascript'>
                        alert('Invalid image')
                        window.parent.CKEDITOR.tools.callFunction({0});
                   </script>""".format(request.GET['CKEditorFuncNum']))

    # Open output file in which to store upload.
    upload_filename = get_upload_filename(upload.name, request.user)
    saved_path = default_storage.save(upload_filename, upload)

    if backend.should_create_thumbnail(saved_path):
        backend.create_thumbnail(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))
Example #10
0
def get_image_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.
    """
    images = []
    for filename in get_image_files(user=user):
        src = utils.get_media_url(filename)
        if getattr(settings, 'CKEDITOR_IMAGE_BACKEND', None):
            thumb = utils.get_media_url(utils.get_thumb_filename(filename))
        else:
            thumb = src
        images.append({
            'thumb': thumb,
            'src': src
        })

    return images
Example #11
0
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)
        backend = getattr(settings, 'CKEDITOR_IMAGE_BACKEND', None)
        if backend == 'pillow':
            thumb = utils.get_media_url(utils.get_thumb_filename(filename))
        elif backend == 'oss':
            thumb = default_storage.image_processed_url(filename)
        else:
            thumb = src
        files.append({
            'thumb': thumb,
            'src': src,
            'is_image': is_image(src)
        })

    return files
Example #12
0
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)
        visible_filename = None
        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
        files.append({"thumb": thumb, "src": src, "is_image": is_image(src), "visible_filename": visible_filename})

    return files
Example #13
0
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):
            thumb = utils.get_media_url(utils.get_thumb_filename(filename))
        else:
            thumb = src
        ext = filename.split('.')[-1].lower()
        fa_icon = 'file'
        if ext == 'xls' or ext == 'xls':
            fa_icon = 'excel'
        elif ext == 'doc' or ext == 'docx':
            fa_icon = 'word'
        elif ext == 'ppt' or ext == 'pptx':
            fa_icon = 'powerpoint'
        elif ext == 'rar' or ext == 'zip':
            fa_icon = 'archive'
        elif ext == 'pdf':
            fa_icon = 'pdf'
        elif ext == 'swf':
            fa_icon = 'video'
        else:
            fa_icon = 'image'
        files.append({
            'thumb': thumb,
            'src': src,
            'is_image': is_image(src),
            'filename': filename.split('/')[-1].split('.')[0].lower(),
            'ext': ext,
            'icon': fa_icon,
        })

    return files
Example #14
0
def upload(request):
    """
    Uploads a file and send back its URL to CKEditor.

    TODO:
        Validate uploads
    """
    # Get the uploaded file from request.
    upload = request.FILES['upload']

    #Verify that file is a valid image
    backend = image_processing.get_backend()
    try:
        backend.image_verify(upload)
    except utils.NotAnImageException:
        is_not_image = True
    else:
        is_not_image = False

    if is_not_image:
        backend = file_processing.get_backend()
        try:
            backend.file_verify(upload)
        except utils.NotAnPermittedFileTypeException:
            return HttpResponse("""
                   <script type='text/javascript'>
                        alert('Invalid file type or invalid image')
                        window.parent.CKEDITOR.tools.callFunction({0});
                   </script>""".format(request.GET['CKEditorFuncNum']))

    # Open output file in which to store upload.
    upload_filename = get_upload_filename(upload.name, request.user)
    saved_path = default_storage.save(upload_filename, upload)

    if backend.should_create_thumbnail(saved_path):
        backend.create_thumbnail(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))
Example #15
0
    def post(self, request, **kwargs):
        """
        Uploads a file and send back its URL to CKEditor.
        """
        # Get the uploaded file from request.
        upload = request.FILES['upload']

        #Verify that file is a valid image
        backend = image_processing.get_backend()
        try:
            backend.image_verify(upload)
        except utils.NotAnImageException:
            return HttpResponse("""
                       <script type='text/javascript'>
                            alert('Invalid image')
                            window.parent.CKEDITOR.tools.callFunction({0});
                       </script>""".format(request.GET['CKEditorFuncNum']))

        # Open output file in which to store upload.
        upload_filename = get_upload_filename(upload.name, request.user)
        saved_path = default_storage.save(upload_filename, upload)

        if backend.should_create_thumbnail(saved_path):
            backend.create_thumbnail(saved_path)

        url = utils.get_media_url(saved_path)

        from qiniu import Auth
        from qiniu import put_file
        from utility import constants
        access_key = constants.QINIU_API
        secret_key = constants.QINIU_SK
        q = Auth(access_key, secret_key)
        bucket_name = "excake"
        token = q.upload_token(bucket_name)
        mime_type = "image/jpeg"
        ret, info = put_file(token, "/".join(url.split('/')[3:]), default_storage.path(upload_filename), mime_type=mime_type)

        # 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))