forked from CERNDocumentServer/cds-videos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
175 lines (150 loc) · 5.31 KB
/
Copy pathapi.py
File metadata and controls
175 lines (150 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2016, 2017 CERN.
#
# Invenio is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# Invenio is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Invenio; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
"""CDS Previewer API."""
import re
from os.path import join, relpath, split
from flask import current_app, url_for
from invenio_files_rest.models import ObjectVersion, as_object_version
from invenio_previewer.api import PreviewFile
def get_relative_path(object_version):
"""Get ObjectVersion's full path relative to its bucket location."""
object_version = as_object_version(object_version)
location_root = object_version.bucket.location.uri
filepath, filename = split(object_version.file.uri)
relative_path = relpath(filepath, location_root)
return join(relative_path, filename)
class CDSPreviewRecordFile(PreviewFile):
"""Preview record files implementation."""
@property
def subformats(self):
"""Get the subformats."""
return self.file.dumps().get("subformat", [])
@property
def uri(self):
"""Get file download link."""
return url_for(
"invenio_records_ui.{0}_files".format(self.pid.pid_type),
pid_value=self.pid.pid_value,
filename=self.file.key,
)
@property
def m3u8_uri(self):
"""Get m3u8 playlist link."""
smil_obj = self.smil_file_object
if smil_obj:
wowza_url = current_app.config["WOWZA_PLAYLIST_URL"]
filepath = get_relative_path(smil_obj)
return wowza_url.format(filepath="{0}".format(filepath))
@property
def poster_uri(self):
"""Get video's poster link."""
try:
return [
f["links"]["self"]
for f in self.record["_files"]
if f["context_type"] == "poster"
][0]
except IndexError:
return url_for(
"invenio_records_ui.{0}_files".format(self.pid.pid_type),
pid_value=self.pid.pid_value,
filename="frame-1.jpg",
)
@property
def record_uri(self):
"""Get the record uri."""
return url_for(
"invenio_records_rest.recid_item",
pid_value=self.pid.pid_value,
_external=True,
)
@property
def embed_uri(self):
"""Get the embed uri."""
rn = self.record.get("report_number", [""])[0]
return url_for(
"cds_redirector.video_embed_alias", report_number=rn, _external=True
)
@property
def thumbnails_uri(self):
"""Get video's thumbnails' link."""
return url_for(
"invenio_records_ui.{0}_export".format(self.pid.pid_type),
pid_value=self.pid.pid_value,
format="vtt",
raw=True,
)
@property
def subtitles(self):
"""Get video's subtitles."""
return [
(f["links"]["self"], f["tags"]["language"])
for f in self.record["_files"]
if f["context_type"] == "subtitle" and "language" in f["tags"]
]
@property
def smil_file_object(self):
"""Get corresponding SMIL file."""
data = self.file.dumps()
if "playlist" in data:
smil_info = data["playlist"][0]
return ObjectVersion.get(smil_info["bucket_id"], smil_info["key"])
@property
def vr(self):
"""Get video's VR flag."""
return self.record.get("vr")
@property
def chapters_uri(self):
"""Get the chapters.vtt file link if available."""
try:
return [
f["links"]["self"]
for f in self.record["_files"]
if f.get("context_type") == "chapters" and f.get("content_type") == "vtt"
][0]
except IndexError:
return None
class CDSPreviewDepositFile(PreviewFile):
"""Preview deposit files implementation."""
@property
def uri(self):
"""Get file download link.
.. note::
This is only for ```<pid_type:depid>``` records
"""
uri = "{api}/{bucket}/{key}".format(
api=current_app.config["DEPOSIT_FILES_API"],
bucket=str(self.file.bucket),
key=self.file.key,
)
return uri
@property
def subtitles(self):
"""Get video's subtitles."""
pattern = re.compile(".*_([a-zA-Z]{2})\.vtt$")
def get_subtitle_tuple(f):
"""Get URL and language of a subtitle file."""
found = pattern.findall(f["key"])
lang = found[0] if len(found) == 1 else ""
return f["links"]["self"], lang
return [
get_subtitle_tuple(f)
for f in self.record["_files"]
if f["content_type"] == "vtt"
]