From 7acea6b9e0a4468fed38bfd6efa332ec67732d6e Mon Sep 17 00:00:00 2001 From: Zed Date: Tue, 2 Jun 2020 20:27:43 +0200 Subject: [PATCH] Add user agent for media requests, improve error --- src/routes/media.nim | 25 +++++++++++++++---------- src/routes/router_utils.nim | 6 +++--- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/routes/media.nim b/src/routes/media.nim index cc80c98..b0639ea 100644 --- a/src/routes/media.nim +++ b/src/routes/media.nim @@ -3,13 +3,14 @@ import asyncfile, uri, strutils, httpclient, os, mimetypes import jester, regex import router_utils -import ".."/[types, formatters] +import ".."/[types, formatters, agents] import ../views/general export asyncfile, httpclient, os, strutils export regex const m3u8Regex* = re"""url="(.+.m3u8)"""" +let mediaAgent* = getAgent() proc createMediaRouter*(cfg: Config) = router media: @@ -33,19 +34,23 @@ proc createMediaRouter*(cfg: Config) = createDir(cfg.cacheDir) if not existsFile(filename): - let client = newAsyncHttpClient() + let client = newAsyncHttpClient(userAgent=mediaAgent) + var failed = false + try: await client.downloadFile($uri, filename) except HttpRequestError: - client.safeClose() + failed = true removeFile(filename) - resp Http404 - except OSError: - echo "Disk full" - resp Http404 + except OSError as e: + echo "Disk full, or network error: ", e.msg + failed = true finally: client.safeClose() + if failed: + resp Http404 + sendFile(filename) get "/gif/@url": @@ -56,7 +61,7 @@ proc createMediaRouter*(cfg: Config) = let url = decodeUrl(@"url") cond isTwitterUrl(url) == true - let content = await safeFetch(url) + let content = await safeFetch(url, mediaAgent) if content.len == 0: resp Http404 let filename = parseUri(url).path.split(".")[^1] @@ -70,14 +75,14 @@ proc createMediaRouter*(cfg: Config) = if getHmac(url) != @"sig": resp showError("Failed to verify signature", cfg) - var content = await safeFetch(url) + var content = await safeFetch(url, mediaAgent) if content.len == 0: resp Http404 if ".vmap" in url: var m: RegexMatch discard content.find(m3u8Regex, m) url = decodeUrl(content[m.group(0)[0]]) - content = await safeFetch(url) + content = await safeFetch(url, mediaAgent) if ".m3u8" in url: content = proxifyVideo(content, prefs.proxyVideos) diff --git a/src/routes/router_utils.nim b/src/routes/router_utils.nim index 9b26819..94c3e23 100644 --- a/src/routes/router_utils.nim +++ b/src/routes/router_utils.nim @@ -1,6 +1,6 @@ import strutils, sequtils, asyncdispatch, httpclient, uri from jester import Request -import ../utils, ../prefs +import ".."/[utils, prefs] export utils, prefs template savePref*(pref, value: string; req: Request; expire=false) = @@ -33,8 +33,8 @@ proc safeClose*(client: AsyncHttpClient) = try: client.close() except: discard -proc safeFetch*(url: string): Future[string] {.async.} = - let client = newAsyncHttpClient() +proc safeFetch*(url, agent: string): Future[string] {.async.} = + let client = newAsyncHttpClient(userAgent=agent) try: result = await client.getContent(url) except: discard finally: client.safeClose()