forked from jason/cpolis
Allow articles to be edited
This commit is contained in:
@ -22,6 +22,38 @@ const (
|
||||
PreviewMode
|
||||
)
|
||||
|
||||
type EditorHTMLData struct {
|
||||
Selected map[int64]bool
|
||||
Content string
|
||||
Action string
|
||||
ActionTitle string
|
||||
ActionButton string
|
||||
HTMLContent template.HTML
|
||||
Article *b.Article
|
||||
Tags []*b.Tag
|
||||
}
|
||||
|
||||
func copyFile(src, dst string) error {
|
||||
srcFile, err := os.Open(src)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error opening source file: %v", err)
|
||||
}
|
||||
defer srcFile.Close()
|
||||
|
||||
dstFile, err := os.Create(dst)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error opening destination file: %v", err)
|
||||
}
|
||||
defer dstFile.Close()
|
||||
|
||||
_, err = io.Copy(dstFile, srcFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error copying file: %v", err)
|
||||
}
|
||||
|
||||
return dstFile.Sync()
|
||||
}
|
||||
|
||||
func WriteArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
session, err := getSession(w, r, c, s)
|
||||
@ -29,22 +61,14 @@ func WriteArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
type htmlData struct {
|
||||
Title string
|
||||
Description string
|
||||
Content string
|
||||
HTMLContent template.HTML
|
||||
Tags []*b.Tag
|
||||
Mode int
|
||||
}
|
||||
var data htmlData
|
||||
data := new(EditorHTMLData)
|
||||
|
||||
if session.Values["article"] == nil {
|
||||
data = htmlData{}
|
||||
data = &EditorHTMLData{Article: new(b.Article)}
|
||||
} else {
|
||||
data = session.Values["article"].(htmlData)
|
||||
data = session.Values["article"].(*EditorHTMLData)
|
||||
}
|
||||
data.Mode = EditMode
|
||||
// data.Mode = EditMode
|
||||
|
||||
data.Tags, err = db.GetTagList()
|
||||
if err != nil {
|
||||
@ -53,6 +77,8 @@ func WriteArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
data.Action = "submit"
|
||||
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/editor.html")
|
||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", data)
|
||||
}
|
||||
@ -185,22 +211,41 @@ func ResubmitArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func ShowUnpublishedArticles(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||
func ShowUnpublishedUnrejectedAndPublishedRejectedArticles(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if _, err := getSession(w, r, c, s); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
unpublishedArticles, err := db.GetCertainArticles(false, false)
|
||||
rejectedArticles, err := db.GetCertainArticles("rejected", true)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
articles := make([]*b.Article, 0)
|
||||
for _, article := range rejectedArticles {
|
||||
if article.Published {
|
||||
articles = append(articles, article)
|
||||
}
|
||||
}
|
||||
|
||||
unpublishedArticles, err := db.GetCertainArticles("published", false)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
for _, article := range unpublishedArticles {
|
||||
if !article.Rejected {
|
||||
articles = append(articles, article)
|
||||
}
|
||||
}
|
||||
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/unpublished-articles.html")
|
||||
tmpl = template.Must(tmpl, err)
|
||||
tmpl.ExecuteTemplate(w, "page-content", unpublishedArticles)
|
||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", articles)
|
||||
}
|
||||
}
|
||||
|
||||
@ -216,7 +261,7 @@ func ShowRejectedArticles(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerF
|
||||
RejectedArticles []*b.Article
|
||||
})
|
||||
|
||||
data.RejectedArticles, err = db.GetCertainArticles(false, true)
|
||||
data.RejectedArticles, err = db.GetCertainArticles("rejected", true)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
@ -236,74 +281,12 @@ func ShowRejectedArticles(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerF
|
||||
}
|
||||
}
|
||||
|
||||
func ReviewUnpublishedArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if _, err := getSession(w, r, c, s); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
data := new(struct {
|
||||
Article *b.Article
|
||||
Content template.HTML
|
||||
Tags []*b.Tag
|
||||
})
|
||||
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
data.Article, err = db.GetArticle(id)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
articleAbsName := fmt.Sprint(c.ArticleDir, "/", data.Article.ID, ".md")
|
||||
contentBytes, err := os.ReadFile(articleAbsName)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
content, err := b.ConvertToHTML(string(contentBytes))
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
data.Content = template.HTML(content)
|
||||
|
||||
data.Tags, err = db.GetArticleTags(data.Article.ID)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/to-be-published.html")
|
||||
tmpl = template.Must(tmpl, err)
|
||||
tmpl.ExecuteTemplate(w, "page-content", data)
|
||||
}
|
||||
}
|
||||
|
||||
func ReviewRejectedArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if _, err := getSession(w, r, c, s); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
data := new(struct {
|
||||
Selected map[int64]bool
|
||||
Article *b.Article
|
||||
Content string
|
||||
Tags []*b.Tag
|
||||
})
|
||||
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
@ -311,6 +294,7 @@ func ReviewRejectedArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.Handler
|
||||
return
|
||||
}
|
||||
|
||||
data := new(EditorHTMLData)
|
||||
data.Article, err = db.GetArticle(id)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
@ -319,14 +303,13 @@ func ReviewRejectedArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.Handler
|
||||
}
|
||||
|
||||
articleAbsName := fmt.Sprint(c.ArticleDir, "/", data.Article.ID, ".md")
|
||||
contentBytes, err := os.ReadFile(articleAbsName)
|
||||
content, err := os.ReadFile(articleAbsName)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
data.Content = string(contentBytes)
|
||||
data.Content = string(content)
|
||||
|
||||
data.Tags, err = db.GetTagList()
|
||||
if err != nil {
|
||||
@ -346,7 +329,9 @@ func ReviewRejectedArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.Handler
|
||||
data.Selected[tag.ID] = true
|
||||
}
|
||||
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/rework-article.html")
|
||||
data.Action = fmt.Sprint("resubmit/", data.Article.ID)
|
||||
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/editor.html")
|
||||
tmpl = template.Must(tmpl, err)
|
||||
tmpl.ExecuteTemplate(w, "page-content", data)
|
||||
}
|
||||
@ -389,6 +374,36 @@ func PublishArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
if article.EditedID != 0 {
|
||||
oldArticle, err := db.GetArticle(article.EditedID)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if err = db.DeleteArticle(oldArticle.ID); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if err = os.Remove(fmt.Sprint(c.ArticleDir, "/", oldArticle.ID, ".md")); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if err = db.UpdateAttributes(
|
||||
&b.Attribute{Table: "articles", ID: id, AttName: "link", Value: fmt.Sprint(c.Domain, "/article/serve/", article.ID)},
|
||||
&b.Attribute{Table: "articles", ID: id, AttName: "edited_id", Value: 0},
|
||||
); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
feed, err := b.GenerateRSS(c, db)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
@ -421,9 +436,7 @@ func RejectArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
if err = db.UpdateAttributes(
|
||||
&b.Attribute{Table: "articles", ID: id, AttName: "rejected", Value: true},
|
||||
); err != nil {
|
||||
if err = db.UpdateAttributes(&b.Attribute{Table: "articles", ID: id, AttName: "rejected", Value: true}); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
@ -497,69 +510,75 @@ func UploadArticleImage(c *b.Config, s *b.CookieStore) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func ShowPublishedArticles(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||
func ShowPublishedArticles(c *b.Config, db *b.DB, s *b.CookieStore, action string) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if _, err := getSession(w, r, c, s); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
publishedArticles, err := db.GetCertainArticles(true, false)
|
||||
data := new(struct {
|
||||
Action string
|
||||
Articles []*b.Article
|
||||
})
|
||||
data.Action = action
|
||||
|
||||
publishedArticles, err := db.GetCertainArticles("published", true)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
filteredArticles := make([]*b.Article, 0)
|
||||
for _, article := range publishedArticles {
|
||||
if !article.AutoGenerated {
|
||||
filteredArticles = append(filteredArticles, article)
|
||||
data.Articles = append(data.Articles, article)
|
||||
}
|
||||
}
|
||||
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/published-articles.html")
|
||||
tmpl = template.Must(tmpl, err)
|
||||
tmpl.ExecuteTemplate(w, "page-content", filteredArticles)
|
||||
tmpl.ExecuteTemplate(w, "page-content", data)
|
||||
}
|
||||
}
|
||||
|
||||
func ReviewArticleForDeletion(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||
func ReviewArticle(c *b.Config, db *b.DB, s *b.CookieStore, action, title, button string) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if _, err := getSession(w, r, c, s); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var err error
|
||||
data := new(struct {
|
||||
Title string
|
||||
Description string
|
||||
Content template.HTML
|
||||
Tags []*b.Tag
|
||||
ID int64
|
||||
})
|
||||
|
||||
data.ID, err = strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
article, err := db.GetArticle(data.ID)
|
||||
article, err := db.GetArticle(id)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
data.Title, err = b.ConvertToPlain(article.Title)
|
||||
data := &EditorHTMLData{
|
||||
Article: &b.Article{
|
||||
ID: id,
|
||||
IsInIssue: article.IsInIssue,
|
||||
},
|
||||
Action: action,
|
||||
ActionTitle: title,
|
||||
ActionButton: button,
|
||||
}
|
||||
|
||||
data.Article.Title, err = b.ConvertToPlain(article.Title)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
data.Description, err = b.ConvertToPlain(article.Description)
|
||||
data.Article.Description, err = b.ConvertToPlain(article.Description)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
@ -567,31 +586,29 @@ func ReviewArticleForDeletion(c *b.Config, db *b.DB, s *b.CookieStore) http.Hand
|
||||
}
|
||||
|
||||
articleAbsName := fmt.Sprint(c.ArticleDir, "/", article.ID, ".md")
|
||||
contentBytes, err := os.ReadFile(articleAbsName)
|
||||
content, err := os.ReadFile(articleAbsName)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
data.Content, err = b.ConvertToHTML(string(content))
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
data.HTMLContent = template.HTML(data.Content)
|
||||
|
||||
data.Tags, err = db.GetArticleTags(id)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
content, err := b.ConvertToHTML(string(contentBytes))
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
data.Content = template.HTML(content)
|
||||
|
||||
data.Tags, err = db.GetArticleTags(data.ID)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/to-be-deleted.html")
|
||||
tmpl = template.Must(tmpl, err)
|
||||
tmpl.ExecuteTemplate(w, "page-content", data)
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/review-article.html")
|
||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", data)
|
||||
}
|
||||
}
|
||||
|
||||
@ -638,3 +655,201 @@ func DeleteArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"].(int))
|
||||
}
|
||||
}
|
||||
|
||||
func AllowEditArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
session, err := getSession(w, r, c, s)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
oldID, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
fmt.Println(oldID)
|
||||
|
||||
oldArticle, err := db.GetArticle(oldID)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
newArticle := oldArticle
|
||||
newArticle.Published = false
|
||||
newArticle.Rejected = true
|
||||
newArticle.EditedID = oldArticle.ID
|
||||
|
||||
newID, err := db.AddArticle(newArticle)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if err = db.UpdateAttributes(&b.Attribute{Table: "articles", ID: oldID, AttName: "edited_id", Value: newID}); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if err = copyFile(fmt.Sprint(c.ArticleDir, "/", oldID, ".md"), fmt.Sprint(c.ArticleDir, "/", newID, ".md")); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
||||
tmpl = template.Must(tmpl, err)
|
||||
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"].(int))
|
||||
}
|
||||
}
|
||||
|
||||
func EditArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if _, err := getSession(w, r, c, s); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
data := new(EditorHTMLData)
|
||||
|
||||
data.Article, err = db.GetArticle(id)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
content, err := os.ReadFile(fmt.Sprint(c.ArticleDir, "/", data.Article.ID, ".md"))
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
data.Content = string(content)
|
||||
|
||||
data.Tags, err = db.GetArticleTags(data.Article.ID)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
selectedTags, err := db.GetArticleTags(id)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
data.Selected = make(map[int64]bool)
|
||||
for _, tag := range selectedTags {
|
||||
data.Selected[tag.ID] = true
|
||||
}
|
||||
|
||||
data.Action = fmt.Sprint("save/", data.Article.ID)
|
||||
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/editor.html")
|
||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", data)
|
||||
}
|
||||
}
|
||||
|
||||
func SaveArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
session, err := getSession(w, r, c, s)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
session.Values["article"] = nil
|
||||
if err = session.Save(r, w); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
article := &b.Article{
|
||||
Title: r.PostFormValue("article-title"),
|
||||
Description: r.PostFormValue("article-description"),
|
||||
Published: false,
|
||||
Rejected: false,
|
||||
AuthorID: session.Values["id"].(int64),
|
||||
IsInIssue: r.PostFormValue("issue") == "on",
|
||||
AutoGenerated: false,
|
||||
}
|
||||
|
||||
oldID, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(oldID)
|
||||
if oldID != 0 {
|
||||
if err = db.DeleteArticle(oldID); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if err = os.Remove(fmt.Sprint(c.ArticleDir, "/", oldID, ".md")); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
article.ID, err = db.AddArticle(article)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
articleAbsName := fmt.Sprint(c.ArticleDir, "/", article.ID, ".md")
|
||||
if err = os.WriteFile(articleAbsName, []byte(r.PostFormValue("article-content")), 0644); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
article.Link = fmt.Sprint(c.Domain, "/article/serve/", article.ID)
|
||||
if err = db.UpdateAttributes(&b.Attribute{Table: "articles", ID: article.ID, AttName: "link", Value: article.Link}); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
r.ParseForm()
|
||||
tags := make([]int64, 0)
|
||||
for _, tag := range r.Form["tags"] {
|
||||
tagID, err := strconv.ParseInt(tag, 10, 64)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
tags = append(tags, tagID)
|
||||
}
|
||||
if err = db.WriteArticleTags(article.ID, tags); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
||||
tmpl = template.Must(tmpl, err)
|
||||
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"])
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user