forked from 0x2620/pandora
fix chunk upload
This commit is contained in:
parent
442722ff71
commit
23e6fedacf
2 changed files with 32 additions and 23 deletions
|
@ -191,7 +191,7 @@ class Frame(models.Model):
|
||||||
return '%s at %s' % (self.file, self.position)
|
return '%s at %s' % (self.file, self.position)
|
||||||
|
|
||||||
def stream_path(f):
|
def stream_path(f):
|
||||||
h = f.oshash
|
h = f.file.oshash
|
||||||
return os.path.join('stream', h[:2], h[2:4], h[4:6], h[6:], f.profile)
|
return os.path.join('stream', h[:2], h[2:4], h[4:6], h[6:], f.profile)
|
||||||
|
|
||||||
class Stream(models.Model):
|
class Stream(models.Model):
|
||||||
|
@ -201,7 +201,7 @@ class Stream(models.Model):
|
||||||
file = models.ForeignKey(File, related_name='streams')
|
file = models.ForeignKey(File, related_name='streams')
|
||||||
profile = models.CharField(max_length=255, default='96p.webm')
|
profile = models.CharField(max_length=255, default='96p.webm')
|
||||||
video = models.FileField(default=None, blank=True, upload_to=lambda f, x: stream_path(f))
|
video = models.FileField(default=None, blank=True, upload_to=lambda f, x: stream_path(f))
|
||||||
source = models.ForeignKey('Stream', related_name='derivatives', default=None, blank=True)
|
source = models.ForeignKey('Stream', related_name='derivatives', default=None, null=True)
|
||||||
available = models.BooleanField(default=False)
|
available = models.BooleanField(default=False)
|
||||||
|
|
||||||
def extract_derivates(self):
|
def extract_derivates(self):
|
||||||
|
@ -215,12 +215,19 @@ class Stream(models.Model):
|
||||||
def save_chunk(self, chunk, chunk_id=-1):
|
def save_chunk(self, chunk, chunk_id=-1):
|
||||||
if not self.available:
|
if not self.available:
|
||||||
if not self.video:
|
if not self.video:
|
||||||
self.video.save(self.profile, ContentFile(chunk))
|
self.video.save(self.profile, chunk)
|
||||||
else:
|
else:
|
||||||
f = open(self.file.path, 'a')
|
f = open(self.video.path, 'a')
|
||||||
#FIXME: should check that chunk_id/offset is right
|
#FIXME: should check that chunk_id/offset is right
|
||||||
f.write(chunk)
|
f.write(chunk.read())
|
||||||
f.close()
|
f.close()
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def save(self, *args, **kwargs):
|
||||||
|
if self.available and not self.file.available:
|
||||||
|
self.file.available = True
|
||||||
|
self.file.save()
|
||||||
|
super(Stream, self).save(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -190,29 +190,16 @@ class VideoChunkForm(forms.Form):
|
||||||
|
|
||||||
@login_required_json
|
@login_required_json
|
||||||
def firefogg_upload(request):
|
def firefogg_upload(request):
|
||||||
|
profile = request.GET['profile']
|
||||||
|
oshash = request.GET['oshash']
|
||||||
#handle video upload
|
#handle video upload
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
#init upload
|
#init upload
|
||||||
profile = request.POST.get('profile', request.GET['profile'])
|
|
||||||
#FIXME: check for valid profile
|
|
||||||
if 'oshash' in request.POST:
|
|
||||||
#404 if oshash is not know, files must be registered via update api first
|
|
||||||
f = get_object_or_404(models.File, oshash=request.POST['oshash'])
|
|
||||||
stream, created = models.Stream.objects.get_or_create(file=file, profile=profile)
|
|
||||||
if stream.video: #FIXME: check permission here instead of just starting over
|
|
||||||
stream.video.delete()
|
|
||||||
stream.available = False
|
|
||||||
stream.save()
|
|
||||||
response = {
|
|
||||||
#is it possible to no hardcode url here?
|
|
||||||
'uploadUrl': request.build_absolute_uri('/api/upload/?oshash=%s&profile=%s' % (f.oshash, profile)),
|
|
||||||
'result': 1
|
|
||||||
}
|
|
||||||
return render_to_json_response(response)
|
|
||||||
#post next chunk
|
#post next chunk
|
||||||
if 'chunk' in request.FILES and 'oshash' in request.GET:
|
if 'chunk' in request.FILES and oshash:
|
||||||
print "all chunk now"
|
print "all chunk now"
|
||||||
stream = get_object_or_404(models.Stream, oshash=request.GET['oshash'], profile=profile)
|
stream = get_object_or_404(models.Stream, file__oshash=oshash, profile=profile)
|
||||||
|
|
||||||
form = VideoChunkForm(request.POST, request.FILES)
|
form = VideoChunkForm(request.POST, request.FILES)
|
||||||
if form.is_valid() and stream.editable(request.user):
|
if form.is_valid() and stream.editable(request.user):
|
||||||
|
@ -231,6 +218,21 @@ def firefogg_upload(request):
|
||||||
response['result'] = 1
|
response['result'] = 1
|
||||||
response['done'] = 1
|
response['done'] = 1
|
||||||
return render_to_json_response(response)
|
return render_to_json_response(response)
|
||||||
|
#FIXME: check for valid profile
|
||||||
|
elif oshash:
|
||||||
|
#404 if oshash is not know, files must be registered via update api first
|
||||||
|
f = get_object_or_404(models.File, oshash=oshash)
|
||||||
|
stream, created = models.Stream.objects.get_or_create(file=f, profile=profile)
|
||||||
|
if stream.video: #FIXME: check permission here instead of just starting over
|
||||||
|
stream.video.delete()
|
||||||
|
stream.available = False
|
||||||
|
stream.save()
|
||||||
|
response = {
|
||||||
|
#is it possible to no hardcode url here?
|
||||||
|
'uploadUrl': request.build_absolute_uri('/api/upload/?oshash=%s&profile=%s' % (f.oshash, profile)),
|
||||||
|
'result': 1
|
||||||
|
}
|
||||||
|
return render_to_json_response(response)
|
||||||
print request.GET, request.POST
|
print request.GET, request.POST
|
||||||
response = json_response(status=400, text='this request requires POST')
|
response = json_response(status=400, text='this request requires POST')
|
||||||
return render_to_json_response(response)
|
return render_to_json_response(response)
|
||||||
|
|
Loading…
Reference in a new issue