3 from django.contrib.auth.decorators import login_required
4 from django.core.exceptions import PermissionDenied, SuspiciousOperation
5 from django.http import JsonResponse, StreamingHttpResponse
6 from django.shortcuts import get_object_or_404
7 from django.template.defaultfilters import filesizeformat
8 from django.utils.formats import date_format
9 from django.views.decorators.http import require_POST
11 from filex.forms import NewDirForm, RenameForm, FavoriteForm
12 from filex.ftp import FTPOperation, FTPException
13 from filex.models import Favorite
14 from filex.uploadhandler import with_ftp_upload_handler
17 def check_auth(request):
18 if not request.user.is_authenticated():
19 raise PermissionDenied("Login required!")
20 if not request.session['proxy']:
21 raise PermissionDenied("No proxy found!")
24 def list_content(request):
27 # TODO data validation
28 host = request.GET.get('host')
29 path = request.GET.get('path')
30 if not host or not path:
31 raise SuspiciousOperation("No path or host given!")
33 url = 'gsiftp://' + host + path
36 listing = FTPOperation(request.session['proxy']).listing(url)
37 except FTPException as e:
38 return JsonResponse({'msg': e.message}, status=400)
42 item['size'] = filesizeformat(item['size'])
43 item['date'] = date_format(item['date'], 'CUSTOM_DATETIME_FORMAT')
47 return JsonResponse(data, safe=False)
50 def download(request):
53 # TODO data validation
54 host = request.GET.get('host')
55 path = request.GET.get('path')
56 name = request.GET.get('name')
57 if not host or not path or not name:
58 raise SuspiciousOperation("No path or host or name given!")
60 url = 'gsiftp://' + host + path + '/' + name
62 mime_type, encoding = mimetypes.guess_type(name)
64 response = StreamingHttpResponse(FTPOperation(request.session['proxy']).get(url),
65 content_type=mime_type or 'application/octet-stream')
66 response['Content-Disposition'] = 'attachment; filename={}'.format(name)
67 # TODO Content-Length (?)
70 response['Content-Encoding'] = encoding
75 @with_ftp_upload_handler
78 return JsonResponse({'success': True})
84 # TODO data validation
85 host = request.GET.get('host')
86 path = request.GET.get('path')
87 if not host or not path:
88 raise SuspiciousOperation("No path or host given!")
90 url = 'gsiftp://' + host + path
93 return JsonResponse(FTPOperation(request.session['proxy']).info(url))
94 except FTPException as e:
96 if 'No such file or directory' in e.message:
98 elif 'Permission denied' in e.message:
101 return JsonResponse({'msg': e.message}, status=status)
108 # TODO data validation
109 host = request.POST.get('host')
110 path = request.POST.get('path')
111 dirs = request.POST.getlist('dirs')
112 files = request.POST.getlist('files')
113 if not host or not path or not (files or dirs):
114 raise SuspiciousOperation("No path or host or files given!")
116 url = 'gsiftp://' + host + path + '/'
117 ftp = FTPOperation(request.session['proxy'])
123 ftp.rmdir(url + name)
124 except FTPException as e:
125 fail[name] = e.message
131 ftp.delete(url + name)
132 except FTPException as e:
133 fail[name] = e.message
137 return JsonResponse({'done': done, 'fail': fail})
144 # TODO actual data validation
145 form = NewDirForm(request.POST)
148 host = form.cleaned_data['host']
149 path = form.cleaned_data['path']
150 name = form.cleaned_data['name']
152 url = 'gsiftp://' + host + path + '/' + name
155 FTPOperation(request.session['proxy']).mkdir(url)
156 except FTPException as e:
159 return JsonResponse({'success': True})
163 return JsonResponse({'msg': msg}, status=400)
170 # TODO actual data validation
171 form = RenameForm(request.POST)
174 host = form.cleaned_data['host']
175 path = form.cleaned_data['path']
176 src = form.cleaned_data['src']
177 dst = form.cleaned_data['dst']
179 src_url = 'gsiftp://' + host + path + '/' + src
180 dst_url = 'gsiftp://' + host + path + '/' + dst
183 FTPOperation(request.session['proxy']).move(src_url, dst_url)
184 except FTPException as e:
187 return JsonResponse({'success': True})
191 return JsonResponse({'msg': msg}, status=400)
195 def compress(request):
198 # TODO data validation
199 host = request.POST.get('host')
200 path = request.POST.get('path')
201 files = request.POST.getlist('files')
202 archive = request.POST.get('archive')
203 if not host or not path or not files or not archive:
204 raise SuspiciousOperation("No path or host or files or archive given!")
206 server = 'gsiftp://' + host
209 # consume generator with command output
210 list(FTPOperation(request.session['proxy']).compress(server, path, files, archive))
211 except FTPException as e:
212 return JsonResponse({'msg': e.message}, status=400)
214 return JsonResponse({'success': True})
218 def extract(request):
221 # TODO data validation
222 host = request.POST.get('host')
223 archive = request.POST.get('archive')
224 dst = request.POST.get('dst')
225 if not host or not archive or not dst:
226 raise SuspiciousOperation("No path or host or files or archive given!")
228 server = 'gsiftp://' + host
231 # consume generator with command output
232 list(FTPOperation(request.session['proxy']).extract(server, archive, dst))
233 except FTPException as e:
234 return JsonResponse({'msg': e.message}, status=400)
236 return JsonResponse({'success': True})
241 def fav_add(request):
242 data = request.POST.copy()
243 data['owner'] = request.user.id
245 form = FavoriteForm(data)
247 # TODO check if path exists
249 instance = form.save()
251 return JsonResponse({'group': 'usr', 'host': instance.host, 'path': instance.path,
252 'value': instance.host + instance.path})
254 return JsonResponse({'msg': form.errors}, status=400)
259 def fav_delete(request):
260 fav = get_object_or_404(Favorite, owner=request.user, host=request.POST['host'], path=request.POST['path'])
263 return JsonResponse({'success': True})