refactor chunk upload: return offset, continue at server offset
This commit is contained in:
parent
22e3a9eedd
commit
7cd152b1d5
8 changed files with 133 additions and 161 deletions
|
|
@ -16,6 +16,7 @@ import ox
|
|||
from ox.django.fields import DictField, TupleField
|
||||
|
||||
from archive import extract
|
||||
from archive.chunk import save_chunk
|
||||
|
||||
import managers
|
||||
|
||||
|
|
@ -278,25 +279,16 @@ class Text(models.Model):
|
|||
|
||||
def save_chunk(self, chunk, offset=None, done=False):
|
||||
if self.uploading:
|
||||
if not self.file:
|
||||
self.file.name = self.path('data.pdf')
|
||||
ox.makedirs(os.path.dirname(self.file.path))
|
||||
with open(self.file.path, 'w') as f:
|
||||
f.write(chunk.read())
|
||||
self.save()
|
||||
else:
|
||||
if offset == None:
|
||||
offset = self.file.size
|
||||
elif offset > self.file.size:
|
||||
return False
|
||||
with open(self.file.path, 'r+') as f:
|
||||
f.seek(offset)
|
||||
f.write(chunk.read())
|
||||
if done:
|
||||
self.uploading = False
|
||||
self.save()
|
||||
return True
|
||||
return False
|
||||
name = self.path('data.pdf')
|
||||
|
||||
def done_cb():
|
||||
if done:
|
||||
self.uploading = False
|
||||
self.save()
|
||||
return True, self.file.size
|
||||
|
||||
return save_chunk(self, self.file, chunk, offset, name, done_cb)
|
||||
return False, 0
|
||||
|
||||
def delete_file(sender, **kwargs):
|
||||
t = kwargs['instance']
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ from django.shortcuts import render_to_response
|
|||
from django.template import RequestContext
|
||||
|
||||
from item import utils
|
||||
from archive.chunk import process_chunk
|
||||
import models
|
||||
|
||||
def get_text_or_404_json(id):
|
||||
|
|
@ -377,11 +378,6 @@ def icon(request, id, size=16):
|
|||
icon = os.path.join(settings.STATIC_ROOT, 'jpg/list256.jpg')
|
||||
return HttpFileResponse(icon, content_type='image/jpeg')
|
||||
|
||||
class ChunkForm(forms.Form):
|
||||
chunk = forms.FileField()
|
||||
offset = forms.IntegerField(required=False)
|
||||
done = forms.IntegerField(required=False)
|
||||
|
||||
def pdf_viewer(request, id):
|
||||
text = get_text_or_404_json(id)
|
||||
if text.type == 'pdf' and text.file and not text.uploading:
|
||||
|
|
@ -409,18 +405,10 @@ def upload(request):
|
|||
if text.editable(request.user):
|
||||
#post next chunk
|
||||
if 'chunk' in request.FILES:
|
||||
form = ChunkForm(request.POST, request.FILES)
|
||||
if form.is_valid() and text.editable(request.user):
|
||||
c = form.cleaned_data['chunk']
|
||||
offset = form.cleaned_data['offset']
|
||||
response = {
|
||||
'result': 1,
|
||||
'resultUrl': request.build_absolute_uri(text.get_absolute_url())
|
||||
}
|
||||
if not text.save_chunk(c, offset, form.cleaned_data['done']):
|
||||
response['result'] = -1
|
||||
if form.cleaned_data['done']:
|
||||
response['done'] = 1
|
||||
if text.editable(request.user):
|
||||
response = process_chunk(request, text.save_chunk)
|
||||
response['resultUrl'] = request.build_absolute_uri(text.get_absolute_url())
|
||||
return render_to_json_response(response)
|
||||
return render_to_json_response(response)
|
||||
#init upload
|
||||
else:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue