From b4bd9756fbfc3e452b4646777c5b08d32ff6e835 Mon Sep 17 00:00:00 2001
From: jimmy <git@jimmy.nz>
Date: Sat, 5 Nov 2022 00:41:07 +1300
Subject: [PATCH] Add server

---
 main.py    | 10 ----------
 printer.py | 12 ++++--------
 server.py  | 19 +++++++++++++++++++
 3 files changed, 23 insertions(+), 18 deletions(-)
 delete mode 100644 main.py
 create mode 100644 server.py

diff --git a/main.py b/main.py
deleted file mode 100644
index b48ee7e..0000000
--- a/main.py
+++ /dev/null
@@ -1,10 +0,0 @@
-from watchgod import watch
-from printer import printer
-
-base = "./"
-def main():
-    for changes in watch('./images'):
-        print(changes)  
-        for change in changes:
-            printer(base + "/" + change[1])
-
diff --git a/printer.py b/printer.py
index 67257ab..3e7ad11 100644
--- a/printer.py
+++ b/printer.py
@@ -3,18 +3,14 @@ from PIL import Image, ImageDraw, ImageFont, ImageOps
 import textwrap
 import random
 
-def printer(path):    
-    #432x432
-    #34 34
-
-    432/48
+def printer():    
     fr=Image.open("frame.png")
-    im=Image.open(path)
+    im=Image.open("image.png")
     im = im.resize((432, 432), Image.Resampling.BOX)
     
     fr.paste(im, (34,34), im)
 
-    fr.show()
+    #fr.show()
     
     raster = StarTSPImage.imageToRaster(fr, cut=True)
 
@@ -22,4 +18,4 @@ def printer(path):
     printer.write(raster)
 
 if __name__ == "__main__":
-    printer("./images/test.png")
\ No newline at end of file
+    printer()
\ No newline at end of file
diff --git a/server.py b/server.py
new file mode 100644
index 0000000..69d044a
--- /dev/null
+++ b/server.py
@@ -0,0 +1,19 @@
+from fastapi import FastAPI, File, UploadFile
+from printer import printer
+
+app = FastAPI()
+
+@app.post("/upload")
+def upload(file: UploadFile = File(...)):
+    try:
+        contents = file.file.read()
+        with open("image.png", 'wb') as f:
+            f.write(contents)
+    except Exception:
+        return {"message": "There was an error uploading the file"}
+    finally:
+        file.file.close()
+    
+    printer()
+
+    return {"message": f"Successfully uploaded {file.filename}"}
\ No newline at end of file