cablegates/pandora/item/views.py

677 lines
23 KiB
Python
Raw Normal View History

2009-08-01 14:14:54 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
from __future__ import division
2009-08-01 14:14:54 +00:00
import os.path
from datetime import datetime
2011-06-27 19:40:44 +00:00
import mimetypes
2011-01-01 11:44:42 +00:00
from django.db.models import Count, Sum, Max
from django.http import HttpResponse, Http404
2011-01-01 11:44:42 +00:00
from django.shortcuts import get_object_or_404, redirect
2010-02-08 10:26:25 +00:00
from django.conf import settings
2010-02-03 12:05:38 +00:00
from ox.utils import json
2010-02-03 12:05:38 +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
2011-06-01 11:03:43 +00:00
from django.db.models import Q
2010-07-07 22:46:41 +00:00
import ox
2009-12-31 15:04:32 +00:00
2009-08-01 14:14:54 +00:00
import models
import utils
2010-06-25 11:53:57 +00:00
import tasks
2010-01-25 09:20:21 +00:00
from archive.models import File
2010-09-07 14:05:38 +00:00
from archive import extract
2011-06-01 11:03:43 +00:00
from annotation.models import Annotation
2010-09-07 14:05:38 +00:00
from api.actions import actions
2010-11-25 15:21:23 +00:00
2011-01-01 11:44:42 +00:00
2010-02-22 09:25:29 +00:00
def _order_query(qs, sort, prefix='sort__'):
2009-08-16 12:23:29 +00:00
order_by = []
if len(sort) == 1:
if sort[0]['key'] == 'title':
sort.append({'operator': '-', 'key': 'year'})
sort.append({'operator': '+', 'key': 'director'})
elif sort[0]['key'] == 'director':
sort.append({'operator': '-', 'key': 'year'})
sort.append({'operator': '+', 'key': 'title'})
elif sort[0]['key'] == 'year':
sort.append({'operator': '+', 'key': 'director'})
sort.append({'operator': '+', 'key': 'title'})
2011-01-02 10:08:17 +00:00
elif not sort[0]['key'] in ('value', 'value_sort'):
sort.append({'operator': '+', 'key': 'director'})
sort.append({'operator': '-', 'key': 'year'})
sort.append({'operator': '+', 'key': 'title'})
2010-02-22 09:25:29 +00:00
for e in sort:
operator = e['operator']
2011-01-01 11:44:42 +00:00
if operator != '-':
operator = ''
key = {
'id': 'itemId',
'accessed': 'accessed__access',
2011-01-24 02:35:52 +00:00
'viewed': 'accessed__access',
}.get(e['key'], e['key'])
2011-01-24 02:35:52 +00:00
if key not in ('accessed__access', 'accessed__accessed'):
key = "%s%s" % (prefix, key)
order = '%s%s' % (operator, key)
2010-02-22 09:25:29 +00:00
order_by.append(order)
if order_by:
qs = qs.order_by(*order_by, nulls_last=True)
2010-02-22 09:25:29 +00:00
return qs
2011-01-24 13:44:38 +00:00
def _order_by_group(query):
if 'sort' in query:
if len(query['sort']) == 1 and query['sort'][0]['key'] == 'items':
if query['group'] == "year":
order_by = query['sort'][0]['operator'] == '-' and 'items' or '-items'
else:
order_by = query['sort'][0]['operator'] == '-' and '-items' or 'items'
if query['group'] != "keyword":
order_by = (order_by, 'value_sort')
else:
order_by = (order_by,)
else:
order_by = query['sort'][0]['operator'] == '-' and '-value_sort' or 'value_sort'
order_by = (order_by, 'items')
else:
order_by = ('-value_sort', 'items')
return order_by
2011-01-04 07:32:32 +00:00
2011-02-22 15:26:06 +00:00
def parse_query(data, user):
2010-02-22 09:25:29 +00:00
query = {}
query['range'] = [0, 100]
2010-07-01 08:49:08 +00:00
query['sort'] = [{'key':'title', 'operator':'+'}]
2011-06-01 11:03:43 +00:00
for key in ('sort', 'keys', 'group', 'range', 'position', 'positions'):
2010-02-22 09:25:29 +00:00
if key in data:
query[key] = data[key]
2010-09-23 16:01:48 +00:00
query['qs'] = models.Item.objects.find(data, user)
2011-06-01 11:03:43 +00:00
if 'annotations' in data:
query['annotations'] = data['annotations']
#FIXME: annotations need manager find(data, user)
query['aqs'] = Annotation.objects.filter(Q(layer__private=False)|Q(user=user))
2010-02-22 09:25:29 +00:00
#group by only allows sorting by name or number of itmes
return query
2009-08-01 14:14:54 +00:00
def find(request):
2010-01-25 09:20:21 +00:00
'''
param data {
'query': query,
'sort': array,
'range': array
}
2011-01-01 11:44:42 +00:00
2010-02-22 09:25:29 +00:00
query: query object, more on query syntax at
2010-06-30 14:21:04 +00:00
https://wiki.0x2620.org/wiki/pandora/QuerySyntax
2010-06-27 12:41:38 +00:00
sort: array of key, operator dics
[
{
key: "year",
operator: "-"
},
{
key: "director",
operator: ""
}
]
2010-02-22 09:25:29 +00:00
range: result range, array [from, to]
keys: array of keys to return
group: group elements by, country, genre, director...
2010-02-22 09:25:29 +00:00
with keys, items is list of dicts with requested properties:
return {'status': {'code': int, 'text': string},
'data': {items: array}}
Groups
param data {
'query': query,
'key': string,
'group': string,
'range': array
}
2011-01-01 11:44:42 +00:00
query: query object, more on query syntax at
2010-06-30 14:21:04 +00:00
https://wiki.0x2620.org/wiki/pandora/QuerySyntax
range: result range, array [from, to]
keys: array of keys to return
group: group elements by, country, genre, director...
possible values for keys: name, items
with keys
items contains list of {'name': string, 'items': int}:
return {'status': {'code': int, 'text': string},
'data': {items: array}}
without keys: return number of items in given query
return {'status': {'code': int, 'text': string},
'data': {items: int}}
Positions
param data {
'query': query,
2011-06-01 11:03:43 +00:00
'positions': [],
'sort': array
}
2011-01-01 11:44:42 +00:00
query: query object, more on query syntax at
2010-06-30 14:21:04 +00:00
https://wiki.0x2620.org/wiki/pandora/QuerySyntax
2011-06-01 11:03:43 +00:00
positions: ids of items for which positions are required
2011-01-13 12:06:05 +00:00
return {
status: {...},
data: {
positions: {
id: position
}
}
}
2010-01-25 09:20:21 +00:00
'''
2010-02-22 09:25:29 +00:00
data = json.loads(request.POST['data'])
if settings.JSON_DEBUG:
print json.dumps(data, indent=2)
2011-02-22 15:26:06 +00:00
query = parse_query(data, request.user)
2011-01-01 11:44:42 +00:00
2010-02-10 13:10:28 +00:00
response = json_response({})
2010-07-01 08:49:08 +00:00
if 'group' in query:
response['data']['items'] = []
2010-09-23 16:01:48 +00:00
items = 'items'
item_qs = query['qs']
2011-01-24 13:44:38 +00:00
order_by = _order_by_group(query)
2010-09-23 16:01:48 +00:00
qs = models.Facet.objects.filter(key=query['group']).filter(item__id__in=item_qs)
qs = qs.values('value').annotate(items=Count('id')).order_by(*order_by)
2011-06-01 11:03:43 +00:00
if 'positions' in query:
2010-07-01 08:49:08 +00:00
response['data']['positions'] = {}
ids = [j['value'] for j in qs]
2011-06-01 11:03:43 +00:00
response['data']['positions'] = utils.get_positions(ids, query['positions'])
2010-07-01 08:49:08 +00:00
elif 'range' in data:
qs = qs[query['range'][0]:query['range'][1]]
response['data']['items'] = [{'name': i['value'], 'items': i[items]} for i in qs]
else:
response['data']['items'] = qs.count()
2011-06-01 11:03:43 +00:00
elif 'position' in query:
qs = _order_query(query['qs'], query['sort'])
2010-09-23 16:01:48 +00:00
ids = [j['itemId'] for j in qs.values('itemId')]
2011-06-01 11:03:43 +00:00
data['conditions'] = data['conditions'] + {
'value': query['position'],
'key': query['sort'][0]['key'],
'operator': '^'
}
query = parse_query(data, request.user)
qs = _order_query(query['qs'], query['sort'])
if qs.count() > 0:
response['data']['position'] = utils.get_positions(ids, [qs[0].itemId])[0]
elif 'positions' in query:
qs = _order_query(query['qs'], query['sort'])
ids = [j['itemId'] for j in qs.values('itemId')]
response['data']['positions'] = utils.get_positions(ids, query['positions'])
2010-07-01 08:49:08 +00:00
elif 'keys' in query:
response['data']['items'] = []
qs = _order_query(query['qs'], query['sort'])
_p = query['keys']
def only_p_sums(m):
r = {}
for p in _p:
2011-01-24 02:35:52 +00:00
if p == 'viewed' and request.user.is_authenticated():
value = m.accessed.filter(user=request.user).annotate(v=Max('access'))
r[p] = value.exists() and value[0].v or None
elif p == 'accessed':
r[p] = m.a
elif p == 'popularity':
2011-01-24 02:35:52 +00:00
r[p] = m.sort.popularity
else:
r[p] = m.json.get(p, '')
2011-06-01 11:03:43 +00:00
if 'annotations' in query:
n = query['annotations']
r['annotations'] = [a.json(layer=True)
for a in query['aqs'].filter(itemID=m.id)[:n]]
return r
2010-07-01 08:49:08 +00:00
def only_p(m):
r = {}
if m:
m = json.loads(m)
for p in _p:
r[p] = m.get(p, '')
2011-06-01 11:03:43 +00:00
if 'annotations' in query:
n = query['annotations']
r['annotations'] = [a.json(layer=True)
for a in query['aqs'].filter(item__itemId=m['id'])[:n]]
2010-07-01 08:49:08 +00:00
return r
qs = qs[query['range'][0]:query['range'][1]]
2011-01-24 13:44:38 +00:00
#response['data']['items'] = [m.get_json(_p) for m in qs]
if 'popularity' in _p:
qs = qs.annotate(popularity=Sum('accessed__accessed'))
2011-01-24 02:35:52 +00:00
if 'accessed' in _p:
qs = qs.annotate(a=Max('accessed__access'))
if 'viewed' in _p or 'popularity' in _p or 'accessed' in _p:
response['data']['items'] = [only_p_sums(m) for m in qs]
else:
response['data']['items'] = [only_p(m['json']) for m in qs.values('json')]
2011-06-01 11:03:43 +00:00
2010-07-01 08:49:08 +00:00
else: # otherwise stats
2010-09-23 16:01:48 +00:00
items = query['qs']
2011-04-08 08:20:59 +00:00
files = File.objects.filter(item__in=items).filter(size__gt=0)
2010-07-01 08:49:08 +00:00
r = files.aggregate(
Sum('duration'),
Sum('pixels'),
Sum('size')
2010-07-01 08:49:08 +00:00
)
response['data']['duration'] = r['duration__sum']
response['data']['files'] = files.count()
2010-09-23 16:01:48 +00:00
response['data']['items'] = items.count()
response['data']['pixels'] = r['pixels__sum']
response['data']['runtime'] = items.aggregate(Sum('sort__runtime'))['sort__runtime__sum']
response['data']['size'] = r['size__sum']
2011-01-24 13:44:38 +00:00
for key in ('runtime', 'duration', 'pixels', 'size'):
if response['data'][key] == None:
response['data'][key] = 0
2009-08-01 14:14:54 +00:00
return render_to_json_response(response)
actions.register(find)
2011-01-04 07:32:32 +00:00
def autocomplete(request):
'''
param data
key
value
operator '', '^', '$'
range
return
'''
data = json.loads(request.POST['data'])
if not 'range' in data:
data['range'] = [0, 10]
op = data.get('operator', '')
2011-01-14 14:32:48 +00:00
site_config = models.site_config()
key = site_config['keys'][data['key']]
2011-01-25 15:14:36 +00:00
order_by = key.get('autocompleteSortKey', False)
2011-01-15 14:22:29 +00:00
if order_by:
order_by = '-sort__%s' % order_by
2011-01-15 14:22:29 +00:00
else:
order_by = '-items'
2011-01-25 15:14:36 +00:00
sort_type = key.get('sort', key.get('type', 'string'))
if sort_type == 'title':
2011-02-22 15:26:06 +00:00
qs = parse_query({'query': data.get('query', {})}, request.user)['qs']
if data['value']:
if op == '':
qs = qs.filter(find__key=data['key'], find__value__icontains=data['value'])
elif op == '^':
qs = qs.filter(find__key=data['key'], find__value__istartswith=data['value'])
elif op == '$':
qs = qs.filter(find__key=data['key'], find__value__iendswith=data['value'])
2011-01-25 15:23:21 +00:00
qs = qs.order_by(order_by, nulls_last=True)
qs = qs[data['range'][0]:data['range'][1]]
response = json_response({})
response['data']['items'] = [i.get(data['key']) for i in qs]
else:
qs = models.Facet.objects.filter(key=data['key'])
if data['value']:
if op == '':
qs = qs.filter(value__icontains=data['value'])
elif op == '^':
qs = qs.filter(value__istartswith=data['value'])
elif op == '$':
qs = qs.filter(value__iendswith=data['value'])
2011-01-15 14:22:29 +00:00
qs = qs.values('value').annotate(items=Count('id'))
qs = qs.order_by(order_by)
qs = qs[data['range'][0]:data['range'][1]]
response = json_response({})
response['data']['items'] = [i['value'] for i in qs]
return render_to_json_response(response)
actions.register(autocomplete)
2011-01-04 07:32:32 +00:00
def get(request):
'''
param data {
id: string
keys: array
}
return item array
'''
response = json_response({})
data = json.loads(request.POST['data'])
item = get_object_or_404_json(models.Item, itemId=data['id'])
if item.access(request.user):
info = item.get_json(data['keys'])
if 'stream' in data['keys']:
info['stream'] = item.get_stream()
if 'layers' in data['keys']:
info['layers'] = item.get_layers(request.user)
2011-07-30 12:23:06 +00:00
response['data'] = info
else:
response = json_response(status=403, text='permission denied')
return render_to_json_response(response)
actions.register(get)
def getItem(request):
2010-01-25 09:20:21 +00:00
'''
param data
string id
2010-09-18 13:03:27 +00:00
return item array
2010-01-25 09:20:21 +00:00
'''
2010-02-10 13:10:28 +00:00
response = json_response({})
2010-01-25 09:20:21 +00:00
itemId = json.loads(request.POST['data'])
2010-09-23 16:01:48 +00:00
item = get_object_or_404_json(models.Item, itemId=itemId)
2011-01-24 13:44:38 +00:00
if item.access(request.user):
info = item.get_json()
info['stream'] = item.get_stream()
2011-02-11 10:21:25 +00:00
info['layers'] = item.get_layers(request.user)
response['data'] = info
2011-01-24 13:44:38 +00:00
else:
response = json_response(status=403, text='permission denied')
2010-01-25 09:20:21 +00:00
return render_to_json_response(response)
actions.register(getItem)
2010-01-25 09:20:21 +00:00
2011-01-04 07:32:32 +00:00
@login_required_json
def editItem(request):
2010-01-26 02:24:05 +00:00
'''
param data {
id: string,
key: value,..
}
return {
status: {'code': int, 'text': string},
data: {}
}
2010-01-26 02:24:05 +00:00
'''
data = json.loads(request.POST['data'])
2010-09-23 16:01:48 +00:00
item = get_object_or_404_json(models.Item, itemId=data['id'])
if item.editable(request.user):
2011-02-25 12:12:56 +00:00
response = json_response(status=200, text='ok')
2010-09-18 13:03:27 +00:00
item.edit(data)
else:
2010-02-10 13:10:28 +00:00
response = json_response(status=403, text='permissino denied')
2010-01-25 09:20:21 +00:00
return render_to_json_response(response)
2011-01-13 08:33:14 +00:00
actions.register(editItem, cache=False)
2010-01-26 02:24:05 +00:00
@login_required_json
def removeItem(request):
2010-01-26 02:24:05 +00:00
'''
param data
string id
return {'status': {'code': int, 'text': string}}
2010-01-26 02:24:05 +00:00
'''
2010-02-10 13:10:28 +00:00
response = json_response({})
itemId = json.loads(request.POST['data'])
2010-09-23 16:01:48 +00:00
item = get_object_or_404_json(models.Item, itemId=itemId)
2010-09-18 13:03:27 +00:00
if item.editable(request.user):
2010-02-10 13:10:28 +00:00
response = json_response(status=501, text='not implemented')
2010-09-18 13:03:27 +00:00
else:
2010-11-25 15:21:23 +00:00
response = json_response(status=403, text='permission denied')
2010-01-25 09:20:21 +00:00
return render_to_json_response(response)
2011-01-13 08:33:14 +00:00
actions.register(removeItem, cache=False)
2010-01-26 02:24:05 +00:00
2010-09-13 13:46:49 +00:00
'''
Poster API
'''
def parse(request): #parse path and return info
'''
param data {
path: string
}
return {
status: {'code': int, 'text': string},
2010-12-24 10:31:44 +00:00
data: {
imdb: string
}
}
'''
path = json.loads(request.POST['data'])['path']
2010-08-07 14:31:20 +00:00
response = json_response(utils.parse_path(path))
return render_to_json_response(response)
actions.register(parse)
2010-08-07 14:31:20 +00:00
def setPosterFrame(request): #parse path and return info
2010-08-07 14:31:20 +00:00
'''
param data {
id: itemId,
position: float
}
return {
status: {'code': int, 'text': string},
2010-12-24 10:31:44 +00:00
data: {
}
}
2010-08-07 14:31:20 +00:00
'''
data = json.loads(request.POST['data'])
2010-09-23 16:01:48 +00:00
item = get_object_or_404_json(models.Item, itemId=data['id'])
2010-08-07 14:31:20 +00:00
if item.editable(request.user):
#FIXME: some things need to be updated after changing this
item.poster_frame = data['position']
item.save()
2011-01-21 10:49:24 +00:00
tasks.update_poster.delay(item.itemId)
response = json_response()
2010-09-18 13:03:27 +00:00
else:
2010-08-07 14:31:20 +00:00
response = json_response(status=403, text='permissino denied')
return render_to_json_response(response)
2011-01-13 08:33:14 +00:00
actions.register(setPosterFrame, cache=False)
2010-08-07 14:31:20 +00:00
def setPoster(request): #parse path and return info
2010-08-07 14:31:20 +00:00
'''
param data {
id: itemId,
2011-07-30 12:52:49 +00:00
source: string
}
return {
status: {'code': int, 'text': string},
2010-12-24 10:31:44 +00:00
data: {
poster: {url,width,height}
}
}
2010-08-07 14:31:20 +00:00
'''
data = json.loads(request.POST['data'])
2010-09-23 16:01:48 +00:00
item = get_object_or_404_json(models.Item, itemId=data['id'])
2011-07-26 17:22:23 +00:00
response = json_response()
2010-08-07 14:31:20 +00:00
if item.editable(request.user):
2011-07-30 12:52:49 +00:00
valid_sources = [p['source'] for p in item.get_posters()]
if data['source'] in valid_sources:
item.poster_source = data['source']
2010-09-10 15:12:22 +00:00
if item.poster:
item.poster.delete()
item.save()
tasks.update_poster.delay(item.itemId)
response = json_response()
2010-09-10 15:12:22 +00:00
response['data']['poster'] = item.get_poster()
else:
response = json_response(status=403, text='invalid poster url')
2010-09-18 13:03:27 +00:00
else:
2010-09-10 15:12:22 +00:00
response = json_response(status=403, text='permission denied')
2010-01-25 09:20:21 +00:00
return render_to_json_response(response)
2011-01-13 08:33:14 +00:00
actions.register(setPoster, cache=False)
2010-01-25 09:20:21 +00:00
2011-06-04 16:15:38 +00:00
def lookup(request):
'''
param data {
title: string,
director: [string],
year: string,
id: string
}
return {
status: {'code': int, 'text': string},
data: {
title: string,
director: [string],
year: string,
id: string
}
}
'''
data = json.loads(request.POST['data'])
if 'id' in data:
i = models.Item.objects.get(itemId=data['id'])
r = {'id': i.itemId}
for key in ('title', 'director', 'year'):
r[key] = i.get(key)
response = json_response(r)
else:
response = json_response(status=404, text='not found')
return render_to_json_response(response)
actions.register(lookup)
def getImdbId(request):
'''
param data {
title: string,
director: string,
year: string
}
return {
status: {'code': int, 'text': string},
2010-12-24 10:31:44 +00:00
data: {
imdbId:string
}
}
'''
2011-01-01 11:44:42 +00:00
data = json.loads(request.POST['data'])
imdbId = ox.web.imdb.getImdbId(data['title'], data['director'], timeout=-1)
if imdbId:
2010-02-10 13:10:28 +00:00
response = json_response({'imdbId': imdbId})
else:
2010-09-18 13:03:27 +00:00
response = json_response(status=404, text='not found')
2010-01-25 09:20:21 +00:00
return render_to_json_response(response)
actions.register(getImdbId)
2010-01-25 09:20:21 +00:00
2010-09-13 13:46:49 +00:00
'''
2010-09-18 15:16:45 +00:00
media delivery
2010-09-13 13:46:49 +00:00
'''
def frame(request, id, position, size):
2010-09-23 16:01:48 +00:00
item = get_object_or_404(models.Item, itemId=id)
position = float(position.replace(',', '.'))
2010-09-23 16:01:48 +00:00
frame = item.frame(position, int(size))
if not frame:
raise Http404
return HttpFileResponse(frame, content_type='image/jpeg')
2011-07-26 17:22:23 +00:00
def poster_frame(request, id, position):
item = get_object_or_404(models.Item, itemId=id)
position = int(position)
frames = item.poster_frames()
print frames, position
if frames and len(frames) > position:
frame = frames[position]
return HttpFileResponse(frame, content_type='image/jpeg')
raise Http404
2011-01-04 07:32:32 +00:00
def image_to_response(item, image, size=None):
if size:
size = int(size)
path = image.path.replace('.jpg', '.%d.jpg'%size)
if not os.path.exists(path):
image_size = max(image.width, image.height)
if size > image_size:
return redirect('/%s/icon.jpg' % item.itemId)
extract.resize_image(image.path, path, size=size)
else:
path = image.path
return HttpFileResponse(path, content_type='image/jpeg')
2011-07-26 17:22:23 +00:00
def poster_local(request, id):
item = get_object_or_404(models.Item, itemId=id)
poster = item.path('poster.local.jpg')
poster = os.path.abspath(os.path.join(settings.MEDIA_ROOT, poster))
return HttpFileResponse(poster, content_type='image/jpeg')
2011-01-04 07:32:32 +00:00
2010-09-13 15:19:38 +00:00
def poster(request, id, size=None):
2010-09-23 16:01:48 +00:00
item = get_object_or_404(models.Item, itemId=id)
if size == 'large':
size = None
2010-09-23 16:01:48 +00:00
if item.poster:
2011-01-04 07:32:32 +00:00
return image_to_response(item, item.poster, size)
else:
2011-01-01 11:44:42 +00:00
if not size:
size='large'
poster_path = os.path.join(settings.STATIC_ROOT, 'png/posterDark.48.png')
2011-06-27 13:39:35 +00:00
response = HttpFileResponse(poster_path, content_type='image/jpeg')
response['Cache-Control'] = 'no-cache'
return response
2011-01-04 07:32:32 +00:00
def icon(request, id, size=None):
item = get_object_or_404(models.Item, itemId=id)
if item.icon:
return image_to_response(item, item.icon, size)
else:
raise Http404
2010-09-08 13:38:44 +00:00
def timeline(request, id, timeline, size, position):
2010-09-23 16:01:48 +00:00
item = get_object_or_404(models.Item, itemId=id)
2010-09-08 13:38:44 +00:00
if timeline == 'strip':
2010-09-23 16:01:48 +00:00
timeline = '%s.%s.%04d.png' %(item.timeline_prefix[:-8] + 'strip', size, int(position))
2010-09-08 13:38:44 +00:00
else:
2010-09-23 16:01:48 +00:00
timeline = '%s.%s.%04d.png' %(item.timeline_prefix, size, int(position))
return HttpFileResponse(timeline, content_type='image/png')
2011-01-04 07:32:32 +00:00
2011-01-16 13:28:57 +00:00
def timeline_overview(request, id, size):
item = get_object_or_404(models.Item, itemId=id)
timeline = '%s.%s.png' %(item.timeline_prefix, size)
return HttpFileResponse(timeline, content_type='image/png')
2011-07-03 16:21:27 +00:00
def torrent(request, id, filename=None):
item = get_object_or_404(models.Item, itemId=id)
if not item.torrent:
raise Http404
if not filename or filename.endswith('.torrent'):
response = HttpFileResponse(item.torrent.path,
content_type='application/x-bittorrent')
filename = "%s.torrent" % item.get('title')
response['Content-Disposition'] = 'attachment; filename="%s"' % filename
return response
while filename.startswith('/'):
filename = filename[1:]
filename = filename.replace('/../', '/')
filename = item.path('torrent/%s' % filename)
filename = os.path.abspath(os.path.join(settings.MEDIA_ROOT, filename))
return HttpFileResponse(filename)
def video(request, id, profile, oshash=None, format=None):
2010-09-23 16:01:48 +00:00
item = get_object_or_404(models.Item, itemId=id)
2011-06-27 19:40:44 +00:00
if oshash:
stream = get_object_or_404(item.files, oshash=oshash)
path = stream.video.path
else:
stream = get_object_or_404(item.streams, profile=profile)
path = stream.video.path
#server side cutting
2011-04-22 23:34:01 +00:00
t = request.GET.get('t')
if t:
t = map(float, t.split(','))
ext = os.path.splitext(profile)[1]
2011-06-27 19:40:44 +00:00
content_type = mimetypes.guess_type(path)[0]
if len(t) == 2 and t[1] > t[0] and stream.info['duration']>=t[1]:
response = HttpResponse(extract.chop(path, t[0], t[1]), content_type=content_type)
filename = "Clip of %s - %s-%s - %s %s%s" % (
item.get('title'),
ox.formatDuration(t[0] * 1000),
ox.formatDuration(t[1] * 1000),
settings.SITENAME,
item.itemId,
ext
)
response['Content-Disposition'] = 'attachment; filename="%s"' % filename
return response
else:
filename = "%s - %s %s%s" % (
item.get('title'),
settings.SITENAME,
item.itemId,
ext
)
response = HttpFileResponse(path, content_type=content_type)
response['Content-Disposition'] = 'attachment; filename="%s"' % filename
return response
2011-06-27 19:40:44 +00:00
return HttpFileResponse(path)