stream, videosupport
This commit is contained in:
parent
fba5070bc2
commit
052aeb7ba4
8 changed files with 203 additions and 208 deletions
|
|
@ -34,6 +34,10 @@ def parse_decimal(string):
|
|||
d = string.split('/')
|
||||
return Decimal(d[0]) / Decimal(d[1])
|
||||
|
||||
def file_path(f, name):
|
||||
h = f.oshash
|
||||
return os.path.join('file', h[:2], h[2:4], h[4:6], h[6:], name)
|
||||
|
||||
class File(models.Model):
|
||||
created = models.DateTimeField(auto_now_add=True)
|
||||
modified = models.DateTimeField(auto_now=True)
|
||||
|
|
@ -136,11 +140,32 @@ class File(models.Model):
|
|||
r[k] = unicode(self[k])
|
||||
return r
|
||||
|
||||
#upload and data handling
|
||||
video_available = models.BooleanField(default=False)
|
||||
video = models.FileField(null=True, blank=True, upload_to=lambda f, x: file_path(f, '%s.webm'%settings.VIDEO_PROFILE))
|
||||
data = models.FileField(null=True, blank=True, upload_to=lambda f, x: file_path(f, 'data.raw'))
|
||||
|
||||
def contents(self):
|
||||
if self.contents_set.count() > 0:
|
||||
return self.contents_set.all()[0].data
|
||||
if self.data:
|
||||
return self.data.read()
|
||||
return None
|
||||
|
||||
def editable(self, user):
|
||||
#FIXME: check that user has instance of this file
|
||||
return True
|
||||
|
||||
def save_chunk(self, chunk, chunk_id=-1):
|
||||
if not self.video_available:
|
||||
if not self.video:
|
||||
self.video.save('%s.webm'%settings.VIDEO_PROFILE, chunk)
|
||||
else:
|
||||
f = open(self.video.path, 'a')
|
||||
#FIXME: should check that chunk_id/offset is right
|
||||
f.write(chunk.read())
|
||||
f.close()
|
||||
return True
|
||||
return False
|
||||
|
||||
class Volume(models.Model):
|
||||
class Meta:
|
||||
unique_together = ("user", "name")
|
||||
|
|
@ -198,88 +223,4 @@ class Frame(models.Model):
|
|||
def __unicode__(self):
|
||||
return u'%s at %s' % (self.file, self.position)
|
||||
|
||||
def stream_path(f):
|
||||
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):
|
||||
class Meta:
|
||||
unique_together = ("file", "profile")
|
||||
|
||||
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, null=True)
|
||||
available = models.BooleanField(default=False)
|
||||
|
||||
def __unicode__(self):
|
||||
return self.video
|
||||
|
||||
def extract_derivates(self):
|
||||
if settings.VIDEO_H264:
|
||||
profile = self.profile.replace('.webm', '.mp4')
|
||||
if Stream.objects.filter(profile=profile, source=self).count() == 0:
|
||||
derivate = Stream(file=self.file, source=self, profile=profile)
|
||||
derivate.video.name = self.video.name.replace(self.profile, profile)
|
||||
derivate.encode()
|
||||
|
||||
for p in settings.VIDEO_DERIVATIVES:
|
||||
profile = p + '.webm'
|
||||
target = self.video.path.replace(self.profile, profile)
|
||||
if Stream.objects.filter(profile=profile, source=self).count() == 0:
|
||||
derivate = Stream(file=self.file, source=self, profile=profile)
|
||||
derivate.video.name = self.video.name.replace(self.profile, profile)
|
||||
derivate.encode()
|
||||
|
||||
if settings.VIDEO_H264:
|
||||
profile = p + '.mp4'
|
||||
if Stream.objects.filter(profile=profile, source=self).count() == 0:
|
||||
derivate = Stream(file=self.file, source=self, profile=profile)
|
||||
derivate.video.name = self.video.name.replace(self.profile, profile)
|
||||
derivate.encode()
|
||||
return True
|
||||
|
||||
def encode(self):
|
||||
if self.source:
|
||||
video = self.source.video.path
|
||||
target = self.video.path
|
||||
profile = self.profile
|
||||
info = self.file.info
|
||||
if extract.stream(video, target, profile, info):
|
||||
self.available=True
|
||||
self.save()
|
||||
|
||||
def editable(self, user):
|
||||
#FIXME: possibly needs user setting for stream
|
||||
return True
|
||||
|
||||
def save_chunk(self, chunk, chunk_id=-1):
|
||||
if not self.available:
|
||||
if not self.video:
|
||||
self.video.save(self.profile, chunk)
|
||||
else:
|
||||
f = open(self.video.path, 'a')
|
||||
#FIXME: should check that chunk_id/offset is right
|
||||
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)
|
||||
|
||||
class FileContents(models.Model):
|
||||
created = models.DateTimeField(auto_now_add=True)
|
||||
modified = models.DateTimeField(auto_now=True)
|
||||
file = models.ForeignKey(File, related_name="contents_set")
|
||||
data = models.TextField(default=u'')
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if self.data and not self.file.available:
|
||||
self.file.available = True
|
||||
self.file.save()
|
||||
super(FileContents, self).save(*args, **kwargs)
|
||||
|
||||
|
|
|
|||
|
|
@ -183,10 +183,8 @@ def api_upload(request):
|
|||
else:
|
||||
response = json_response(status=403, text='permissino denied')
|
||||
if 'file' in request.FILES:
|
||||
if f.contents.count() == 0:
|
||||
contents = models.FileContents(file=f)
|
||||
contents.data = request.FILES['file'].read()
|
||||
contents.save()
|
||||
if not f.data:
|
||||
f.data.save('data.raw', request.FILES['file'])
|
||||
response = json_response({})
|
||||
else:
|
||||
response = json_response(status=403, text='permissino denied')
|
||||
|
|
@ -203,44 +201,42 @@ def firefogg_upload(request):
|
|||
oshash = request.GET['oshash']
|
||||
#handle video upload
|
||||
if request.method == 'POST':
|
||||
#init upload
|
||||
|
||||
#post next chunk
|
||||
if 'chunk' in request.FILES and oshash:
|
||||
stream = get_object_or_404(models.Stream, file__oshash=oshash, profile=profile)
|
||||
|
||||
f = get_object_or_404(models.File, oshash=oshash)
|
||||
form = VideoChunkForm(request.POST, request.FILES)
|
||||
if form.is_valid() and stream.editable(request.user):
|
||||
if form.is_valid() and profile == settings.VIDEO_PROFILE and f.editable(request.user):
|
||||
c = form.cleaned_data['chunk']
|
||||
chunk_id = form.cleaned_data['chunkId']
|
||||
response = {
|
||||
'result': 1,
|
||||
'resultUrl': request.build_absolute_uri('/')
|
||||
}
|
||||
if not stream.save_chunk(c, chunk_id):
|
||||
if not f.save_chunk(c, chunk_id):
|
||||
response['result'] = -1
|
||||
elif form.cleaned_data['done']:
|
||||
#FIXME: send message to encode deamon to create derivates instead
|
||||
stream.available = True
|
||||
stream.save()
|
||||
f.available = True
|
||||
f.save()
|
||||
response['result'] = 1
|
||||
response['done'] = 1
|
||||
return render_to_json_response(response)
|
||||
#FIXME: check for valid profile
|
||||
elif oshash:
|
||||
#init upload
|
||||
elif oshash and profile == settings.VIDEO_PROFILE:
|
||||
#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)
|
||||
if f.editable(request.user):
|
||||
if f.video:
|
||||
f.video.delete()
|
||||
f.video_available = False
|
||||
f.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)
|
||||
response = json_response(status=400, text='this request requires POST')
|
||||
return render_to_json_response(response)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue