Use sendFile, refactor mimetypes

This commit is contained in:
Zed 2019-10-21 05:55:18 +02:00
parent 59e392e3b4
commit cfcd471ffc
3 changed files with 8 additions and 22 deletions

View file

@ -1,4 +1,4 @@
import asyncdispatch import asyncdispatch, mimetypes
from net import Port from net import Port
import jester import jester
@ -55,4 +55,6 @@ routes:
extend status, "" extend status, ""
extend media, "" extend media, ""
settings.mimes.register("1500x500", "image/jpeg")
runForever() runForever()

View file

@ -1,4 +1,4 @@
import asyncfile, uri, strutils, httpclient, os import asyncfile, uri, strutils, httpclient, os, mimetypes
import jester, regex import jester, regex
@ -32,14 +32,7 @@ proc createMediaRouter*(cfg: Config) =
except: except:
discard discard
if not existsFile(filename): sendFile(filename)
halt Http404
let file = openAsync(filename)
let buf = await readAll(file)
file.close()
resp buf, mimetype(filename)
get "/gif/@url": get "/gif/@url":
cond "http" in @"url" cond "http" in @"url"
@ -60,7 +53,7 @@ proc createMediaRouter*(cfg: Config) =
if content.len == 0: if content.len == 0:
halt Http404 halt Http404
resp content, mimetype(url) resp content, settings.mimes.getMimetype(url.split(".")[^1])
get "/video/@sig/@url": get "/video/@sig/@url":
cond "http" in @"url" cond "http" in @"url"
@ -83,4 +76,5 @@ proc createMediaRouter*(cfg: Config) =
content = proxifyVideo(content, prefs.proxyVideos) content = proxifyVideo(content, prefs.proxyVideos)
client.close() client.close()
resp content, mimetype(url) let ext = parseUri(url).path.split(".")[^1]
resp content, settings.mimes.getMimetype(ext)

View file

@ -11,16 +11,6 @@ const
"video.twimg.com" "video.twimg.com"
] ]
proc mimetype*(filename: string): string =
if ".png" in filename:
"image/" & "png"
elif ".jpg" in filename or ".jpeg" in filename or "1500x500" in filename:
"image/" & "jpg"
elif ".mp4" in filename:
"video/" & "mp4"
else:
"text/plain"
proc getHmac*(data: string): string = proc getHmac*(data: string): string =
($hmac(sha256, key, data))[0 .. 12] ($hmac(sha256, key, data))[0 .. 12]