Coverage for src/couchers/servicers/media.py: 96%
23 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-11-21 04:21 +0000
« prev ^ index » next coverage.py v7.5.0, created at 2024-11-21 04:21 +0000
1import logging
3import grpc
4from google.protobuf import empty_pb2
6from couchers import errors
7from couchers.crypto import secure_compare
8from couchers.interceptors import ManualAuthValidatorInterceptor
9from couchers.models import InitiatedUpload, Upload
10from couchers.sql import couchers_select as select
11from proto import media_pb2_grpc
13logger = logging.getLogger(__name__)
16def get_media_auth_interceptor(secret_token):
17 def is_authorized(token):
18 return secure_compare(token.encode("ascii"), secret_token.encode("ascii"))
20 return ManualAuthValidatorInterceptor(is_authorized)
23class Media(media_pb2_grpc.MediaServicer):
24 def UploadConfirmation(self, request, context, session):
25 initiated_upload = session.execute(
26 select(InitiatedUpload).where(InitiatedUpload.key == request.key).where(InitiatedUpload.is_valid)
27 ).scalar_one_or_none()
29 if not initiated_upload:
30 context.abort(grpc.StatusCode.NOT_FOUND, errors.UPLOAD_NOT_FOUND)
32 # move it to a completed upload
33 upload = Upload(
34 key=request.key,
35 filename=request.filename,
36 creator_user_id=initiated_upload.initiator_user_id,
37 )
38 session.add(upload)
40 # delete the old upload
41 session.delete(initiated_upload)
43 return empty_pb2.Empty()