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)
|
||||
|
||||
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)
|
||||
|
||||
class Stream(models.Model):
|
||||
|
@ -201,7 +201,7 @@ class Stream(models.Model):
|
|||
file = models.ForeignKey(File, related_name='streams')
|
||||
profile = models.CharField(max_length=255, default='96p.webm')
|
||||
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)
|
||||
|
||||
def extract_derivates(self):
|
||||
|
@ -215,12 +215,19 @@ class Stream(models.Model):
|
|||
def save_chunk(self, chunk, chunk_id=-1):
|
||||
if not self.available:
|
||||
if not self.video:
|
||||
self.video.save(self.profile, ContentFile(chunk))
|
||||
self.video.save(self.profile, chunk)
|
||||
else:
|
||||
f = open(self.file.path, 'a')
|
||||
f = open(self.video.path, 'a')
|
||||
#FIXME: should check that chunk_id/offset is right
|
||||
f.write(chunk)
|
||||
f.write(chunk.read())
|
||||
f.close()
|
||||
return True
|
||||
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
|
||||
def firefogg_upload(request):
|
||||
profile = request.GET['profile']
|
||||
oshash = request.GET['oshash']
|
||||
#handle video upload
|
||||
if request.method == 'POST':
|
||||
#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
|
||||
if 'chunk' in request.FILES and 'oshash' in request.GET:
|
||||
if 'chunk' in request.FILES and oshash:
|
||||
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)
|
||||
if form.is_valid() and stream.editable(request.user):
|
||||
|
@ -231,6 +218,21 @@ def firefogg_upload(request):
|
|||
response['result'] = 1
|
||||
response['done'] = 1
|
||||
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
|
||||
response = json_response(status=400, text='this request requires POST')
|
||||
return render_to_json_response(response)
|
||||
|
|
Loading…
Reference in a new issue