diff --git a/.gitattributes b/.gitattributes index 90eaec19..0198fdf1 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,3 +1,4 @@ * text eol=lf *.png binary +*.gif binary diff --git a/copyparty/__init__.py b/copyparty/__init__.py index 60b4e450..1d427577 100644 --- a/copyparty/__init__.py +++ b/copyparty/__init__.py @@ -14,6 +14,7 @@ if PY2: class EnvParams(object): def __init__(self): + self.mod = os.path.dirname(os.path.realpath(__file__)) if sys.platform == "win32": self.cfg = os.path.normpath(os.environ["APPDATA"] + "/copyparty") elif sys.platform == "darwin": diff --git a/copyparty/httpcli.py b/copyparty/httpcli.py index 983f362d..97cd0199 100644 --- a/copyparty/httpcli.py +++ b/copyparty/httpcli.py @@ -4,6 +4,7 @@ from __future__ import print_function import time import hashlib +import mimetypes from .__init__ import * from .util import * @@ -84,9 +85,9 @@ class HttpCli(object): return ret[:-4].decode("utf-8", "replace").split("\r\n") - def reply(self, body): - header = "HTTP/1.1 200 OK\r\nConnection: Keep-Alive\r\nContent-Type: text/html\r\nContent-Length: {0}\r\n\r\n".format( - len(body) + def reply(self, body, status="200 OK", mime="text/html"): + header = "HTTP/1.1 {}\r\nConnection: Keep-Alive\r\nContent-Type: {}\r\nContent-Length: {}\r\n\r\n".format( + status, mime, len(body) ).encode( "utf-8" ) @@ -95,16 +96,43 @@ class HttpCli(object): return body - def loud_reply(self, body): + def loud_reply(self, body, **kwargs): self.log(body.rstrip()) - self.reply(b"
" + body.encode("utf-8"))
+        self.reply(b"
" + body.encode("utf-8"), **kwargs)
+
+    def send_file(self, path):
+        sz = os.path.getsize(path)
+        mime = mimetypes.guess_type(path)[0]
+        header = "HTTP/1.1 200 OK\r\nConnection: Keep-Alive\r\nContent-Type: {}\r\nContent-Length: {}\r\n\r\n".format(
+            mime, sz
+        ).encode(
+            "utf-8"
+        )
+
+        if self.ok:
+            self.s.send(header)
+
+        with open(path, "rb") as f:
+            while self.ok:
+                buf = f.read(4096)
+                if not buf:
+                    break
+
+                self.s.send(buf)
 
     def handle_get(self):
         self.log("")
         self.log("GET  {0} {1}".format(self.addr[0], self.req))
-        self.reply(
-            b'
' - ) + + static_path = os.path.join(E.mod, "web", self.req.split("?")[0][1:]) + + if os.path.isfile(static_path): + return self.send_file(static_path) + + if self.req == "/": + return self.send_file(os.path.join(E.mod, "web/splash.html")) + + return self.loud_reply("404 not found", status="404 Not Found") def handle_post(self): self.log("") diff --git a/copyparty/web/copyparty.gif b/copyparty/web/copyparty.gif new file mode 100644 index 00000000..fccc98d4 Binary files /dev/null and b/copyparty/web/copyparty.gif differ diff --git a/copyparty/web/splash.css b/copyparty/web/splash.css new file mode 100644 index 00000000..dd48bc2f --- /dev/null +++ b/copyparty/web/splash.css @@ -0,0 +1,15 @@ +html, body, #wrap { + color: #333; + background: #f7f7f7; + font-family: sans-serif; +} +#wrap { + max-width: 40em; + margin: 2em auto; + padding: 0 1em 3em 1em; +} +h1 { + border-bottom: 1px solid #ccc; + margin: 2em 0 .4em 0; + padding: 0 0 .2em 0; +} \ No newline at end of file diff --git a/copyparty/web/splash.html b/copyparty/web/splash.html new file mode 100644 index 00000000..8c247f93 --- /dev/null +++ b/copyparty/web/splash.html @@ -0,0 +1,19 @@ + + + + + + copyparty + + + + + + +
+

hello world

+
+ + + + \ No newline at end of file diff --git a/copyparty/web/splash.js b/copyparty/web/splash.js new file mode 100644 index 00000000..0f4dddb0 --- /dev/null +++ b/copyparty/web/splash.js @@ -0,0 +1 @@ +document.getElementsByTagName('h1')[0].insertAdjacentHTML('afterend', '') diff --git a/scripts/make-pypi-release.sh b/scripts/make-pypi-release.sh index bca7a0bf..26c613ff 100755 --- a/scripts/make-pypi-release.sh +++ b/scripts/make-pypi-release.sh @@ -52,12 +52,13 @@ EOF chmod 600 ~/.pypirc sed -ri 's/qwer/username/;s/asdf/password/' ~/.pypirc - # setup build env - cd ~/dev/copyparty && - virtualenv buildenv - . buildenv/bin/activate - pip install m2r - deactivate + # if PY2: create build env + cd ~/dev/copyparty && virtualenv buildenv + (. buildenv/bin/activate && pip install m2r) + + # if PY3: create build env + cd ~/dev/copyparty && python3 -m venv buildenv + (. buildenv/bin/activate && pip install m2r wheel) # test rst pip install docutils diff --git a/setup.py b/setup.py index fcfb660a..3d701a73 100755 --- a/setup.py +++ b/setup.py @@ -37,10 +37,11 @@ VERSION = None data_files = [("share/doc/copyparty", ["README.rst", "README.md", "LICENSE"])] manifest = "" for dontcare, files in data_files: - # print(dontcare) for fn in files: manifest += "include {0}\n".format(fn) +manifest += "recursive-include copyparty/web *\n" + here = os.path.abspath(os.path.dirname(__file__)) with open(here + "/MANIFEST.in", "wb") as f: