122 lines
4.3 KiB
Python
122 lines
4.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
|
# GPL 2013
|
|
from __future__ import division, with_statement
|
|
import os
|
|
import json
|
|
|
|
from django.shortcuts import render_to_response, get_object_or_404, get_list_or_404
|
|
from django.template import RequestContext
|
|
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseNotFound, Http404
|
|
from django.views.decorators.http import condition
|
|
from django import forms
|
|
from django.db.models import F
|
|
from django.conf import settings
|
|
|
|
from ox.django.http import HttpFileResponse
|
|
from ox.django.shortcuts import render_to_json_response, json_response
|
|
import models
|
|
|
|
class ChunkForm(forms.Form):
|
|
chunk = forms.FileField()
|
|
done = forms.IntegerField(required=False)
|
|
|
|
def chunk(request, slug):
|
|
if request.method == 'POST':
|
|
item = get_object_or_404(models.Item, slug=slug)
|
|
form = ChunkForm(request.POST, request.FILES)
|
|
canEdit = True
|
|
if form.is_valid() and canEdit:
|
|
f = form.cleaned_data['chunk']
|
|
response = {
|
|
'resultUrl': request.build_absolute_uri(item.get_absolute_url())
|
|
}
|
|
if item.filename:
|
|
name = item.filename
|
|
else:
|
|
name = f.name
|
|
if not item.save_chunk(f.read(), name):
|
|
response['result'] = 'failed'
|
|
elif form.cleaned_data['done']:
|
|
item.done = True
|
|
item.save()
|
|
response['done'] = 1
|
|
response['result'] = 1
|
|
else:
|
|
response['result'] = 1
|
|
return render_to_json_response(response)
|
|
response = {
|
|
'result': -1,
|
|
'fileUrl': '/'
|
|
}
|
|
return render_to_json_response(response)
|
|
|
|
def add(request):
|
|
if request.method == 'POST':
|
|
if request.POST.get('firefogg', False):
|
|
if not request.session.session_key:
|
|
request.session.save()
|
|
request.session.modified = True
|
|
file = models.Item()
|
|
file.user = request.user
|
|
file.filename = request.POST.get('name', '')
|
|
file.uploader = request.session.session_key
|
|
file.save()
|
|
response = {
|
|
'result': 1,
|
|
'maxRetry': 10,
|
|
'uploadUrl': request.build_absolute_uri("%s/chunk" % file.get_absolute_url())
|
|
}
|
|
return render_to_json_response(response)
|
|
# Save any files that were uploaded (ignoring empty form fields)
|
|
if 'file' in request.FILES:
|
|
new_file = request.FILES['file']
|
|
file = models.Item(filename=new_file.name)
|
|
file.user = request.user
|
|
file.done = True
|
|
file.file.save(new_file.name, new_file)
|
|
os.chmod(file.file.path, 0644)
|
|
file.save()
|
|
if request.POST.get('api', False):
|
|
return HttpResponse(request.build_absolute_uri(file.get_absolute_url()), content_type="text/plain")
|
|
return HttpResponseRedirect(file.get_absolute_url())
|
|
|
|
#no upload
|
|
return HttpResponseRedirect('/')
|
|
|
|
def index(request):
|
|
context = RequestContext(request, {
|
|
'PREFIX': settings.PREFIX,
|
|
'anonymous': request.user.is_anonymous(),
|
|
})
|
|
return render_to_response('index.html', context)
|
|
|
|
def item(request, slug):
|
|
item = get_object_or_404(models.Item, slug=slug)
|
|
editable = item.editable(request.user)
|
|
context = RequestContext(request, {
|
|
'PREFIX': settings.PREFIX,
|
|
'item': item,
|
|
'editable': json.dumps(editable)
|
|
})
|
|
return render_to_response('item.html', context)
|
|
|
|
def pdf(request, slug, filename):
|
|
item = get_object_or_404(models.Item, slug=slug)
|
|
if filename == 'download':
|
|
filename = item.filename
|
|
filename = filename.encode('utf-8')
|
|
content_type = None
|
|
return HttpFileResponse(item.file.path.encode('utf-8'),
|
|
content_type=content_type, filename=filename)
|
|
|
|
def save(request):
|
|
data = json.loads(request.POST['data'])
|
|
item = get_object_or_404(models.Item, slug=data['id'])
|
|
response = json_response({})
|
|
if item.editable(request.user):
|
|
item.overlay = data['overlay']
|
|
item.save()
|
|
else:
|
|
response = json_response(status=403, text='permission denied')
|
|
return render_to_json_response(response)
|