def video(request, id, resolution, format, index=None): resolution = int(resolution) resolutions = sorted(settings.CONFIG['video']['resolutions']) if resolution not in resolutions: raise Http404 item = get_object_or_404(models.Item, itemId=id) if not item.access(request.user): return HttpResponseForbidden() if index: index = int(index) - 1 else: index = 0 streams = item.streams() #wafaa print "%s" % streams logger.error(streams) if index + 1 > streams.count(): raise Http404 stream = streams[index].get(resolution, format) if not stream.available or not stream.media: raise Http404 path = stream.media.path #server side cutting #FIXME: this needs to join segments if needed t = request.GET.get('t') if t: def parse_timestamp(s): if ':' in s: s = ox.time2ms(s) / 1000 return float(s) t = map(parse_timestamp, t.split(',')) ext = '.%s' % format 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 = u"Clip of %s - %s-%s - %s %s%s" % ( item.get('title'), ox.format_duration(t[0] * 1000).replace( ':', '.')[:-4], ox.format_duration(t[1] * 1000).replace( ':', '.')[:-4], settings.SITENAME, item.itemId, ext) response[ 'Content-Disposition'] = "attachment; filename*=UTF-8''%s" % quote( filename.encode('utf-8')) 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*=UTF-8''%s" % quote( filename.encode('utf-8')) return response if not settings.XSENDFILE and not settings.XACCELREDIRECT: return redirect(stream.media.url) response = HttpFileResponse(path) response['Cache-Control'] = 'public' return response
def timeline(request, id, size, position=-1, format='jpg', mode=None): item = get_object_or_404(models.Item, itemId=id) if not item.access(request.user): return HttpResponseForbidden() if not mode: mode = 'antialias' modes = [t['id'] for t in settings.CONFIG['timelines']] if mode not in modes: raise Http404 modes.pop(modes.index(mode)) prefix = os.path.join(item.timeline_prefix, 'timeline') def timeline(): timeline = '%s%s%sp' % (prefix, mode, size) if position > -1: timeline += '%d' % int(position) return timeline + '.jpg' path = timeline() while modes and not os.path.exists(path): mode = modes.pop(0) path = timeline() return HttpFileResponse(path, content_type='image/jpeg')
def download(request, id, index=1): item = get_object_or_404(models.Item, itemId=id) resolution = max(settings.CONFIG['video']['resolutions']) #wafaa #format = 'webm' format = 'png' if not item.access(request.user): return HttpResponseForbidden() if index: index = int(index) - 1 else: index = 0 streams = item.streams() if index + 1 > streams.count(): raise Http404 stream = streams[index].get(resolution, format) if not stream.available or not stream.media: raise Http404 path = stream.media.path content_type = mimetypes.guess_type(path)[0] ext = os.path.splitext(path)[-1] 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*=UTF-8''%s" % quote( filename.encode('utf-8')) return response
def frame(request, id, size, position=None): item = get_object_or_404(models.Item, itemId=id) if not item.access(request.user): return HttpResponseForbidden() frame = None if not position: if settings.CONFIG['media']['importFrames'] or item.poster_frame == -1: frames = item.poster_frames() if frames: position = item.poster_frame if position == -1 or position > len(frames): position = int(len(frames) / 2) position = frames[int(position)]['position'] elif item.poster_frame == -1 and item.sort.duration: position = item.sort.duration / 2 else: position = item.poster_frame else: position = float(position.replace(',', '.')) if not frame: frame = item.frame(position, int(size)) if not frame: frame = os.path.join(settings.STATIC_ROOT, 'jpg/list256.jpg') #raise Http404 return HttpFileResponse(frame, content_type='image/jpeg')
def poster_frame(request, id, position): item = get_object_or_404(models.Item, itemId=id) if not item.access(request.user): return HttpResponseForbidden() position = int(position) frames = item.poster_frames() if frames and len(frames) > position: frame = frames[position]['path'] return HttpFileResponse(frame, content_type='image/jpeg') raise Http404
def sitemap_xml(request): sitemap = os.path.abspath(os.path.join(settings.MEDIA_ROOT, 'sitemap.xml')) if not os.path.exists(sitemap): tasks.update_sitemap(request.build_absolute_uri('/')) elif time.mktime( time.localtime()) - os.stat(sitemap).st_ctime > 24 * 60 * 60: tasks.update_sitemap.delay(request.build_absolute_uri('/')) response = HttpFileResponse(sitemap) response['Content-Type'] = 'application/xml' return response
def icon(request, id, size=None): item = get_object_or_404(models.Item, itemId=id) if not item.access(request.user): return HttpResponseForbidden() if item.icon: return image_to_response(item.icon, size) else: poster_path = os.path.join(settings.STATIC_ROOT, 'jpg/poster.jpg') response = HttpFileResponse(poster_path, content_type='image/jpeg') response['Cache-Control'] = 'no-cache' return response
def image_to_response(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: path = image.path else: extract.resize_image(image.path, path, size=size) else: path = image.path return HttpFileResponse(path, content_type='image/jpeg')
def icon(request, id, size=16): if not size: size = 16 id = id.split(':') username = id[0] name = ":".join(id[1:]) qs = models.Edit.objects.filter(user__username=username, name=name) if qs.count() == 1 and qs[0].accessible(request.user): edit = qs[0] icon = edit.get_icon(int(size)) else: icon = os.path.join(settings.STATIC_ROOT, 'jpg/list256.jpg') return HttpFileResponse(icon, content_type='image/jpeg')
def siteposter(request, id, size=None): item = get_object_or_404(models.Item, itemId=id) if not item.access(request.user): return HttpResponseForbidden() poster = item.path('siteposter.jpg') poster = os.path.abspath(os.path.join(settings.MEDIA_ROOT, poster)) if size: size = int(size) image = Image.open(poster) image_size = max(image.size) if size < image_size: path = poster.replace('.jpg', '.%d.jpg' % size) extract.resize_image(poster, path, size=size) poster = path return HttpFileResponse(poster, content_type='image/jpeg')
def torrent(request, id, filename=None): item = get_object_or_404(models.Item, itemId=id) if not item.access(request.user): return HttpResponseForbidden() if not item.torrent: raise Http404 if not filename or filename.endswith('.torrent'): response = HttpResponse(item.get_torrent(request), content_type='application/x-bittorrent') filename = utils.safe_filename("%s.torrent" % item.get('title')) response[ 'Content-Disposition'] = "attachment; filename*=UTF-8''%s" % quote( filename.encode('utf-8')) 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)) response = HttpFileResponse(filename) response['Content-Disposition'] = "attachment; filename*=UTF-8''%s" % \ quote(os.path.basename(filename.encode('utf-8'))) return response
def poster(request, id, size=None): item = get_object_or_404(models.Item, itemId=id) if not item.access(request.user): return HttpResponseForbidden() if not item.poster: poster_path = os.path.join(settings.MEDIA_ROOT, item.path('poster.jpg')) if os.path.exists(poster_path): item.poster.name = item.path('poster.jpg') item.poster_height = item.poster.height item.poster_width = item.poster.width models.Item.objects.filter(pk=item.id).update( poster=item.poster.name, poster_height=item.poster_height, poster_width=item.poster_width, icon=item.icon.name, json=item.get_json()) if item.poster: return image_to_response(item.poster, size) else: poster_path = os.path.join(settings.STATIC_ROOT, 'jpg/poster.jpg') response = HttpFileResponse(poster_path, content_type='image/jpeg') response['Cache-Control'] = 'no-cache' return response
def thumbnail(request, id, size=256): size = int(size) document = models.Document.get(id) return HttpFileResponse(document.thumbnail(size))
def file(request, id, name=None): document = models.Document.get(id) return HttpFileResponse(document.file.path)
def pdf(request, id): text = get_text_or_404_json(id) if text.type == 'pdf' and text.file and not text.uploading: return HttpFileResponse(text.file.path, content_type='application/pdf') response = json_response(status=404, text='file not found') return render_to_json_response(response)
def serve_static_file(path, location, content_type): return HttpFileResponse(location, content_type=content_type)