-
Notifications
You must be signed in to change notification settings - Fork 0
/
answer-upload.py
310 lines (282 loc) · 10.6 KB
/
answer-upload.py
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#
# This software is Copyright ©️ 2020 The University of Southern California. All Rights Reserved.
# Permission to use, copy, modify, and distribute this software and its documentation for educational, research and non-profit purposes, without fee, and without a written agreement is hereby granted, provided that the above copyright notice and subject to the full license file found in the root of this software deliverable. Permission to make commercial use of this software may be obtained by contacting: USC Stevens Center for Innovation University of Southern California 1150 S. Olive Street, Suite 2300, Los Angeles, CA 90115, USA Email: [email protected]
#
# The full terms of this copyright and license should always be found in the root directory of this software deliverable as "license.txt" and if these terms are not found with this software, please contact the USC Stevens Center for the full license.
#
import json
import uuid
import boto3
import base64
import tempfile
import os
from module.constants import Supported_Video_Type, supported_video_types, MP4
from media_tools import assert_video_duration, get_video_file_type
from module.utils import (
create_json_response,
s3_bucket,
is_authorized,
load_sentry,
require_env,
get_auth_headers,
)
from module.api import (
is_upload_in_progress,
FetchUploadTaskReq,
upload_answer_and_task_update,
AnswerUpdateRequest,
UploadTaskRequest,
upload_answer_update,
)
from module.logger import get_logger
load_sentry()
log = get_logger("upload-answer")
aws_region = require_env("REGION")
s3_client = boto3.client("s3", region_name=aws_region)
sns = boto3.client("sns", region_name=aws_region)
upload_bucket = require_env("SIGNED_UPLOAD_BUCKET")
sqs_client = boto3.client("sqs", region_name=aws_region)
sfn_client = boto3.client("stepfunctions", region_name=aws_region)
step_fn_arn = require_env("ANSWER_UPLOAD_STEP_FUNCTION_ARN")
def create_task_list(trim, has_edited_transcript):
transcode_web_task = {
"task_name": "transcoding-web",
"task_id": str(uuid.uuid4()),
"status": "QUEUED",
}
transcode_mobile_task = {
"task_name": "transcoding-mobile",
"task_id": str(uuid.uuid4()),
"status": "QUEUED",
}
transcribe_task = (
{
"task_name": "transcribing",
"task_id": str(uuid.uuid4()),
"status": "QUEUED",
}
if not has_edited_transcript
else None
)
trim_upload_task = (
{
"task_name": "trim-upload",
"task_id": str(uuid.uuid4()),
"status": "QUEUED",
}
if trim
else None
)
return transcode_web_task, transcode_mobile_task, transcribe_task, trim_upload_task
def upload_to_s3(
file_path,
video_file_type: Supported_Video_Type,
s3_path,
mentor,
question,
auth_headers,
):
log.info("uploading %s to %s", file_path, s3_path)
# to prevent data inconsistency by partial failures (new web.mp3 - old transcript...)
# first remove old media urls from DB
upload_answer_update(
AnswerUpdateRequest(
mentor,
question,
web_media={"type": "video", "tag": "web", "url": ""},
mobile_media={"type": "video", "tag": "mobile", "url": ""},
vtt_media={"type": "subtitles", "tag": "en", "url": ""},
),
auth_headers,
)
# then remove old media in s3
supported_extensions = list(
map(lambda video_type: video_type.extension, supported_video_types)
)
all_artifacts = [
*[f"original.{extension}" for extension in supported_extensions],
*[f"web.{extension}" for extension in supported_extensions],
*[f"mobile.{extension}" for extension in supported_extensions],
"en.vtt",
]
s3_client.delete_objects(
Bucket=s3_bucket,
Delete={"Objects": [{"Key": f"{s3_path}/{name}"} for name in all_artifacts]},
)
s3_client.upload_file(
file_path,
s3_bucket,
f"{s3_path}/original.{video_file_type.extension}",
ExtraArgs={"ContentType": video_file_type.mime},
)
def get_original_video_url(
mentor: str, question: str, video_file_type: Supported_Video_Type
) -> str:
base_url = os.environ.get("STATIC_URL_BASE", "")
return f"{base_url}/videos/{mentor}/{question}/original.{video_file_type.extension}"
def handler(event, context):
log.info(event)
if "body" not in event:
data = {
"error": "Bad Request",
"message": "body payload is required",
}
return create_json_response(401, data, event)
if event["isBase64Encoded"]:
body = base64.b64decode(event["body"])
else:
body = event["body"]
upload_request = json.loads(body)
if "mentor" not in upload_request:
data = {
"error": "Bad Request",
"message": "mentor parameter missing",
}
return create_json_response(401, data, event)
if "question" not in upload_request:
data = {
"error": "Bad Request",
"message": "question parameter missing",
}
return create_json_response(401, data, event)
if "video" not in upload_request:
data = {
"error": "Bad Request",
"message": "video s3 object parameter missing",
}
return create_json_response(401, data, event)
auth_headers = get_auth_headers(event)
mentor = upload_request["mentor"]
question = upload_request["question"]
video_key = upload_request["video"]
maintain_original_aspect_ratio = (
upload_request["maintain_original_aspect_ratio"]
if "maintain_original_aspect_ratio" in upload_request
else False
)
external_video_ids = (
upload_request["externalVideoIds"]
if "externalVideoIds" in upload_request
else None
)
print(external_video_ids, flush=True)
generate_thumbnail = (
upload_request["generateThumbnail"]
if "generateThumbnail" in upload_request
else False
)
is_vbg_video = (
upload_request["isVbgVideo"] if "isVbgVideo" in upload_request else False
) # vbg videos are expected to be in format of mime type webm with vp9 encoding
trim = upload_request.get("trim")
has_edited_transcript = upload_request.get("hasEditedTranscript")
token = json.loads(event["requestContext"]["authorizer"]["token"])
if not is_authorized(mentor, token):
data = {
"error": "not authorized",
"message": "not authorized",
}
return create_json_response(401, data, event)
upload_in_progress = is_upload_in_progress(
FetchUploadTaskReq(mentor, question), auth_headers
)
if upload_in_progress:
data = {
"error": "upload in progress",
"message": "There is an upload already in progress, please wait.",
}
return create_json_response(401, data, event)
with tempfile.TemporaryDirectory() as work_dir:
file_path = os.path.join(
work_dir, "original_video"
) # don't assume video file type
s3_client.download_file(upload_bucket, video_key, file_path)
try:
video_file_type = get_video_file_type(file_path)
except Exception as e:
log.debug(e)
log.debug("unknown file mime type, will attempt to transcode to mp4")
video_file_type = MP4
if not assert_video_duration(file_path, 1000):
data = {
"error": "Bad Request",
"message": "No video found or too short (1sec)!",
}
return create_json_response(401, data, event)
s3_path = f"videos/{mentor}/{question}"
# this will overwrite any existing file
upload_to_s3(
file_path, video_file_type, s3_path, mentor, question, auth_headers
)
(
transcode_web_task,
transcode_mobile_task,
transcribe_task,
trim_upload_task,
) = create_task_list(trim, has_edited_transcript)
task_list = [transcode_web_task, transcode_mobile_task]
if transcribe_task is not None:
task_list.append(transcribe_task)
if trim_upload_task is not None:
task_list.append(trim_upload_task)
req = {
"request": {
"mentor": mentor,
"question": question,
"video": f"{s3_path}/original.{video_file_type.extension}",
"isVbgVideo": is_vbg_video,
**({"trim": trim} if trim is not None else {}),
"transcodeWebTask": transcode_web_task,
"transcodeMobileTask": transcode_mobile_task,
"trimUploadTask": trim_upload_task,
"transcribeTask": transcribe_task,
"authHeaders": auth_headers,
"maintain_original_aspect_ratio": maintain_original_aspect_ratio,
"generate_thumbnail": generate_thumbnail,
}
}
original_video_url = get_original_video_url(mentor, question, video_file_type)
# we risk here overriding values, perhaps processing was already done, so status is DONE
# but this will overwrite and revert them back to QUEUED. Can we just append?
upload_answer_and_task_update(
AnswerUpdateRequest(
mentor=mentor,
question=question,
transcript="",
external_video_ids=external_video_ids,
),
UploadTaskRequest(
mentor=mentor,
question=question,
transcode_web_task=transcode_web_task,
transcode_mobile_task=transcode_mobile_task,
trim_upload_task=trim_upload_task,
transcribe_task=transcribe_task,
transcript="",
original_media={
"type": "video",
"tag": "original",
"url": original_video_url,
},
),
auth_headers,
)
# name must be unique for AWS account, region, and state machine for 90 days
job_name = f"{mentor}-{question}-{transcode_web_task['task_id']}"
sfn_job_id = sfn_client.start_execution(
stateMachineArn=step_fn_arn,
name=job_name[:80], # max length is 80
input=json.dumps(req),
# traceHeader='string' # TODO xray
)
log.info("step function executed %s", sfn_job_id)
data = {
"taskList": task_list,
"statusUrl": "use taskList to fetch from graphql",
}
return create_json_response(200, data, event)
# # for local debugging:
# if __name__ == "__main__":
# with open("__events__/answer-upload-event.json.dist") as f:
# event = json.loads(f.read())
# handler(event, {})