diff --git a/api/article.go b/api/article.go index debc9a1..38827d1 100644 --- a/api/article.go +++ b/api/article.go @@ -10,6 +10,7 @@ import ( "github.com/gomarkdown/markdown" "go.uber.org/zap" "net/http" + "net/url" "path" "strings" "time" @@ -69,7 +70,7 @@ func articlePost(w http.ResponseWriter, r *http.Request) { Image: payload.Image, Created: time.Now().Unix(), Modified: time.Unix(0, 0).Unix(), - Link: "/" + path.Join(config.Prefix, "article", payload.Link), + Link: payload.Link, Markdown: string(rawMarkdown), Html: string(markdown.ToHTML(rawMarkdown, nil, nil)), } @@ -82,14 +83,16 @@ func articlePost(w http.ResponseWriter, r *http.Request) { }) } database.GetDB().Table("article_author").Create(&authors) - var tags []map[string]interface{} - for _, tag := range payload.Tags { - authors = append(authors, map[string]interface{}{ - "article_id": a.ID, - "tag": tag, - }) + if len(payload.Tags) > 0 { + var tags []map[string]interface{} + for _, tag := range payload.Tags { + authors = append(authors, map[string]interface{}{ + "article_id": a.ID, + "tag": tag, + }) + } + database.GetDB().Table("article_tag").Create(&tags) } - database.GetDB().Table("article_tag").Create(&tags) var articleSummary schema.ArticleSummary database.GetDB().Table("article").Find(&articleSummary, &a.ID) @@ -99,6 +102,7 @@ func articlePost(w http.ResponseWriter, r *http.Request) { } else { articleSummary.Tags = []string{} } + articleSummary.Link = "/" + path.Join(config.Prefix, "article", url.PathEscape(articleSummary.Link)) w.WriteHeader(http.StatusCreated) json.NewEncoder(w).Encode(articleSummary) @@ -175,7 +179,7 @@ func articlePatch(w http.ResponseWriter, r *http.Request) { updates["image"] = *payload.Image } if payload.Link != nil { - updates["link"] = path.Join(config.Prefix, "article", *payload.Link) + updates["link"] = *payload.Link } if payload.Content != nil { rawMarkdown, err := base64.StdEncoding.DecodeString(*payload.Content) @@ -212,6 +216,7 @@ func articlePatch(w http.ResponseWriter, r *http.Request) { } else { articleSummary.Tags = []string{} } + articleSummary.Link = "/" + path.Join(config.Prefix, "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 d5f8dbe..5d18627 100644 --- a/api/article_test.go +++ b/api/article_test.go @@ -20,7 +20,7 @@ func TestArticlePost(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": "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
", }, @@ -93,7 +93,7 @@ func TestArticlePatch(t *testing.T) { "title": "test article", "summary": "example summary", "created": time.Now().Unix(), - "link": "/article/test-article", + "link": "test-article", "markdown": "Just a simple test article", "html": "
Just a simple test article
", }, @@ -190,14 +190,14 @@ func TestArticleDelete(t *testing.T) { "image": "https://upload.wikimedia.org/wikipedia/commons/0/05/Go_Logo_Blue.svg", "created": time.Now().Unix(), "modified": time.Now().Unix(), - "link": "/article/test", + "link": "test", "markdown": "# Title", "html": "
owo
", }, diff --git a/api/assets.go b/api/assets.go index ee15c06..e0f0cae 100644 --- a/api/assets.go +++ b/api/assets.go @@ -1,6 +1,7 @@ package api import ( + "TheAdversary/config" "TheAdversary/database" "TheAdversary/schema" "encoding/base64" @@ -56,6 +57,10 @@ func assetsGet(w http.ResponseWriter, r *http.Request) { var assets []schema.Asset request.Find(&assets) + for _, asset := range assets { + asset.Link = "/" + path.Join(config.Prefix, "assets", asset.Link) + } + w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(&assets) } @@ -84,7 +89,7 @@ func assetsPost(w http.ResponseWriter, r *http.Request) { Name string Data []byte Link string - }{Name: payload.Name, Data: rawData, Link: "/" + path.Join("assets", url.PathEscape(payload.Name))} + }{Name: payload.Name, Data: rawData, Link: url.PathEscape(payload.Name)} if database.GetDB().Table("assets").Clauses(clause.OnConflict{ Columns: []clause.Column{{Name: "name"}}, @@ -96,7 +101,7 @@ func assetsPost(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(schema.Asset{ Id: tmpDatabaseSchema.Id, Name: tmpDatabaseSchema.Name, - Link: tmpDatabaseSchema.Link, + Link: "/" + path.Join(config.Prefix, "assets", tmpDatabaseSchema.Link), }) } } diff --git a/api/recent.go b/api/recent.go index 569dc12..ae0124c 100644 --- a/api/recent.go +++ b/api/recent.go @@ -1,10 +1,12 @@ package api import ( + "TheAdversary/config" "TheAdversary/database" "TheAdversary/schema" "encoding/json" "net/http" + "path" "strconv" ) @@ -33,6 +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.Prefix, "article", summary.Link) articleSummaries[i] = summary } diff --git a/api/recent_test.go b/api/recent_test.go index cec6b08..26f22f8 100644 --- a/api/recent_test.go +++ b/api/recent_test.go @@ -5,6 +5,7 @@ import ( "TheAdversary/schema" "net/http" "net/http/httptest" + "path" "testing" "time" ) @@ -49,7 +50,7 @@ func TestRecent(t *testing.T) { "summary": articles[0].Summary, "created": articles[0].Created, "modified": articles[0].Modified, - "link": articles[0].Link, + "link": path.Base(articles[0].Link), "markdown": "# Title", "html": "
This is the most recent article
", }, diff --git a/api/search.go b/api/search.go index 7d1e21f..fb5ef9d 100644 --- a/api/search.go +++ b/api/search.go @@ -1,10 +1,12 @@ package api import ( + "TheAdversary/config" "TheAdversary/database" "TheAdversary/schema" "encoding/json" "net/http" + "path" "strconv" "strings" ) @@ -70,6 +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.Prefix, "article", summary.Link) articleSummaries[i] = summary } diff --git a/api/search_test.go b/api/search_test.go index f343c14..fee2559 100644 --- a/api/search_test.go +++ b/api/search_test.go @@ -20,7 +20,7 @@ func TestSearch(t *testing.T) { { "title": "First article", "created": now.Unix(), - "link": "/article/first-article", + "link": "first-article", "markdown": "This is my first article", "html": "This is my first article
", }, @@ -30,7 +30,7 @@ func TestSearch(t *testing.T) { "image": "https://upload.wikimedia.org/wikipedia/commons/0/05/Go_Logo_Blue.svg", "created": now.Unix(), "modified": now.Add(24 * time.Hour).Unix(), - "link": "/article/test", + "link": "test", "markdown": "# Title", "html": "owo
", },