2010-07-12 14:56:14 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
|
|
|
from __future__ import division
|
|
|
|
import os.path
|
|
|
|
import re
|
|
|
|
from datetime import datetime
|
|
|
|
from urllib2 import unquote
|
|
|
|
import mimetypes
|
|
|
|
|
|
|
|
from django import forms
|
|
|
|
from django.core.paginator import Paginator
|
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.db.models import Q, Avg, Count, Sum
|
|
|
|
from django.http import HttpResponse, Http404
|
2010-09-14 14:10:37 +00:00
|
|
|
from django.shortcuts import render_to_response, get_object_or_404, get_list_or_404, redirect
|
2010-07-12 14:56:14 +00:00
|
|
|
from django.template import RequestContext
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
try:
|
|
|
|
import simplejson as json
|
|
|
|
except ImportError:
|
|
|
|
from django.utils import simplejson as json
|
|
|
|
|
2010-11-23 09:53:12 +00:00
|
|
|
from ox.django.decorators import login_required_json
|
|
|
|
from ox.django.shortcuts import render_to_json_response, get_object_or_404_json, json_response
|
|
|
|
from ox.django.http import HttpFileResponse
|
2010-12-22 07:45:37 +00:00
|
|
|
from ox.django.views import task_status
|
2010-07-12 14:56:14 +00:00
|
|
|
import ox
|
|
|
|
|
2010-11-08 16:34:25 +00:00
|
|
|
from item.utils import oxid, parse_path
|
2010-11-27 12:12:53 +00:00
|
|
|
from item.models import get_item
|
2010-11-08 16:34:25 +00:00
|
|
|
import item.tasks
|
2010-12-22 07:45:37 +00:00
|
|
|
from api.actions import actions
|
2010-07-12 14:56:14 +00:00
|
|
|
|
2010-12-23 15:01:53 +00:00
|
|
|
import models
|
|
|
|
import tasks
|
|
|
|
|
2010-12-24 09:23:34 +00:00
|
|
|
|
2010-08-10 22:01:41 +00:00
|
|
|
@login_required_json
|
2010-12-22 07:45:37 +00:00
|
|
|
def removeVolume(request):
|
2010-08-10 22:01:41 +00:00
|
|
|
data = json.loads(request.POST['data'])
|
|
|
|
user = request.user
|
|
|
|
try:
|
|
|
|
volume = models.Volume.objects.get(user=user, name=data['volume'])
|
|
|
|
volume.files.delete()
|
|
|
|
volume.delete()
|
2010-12-24 09:23:34 +00:00
|
|
|
response = json_response()
|
2010-08-10 22:01:41 +00:00
|
|
|
except models.Volume.DoesNotExist:
|
|
|
|
response = json_response(status=404, text='volume not found')
|
|
|
|
return render_to_json_response(response)
|
2010-12-22 07:45:37 +00:00
|
|
|
actions.register(removeVolume)
|
2010-08-10 22:01:41 +00:00
|
|
|
|
|
|
|
@login_required_json
|
2010-12-22 07:45:37 +00:00
|
|
|
def update(request):
|
2010-07-12 14:56:14 +00:00
|
|
|
'''
|
2010-08-10 22:01:41 +00:00
|
|
|
2 calls possible:
|
|
|
|
volume/files
|
|
|
|
info
|
|
|
|
call volume/files first and fill in requested info after that
|
|
|
|
|
2010-12-24 10:31:44 +00:00
|
|
|
param data {
|
2010-08-10 22:01:41 +00:00
|
|
|
volume: '',
|
2010-08-07 14:31:20 +00:00
|
|
|
files: [
|
2010-12-24 10:31:44 +00:00
|
|
|
{oshash:, path:, mtime:, },
|
|
|
|
...
|
|
|
|
],
|
2010-08-07 14:31:20 +00:00
|
|
|
info: {oshash: object}
|
2010-12-24 10:31:44 +00:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
status: {'code': int, 'text': string},
|
|
|
|
data: {
|
|
|
|
info: list,
|
|
|
|
data: list,
|
|
|
|
file: list
|
|
|
|
}
|
|
|
|
}
|
2010-07-12 14:56:14 +00:00
|
|
|
'''
|
|
|
|
data = json.loads(request.POST['data'])
|
2010-08-07 14:31:20 +00:00
|
|
|
user = request.user
|
|
|
|
|
2010-08-10 22:01:41 +00:00
|
|
|
response = json_response({'info': [], 'data': [], 'file': []})
|
2010-09-17 21:06:01 +00:00
|
|
|
volume = None
|
2010-08-07 14:31:20 +00:00
|
|
|
if 'files' in data:
|
2010-12-23 15:01:53 +00:00
|
|
|
#update files info async, this takes to long otherwise
|
|
|
|
#FIXME: how can client know if update is done? possibly with taksStatus?
|
|
|
|
t = tasks.update_files.delay(user.username, data['volume'], data['files'])
|
|
|
|
response['data']['taskId'] = t.task_id
|
2010-08-07 14:31:20 +00:00
|
|
|
|
|
|
|
user_profile = user.get_profile()
|
|
|
|
user_profile.files_updated = datetime.now()
|
|
|
|
user_profile.save()
|
|
|
|
|
|
|
|
if 'info' in data:
|
|
|
|
for oshash in data['info']:
|
|
|
|
info = data['info'][oshash]
|
2010-11-08 17:43:59 +00:00
|
|
|
instance = models.Instance.objects.filter(file__oshash=oshash, volume__user=user)
|
2010-08-07 14:31:20 +00:00
|
|
|
if instance.count()>0:
|
|
|
|
instance = instance[0]
|
|
|
|
if not instance.file.info:
|
2010-08-10 22:01:41 +00:00
|
|
|
for key in ('atime', 'mtime', 'ctime'):
|
|
|
|
if key in info:
|
|
|
|
del info[key]
|
2010-08-07 14:31:20 +00:00
|
|
|
instance.file.info = info
|
|
|
|
instance.file.save()
|
|
|
|
|
2010-11-08 17:43:59 +00:00
|
|
|
files = models.Instance.objects.filter(volume__user=user, file__available=False)
|
2010-09-17 21:06:01 +00:00
|
|
|
if volume:
|
|
|
|
files = files.filter(volume=volume)
|
2010-08-10 22:01:41 +00:00
|
|
|
response['data']['info'] = [f.file.oshash for f in files.filter(file__info='{}')]
|
|
|
|
#needs some flag to find those that are actually used main is to generic
|
|
|
|
response['data']['data'] = [f.file.oshash for f in files.filter(file__is_video=True, file__is_main=True)]
|
|
|
|
response['data']['file'] = [f.file.oshash for f in files.filter(file__is_subtitle=True)]
|
2010-08-07 14:31:20 +00:00
|
|
|
|
2010-07-12 14:56:14 +00:00
|
|
|
return render_to_json_response(response)
|
2010-12-22 07:45:37 +00:00
|
|
|
actions.register(update)
|
2010-08-07 14:31:20 +00:00
|
|
|
|
2010-11-15 19:15:52 +00:00
|
|
|
@login_required_json
|
2010-12-22 07:45:37 +00:00
|
|
|
def encodingProfile(request):
|
2010-11-15 19:15:52 +00:00
|
|
|
response = json_response({'profile': settings.VIDEO_PROFILE})
|
|
|
|
return render_to_json_response(response)
|
2010-12-22 07:45:37 +00:00
|
|
|
actions.register(encodingProfile)
|
2010-11-15 19:15:52 +00:00
|
|
|
|
2010-08-10 22:01:41 +00:00
|
|
|
@login_required_json
|
2010-12-22 07:45:37 +00:00
|
|
|
def upload(request):
|
2010-07-12 14:56:14 +00:00
|
|
|
'''
|
2010-08-24 17:16:33 +00:00
|
|
|
oshash: string
|
|
|
|
frame: [] //multipart frames
|
|
|
|
file: [] //multipart file
|
|
|
|
|
2010-12-24 10:31:44 +00:00
|
|
|
return {
|
|
|
|
status: {'code': int, 'text': string},
|
|
|
|
data: {
|
|
|
|
info: object,
|
|
|
|
rename: object
|
|
|
|
}
|
|
|
|
}
|
2010-07-12 14:56:14 +00:00
|
|
|
'''
|
2010-08-07 14:31:20 +00:00
|
|
|
user = request.user
|
2010-12-24 09:23:34 +00:00
|
|
|
f = get_object_or_404_json(models.File, oshash=request.POST['oshash'])
|
2010-08-24 17:16:33 +00:00
|
|
|
if 'frame' in request.FILES:
|
|
|
|
if f.frames.count() == 0:
|
|
|
|
for frame in request.FILES.getlist('frame'):
|
|
|
|
name = frame.name
|
|
|
|
#float required?
|
|
|
|
position = float(os.path.splitext(name)[0])
|
|
|
|
fr = models.Frame(file=f, position=position)
|
|
|
|
fr.save()
|
|
|
|
fr.frame.save(name, frame)
|
|
|
|
response = json_response({})
|
|
|
|
else:
|
|
|
|
response = json_response(status=403, text='permissino denied')
|
|
|
|
if 'file' in request.FILES:
|
2010-09-17 21:06:01 +00:00
|
|
|
if not f.available:
|
2010-09-03 13:28:44 +00:00
|
|
|
f.data.save('data.raw', request.FILES['file'])
|
2010-09-17 21:06:01 +00:00
|
|
|
f.available = True
|
|
|
|
f.save()
|
2010-12-24 09:23:34 +00:00
|
|
|
response = json_response(text='file saved')
|
2010-08-24 17:16:33 +00:00
|
|
|
else:
|
|
|
|
response = json_response(status=403, text='permissino denied')
|
2010-08-07 14:31:20 +00:00
|
|
|
return render_to_json_response(response)
|
2010-12-22 07:45:37 +00:00
|
|
|
actions.register(upload)
|
2010-08-07 14:31:20 +00:00
|
|
|
|
|
|
|
class VideoChunkForm(forms.Form):
|
|
|
|
chunk = forms.FileField()
|
|
|
|
chunkId = forms.IntegerField(required=False)
|
|
|
|
done = forms.IntegerField(required=False)
|
|
|
|
|
|
|
|
@login_required_json
|
|
|
|
def firefogg_upload(request):
|
2010-08-13 17:17:23 +00:00
|
|
|
profile = request.GET['profile']
|
2010-09-05 21:08:05 +00:00
|
|
|
if profile.endswith('.webm'):
|
|
|
|
profile = os.path.splitext(profile)[0]
|
2010-08-13 17:17:23 +00:00
|
|
|
oshash = request.GET['oshash']
|
2010-08-07 14:31:20 +00:00
|
|
|
#handle video upload
|
|
|
|
if request.method == 'POST':
|
|
|
|
#post next chunk
|
2010-08-13 17:17:23 +00:00
|
|
|
if 'chunk' in request.FILES and oshash:
|
2010-09-03 13:28:44 +00:00
|
|
|
f = get_object_or_404(models.File, oshash=oshash)
|
2010-08-07 14:31:20 +00:00
|
|
|
form = VideoChunkForm(request.POST, request.FILES)
|
2010-09-03 13:28:44 +00:00
|
|
|
if form.is_valid() and profile == settings.VIDEO_PROFILE and f.editable(request.user):
|
2010-08-07 14:31:20 +00:00
|
|
|
c = form.cleaned_data['chunk']
|
|
|
|
chunk_id = form.cleaned_data['chunkId']
|
|
|
|
response = {
|
|
|
|
'result': 1,
|
|
|
|
'resultUrl': request.build_absolute_uri('/')
|
|
|
|
}
|
2010-09-03 13:28:44 +00:00
|
|
|
if not f.save_chunk(c, chunk_id):
|
2010-08-07 14:31:20 +00:00
|
|
|
response['result'] = -1
|
|
|
|
elif form.cleaned_data['done']:
|
2010-09-03 13:28:44 +00:00
|
|
|
f.available = True
|
|
|
|
f.save()
|
2010-11-15 19:15:52 +00:00
|
|
|
#FIXME: this fails badly if rabbitmq goes down
|
2010-11-30 15:36:09 +00:00
|
|
|
try:
|
2010-12-23 15:01:53 +00:00
|
|
|
t = item.tasks.update_streams.delay(f.item.itemId)
|
2010-12-22 07:45:37 +00:00
|
|
|
data['resultUrl'] = t.task_id
|
2010-11-30 15:36:09 +00:00
|
|
|
except:
|
|
|
|
pass
|
2010-08-07 14:31:20 +00:00
|
|
|
response['result'] = 1
|
|
|
|
response['done'] = 1
|
|
|
|
return render_to_json_response(response)
|
2010-09-03 13:28:44 +00:00
|
|
|
#init upload
|
|
|
|
elif oshash and profile == settings.VIDEO_PROFILE:
|
2010-08-13 17:17:23 +00:00
|
|
|
#404 if oshash is not know, files must be registered via update api first
|
|
|
|
f = get_object_or_404(models.File, oshash=oshash)
|
2010-09-03 13:28:44 +00:00
|
|
|
if f.editable(request.user):
|
|
|
|
if f.video:
|
|
|
|
f.video.delete()
|
2010-09-08 17:14:01 +00:00
|
|
|
f.available = False
|
2010-09-03 13:28:44 +00:00
|
|
|
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)
|
2010-08-07 14:31:20 +00:00
|
|
|
response = json_response(status=400, text='this request requires POST')
|
2010-07-12 14:56:14 +00:00
|
|
|
return render_to_json_response(response)
|
|
|
|
|
|
|
|
@login_required_json
|
2010-12-22 07:45:37 +00:00
|
|
|
def taskStatus(request):
|
|
|
|
#FIXME: should check if user has permissions to get status
|
|
|
|
data = json.loads(request.POST['data'])
|
|
|
|
user = request.user
|
|
|
|
task_id = data['task_id']
|
|
|
|
response = task_status(request, task_id)
|
|
|
|
return render_to_json_response(response)
|
|
|
|
actions.register(taskStatus)
|
|
|
|
|
|
|
|
@login_required_json
|
2010-12-24 09:23:34 +00:00
|
|
|
def editFile(request):
|
2010-07-12 14:56:14 +00:00
|
|
|
'''
|
2010-12-24 09:23:34 +00:00
|
|
|
change file / item link
|
2010-12-24 10:31:44 +00:00
|
|
|
param data {
|
|
|
|
oshash: hash of file
|
|
|
|
itemId: new itemId
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
status: {'code': int, 'text': string},
|
|
|
|
data: {
|
|
|
|
imdbId:string
|
|
|
|
}
|
|
|
|
}
|
2010-07-12 14:56:14 +00:00
|
|
|
'''
|
2010-12-24 09:23:34 +00:00
|
|
|
#FIXME: permissions, need to be checked
|
|
|
|
data = json.loads(request.POST['data'])
|
|
|
|
f = get_object_or_404_json(models.File, oshash=data['oshash'])
|
|
|
|
response = json_response()
|
|
|
|
if f.item.id != data['itemId']:
|
|
|
|
if len(data['itemId']) != 7:
|
|
|
|
folder = f.instances.all()[0].folder
|
|
|
|
item_info = parse_path(folder)
|
|
|
|
item = get_item(item_info)
|
|
|
|
else:
|
|
|
|
item = get_item({'imdbId': data['itemId']})
|
|
|
|
f.item = item
|
|
|
|
f.save()
|
|
|
|
#FIXME: other things might need updating here
|
|
|
|
response = json_response(text='updated')
|
2010-08-07 14:31:20 +00:00
|
|
|
return render_to_json_response(response)
|
2010-12-22 07:45:37 +00:00
|
|
|
actions.register(editFile)
|
2010-08-07 14:31:20 +00:00
|
|
|
|
2010-09-14 14:10:37 +00:00
|
|
|
def lookup_file(request, oshash):
|
|
|
|
f = get_object_or_404(models.File, oshash=oshash)
|
2010-09-23 16:01:48 +00:00
|
|
|
return redirect(f.item.get_absolute_url())
|
2010-09-14 14:10:37 +00:00
|
|
|
|
2010-09-13 13:46:49 +00:00
|
|
|
"""
|
2010-12-22 07:45:37 +00:00
|
|
|
def fileInfo(request):
|
2010-07-12 14:56:14 +00:00
|
|
|
'''
|
|
|
|
param data
|
2010-08-07 14:31:20 +00:00
|
|
|
oshash string
|
|
|
|
return {'status': {'code': int, 'text': string},
|
|
|
|
'data': {imdbId:string }}
|
2010-07-12 14:56:14 +00:00
|
|
|
'''
|
2010-08-07 14:31:20 +00:00
|
|
|
if 'data' in request.POST:
|
|
|
|
oshash = json.loads(request.POST['data'])
|
|
|
|
elif 'oshash' in request.GET:
|
|
|
|
oshash = request.GET['oshash']
|
2010-09-23 16:01:48 +00:00
|
|
|
f = models.ItemFile.objects.get(oshash=oshash)
|
2010-08-07 14:31:20 +00:00
|
|
|
response = {'data': f.json()}
|
2010-07-12 14:56:14 +00:00
|
|
|
return render_to_json_response(response)
|
2010-12-22 07:45:37 +00:00
|
|
|
actions.register(fileInfo)
|
2010-07-12 14:56:14 +00:00
|
|
|
|
2010-12-22 07:45:37 +00:00
|
|
|
def subtitles(request):
|
2010-08-07 14:31:20 +00:00
|
|
|
'''
|
|
|
|
param data
|
|
|
|
oshash string
|
|
|
|
language string
|
|
|
|
subtitle string
|
|
|
|
return
|
|
|
|
if no language is provided:
|
|
|
|
{data: {languages: array}}
|
|
|
|
if language is set:
|
|
|
|
{data: {subtitle: string}}
|
|
|
|
if subtitle is set:
|
|
|
|
saves subtitle for given language
|
|
|
|
'''
|
|
|
|
if 'data' in request.POST:
|
|
|
|
data = json.loads(request.POST['data'])
|
|
|
|
oshash = data['oshash']
|
|
|
|
language = data.get('language', None)
|
|
|
|
srt = data.get('subtitle', None)
|
|
|
|
if srt:
|
|
|
|
user = request.user
|
|
|
|
sub = models.Subtitles.objects.get_or_create(user, oshash, language)
|
|
|
|
sub.srt = srt
|
|
|
|
sub.save()
|
|
|
|
else:
|
|
|
|
response = json_response({})
|
|
|
|
if language:
|
2010-09-23 16:01:48 +00:00
|
|
|
q = models.Subtitles.objects.filter(item_file__oshash=oshash, language=language)
|
2010-08-07 14:31:20 +00:00
|
|
|
if q.count() > 0:
|
|
|
|
response['data']['subtitle'] = q[0].srt
|
|
|
|
return render_to_json_response(response)
|
2010-09-23 16:01:48 +00:00
|
|
|
l = models.Subtitles.objects.filter(item_file__oshash=oshash).values('language')
|
2010-08-07 14:31:20 +00:00
|
|
|
response['data']['languages'] = [f['language'] for f in l]
|
|
|
|
return render_to_json_response(response)
|
2010-12-22 07:45:37 +00:00
|
|
|
actions.register(subtitles)
|
2010-08-07 14:31:20 +00:00
|
|
|
"""
|