Showing
3 changed files
with
33 additions
and
5 deletions
... | @@ -30,7 +30,7 @@ def list_path(bucket, user, path): | ... | @@ -30,7 +30,7 @@ def list_path(bucket, user, path): |
30 | files.append({'type':'file', 'name':file}) | 30 | files.append({'type':'file', 'name':file}) |
31 | 31 | ||
32 | return {'files':files} | 32 | return {'files':files} |
33 | - | 33 | + |
34 | 34 | ||
35 | def upload_file(bucket, user, local_path, key): | 35 | def upload_file(bucket, user, local_path, key): |
36 | return S3.upload_file(local_path, bucket, user+"/"+key) | 36 | return S3.upload_file(local_path, bucket, user+"/"+key) | ... | ... |
... | @@ -57,7 +57,11 @@ | ... | @@ -57,7 +57,11 @@ |
57 | <a href="{% url 'file_delete' path=new_path %}"><i class="trash alternate outline icon"></i></a> | 57 | <a href="{% url 'file_delete' path=new_path %}"><i class="trash alternate outline icon"></i></a> |
58 | {% endwith %} | 58 | {% endwith %} |
59 | </td> | 59 | </td> |
60 | - <td class="center aligned collapsing"><i style="cursor: pointer;" class="download icon"></i></td> | 60 | + <td class="center aligned collapsing"> |
61 | + {% with new_path=path|add:file.name %} | ||
62 | + <a href="{% url 'file_download' path=new_path %}"><i style="cursor: pointer;" class="download icon"></i></a> | ||
63 | + {% endwith %} | ||
64 | + </td> | ||
61 | </tr> | 65 | </tr> |
62 | {% endfor %} | 66 | {% endfor %} |
63 | </tbody> | 67 | </tbody> | ... | ... |
... | @@ -6,10 +6,12 @@ from restful.models import File | ... | @@ -6,10 +6,12 @@ from restful.models import File |
6 | from django.views import View | 6 | from django.views import View |
7 | from django.core.files.base import ContentFile | 7 | from django.core.files.base import ContentFile |
8 | from django.middleware import csrf | 8 | from django.middleware import csrf |
9 | +from dcloud import settings | ||
10 | +from django.http import HttpResponse | ||
11 | +import os | ||
9 | import requests | 12 | import requests |
10 | 13 | ||
11 | 14 | ||
12 | - | ||
13 | def home(request): | 15 | def home(request): |
14 | return render(request, 'website/home.html') | 16 | return render(request, 'website/home.html') |
15 | 17 | ||
... | @@ -29,6 +31,7 @@ def file_upload(request, path): | ... | @@ -29,6 +31,7 @@ def file_upload(request, path): |
29 | cookies['csrftoken'] = csrf.get_token(request) | 31 | cookies['csrftoken'] = csrf.get_token(request) |
30 | headers = {'X-CSRFToken': cookies['csrftoken']} | 32 | headers = {'X-CSRFToken': cookies['csrftoken']} |
31 | requests.post('http://localhost:8000/restapi/list/'+path, files={'file': file}, headers=headers, cookies=cookies) | 33 | requests.post('http://localhost:8000/restapi/list/'+path, files={'file': file}, headers=headers, cookies=cookies) |
34 | + # TODO delete mdeia/file | ||
32 | return redirect('file_list', path=path) | 35 | return redirect('file_list', path=path) |
33 | 36 | ||
34 | @login_required | 37 | @login_required |
... | @@ -46,10 +49,31 @@ def file_delete(request, path): | ... | @@ -46,10 +49,31 @@ def file_delete(request, path): |
46 | cookies['csrftoken'] = csrf.get_token(request) | 49 | cookies['csrftoken'] = csrf.get_token(request) |
47 | headers = {'X-CSRFToken': cookies['csrftoken']} | 50 | headers = {'X-CSRFToken': cookies['csrftoken']} |
48 | requests.delete('http://localhost:8000/restapi/file/'+path, headers=headers, cookies=cookies) | 51 | requests.delete('http://localhost:8000/restapi/file/'+path, headers=headers, cookies=cookies) |
49 | - return redirect('file_list', path="/".join(path.split("/")[:-2])) | 52 | + new_path = "/".join(path.split("/")[:-1]) |
53 | + if new_path != '': | ||
54 | + new_path = new_path+'/' | ||
55 | + return redirect('file_list', path=new_path) | ||
50 | 56 | ||
51 | @login_required | 57 | @login_required |
52 | def file_download(request, path): | 58 | def file_download(request, path): |
53 | cookies = {'sessionid' : request.session.session_key} | 59 | cookies = {'sessionid' : request.session.session_key} |
54 | requests.get('http://localhost:8000/restapi/file/'+path, cookies=cookies) | 60 | requests.get('http://localhost:8000/restapi/file/'+path, cookies=cookies) |
55 | - return redirect('file_list', path="/".join(path.split("/")[:-2])) | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
61 | + file_path = os.path.join(settings.MEDIA_ROOT, path) | ||
62 | + if os.path.exists(file_path): | ||
63 | + with open(file_path, 'rb') as fh: | ||
64 | + response = HttpResponse(fh.read(), content_type='multipart/form-data' ) | ||
65 | + response['Content-Disposition'] = 'inline; filename=' + os.path.basename(file_path) | ||
66 | + return response | ||
67 | + raise Http404 | ||
68 | + | ||
69 | +@login_required | ||
70 | +def file_view(request, path): | ||
71 | + cookies = {'sessionid' : request.session.session_key} | ||
72 | + requests.get('http://localhost:8000/restapi/file/'+path, cookies=cookies) | ||
73 | + file_path = os.path.join(settings.MEDIA_ROOT, path) | ||
74 | + if os.path.exists(file_path): | ||
75 | + with open(file_path, 'rb') as fh: | ||
76 | + response = HttpResponse(fh.read(), content_type='text/plain' ) | ||
77 | + response['Content-Disposition'] = 'inline; filename=' + os.path.basename(file_path) | ||
78 | + return response | ||
79 | + raise Http404 | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or login to post a comment