forked from jason/cpolis
Initial version of .docx upload support
This commit is contained in:
@ -7,6 +7,8 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Article struct {
|
||||
@ -14,6 +16,7 @@ type Article struct {
|
||||
Title string
|
||||
BannerLink string
|
||||
Summary string
|
||||
UUID uuid.UUID
|
||||
ID int64
|
||||
CreatorID int64
|
||||
IssueID int64
|
||||
@ -31,8 +34,8 @@ func (db *DB) AddArticle(a *Article) (int64, error) {
|
||||
selectQuery := "SELECT id FROM issues WHERE published = false"
|
||||
insertQuery := `
|
||||
INSERT INTO articles
|
||||
(title, banner_link, summary, published, rejected, creator_id, issue_id, edited_id, clicks, is_in_issue, auto_generated)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
(title, banner_link, summary, published, rejected, creator_id, issue_id, edited_id, clicks, is_in_issue, auto_generated, uuid)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`
|
||||
|
||||
for i := 0; i < TxMaxRetries; i++ {
|
||||
@ -49,7 +52,7 @@ func (db *DB) AddArticle(a *Article) (int64, error) {
|
||||
return 0, fmt.Errorf("error getting issue ID when adding article to DB: %v", err)
|
||||
}
|
||||
|
||||
result, err := tx.Exec(insertQuery, a.Title, a.BannerLink, a.Summary, a.Published, a.Rejected, a.CreatorID, id, a.EditedID, 0, a.IsInIssue, a.AutoGenerated)
|
||||
result, err := tx.Exec(insertQuery, a.Title, a.BannerLink, a.Summary, a.Published, a.Rejected, a.CreatorID, id, a.EditedID, 0, a.IsInIssue, a.AutoGenerated, a.UUID.String())
|
||||
if err != nil {
|
||||
if rollbackErr := tx.Rollback(); rollbackErr != nil {
|
||||
log.Fatalf("transaction error: %v, rollback error: %v", err, rollbackErr)
|
||||
@ -83,7 +86,7 @@ func (db *DB) AddArticle(a *Article) (int64, error) {
|
||||
|
||||
func (db *DB) GetArticle(id int64) (*Article, error) {
|
||||
query := `
|
||||
SELECT title, created, banner_link, summary, published, creator_id, issue_id, edited_id, clicks, is_in_issue, auto_generated
|
||||
SELECT title, created, banner_link, summary, published, creator_id, issue_id, edited_id, clicks, is_in_issue, auto_generated, uuid
|
||||
FROM articles
|
||||
WHERE id = ?
|
||||
`
|
||||
@ -91,9 +94,10 @@ func (db *DB) GetArticle(id int64) (*Article, error) {
|
||||
|
||||
article := new(Article)
|
||||
var created []byte
|
||||
var uuidString string
|
||||
var err error
|
||||
|
||||
if err := row.Scan(&article.Title, &created, &article.BannerLink, &article.Summary, &article.Published, &article.CreatorID, &article.IssueID, &article.EditedID, &article.Clicks, &article.IsInIssue, &article.AutoGenerated); err != nil {
|
||||
if err := row.Scan(&article.Title, &created, &article.BannerLink, &article.Summary, &article.Published, &article.CreatorID, &article.IssueID, &article.EditedID, &article.Clicks, &article.IsInIssue, &article.AutoGenerated, &uuidString); err != nil {
|
||||
return nil, fmt.Errorf("error scanning article row: %v", err)
|
||||
}
|
||||
|
||||
@ -103,12 +107,17 @@ func (db *DB) GetArticle(id int64) (*Article, error) {
|
||||
return nil, fmt.Errorf("error parsing created: %v", err)
|
||||
}
|
||||
|
||||
article.UUID, err = uuid.Parse(uuidString)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error parsing uuid: %v", err)
|
||||
}
|
||||
|
||||
return article, nil
|
||||
}
|
||||
|
||||
func (db *DB) GetCertainArticles(attribute string, value bool) ([]*Article, error) {
|
||||
query := fmt.Sprintf(`
|
||||
SELECT id, title, created, banner_link, summary, creator_id, issue_id, clicks, published, rejected, is_in_issue, auto_generated
|
||||
SELECT id, title, created, banner_link, summary, creator_id, issue_id, clicks, published, rejected, is_in_issue, auto_generated, uuid
|
||||
FROM articles
|
||||
WHERE %s = ?
|
||||
`, attribute)
|
||||
@ -121,8 +130,9 @@ func (db *DB) GetCertainArticles(attribute string, value bool) ([]*Article, erro
|
||||
for rows.Next() {
|
||||
article := new(Article)
|
||||
var created []byte
|
||||
var uuidString string
|
||||
|
||||
if err = rows.Scan(&article.ID, &article.Title, &created, &article.BannerLink, &article.Summary, &article.CreatorID, &article.IssueID, &article.Clicks, &article.Published, &article.Rejected, &article.IsInIssue, &article.AutoGenerated); err != nil {
|
||||
if err = rows.Scan(&article.ID, &article.Title, &created, &article.BannerLink, &article.Summary, &article.CreatorID, &article.IssueID, &article.Clicks, &article.Published, &article.Rejected, &article.IsInIssue, &article.AutoGenerated, &uuidString); err != nil {
|
||||
return nil, fmt.Errorf("error scanning article row: %v", err)
|
||||
}
|
||||
|
||||
@ -131,6 +141,11 @@ func (db *DB) GetCertainArticles(attribute string, value bool) ([]*Article, erro
|
||||
return nil, fmt.Errorf("error parsing created: %v", err)
|
||||
}
|
||||
|
||||
article.UUID, err = uuid.Parse(uuidString)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error parsing uuid: %v", err)
|
||||
}
|
||||
|
||||
articleList = append(articleList, article)
|
||||
}
|
||||
|
||||
@ -142,7 +157,7 @@ func (db *DB) GetCurrentIssueArticles() ([]*Article, error) {
|
||||
txOptions := &sql.TxOptions{Isolation: sql.LevelSerializable}
|
||||
issueQuery := "SELECT id FROM issues WHERE published = false"
|
||||
articlesQuery := `
|
||||
SELECT id, title, created, banner_link, summary, clicks, auto_generated
|
||||
SELECT id, title, created, banner_link, summary, clicks, auto_generated, uuid
|
||||
FROM articles
|
||||
WHERE issue_id = ? AND published = true AND is_in_issue = true
|
||||
`
|
||||
@ -174,8 +189,9 @@ func (db *DB) GetCurrentIssueArticles() ([]*Article, error) {
|
||||
for rows.Next() {
|
||||
article := new(Article)
|
||||
var created []byte
|
||||
var uuidString string
|
||||
|
||||
if err = rows.Scan(&article.ID, &article.Title, &created, &article.BannerLink, &article.Summary, &article.Clicks, &article.AutoGenerated); err != nil {
|
||||
if err = rows.Scan(&article.ID, &article.Title, &created, &article.BannerLink, &article.Summary, &article.Clicks, &article.AutoGenerated, &uuidString); err != nil {
|
||||
if rollbackErr := tx.Rollback(); rollbackErr != nil {
|
||||
log.Fatalf("transaction error: %v, rollback error: %v", err, rollbackErr)
|
||||
}
|
||||
@ -190,6 +206,14 @@ func (db *DB) GetCurrentIssueArticles() ([]*Article, error) {
|
||||
return nil, fmt.Errorf("error parsing created: %v", err)
|
||||
}
|
||||
|
||||
article.UUID, err = uuid.Parse(uuidString)
|
||||
if err != nil {
|
||||
if rollbackErr := tx.Rollback(); rollbackErr != nil {
|
||||
log.Fatalf("transaction error: %v, rollback error: %v", err, rollbackErr)
|
||||
}
|
||||
return nil, fmt.Errorf("error parsing uuid: %v", err)
|
||||
}
|
||||
|
||||
articleList = append(articleList, article)
|
||||
}
|
||||
|
||||
@ -284,11 +308,11 @@ func (db *DB) DeleteArticle(id int64) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func WriteArticleToFile(c *Config, articleID int64, content []byte) error {
|
||||
articleAbsName := fmt.Sprint(c.ArticleDir, "/", articleID, ".md")
|
||||
func WriteArticleToFile(c *Config, articleUUID uuid.UUID, content []byte) error {
|
||||
articleAbsName := fmt.Sprint(c.ArticleDir, "/", articleUUID, ".md")
|
||||
|
||||
if err := os.WriteFile(articleAbsName, content, 0644); err != nil {
|
||||
return fmt.Errorf("error writing article %v to file: %v", articleID, err)
|
||||
return fmt.Errorf("error writing article %v to file: %v", articleUUID, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -33,7 +33,7 @@ func GenerateAtomFeed(c *Config, db *DB) (*string, error) {
|
||||
entry := atom.NewEntry(articleTitle)
|
||||
entry.ID = atom.NewID(fmt.Sprint("urn:entry:", article.ID))
|
||||
entry.Published = atom.NewDate(article.Created)
|
||||
entry.Content = atom.NewContent(atom.OutOfLine, "text/hmtl", fmt.Sprint(c.Domain, "/article/serve/", article.ID))
|
||||
entry.Content = atom.NewContent(atom.OutOfLine, "text/html", fmt.Sprint(c.Domain, "/article/serve/", article.UUID))
|
||||
|
||||
if article.AutoGenerated {
|
||||
entry.Summary = atom.NewText("text", "automatically generated")
|
||||
|
59
cmd/backend/docx.go
Normal file
59
cmd/backend/docx.go
Normal file
@ -0,0 +1,59 @@
|
||||
package backend
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func ConvertToMarkdown(c *Config, filename string) ([]byte, error) {
|
||||
var stderr bytes.Buffer
|
||||
|
||||
articleID := uuid.New()
|
||||
articleFileName := fmt.Sprint("/tmp/", articleID, ".md")
|
||||
|
||||
tmpDir, err := os.MkdirTemp("/tmp", "cpolis_images")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error creating temporary directory: %v", err)
|
||||
}
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
cmd := exec.Command("pandoc", "-s", "-f", "docx", "-t", "commonmark_x", "-o", articleFileName, "--extract-media", tmpDir, filename) // TODO: Is writing to a file necessary?
|
||||
cmd.Stderr = &stderr
|
||||
if err = cmd.Run(); err != nil {
|
||||
return nil, fmt.Errorf("error converting docx to markdown: %v: %v", err, stderr.String())
|
||||
}
|
||||
defer os.Remove(articleFileName)
|
||||
|
||||
articleContent, err := os.ReadFile(articleFileName)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error reading markdown file: %v", err)
|
||||
}
|
||||
|
||||
imageNames, err := filepath.Glob(filepath.Join(tmpDir, "/media/*"))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error getting docx images from temporary directory: %v", err)
|
||||
}
|
||||
|
||||
for _, name := range imageNames {
|
||||
image, err := os.Open(name)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error opening image file %v: %v", name, err)
|
||||
}
|
||||
defer image.Close()
|
||||
|
||||
newImageName, err := SaveImage(image, c.MaxImgHeight, c.MaxImgWidth, c.PicsDir)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error saving image %v: %v", name, err)
|
||||
}
|
||||
|
||||
articleContent = regexp.MustCompile(name).ReplaceAll(articleContent, []byte(c.PicsDir+"/"+newImageName))
|
||||
}
|
||||
|
||||
return articleContent, nil
|
||||
}
|
Reference in New Issue
Block a user