2019-09-05 20:40:36 +00:00
|
|
|
import strutils, strformat, sequtils, uri, tables
|
2019-06-23 00:46:46 +00:00
|
|
|
import nimcrypto, regex
|
2019-06-20 14:16:20 +00:00
|
|
|
|
2019-09-13 10:27:04 +00:00
|
|
|
const
|
|
|
|
key = "supersecretkey"
|
|
|
|
twitterDomains = @[
|
|
|
|
"twitter.com",
|
|
|
|
"twimg.com",
|
|
|
|
"abs.twimg.com",
|
|
|
|
"pbs.twimg.com",
|
|
|
|
"video.twimg.com"
|
|
|
|
]
|
2019-06-20 14:16:20 +00:00
|
|
|
|
|
|
|
proc mimetype*(filename: string): string =
|
|
|
|
if ".png" in filename:
|
2019-06-23 12:34:19 +00:00
|
|
|
"image/" & "png"
|
2019-09-07 23:43:54 +00:00
|
|
|
elif ".jpg" in filename or ".jpeg" in filename or "1500x500" in filename:
|
2019-06-23 12:34:19 +00:00
|
|
|
"image/" & "jpg"
|
2019-06-20 14:16:20 +00:00
|
|
|
elif ".mp4" in filename:
|
2019-06-23 12:34:19 +00:00
|
|
|
"video/" & "mp4"
|
2019-06-20 14:16:20 +00:00
|
|
|
else:
|
2019-06-23 12:34:19 +00:00
|
|
|
"text/plain"
|
2019-06-20 14:16:20 +00:00
|
|
|
|
|
|
|
proc getHmac*(data: string): string =
|
|
|
|
($hmac(sha256, key, data))[0 .. 12]
|
|
|
|
|
2019-09-13 10:27:04 +00:00
|
|
|
proc getVidUrl*(link: string): string =
|
2019-06-20 14:16:20 +00:00
|
|
|
let
|
|
|
|
sig = getHmac(link)
|
|
|
|
url = encodeUrl(link)
|
2019-09-13 10:27:04 +00:00
|
|
|
&"/video/{sig}/{url}"
|
|
|
|
|
|
|
|
proc getGifUrl*(link: string): string =
|
|
|
|
&"/gif/{encodeUrl(link)}"
|
|
|
|
|
|
|
|
proc getPicUrl*(link: string): string =
|
|
|
|
&"/pic/{encodeUrl(link)}"
|
2019-06-23 00:46:46 +00:00
|
|
|
|
|
|
|
proc cleanFilename*(filename: string): string =
|
|
|
|
const reg = re"[^A-Za-z0-9._-]"
|
|
|
|
filename.replace(reg, "_")
|
2019-09-05 20:40:36 +00:00
|
|
|
|
|
|
|
proc filterParams*(params: Table): seq[(string, string)] =
|
|
|
|
let filter = ["name", "id"]
|
|
|
|
toSeq(params.pairs()).filterIt(it[0] notin filter)
|
2019-09-13 10:27:04 +00:00
|
|
|
|
|
|
|
proc isTwitterUrl*(url: string): bool =
|
|
|
|
parseUri(url).hostname in twitterDomains
|