# Path: constants/exceptions.py
# class invalidArgumentsException(Exception):
# class loginFailedException(Exception):
# class userBannedException(Exception):
# class userLockedException(Exception):
# class noBanchoSessionException(Exception):
# class osuApiFailException(Exception):
# class fileNotFoundException(Exception):
# class invalidBeatmapException(Exception):
# class unsupportedGameModeException(Exception):
# class beatmapTooLongException(Exception):
# class need2FAException(Exception):
# class noAPIDataError(Exception):
# class scoreNotFoundError(Exception):
# class ppCalcException(Exception):
# 	def __init__(self, handler):
# 	def __init__(self, handler, who):
# 	def __init__(self, handler, who):
# 	def __init__(self, handler, who):
# 	def __init__(self, handler, who, ip):
# 	def __init__(self, handler):
# 	def __init__(self, handler, f):
# 	def __init__(self, handler):
# 	def __init__(self, handler, who, ip):
# 	def __init__(self, exception):
# 
# Path: helpers/replayHelper.py
# def toDotTicks(unixTime):
# def _getRawReplayFailedLocal(scoreID):
# def getRawReplayS3(scoreID):
# def _getFirstReplayFileName(scoreID):
# def buildFullReplay(scoreID=None, scoreData=None, rawReplay=None):
# 
# Path: objects/glob.py
# 		VERSION = f.read().strip()
# 	VERSION = "Unknown"
# ACHIEVEMENTS_VERSION = 1
# DATADOG_PREFIX = "lets"


import tornado.gen
import tornado.web
import timeout_decorator
import common.log.logUtils as log
from common.web import requestsManager
from constants import exceptions
from helpers import replayHelper
from common.sentry import sentry
from objects import glob



class handler(requestsManager.asyncRequestHandler):
	"""
	Handler for /replay/
	"""
	MODULE_NAME = "get_full_replay"

	@tornado.web.asynchronous
	@tornado.gen.engine
	@sentry.captureTornado
	def asyncGet(self, replayID):
		try:
			replayID = int(replayID)
			fullReplay = replayHelper.buildFullReplay(scoreID=replayID)
			self.write(fullReplay)
			self.add_header("Content-type", "application/octet-stream")
			self.set_header("Content-length", len(fullReplay))
			self.set_header("Content-Description", "File Transfer")
			self.set_header("Content-Disposition", "attachment; filename=\"{}.osr\"".format(replayID))
		except (exceptions.fileNotFoundException, exceptions.scoreNotFoundError, ValueError):
			self.set_status(404)
			self.write("Replay not found")
		except timeout_decorator.TimeoutError:
			log.error("S3 timed out")
			sentry.captureMessage("S3 timeout while fetching replay.")
			self.set_status(500)
			self.write("S3 Error")

# Path: constants/exceptions.py
# class invalidArgumentsException(Exception):
# class loginFailedException(Exception):
# class userBannedException(Exception):
# class userLockedException(Exception):
# class noBanchoSessionException(Exception):
# class osuApiFailException(Exception):
# class fileNotFoundException(Exception):
# class invalidBeatmapException(Exception):
# class unsupportedGameModeException(Exception):
# class beatmapTooLongException(Exception):
# class need2FAException(Exception):
# class noAPIDataError(Exception):
# class scoreNotFoundError(Exception):
# class ppCalcException(Exception):
# 	def __init__(self, handler):
# 	def __init__(self, handler, who):
# 	def __init__(self, handler, who):
# 	def __init__(self, handler, who):
# 	def __init__(self, handler, who, ip):
# 	def __init__(self, handler):
# 	def __init__(self, handler, f):
# 	def __init__(self, handler):
# 	def __init__(self, handler, who, ip):
# 	def __init__(self, exception):
# 
# Path: helpers/replayHelper.py
# def toDotTicks(unixTime):
# def _getRawReplayFailedLocal(scoreID):
# def getRawReplayS3(scoreID):
# def _getFirstReplayFileName(scoreID):
# def buildFullReplay(scoreID=None, scoreData=None, rawReplay=None):
# 
# Path: objects/glob.py
# 		VERSION = f.read().strip()
# 	VERSION = "Unknown"
# ACHIEVEMENTS_VERSION = 1
# DATADOG_PREFIX = "lets"


# Path: handlers/getFullReplayHandler.py
import tornado.gen
import tornado.web
import timeout_decorator
import common.log.logUtils as log
from common.web import requestsManager
from constants import exceptions
from helpers import replayHelper
from common.sentry import sentry
from objects import glob




class handler(requestsManager.asyncRequestHandler):
	"""
	Handler for /replay/
	"""
	MODULE_NAME = "get_full_replay"

	@tornado.web.asynchronous
	@tornado.gen.engine
	@sentry.captureTornado
	def asyncGet(self, replayID):
		try:
			replayID = int(replayID)
			fullReplay = replayHelper.buildFullReplay(scoreID=replayID)
			self.write(fullReplay)
			self.add_header("Content-type", "application/octet-stream")
			self.set_header("Content-length", len(fullReplay))
			self.set_header("Content-Description", "File Transfer")
			self.set_header("Content-Disposition", "attachment; filename=\"{}.osr\"".format(replayID))
		except (exceptions.fileNotFoundException, exceptions.scoreNotFoundError, ValueError):
			self.set_status(404)
			self.write("Replay not found")
		except timeout_decorator.TimeoutError:
			log.error("S3 timed out")
			sentry.captureMessage("S3 timeout while fetching replay.")
			self.set_status(500)
			self.write("S3 Error")

			glob.stats["replay_download_failures"].labels(type="full_s3_timeout").inc()