Runtime: add arbitrary file upload to working dir in web interface
authorEdouard Tisserant
Tue, 29 Nov 2022 10:59:53 +0100
changeset 3703 4b75f1506ea1
parent 3702 6dc619fa28aa
child 3704 c17fac18c663
Runtime: add arbitrary file upload to working dir in web interface
runtime/NevowServer.py
--- a/runtime/NevowServer.py	Fri Nov 25 11:12:14 2022 +0100
+++ b/runtime/NevowServer.py	Tue Nov 29 10:59:53 2022 +0100
@@ -27,6 +27,7 @@
 from __future__ import print_function
 import os
 import collections
+import shutil
 import platform as platform_module
 from zope.interface import implements
 from nevow import appserver, inevow, tags, loaders, athena, url, rend
@@ -230,6 +231,18 @@
                                                "Restart or Repair"),
                                            action=_("Do"))
 
+    # pylint: disable=no-self-argument
+    def uploadFile(
+            ctx=annotate.Context(),
+            uploadedfile=annotate.FileUpload(required=True,
+                                  label=_("File to upload"))):
+        pass
+
+    uploadFile = annotate.autocallable(uploadFile,
+                                           label=_(
+                                               "Upload a file to PLC working directory"),
+                                           action=_("Upload"))
+
 customSettingsURLs = {
 }
 
@@ -304,8 +317,14 @@
             GetPLCObjectSingleton().RepairPLC()
         else:
             MainWorker.quit()
-            
-        
+
+    def uploadFile(self, uploadedfile, **kwargs):
+        if uploadedfile is not None:
+            fobj = getattr(uploadedfile, "file", None)
+        if fobj is not None:
+            with open(uploadedfile.filename, 'w') as destfd:
+                fobj.seek(0)
+                shutil.copyfileobj(fobj,destfd)
 
     def locateChild(self, ctx, segments):
         if segments[0] in customSettingsURLs: