From e582a75d9ece40a1a748ca07d000cf61e1e10c50 Mon Sep 17 00:00:00 2001 From: bytedream Date: Sun, 6 Feb 2022 18:27:30 +0100 Subject: [PATCH] Fixing more stuff --- api/article.go | 4 ++-- api/article_test.go | 4 ++-- api/assets.go | 10 ++-------- api/assets_test.go | 16 ++++++++-------- api/recent.go | 2 +- api/recent_test.go | 4 ++-- api/search.go | 2 +- api/search_test.go | 14 +++++++------- api/tags_test.go | 2 +- database.sqlite3 | Bin 65536 -> 65536 bytes main.go | 11 ++++++----- server/article.go | 2 +- server/assets.go | 23 +++++++++++++++++++++++ 13 files changed, 56 insertions(+), 38 deletions(-) create mode 100644 server/assets.go diff --git a/api/article.go b/api/article.go index 065b040..2dca9c6 100644 --- a/api/article.go +++ b/api/article.go @@ -158,7 +158,7 @@ func articlePost(w http.ResponseWriter, r *http.Request) { } else { articleSummary.Tags = []string{} } - articleSummary.Link = path.Join("/", config.SubPath, "article", url.PathEscape(articleSummary.Link)) + articleSummary.Link = path.Join(config.SubPath, "article", url.PathEscape(articleSummary.Link)) w.WriteHeader(http.StatusCreated) json.NewEncoder(w).Encode(articleSummary) @@ -272,7 +272,7 @@ func articlePatch(w http.ResponseWriter, r *http.Request) { } else { articleSummary.Tags = []string{} } - articleSummary.Link = path.Join("/", config.SubPath, "article", url.PathEscape(articleSummary.Link)) + articleSummary.Link = path.Join(config.SubPath, "article", url.PathEscape(articleSummary.Link)) w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(articleSummary) diff --git a/api/article_test.go b/api/article_test.go index b81050b..2de9e2b 100644 --- a/api/article_test.go +++ b/api/article_test.go @@ -134,7 +134,7 @@ func TestArticlePost(t *testing.T) { }, }, Tags: []string{}, - Link: "/article/testooo", + Link: "article/testooo", }, Code: http.StatusCreated, AfterExec: func(information *testInformation) { @@ -229,7 +229,7 @@ func TestArticlePatch(t *testing.T) { }, Created: created, Tags: []string{}, - Link: "/article/test-article", + Link: "article/test-article", }, AfterExec: func(information *testInformation) { var modified int64 diff --git a/api/assets.go b/api/assets.go index 1d98200..0ad8f3c 100644 --- a/api/assets.go +++ b/api/assets.go @@ -19,12 +19,6 @@ var assetsPayload struct { } func Assets(w http.ResponseWriter, r *http.Request) { - _, ok := authorizedSession(r) - if !ok { - w.WriteHeader(http.StatusUnauthorized) - return - } - switch r.Method { case http.MethodGet: assetsGet(w, r) @@ -63,7 +57,7 @@ func assetsGet(w http.ResponseWriter, r *http.Request) { request.Find(&assets) for _, asset := range assets { - asset.Link = path.Join("/", config.SubPath, "assets", asset.Link) + asset.Link = path.Join(config.SubPath, "assets", asset.Link) } w.WriteHeader(http.StatusOK) @@ -116,7 +110,7 @@ func assetsPost(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(schema.Asset{ Id: tmpDatabaseSchema.Id, Name: tmpDatabaseSchema.Name, - Link: path.Join("/", config.SubPath, "assets", tmpDatabaseSchema.Link), + Link: path.Join(config.SubPath, "assets", tmpDatabaseSchema.Link), }) } } diff --git a/api/assets_test.go b/api/assets_test.go index 6dcb9ea..814c106 100644 --- a/api/assets_test.go +++ b/api/assets_test.go @@ -20,12 +20,12 @@ func TestAssetsGet(t *testing.T) { { "name": "linux", "data": "this should be an image of tux :3", - "link": "/assets/linux", + "link": "assets/linux", }, { "name": "get test", "data": "", - "link": "/assets/get-test", + "link": "assets/get-test", }, }) @@ -47,7 +47,7 @@ func TestAssetsGet(t *testing.T) { { Id: 1, Name: "linux", - Link: "/assets/linux", + Link: "assets/linux", }, }, Code: http.StatusOK, @@ -64,7 +64,7 @@ func TestAssetsGet(t *testing.T) { { Id: 1, Name: "linux", - Link: "/assets/linux", + Link: "assets/linux", }, }, Code: http.StatusOK, @@ -78,12 +78,12 @@ func TestAssetsGet(t *testing.T) { { Id: 1, Name: "linux", - Link: "/assets/linux", + Link: "assets/linux", }, { Id: 2, Name: "get test", - Link: "/assets/get-test", + Link: "assets/get-test", }, }, Code: http.StatusOK, @@ -124,7 +124,7 @@ func TestAssetsPost(t *testing.T) { ResultBody: schema.Asset{ Id: 1, Name: "test", - Link: "/assets/test", + Link: "assets/test", }, Code: http.StatusCreated, }, @@ -150,7 +150,7 @@ func TestAssetsDelete(t *testing.T) { database.GetDB().Table("assets").Create(map[string]interface{}{ "name": "example", "data": "just a normal string", - "link": "/assets/example", + "link": "assets/example", }) server := httptest.NewServer(http.HandlerFunc(assetsDelete)) diff --git a/api/recent.go b/api/recent.go index 4ab71c2..968e234 100644 --- a/api/recent.go +++ b/api/recent.go @@ -35,7 +35,7 @@ func Recent(w http.ResponseWriter, r *http.Request) { database.GetDB().Table("author").Where("id IN (?)", database.GetDB().Table("article_author").Select("author_id").Where("article_id = ?", summary.Id)).Find(&summary.Authors) summary.Tags = []string{} database.GetDB().Table("article_tag").Select("tag").Where("article_id = ?", summary.Id).Find(&summary.Tags) - summary.Link = path.Join("/", config.SubPath, "article", summary.Link) + summary.Link = path.Join(config.SubPath, "article", summary.Link) articleSummaries[i] = summary } diff --git a/api/recent_test.go b/api/recent_test.go index 26f22f8..d1aae0e 100644 --- a/api/recent_test.go +++ b/api/recent_test.go @@ -31,7 +31,7 @@ func TestRecent(t *testing.T) { Tags: []string{}, Created: time.Unix(0, 0).Unix(), Modified: time.Now().Unix(), - Link: "/article/test", + Link: "article/test", }, { Id: 2, @@ -40,7 +40,7 @@ func TestRecent(t *testing.T) { Authors: authors, Tags: []string{}, Created: time.Now().Unix(), - Link: "/article/recent", + Link: "article/recent", }, } diff --git a/api/search.go b/api/search.go index 5f96e7c..0026f18 100644 --- a/api/search.go +++ b/api/search.go @@ -72,7 +72,7 @@ func Search(w http.ResponseWriter, r *http.Request) { database.GetDB().Table("author").Where("id IN (?)", database.GetDB().Table("article_author").Select("author_id").Where("article_id = ?", summary.Id)).Find(&summary.Authors) summary.Tags = []string{} database.GetDB().Table("article_tag").Select("tag").Where("article_id = ?", summary.Id).Find(&summary.Tags) - summary.Link = path.Join("/", config.SubPath, "article", summary.Link) + summary.Link = path.Join(config.SubPath, "article", summary.Link) articleSummaries[i] = summary } diff --git a/api/search_test.go b/api/search_test.go index fee2559..414c99d 100644 --- a/api/search_test.go +++ b/api/search_test.go @@ -95,7 +95,7 @@ func TestSearch(t *testing.T) { Tags: []string{ "example", }, - Link: "/article/first-article", + Link: "article/first-article", }, }, Code: http.StatusOK, @@ -121,7 +121,7 @@ func TestSearch(t *testing.T) { Created: now.Unix(), Modified: now.Add(24 * time.Hour).Unix(), Tags: []string{}, - Link: "/article/test", + Link: "article/test", }, { Id: 3, @@ -135,7 +135,7 @@ func TestSearch(t *testing.T) { Created: now.Unix(), Modified: now.Add(12 * time.Hour).Unix(), Tags: []string{}, - Link: "/article/owo", + Link: "article/owo", }, }, Code: http.StatusOK, @@ -158,7 +158,7 @@ func TestSearch(t *testing.T) { Created: now.Unix(), Modified: now.Add(12 * time.Hour).Unix(), Tags: []string{}, - Link: "/article/owo", + Link: "article/owo", }, }, Code: http.StatusOK, @@ -182,7 +182,7 @@ func TestSearch(t *testing.T) { Tags: []string{ "example", }, - Link: "/article/first-article", + Link: "article/first-article", }, }, Code: http.StatusOK, @@ -206,7 +206,7 @@ func TestSearch(t *testing.T) { Tags: []string{ "example", }, - Link: "/article/first-article", + Link: "article/first-article", }, { Id: 2, @@ -222,7 +222,7 @@ func TestSearch(t *testing.T) { Created: now.Unix(), Modified: now.Add(24 * time.Hour).Unix(), Tags: []string{}, - Link: "/article/test", + Link: "article/test", }, }, Code: http.StatusOK, diff --git a/api/tags_test.go b/api/tags_test.go index 59de65e..0e0385d 100644 --- a/api/tags_test.go +++ b/api/tags_test.go @@ -23,7 +23,7 @@ func TestTags(t *testing.T) { "title": "Upload test", "summary": "An example article to test the upload api endpoint", "created": time.Now().Unix(), - "link": "/article/upload-test", + "link": "article/upload-test", "markdown": "Oh god i have to test all this, what am i doing with my life", "html": "

Oh god i have to test all this, what am i doing with my life

", }, diff --git a/database.sqlite3 b/database.sqlite3 index e1312f02def07d15cb2001c4ea7839da3e5d5597..0231d247862a7b7548d42dea95ecef2e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e1ldIg0d8C1;BMd~ z04!`e4f@^S=!bf$aAvwqLpo0v=!Fqo-^J4fo-Xiofu{=%A?IZe<8*s+?Y5yg4iRN)lv*H*y zIW^1{POELey`rmTO+BvH&*n9BOGz1W4idV zA~%&Gt0Ac(P8S$G$x?$ zT|Pc12P?cjF1Kwy-$Bb?!ed=MBbTZ@b*5_9#9TOr;ch-%Lw?I1%8o-?Zf;j2A8obZ z=tl_1;cu`E0|r80ELAiZ4};QeYS^b87rW^AgJZz;7qaDn8~5#Nr$KE(o}zvH_BFm` z!naJqongLZ@>83)VAt4&PZN@BK?z&-8BGztg*1`mJzTv6~J2!WUq-{vBGz zb;YKkBiySm$mQKTk!4kv5SKi`qEFtRDB$Ap^V*lkmHXZkdP+NZP#YOJcx3hxzAO13M$gS?oD%bmf4=d*^=0;a z<6mw8c5j~XjeoxJ&o} zl`-?*W8nY7|C0Y6|C!B#0{i)`WjPXyi&IOA<5SY(OL7Z1xtS%QBAiUjlAMX9B^mie zT#U@poQXvxnaMe+TtH)xjNkmjo=0FY%LakXEF1ptZz^CB09yW=f&Vwq^7s7gKr0!U zIXM??5Mbom%(#IWp%lz#5n$y%&A@*e=$3>0lTW|5F=YlC4{|9V5QD7Z24XfK=7(Yq SAPvGGK1dCS%?8wdgdYHr*+ZEC diff --git a/main.go b/main.go index c73c256..9b94c0f 100644 --- a/main.go +++ b/main.go @@ -62,18 +62,19 @@ func setupApi(r *mux.Router) { func setupFrontend(r *mux.Router) { r.HandleFunc("/article/{article}", server.Article).Methods(http.MethodGet) + r.HandleFunc("/assets/{asset}", server.Assets).Methods(http.MethodGet) - r.PathPrefix("/css/").HandlerFunc(server.ServePath) - r.PathPrefix("/img/").HandlerFunc(server.ServePath) - r.PathPrefix("/js/").HandlerFunc(server.ServePath) - r.PathPrefix("/html/").HandlerFunc(server.ServePath) + r.PathPrefix("/css/").HandlerFunc(server.ServePath).Methods(http.MethodGet) + r.PathPrefix("/img/").HandlerFunc(server.ServePath).Methods(http.MethodGet) + r.PathPrefix("/js/").HandlerFunc(server.ServePath).Methods(http.MethodGet) + r.PathPrefix("/html/").HandlerFunc(server.ServePath).Methods(http.MethodGet) landingpage := template.Must(template.ParseFiles(filepath.Join(config.FrontendDir, "html", "landingpage.gohtml"))) r.Path("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { landingpage.Execute(w, struct { BasePath string }{BasePath: config.Address + strings.TrimSuffix(path.Join("/", config.SubPath), "/") + "/"}) - }) + }).Methods(http.MethodGet) r.NotFoundHandler = http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { server.Error404(w, r) diff --git a/server/article.go b/server/article.go index 6f8063c..8606999 100644 --- a/server/article.go +++ b/server/article.go @@ -36,7 +36,7 @@ func Article(w http.ResponseWriter, r *http.Request) { Error500(w, r) } else { var authors, tags []string - database.GetDB().Table("author").Select("id").Where("id IN (?)", database.GetDB().Table("article_author").Select("author_id").Where("article_id = ?", article.Id)).Find(&authors) + database.GetDB().Table("author").Select("name").Where("id IN (?)", database.GetDB().Table("article_author").Select("author_id").Where("article_id = ?", article.Id)).Find(&authors) database.GetDB().Table("article_tag").Where("article_id = ?", article.Id).Find(&tags) ta := tmplArticle{ diff --git a/server/assets.go b/server/assets.go new file mode 100644 index 0000000..aa00962 --- /dev/null +++ b/server/assets.go @@ -0,0 +1,23 @@ +package server + +import ( + "TheAdversary/database" + "github.com/gorilla/mux" + "mime" + "net/http" + "path" +) + +func Assets(w http.ResponseWriter, r *http.Request) { + assetName := mux.Vars(r)["asset"] + var buf []interface{} + database.GetDB().Table("assets").Select("data").Find(&buf, "link = ?", assetName) + + if buf == nil { + Error404(w, r) + } else { + data := buf[0].([]byte) + w.Header().Set("Content-Type", mime.TypeByExtension(path.Ext(assetName))) + w.Write(data) + } +}