Ejemplo n.º 1
0
def private_bucket(request):
    permission_classes = (permissions.IsAuthenticated, )
    user = request.user
    bucket_private = 'khugle-drive-' + user.username

    if request.method == 'GET':
        file_list = s3.list_object('khugle-drive-' + user.username, '', user)
        return render(request, 'bucket/private_bucket.html',
                      {'file_list': file_list})

    if request.method == 'POST':
        try:
            myfile = request.FILES['upload']

            if s3.check_file_exist(bucket_private, '', myfile):
                messages.info(request, "이미 존재하는 파일입니다.")
                return redirect('/bucket/private/file')

            fs = FileSystemStorage("static/files")
            filename = fs.save(myfile.name, myfile)
            print(filename)
            messages.info(request, "성공적으로 파일을 저장했습니다.")
            s3.upload_file(filename, 'khugle-drive-' + request.user.username)
            file_list = s3.list_object('khugle-drive-' + user.username, '',
                                       user)
            return render(request, 'bucket/private_bucket.html',
                          {'file_list': file_list})
        except:
            file_list = s3.list_object('khugle-drive-' + user.username, '',
                                       user)
            return render(request, 'bucket/private_bucket.html',
                          {'file_list': file_list})
Ejemplo n.º 2
0
def post_create(request, folder_path):
    form = PostForm()
    user = request.user
    bucket_group = 'khugle-drive-'+user.major.lower()

    if request.method == 'POST':
        form = PostForm(request.POST, request.FILES)
        filename = str(request.FILES.get('file'))
        print(filename)
        if form.is_valid():
            if not s3.check_file_exist(bucket_group, folder_path, filename):
                messages.info(request, "이미 존재하는 파일입니다.")
                return redirect('/bucket/group/file/' + folder_path)
            post = form.save(commit=False)
            post.author = request.user
            post.created_at = timezone.now()
            post.major = request.user.major
            post.file_path = folder_path + str(request.FILES.get('file'))
            post.save()
            for file in form.files:
                 s3.upload_file(str(request.FILES.get('file')), bucket_group, folder_path + filename)
            messages.info(request, "파일이 성공적으로 업로드 되었습니다.")
            return redirect('bucket:group_bucket_file', folder_path=folder_path)
    else:
        form = PostForm()
    context = {'form': form}
    return render(request, 'KHUgle/post_form.html', context)
Ejemplo n.º 3
0
def private_bucket_file(request, folder_path):
    permission_classes = (permissions.IsAuthenticated, )
    user = request.user
    bucket_private = 'khugle-drive-' + user.username

    if request.method == 'GET':
        folders = folder_path.split('/')
        folder_path = ''
        for i in range(len(folders) - 1):
            folder_path += folders[i] + '/'
        file_list = s3.list_object(bucket_private, folder_path, user)
        return render(request, 'bucket/private_bucket_file.html', {
            'file_list': file_list,
            'current_path': folder_path
        })

    if request.method == 'POST':
        try:
            myfile = request.FILES['upload']

            if s3.check_file_exist(bucket_private, folder_path, myfile):
                messages.info(request, "이미 존재하는 파일입니다.")
                return redirect('/bucket/private/file/' + folder_path)

            folders = folder_path.split('/')
            folder_path = ''
            for i in range(len(folders) - 1):
                folder_path += folders[i] + '/'
            fs = FileSystemStorage("static/files")
            filename = fs.save(myfile.name, myfile)

            s3.upload_file(filename, 'khugle-drive-' + request.user.username,
                           folder_path + filename)
            file_list = s3.list_object('khugle-drive-' + user.username,
                                       folder_path, user)
            return render(request, 'bucket/private_bucket_file.html', {
                'file_list': file_list,
                'current_path': folder_path
            })
        except:
            file_list = s3.list_object('khugle-drive-' + user.username,
                                       folder_path, user)
            return render(request, 'bucket/private_bucket_file.html', {
                'file_list': file_list,
                'current_path': folder_path
            })