Skip to content

Commit 1c5e14f

Browse files
authored
FTP Upload Implementation
1 parent 0839814 commit 1c5e14f

File tree

4 files changed

+91
-22
lines changed

4 files changed

+91
-22
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@
4040
4141
> ## Client Configuration
4242
> At this point you will have to set the client to use the personal server, after loading the server folder with the [configuration executed previously](https://github.com/PoulDev/PyScreenSaver#server-configuration) we open the configs.json file in the "data" folder and modify some values.
43-
> - The first value to set is "use_server", which will have to be set to true
43+
> - The first value to set is "enabled", which will have to be set to true
4444
> - The second value is "server_password", and you have to enter the chosen password in the server configuration
45-
> - The last important value to change is "server_ip", where you have to put the ip of your server.
45+
> - The last important value to change is "server_ip", where you have to put the ip of your server.

data/configs.json

+10-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,17 @@
55
"outline_rgb": [255, 255, 255]
66
},
77
"server": {
8-
"use_server": false,
8+
"enabled": false,
99
"server_ip": "127.0.0.1",
1010
"server_port": 8482,
1111
"server_password": "Your Super Secret Password"
12+
},
13+
"ftp_server": {
14+
"enabled": false,
15+
"address": "",
16+
"username": "",
17+
"password": "",
18+
"directory": "",
19+
"url": "http://127.0.0.1/"
1220
}
13-
}
21+
}

main.py

+79-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import ctypes
22
import hashlib
3+
from typing import Any, Callable, Union
34
from PySide6.QtCore import *
45
from PySide6.QtGui import QPixmap
56
from PySide6.QtWidgets import *
@@ -12,14 +13,21 @@
1213
import sys
1314
import mss
1415
import json
15-
import time
16+
import win32con
17+
import win32gui
18+
import ctypes
1619
import keyboard
1720
import pyperclip
1821
import requests
22+
import random
23+
import string
1924
import win32clipboard
25+
from ftplib import FTP
26+
2027

2128
from data.selector import Ui_MainWindow
2229

30+
2331
configs = json.loads(open('data\\configs.json', 'r').read())
2432

2533
OUTLINE = tuple(configs['selector']['outline_rgb'])
@@ -46,6 +54,73 @@ def uploadToServer():
4654
headers = {"authentication" : hashlib.sha256(configs['server']['server_password'].encode()).hexdigest()}
4755
).json()
4856

57+
class UploadMethod:
58+
INSTANCE: Any = None
59+
60+
def __init__(self):
61+
if UploadMethod.INSTANCE:
62+
raise RuntimeError('UploadMethod is a singletone Object.')
63+
UploadMethod.INSTANCE = self
64+
self.save_image = self._find_upload_method()
65+
66+
def animate_cursor(self):
67+
cursor = win32gui.LoadImage(0, 32512, win32con.IMAGE_CURSOR,
68+
0, 0, win32con.LR_SHARED)
69+
self.system_cursor = ctypes.windll.user32.CopyImage(cursor, win32con.IMAGE_CURSOR,
70+
0, 0, win32con.LR_COPYFROMRESOURCE)
71+
72+
cursor = win32gui.LoadImage(0, "working.ani", win32con.IMAGE_CURSOR,
73+
0, 0, win32con.LR_LOADFROMFILE);
74+
ctypes.windll.user32.SetSystemCursor(cursor, 32512)
75+
ctypes.windll.user32.DestroyCursor(cursor);
76+
77+
78+
def restore_cursor(self):
79+
ctypes.windll.user32.SetSystemCursor(self.system_cursor, 32512)
80+
ctypes.windll.user32.DestroyCursor(self.system_cursor);
81+
82+
def _find_upload_method(self) -> Callable:
83+
for upload_method, method_function in {'ftp_server': self._ftp_upload, 'server': self._server_upload}.items():
84+
if configs[upload_method]['enabled']:
85+
return method_function
86+
return self._clipboard_save
87+
88+
def _ftp_upload(self, image: Image.Image) -> None:
89+
filename = 'screenshot_' + ''.join(random.choices(string.ascii_letters, k=15)) + '.png'
90+
image.save(filename)
91+
with FTP(
92+
configs['ftp_server']['address'],
93+
configs['ftp_server']['username'],
94+
configs['ftp_server']['password']) as ftp, open(filename, 'rb') as file:
95+
ftp.cwd(configs['ftp_server']['directory'])
96+
ftp.storbinary(f'STOR {filename}', file)
97+
98+
pyperclip.copy(f'{configs["ftp_server"]["url"]}/{filename}')
99+
try:
100+
os.remove(filename)
101+
except: pass
102+
103+
def _server_upload(self, image: Image.Image) -> None:
104+
image.save('screenshot.png')
105+
res = uploadToServer()
106+
if not 'uid' in res:
107+
ctypes.windll.user32.MessageBoxW(0, res['error'], "Upload Error", 0)
108+
else:
109+
pyperclip.copy(f'http://{configs["server"]["server_ip"]}:{configs["server"]["server_port"]}/{res["uid"]}')
110+
try:
111+
os.remove('screenshot.png')
112+
except: pass
113+
114+
def _clipboard_save(self, image: Image.Image) -> None:
115+
output = io.BytesIO()
116+
image.convert("RGB").save(output, "BMP")
117+
data = output.getvalue()[14:]
118+
output.close()
119+
send_to_clipboard(win32clipboard.CF_DIB, data)
120+
121+
122+
UploadMethod()
123+
49124
class SelectorWindow(QMainWindow):
50125
def __init__(self):
51126
QMainWindow.__init__(self)
@@ -100,24 +175,10 @@ def mouseReleaseEvent(self, event):
100175
print(e)
101176
return self.ui.image.setPixmap(QPixmap.fromImage(ImageQt(self.originalImg)))
102177

103-
# Copy screenshot to clipboard
104178
self.close()
105-
if configs['server']['use_server']:
106-
cropped_im.save('screenshot.png')
107-
res = uploadToServer()
108-
if not 'uid' in res:
109-
ctypes.windll.user32.MessageBoxW(0, res['error'], "Upload Error", 0)
110-
else:
111-
pyperclip.copy(f'http://{configs["server"]["server_ip"]}:{configs["server"]["server_port"]}/{res["uid"]}')
112-
try:
113-
os.remove('screenshot.png')
114-
except: pass
115-
else:
116-
output = io.BytesIO()
117-
cropped_im.convert("RGB").save(output, "BMP")
118-
data = output.getvalue()[14:]
119-
output.close()
120-
send_to_clipboard(win32clipboard.CF_DIB, data)
179+
UploadMethod.INSTANCE.animate_cursor()
180+
UploadMethod.INSTANCE.save_image(cropped_im)
181+
UploadMethod.INSTANCE.restore_cursor()
121182

122183
self.startpos = None
123184
super().mouseReleaseEvent(event)

working.ani

155 KB
Binary file not shown.

0 commit comments

Comments
 (0)