Example #1
0
 def parse(relative=None, absolute=None):
     if relative is not None:
         relative = re.split('[/\\\\]', relative)
         absolute = fsutil.join_path(base_files_dir, *relative)
         url = static(fsutil.join_path('files', *relative))
         return Node(fsutil.join_path(*relative), absolute, url)
     elif absolute is not None:
         relative = absolute.replace(base_files_dir, '')
         url = static(fsutil.join_path('files', relative))
         return Node(relative, absolute, url)
     raise FileNotFoundError
Example #2
0
    def put(self, request, absolute_path=''):
        file_obj = request.FILES.get('file', None)
        if not file_obj:
            raise ParseError
        absolute_path = self.atob(absolute_path)
        node = Node.parse(relative=absolute_path)
        if not node.is_exist:
            fsutil.create_dir(node.absolute)
        file_name = file_obj.name
        base_file_name, extension = fsutil.split_filename(file_name)
        absolute_path = fsutil.join_path(
            absolute_path, base_file_name + '-' + str(int(time.time())) +
            ('.' + extension if extension else ''))
        node = Node.parse(relative=absolute_path)

        if node.is_exist:
            raise ParseError('File already existed.')

        with open(node.absolute, 'wb+') as destination:
            for chunk in file_obj.chunks():
                destination.write(chunk)

        return Response(data=True)
Example #3
0
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from django.shortcuts import render
from django.http import JsonResponse, Http404, HttpResponseBadRequest
import fsutil
import re
from django.templatetags.static import static
from django.urls import reverse as original_reverse
from django.utils.http import urlencode
from django.views.decorators.csrf import ensure_csrf_cookie
from django_bloom.settings import BASE_DIR, STATIC_URL, STATICFILES_DIRS
import base64
import time

static_dir = fsutil.join_path(str(BASE_DIR), STATICFILES_DIRS[0])
base_files_dir = fsutil.join_path(static_dir, 'files')
if not fsutil.is_dir(base_files_dir):
    if fsutil.is_file(base_files_dir):
        fsutil.delete_file(base_files_dir)
    fsutil.create_dir(base_files_dir)


class Node:
    def __init__(self, relative, absolute, url):
        self.url = url
        self.absolute = absolute
        self.relative = relative
        self.name = fsutil.get_filename(absolute)
        self.is_exist = fsutil.exists(absolute)
        self.is_file = fsutil.is_file(absolute)
Example #4
0
 def test_join_path_with_parent_dirs(self):
     self.assertEqual(
         fsutil.join_path("/a/b/c/", "../../document.txt"), "/a/document.txt"
     )
Example #5
0
 def test_join_path_with_absolute_path(self):
     self.assertEqual(
         fsutil.join_path("/a/b/c/", "/document.txt"), "/a/b/c/document.txt"
     )
Example #6
0
 def temp_path(filepath=""):
     return fsutil.join_path(__file__, "temp/{}".format(filepath))
Example #7
0
 def temp_path(filepath=''):
     return fsutil.join_path(__file__, 'temp/{}'.format(filepath))