|
| 1 | +# Copyright 2021 Universität Tübingen, DKFZ and EMBL for the German Human Genome-Phenome Archive (GHGA) |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from pyramid.view import view_config |
| 16 | +from pyramid.httpexceptions import HTTPNotFound, HTTPOk, HTTPTemporaryRedirect |
| 17 | +from pyramid.request import Request |
| 18 | +from pyramid.response import FileResponse |
| 19 | +from datetime import datetime, timedelta |
| 20 | +import urllib.parse |
| 21 | +from .. import security, models, storage |
| 22 | +from . import base_url |
| 23 | +from .files import access_file_by_user |
| 24 | +from ..errors import get_validation_error |
| 25 | +from sqlalchemy import and_ |
| 26 | + |
| 27 | + |
| 28 | +@view_config( |
| 29 | + route_name = "rpc_get_file_url", |
| 30 | + renderer = "json", |
| 31 | + request_method = "GET", |
| 32 | + openapi = True |
| 33 | +) |
| 34 | +def get_file_url(request) -> HTTPTemporaryRedirect: |
| 35 | + """Redirects to a temporary, pre-sign HTTP-URL for downloading a file. |
| 36 | + """ |
| 37 | + file_id = request.openapi_validated.parameters.path['id'] |
| 38 | + expires_after = request.openapi_validated.parameters.query['expires'] |
| 39 | + auth_user = security.revalidate_user(request) |
| 40 | + |
| 41 | + # get file from db: |
| 42 | + db_file = access_file_by_user( |
| 43 | + request, |
| 44 | + user = auth_user, |
| 45 | + file_id = file_id |
| 46 | + ) |
| 47 | + |
| 48 | + # retrieve URL: |
| 49 | + url = storage.get_download_url( |
| 50 | + request=request, |
| 51 | + db_file=db_file, |
| 52 | + expires_after=expires_after |
| 53 | + ) |
| 54 | + |
| 55 | + return HTTPTemporaryRedirect(url) |
| 56 | + |
| 57 | + |
| 58 | +@view_config( |
| 59 | + route_name = "download_by_token", |
| 60 | + renderer = "json", |
| 61 | + request_method = "GET" |
| 62 | +) |
| 63 | +def download_by_token(request) -> HTTPOk: |
| 64 | + """Download a file using a file download token. |
| 65 | +
|
| 66 | + Usage: /download/{download_token} |
| 67 | + """ |
| 68 | + token = request.matchdict['token'] |
| 69 | + hashed_token = security.hash_token(token) |
| 70 | + |
| 71 | + # get download token from db |
| 72 | + db = request.dbsession |
| 73 | + db_token = db.query(models.DownloadToken).filter( |
| 74 | + and_( |
| 75 | + models.DownloadToken.value==hashed_token, |
| 76 | + models.DownloadToken.expires>datetime.now() |
| 77 | + ) |
| 78 | + ).one_or_none() |
| 79 | + |
| 80 | + if db_token is None: |
| 81 | + raise HTTPNotFound() |
| 82 | + |
| 83 | + # serve file: |
| 84 | + response = FileResponse( |
| 85 | + storage.get_local_storage_path(request, db_token.file.storage_uri), |
| 86 | + request=request, |
| 87 | + content_type='application/octet-stream' |
| 88 | + ) |
| 89 | + response.content_disposition = f"attachment; filename=\"{db_token.file.name}\"" |
| 90 | + |
| 91 | + return response |
0 commit comments