예제 #1
0
def list_dir(request, *arg, **args):
  s_dir = request.session.get('dir', '')
  a_dir = args.get('dir', '')
  if not a_dir:
    a_dir = ''
  if a_dir == '' and s_dir == '':
    return HttpResponseRedirect('/files/check')
  if a_dir <> '' and s_dir <> a_dir:
    return HttpResponseRedirect('/files/check')
  if a_dir == '':
    return HttpResponseRedirect('/files/list/{0}'.format(s_dir))

  d = get_object_or_404(Directory, directory=s_dir)
  if request.session.get('check', '') <> d.password:
    return HttpResponseRedirect('/files/check')

  check_used(d)
  files = []
  if d.allow_list:
    files = list(UploadFile.objects.filter(directory=d))
    for f in files:
      f.file_size = '{0:.2f}'.format(f.file_size / 1024.0)
      if not ((f.content_type in ['text/plain', 'text/html', 'text/xml', 
                                'application/pdf']) or \
         re.match('image/.*', f.content_type)):
        f.content_type = ''
  return render_to_response('fileupload/listfiles.html', {
    'dir': d,
    'dir_used': '{:.3f}'.format(d.used_size / 1024.0 / 1024),
    'files': files,
    'login_dir': request.session.get('dir', ''),
  })
예제 #2
0
def download(request, *arg, **args):
  d = get_object_or_404(Directory, directory=args['dir'])
  f = get_object_or_404(UploadFile, directory=d, 
                        file_name=args['filename'])
  if not os.path.exists(f.save_path.encode('utf-8')):
    raise Http404
  if f.auto_delete_days > 0 and \
     f.upload_date + datetime.timedelta(days=f.auto_delete_days) \
     < datetime.datetime.now():
    check_used(d)
    raise Http404
  wrapper = FileWrapper(file(f.save_path.encode('utf-8')))
  response = HttpResponse(wrapper, content_type=f.content_type)
  response['Content-Length'] = os.path.getsize(f.save_path.encode('utf-8'))
  x = response['Content-Length']
  if args['method'] == 'get':
    response['Content-Disposition'] = 'attachment; filename={0}'.format(f.file_name)
  f.download_count += 1
  f.save()
  return response
예제 #3
0
def upload(request):
  if request.method == 'POST':
    form = UploadForm(request.POST, request.FILES)
    if form.is_valid():
      form_data = form.cleaned_data
      try:
        d = Directory.objects.get(directory=form_data['directory'])
      except:
        return HttpResponseRedirect('../failed/?e=' + '文件夹不存在或密码错误')
      if not d.check_password(form_data['password']):
        return HttpResponseRedirect('../failed/?e=' + '文件夹不存在或密码错误')
      f = request.FILES['file']
      name = form_data['filename'] if form_data['filename'] <> '' else f.name
      if UploadFile.objects.filter(directory=d, file_name=name).count() <> 0:
        return HttpResponseRedirect('../failed/?e=' + '该文件名已存在')
      if f.size > d.file_max_size * 1024 * 1024:
        return HttpResponseRedirect('../failed/?e=' \
                                    + '文件大小超过文件夹允许单个文件的上限')
      check_used(d)
      if f.size + d.used_size > d.total_size * 1024 * 1024:
        return HttpResponseRedirect('../failed/?e=' + '文件夹已满')
      try:
        handle_upload_file(d, f, name, form_data)
      except:
        return HttpResponseRedirect('../failed/?e=' + '保存文件失败')

      return HttpResponseRedirect(
        '../success/?u={0:.3f}&t={1}&d={2}&fs={3}&on={4}&nn={5}'.format(
        d.used_size / 1024.0 / 1024, d.total_size, d, f.size, 
          f.name.encode('utf-8'), name.encode('utf-8')))
  else:
    form = UploadForm()
  return render(request, 'fileupload/upload.html', 
                {'form': form,
                 'login_dir': request.session.get('dir', ''),
                })