From cfcd471ffc3f43ec9c6763acf921e6b8547ab007 Mon Sep 17 00:00:00 2001 From: Zed Date: Mon, 21 Oct 2019 05:55:18 +0200 Subject: [PATCH] Use sendFile, refactor mimetypes --- src/nitter.nim | 4 +++- src/routes/media.nim | 16 +++++----------- src/utils.nim | 10 ---------- 3 files changed, 8 insertions(+), 22 deletions(-) diff --git a/src/nitter.nim b/src/nitter.nim index aa0be9a..5d71157 100644 --- a/src/nitter.nim +++ b/src/nitter.nim @@ -1,4 +1,4 @@ -import asyncdispatch +import asyncdispatch, mimetypes from net import Port import jester @@ -55,4 +55,6 @@ routes: extend status, "" extend media, "" +settings.mimes.register("1500x500", "image/jpeg") + runForever() diff --git a/src/routes/media.nim b/src/routes/media.nim index 778610d..3adb245 100644 --- a/src/routes/media.nim +++ b/src/routes/media.nim @@ -1,4 +1,4 @@ -import asyncfile, uri, strutils, httpclient, os +import asyncfile, uri, strutils, httpclient, os, mimetypes import jester, regex @@ -32,14 +32,7 @@ proc createMediaRouter*(cfg: Config) = except: discard - if not existsFile(filename): - halt Http404 - - let file = openAsync(filename) - let buf = await readAll(file) - file.close() - - resp buf, mimetype(filename) + sendFile(filename) get "/gif/@url": cond "http" in @"url" @@ -60,7 +53,7 @@ proc createMediaRouter*(cfg: Config) = if content.len == 0: halt Http404 - resp content, mimetype(url) + resp content, settings.mimes.getMimetype(url.split(".")[^1]) get "/video/@sig/@url": cond "http" in @"url" @@ -83,4 +76,5 @@ proc createMediaRouter*(cfg: Config) = content = proxifyVideo(content, prefs.proxyVideos) client.close() - resp content, mimetype(url) + let ext = parseUri(url).path.split(".")[^1] + resp content, settings.mimes.getMimetype(ext) diff --git a/src/utils.nim b/src/utils.nim index 1bfc904..1178707 100644 --- a/src/utils.nim +++ b/src/utils.nim @@ -11,16 +11,6 @@ const "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 = ($hmac(sha256, key, data))[0 .. 12]