From 7a614eaa579d58a3f2e6f6e216165e99e8d3636f Mon Sep 17 00:00:00 2001 From: Lost-MSth Date: Tue, 4 Oct 2022 00:28:19 +0800 Subject: [PATCH] [Enhance] Add download error status code --- latest version/core/download.py | 2 +- latest version/main.py | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/latest version/core/download.py b/latest version/core/download.py index 288bcfe..e5a8d36 100644 --- a/latest version/core/download.py +++ b/latest version/core/download.py @@ -93,7 +93,7 @@ class UserDownload: x = self.c.fetchone() if not x: - raise NoAccess('The token `%s` is not valid.' % self.token) + raise NoAccess('The token `%s` is not valid.' % self.token, status=403) self.user = User() self.user.user_id = x[0] self.token_time = x[1] diff --git a/latest version/main.py b/latest version/main.py index 7e02c6d..ec8d1c8 100644 --- a/latest version/main.py +++ b/latest version/main.py @@ -62,14 +62,16 @@ def download(file_path): x.song_id, x.file_name = file_path.split('/', 1) x.select_for_check() if x.is_limited: - raise ArcError('You have reached the download limit.', 903) - if x.is_valid: - x.insert_user_download() - # response = make_response() - # response.headers['Content-Type'] = 'application/octet-stream' - # response.headers['X-Accel-Redirect'] = '/nginx_download/' + file_path - # return response - return send_from_directory(Constant.SONG_FILE_FOLDER_PATH, file_path, as_attachment=True, conditional=True) + raise ArcError( + 'You have reached the download limit.', 903, status=403) + if not x.is_valid: + raise ArcError('Expired token.', status=403) + x.insert_user_download() + # response = make_response() + # response.headers['Content-Type'] = 'application/octet-stream' + # response.headers['X-Accel-Redirect'] = '/nginx_download/' + file_path + # return response + return send_from_directory(Constant.SONG_FILE_FOLDER_PATH, file_path, as_attachment=True, conditional=True) except ArcError as e: if Config.ALLOW_WARNING_LOG: app.logger.warning(format_exc())