views.py
3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
from django.shortcuts import render, get_object_or_404, redirect, Http404
from django.utils import timezone
from django.contrib.auth.decorators import login_required
from django.views.generic import FormView
from restful.models import File
from django.views import View
from django.core.files.base import ContentFile
from django.middleware import csrf
from dcloud import settings
from django.http import HttpResponse
import os
import requests
def home(request):
return render(request, 'website/home.html')
@login_required
def file_list(request, path):
cookies = {'sessionid' : request.session.session_key}
files = requests.get('http://localhost:8000/restapi/list/'+path, cookies=cookies)
ret = files.json()
ret['path'] = path
return render(request, 'website/file_list.html', ret)
@login_required
def file_upload(request, path):
file = request.FILES.get('file')
cookies = {'sessionid' : request.session.session_key}
cookies['csrftoken'] = csrf.get_token(request)
headers = {'X-CSRFToken': cookies['csrftoken']}
requests.post('http://localhost:8000/restapi/list/'+path, files={'file': file}, headers=headers, cookies=cookies)
# TODO delete mdeia/file
return redirect('file_list', path=path)
@login_required
def make_folder(request, path):
dir_name = request.POST.get('dir_name')
cookies = {'sessionid' : request.session.session_key}
cookies['csrftoken'] = csrf.get_token(request)
headers = {'X-CSRFToken': cookies['csrftoken']}
files = requests.put('http://localhost:8000/restapi/list/'+path, headers=headers, cookies=cookies)
return redirect('file_list', path=path)
@login_required
def file_delete(request, path):
cookies = {'sessionid' : request.session.session_key}
cookies['csrftoken'] = csrf.get_token(request)
headers = {'X-CSRFToken': cookies['csrftoken']}
requests.delete('http://localhost:8000/restapi/file/'+path, headers=headers, cookies=cookies)
new_path = "/".join(path.split("/")[:-1])
if new_path != '':
new_path = new_path+'/'
return redirect('file_list', path=new_path)
@login_required
def file_download(request, path):
cookies = {'sessionid' : request.session.session_key}
requests.get('http://localhost:8000/restapi/file/'+path, cookies=cookies)
file_path = os.path.join(settings.MEDIA_ROOT, path)
if os.path.exists(file_path):
with open(file_path, 'rb') as fh:
response = HttpResponse(fh.read(), content_type='multipart/form-data' )
response['Content-Disposition'] = 'inline; filename=' + os.path.basename(file_path)
return response
raise Http404
@login_required
def file_view(request, path):
cookies = {'sessionid' : request.session.session_key}
requests.get('http://localhost:8000/restapi/file/'+path, cookies=cookies)
file_path = os.path.join(settings.MEDIA_ROOT, path)
if os.path.exists(file_path):
with open(file_path, 'rb') as fh:
response = HttpResponse(fh.read(), content_type='text/plain' )
response['Content-Disposition'] = 'inline; filename=' + os.path.basename(file_path)
return response
raise Http404
@login_required
def file_copy(request, old_path, new_path):
cookies = {'sessionid' : request.session.session_key}
cookies['csrftoken'] = csrf.get_token(request)
headers = {'X-CSRFToken': cookies['csrftoken']}
files = requests.post('http://localhost:8000/restapi/file-mod/'+old_path+'&'+new_path, data={'method': 'cp'}, headers=headers, cookies=cookies)
print(files.json())
new_path = "/".join(new_path.split("/")[:-1])
if new_path != '':
new_path = new_path+'/'
return redirect('file_list', path=new_path)
@login_required
def file_move(request, old_path, new_path):
cookies = {'sessionid' : request.session.session_key}
cookies['csrftoken'] = csrf.get_token(request)
headers = {'X-CSRFToken': cookies['csrftoken']}
files = requests.post('http://localhost:8000/restapi/file-mod/'+old_path+'&'+new_path, data={'method': 'mv'}, headers=headers, cookies=cookies)
print(files.json())
new_path = "/".join(new_path.split("/")[:-1])
if new_path != '':
new_path = new_path+'/'
return redirect('file_list', path=new_path)