Skip to content

Commit e384df0

Browse files
committed
1 parent ac08f47 commit e384df0

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

Server/Controller/Api.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from googleapiclient.http import MediaIoBaseDownload
2020
import io
2121
import hashlib
22+
import traceback
2223

2324
SCOPES = ['https://www.googleapis.com/auth/drive']
2425

@@ -298,6 +299,7 @@ def serveFile(response, path, filename = None, start = None, end = None):
298299
status.close()
299300
except BaseException as e:
300301
Print.error('File download exception: ' + str(e))
302+
traceback.print_exc(file=sys.stdout)
301303

302304
if response.bytesSent == 0:
303305
response.write(b'')
@@ -378,6 +380,7 @@ def getDownload(request, response, start = None, end = None):
378380
status.close()
379381
except BaseException as e:
380382
Print.error('NSP download exception: ' + str(e))
383+
traceback.print_exc(file=sys.stdout)
381384
if response.bytesSent == 0:
382385
response.write(b'')
383386

@@ -700,6 +703,9 @@ def getGdriveToken(request, response):
700703

701704
with open('token.pickle', 'wb') as token:
702705
pickle.dump(creds, token)
706+
707+
with open('gdrive.token', 'w') as token:
708+
token.write(json.dumps({'access_token': creds.token, 'refresh_token': creds.refresh_token}))
703709

704710
r = {}
705711
r['access_token'] = creds.token
@@ -708,8 +714,8 @@ def getGdriveToken(request, response):
708714
with open(Config.getGdriveCredentialsFile(), 'r') as f:
709715
r['credentials'] = json.loads(f.read())
710716

711-
712-
response.write(json.dumps(r))
717+
if response is not None:
718+
response.write(json.dumps(r))
713719

714720
def listGdriveDir(path):
715721
r = {'dirs': [], 'files': []}
@@ -922,3 +928,4 @@ def getFileSize(request, response):
922928
response.write(json.dumps({'success': False, 'message': str(e)}))
923929

924930

931+

Server/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def setMime(self, fileName):
189189
def attachFile(self, fileName):
190190
#Print.info('Attaching file ' + fileName)
191191
self.setMime(fileName)
192-
self.headers['Content-Disposition'] = 'attachment; filename=' + fileName
192+
self.headers['Content-Disposition'] = 'attachment; filename=' + re.sub(r'[^\x00-\x7F]+','_', fileName)
193193

194194
def sendHeader(self):
195195
self.handler.send_response(self.status)

server.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import urllib3
99
import urllib
1010
import json
11-
# import webbrowser
11+
import webbrowser
1212
import Server
1313

1414
import nut
@@ -28,7 +28,7 @@
2828
import socket
2929

3030
import sys
31-
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QAction, QTableWidget,QTableWidgetItem,QVBoxLayout,QDesktopWidget, QTabWidget, QProgressBar, QLabel,QHBoxLayout, QLineEdit, QPushButton, QCheckBox
31+
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QAction, QTableWidget,QTableWidgetItem,QVBoxLayout,QDesktopWidget, QTabWidget, QProgressBar, QLabel,QHBoxLayout, QLineEdit, QPushButton, QCheckBox, QMessageBox
3232
from PyQt5.QtGui import QIcon
3333
from PyQt5.QtCore import pyqtSlot,Qt,QTimer
3434
from PyQt5 import QtWidgets
@@ -58,6 +58,10 @@ def __init__(self, app):
5858
self.scan = QPushButton('Scan', app)
5959
self.scan.clicked.connect(app.on_scan)
6060
self.layout.addWidget(self.scan)
61+
62+
self.gdrive = QPushButton('Setup GDrive OAuth', app)
63+
self.gdrive.clicked.connect(app.on_gdrive)
64+
self.layout.addWidget(self.gdrive)
6165

6266
# self.autolaunchBrowser = QCheckBox("Launch Web Browser?", app)
6367
# self.autolaunchBrowser.setChecked(Config.autolaunchBrowser)
@@ -144,7 +148,7 @@ def __init__(self):
144148
super().__init__()
145149
self.setWindowIcon(QIcon('public_html/images/logo.jpg'))
146150
screen = QDesktopWidget().screenGeometry()
147-
self.title = 'NUT USB / Web Server v2.6'
151+
self.title = 'NUT USB / Web Server v2.7'
148152
self.left = screen.width() / 4
149153
self.top = screen.height() / 4
150154
self.width = screen.width() / 2
@@ -202,6 +206,28 @@ def on_scan(self):
202206
self.tableWidget.setRowCount(0)
203207
nut.scan()
204208
self.refreshTable()
209+
210+
@pyqtSlot()
211+
def on_gdrive(self):
212+
if Config.getGdriveCredentialsFile() is None:
213+
webbrowser.open_new_tab('https://developers.google.com/drive/api/v3/quickstart/go')
214+
QMessageBox.information(self, 'Google Drive OAuth Setup', "You require a credentials.json file to set up Google Drive OAuth. This file can be obtained from https://developers.google.com/drive/api/v3/quickstart/go , click on the blue button that says 'Enable the Drive API' and save the credentials.json to t his application's directory.")
215+
else:
216+
buttonReply = QMessageBox.question(self, 'Google Drive OAuth Setup', "Do you you want to setup GDrive OAuth?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
217+
218+
if buttonReply == QMessageBox.Yes:
219+
try:
220+
os.unlink('gdrive.token')
221+
except:
222+
pass
223+
224+
try:
225+
os.unlink('token.pickle')
226+
except:
227+
pass
228+
229+
Server.Controller.Api.getGdriveToken(None, None)
230+
QMessageBox.information(self, 'Google Drive OAuth Setup', "OAuth has completed. Please copy gdrive.token and credentials.json to your Nintendo Switch's sdmc:/switch/tinfoil/ and/or sdmc:/switch/sx/ directories.")
205231

206232
@pyqtSlot()
207233
def refreshTable(self):

0 commit comments

Comments
 (0)