def receive_chunk(self, torrent_id, chunk_number): """ This Handler receives chunks of a file being uploaded, previously registered through FileUploader. Subclass must implement download_tmp_dir and destination_dir property """ receiver = TorrentReceiver(torrent_id, download_tmp_dir=self.download_tmp_dir, destination_dir=self.destination_dir) receiver.receive(chunk_number, self.request.body) response = { 'torrent_id': torrent_id, 'chunk_number': chunk_number, 'percent': receiver.percent, 'complete': False, 'ok': True, } def finish(): self.set_header('Content-type', 'application/json') response['result'] = self.result self.write(json.dumps(response)) self.finish() if receiver.complete: response['complete'] = True receiver.finish() self.process_file(receiver.torrent, callback=finish) else: finish()
def generate_session(self): """ This Handler receives a torrent file and returns a session id to browser, so that chunks of this file may be uploaded through ChunkUploader using this session id Subclass must implement download_tmp_dir, remote_public_key properties and destination_dir """ torrent_data = self.request.body.decode("utf-8", errors="ignore") receiver = TorrentReceiver(download_tmp_dir=self.download_tmp_dir, remote_public_key=self.remote_public_key, destination_dir=self.destination_dir) try: receiver.load(torrent_data) except Receiver.UnauthorizedMessage: self.write( json.dumps({ 'ok': False, 'reason': "This file's source cannot be recognized. Downloading it is not safe", })) self.finish() return info = { 'ok': True, # using int instead of boolean saves bandwidth 'status': [int(i) for i in receiver.status], 'id': receiver.torrent_id, } def finish(): self.set_header('Content-type', 'application/json') info['result'] = self.result self.write(json.dumps(info)) self.finish() if receiver.complete: try: receiver.torrent.pop('data') except KeyError: pass receiver.finish() self.process_file(receiver.torrent, callback=finish) else: finish()
def generate_session(self): """ This Handler receives a torrent file and returns a session id to browser, so that chunks of this file may be uploaded through ChunkUploader using this session id Subclass must implement download_tmp_dir, remote_public_key properties and destination_dir """ torrent_data = self.request.body.decode("utf-8", errors="ignore") receiver = TorrentReceiver(download_tmp_dir=self.download_tmp_dir, remote_public_key=self.remote_public_key, destination_dir=self.destination_dir) try: receiver.load(torrent_data) except Receiver.UnauthorizedMessage: self.write(json.dumps({ 'ok': False, 'reason': "This file's source cannot be recognized. Downloading it is not safe", })) self.finish() return info = { 'ok': True, # using int instead of boolean saves bandwidth 'status': [ int(i) for i in receiver.status ], 'id': receiver.torrent_id, } def finish(): self.set_header('Content-type', 'application/json') info['result'] = self.result self.write(json.dumps(info)) self.finish() if receiver.complete: try: receiver.torrent.pop('data') except KeyError: pass receiver.finish() self.process_file(receiver.torrent, callback=finish) else: finish()