Add simple file cache (no auto deletion yet)

This commit is contained in:
Zed 2019-06-23 02:46:46 +02:00
parent 64259ef1ea
commit d070e76e1a
2 changed files with 25 additions and 11 deletions

View File

@ -1,9 +1,11 @@
import asyncdispatch, httpclient, times, strutils, hashes, random, uri import asyncdispatch, asyncfile, httpclient, strutils, uri, os
import jester, regex import jester
import api, utils, types, cache import api, utils, types, cache
import views/[user, general, conversation] import views/[user, general, conversation]
const cacheDir {.strdefine.} = "/tmp/nitter"
proc showTimeline(name: string; num=""): Future[string] {.async.} = proc showTimeline(name: string; num=""): Future[string] {.async.} =
let let
username = name.strip(chars={'/'}) username = name.strip(chars={'/'})
@ -28,6 +30,7 @@ routes:
get "/@name/?": get "/@name/?":
cond '.' notin @"name" cond '.' notin @"name"
let timeline = await showTimeline(@"name", @"after") let timeline = await showTimeline(@"name", @"after")
if timeline.len == 0: if timeline.len == 0:
resp Http404, showError("User \"" & @"name" & "\" not found") resp Http404, showError("User \"" & @"name" & "\" not found")
@ -36,6 +39,7 @@ routes:
get "/@name/status/@id": get "/@name/status/@id":
cond '.' notin @"name" cond '.' notin @"name"
let conversation = await getTweet(@"id") let conversation = await getTweet(@"id")
if conversation.tweet.id.len == 0: if conversation.tweet.id.len == 0:
resp Http404, showError("Tweet not found") resp Http404, showError("Tweet not found")
@ -45,18 +49,24 @@ routes:
get "/pic/@sig/@url": get "/pic/@sig/@url":
cond "http" in @"url" cond "http" in @"url"
cond "twimg" in @"url" cond "twimg" in @"url"
let url = decodeUrl(@"url")
let
url = decodeUrl(@"url")
path = parseUri(url).path.split("/")[2 .. ^1].join("/")
filename = cacheDir / cleanFilename(path)
if getHmac(url) != @"sig": if getHmac(url) != @"sig":
resp showError("Failed to verify signature") resp showError("Failed to verify signature")
let if not existsDir(cacheDir):
client = newAsyncHttpClient() createDir(cacheDir)
pic = await client.getContent(url)
client.close() if not existsFile(filename):
let client = newAsyncHttpClient()
await client.downloadFile(url, filename)
client.close()
resp pic, mimetype(url) sendFile(filename)
get "/video/@sig/@url": get "/video/@sig/@url":
cond "http" in @"url" cond "http" in @"url"
@ -68,9 +78,9 @@ routes:
let let
client = newAsyncHttpClient() client = newAsyncHttpClient()
pic = await client.getContent(url) video = await client.getContent(url)
defer: client.close() defer: client.close()
resp pic, mimetype(url) resp video, mimetype(url)
runForever() runForever()

View File

@ -1,5 +1,5 @@
import strutils, strformat, uri import strutils, strformat, uri
import nimcrypto import nimcrypto, regex
const key = "supersecretkey" const key = "supersecretkey"
@ -21,3 +21,7 @@ proc getSigUrl*(link: string; path: string): string =
sig = getHmac(link) sig = getHmac(link)
url = encodeUrl(link) url = encodeUrl(link)
&"/{path}/{sig}/{url}" &"/{path}/{sig}/{url}"
proc cleanFilename*(filename: string): string =
const reg = re"[^A-Za-z0-9._-]"
filename.replace(reg, "_")