aboutsummaryrefslogtreecommitdiff
path: root/routes/api.go
diff options
context:
space:
mode:
authorFChannel <>2022-05-02 12:07:00 -0700
committerFChannel <>2022-06-19 12:53:29 -0700
commit733f911fadc872933481bcbe087d519ce00372df (patch)
tree54788da4f730bd11934401c1eca48a3c87f08027 /routes/api.go
parente80fe14f7985f9e85bfb9582926acd7891455786 (diff)
cleaned up main
Diffstat (limited to 'routes/api.go')
-rw-r--r--routes/api.go54
1 files changed, 54 insertions, 0 deletions
diff --git a/routes/api.go b/routes/api.go
new file mode 100644
index 0000000..2fb0f3f
--- /dev/null
+++ b/routes/api.go
@@ -0,0 +1,54 @@
+package routes
+
+import (
+ "io/ioutil"
+ "net/http"
+ "time"
+
+ "github.com/FChannel0/FChannel-Server/config"
+ "github.com/gofiber/fiber/v2"
+)
+
+func Media(c *fiber.Ctx) error {
+ if c.Query("hash") != "" {
+ return RouteImages(c, c.Query("hash"))
+ }
+
+ return c.SendStatus(404)
+}
+
+func RouteImages(ctx *fiber.Ctx, media string) error {
+ req, err := http.NewRequest("GET", config.MediaHashs[media], nil)
+ if err != nil {
+ return err
+ }
+
+ client := http.Client{
+ Timeout: 5 * time.Second,
+ }
+
+ resp, err := client.Do(req)
+ if err != nil {
+ return err
+ }
+ defer resp.Body.Close()
+
+ if resp.StatusCode != 200 {
+ fileBytes, err := ioutil.ReadFile("./static/notfound.png")
+ if err != nil {
+ return err
+ }
+
+ _, err = ctx.Write(fileBytes)
+ return err
+ }
+
+ body, _ := ioutil.ReadAll(resp.Body)
+ for name, values := range resp.Header {
+ for _, value := range values {
+ ctx.Append(name, value)
+ }
+ }
+
+ return ctx.Send(body)
+}