Initial commit
Test / test (push) Successful in 7m5s
Release / gates (push) Successful in 7m28s
Release / build (amd64, freebsd) (push) Successful in 2m52s
Release / build (amd64, linux) (push) Successful in 2m46s
Release / build (arm64, freebsd) (push) Successful in 2m22s
Release / build (arm64, linux) (push) Successful in 2m38s
Release / build (loong64, linux) (push) Successful in 2m7s
Release / build (riscv64, linux) (push) Successful in 2m17s
Release / release (push) Successful in 1m0s
Test / test (push) Successful in 7m5s
Release / gates (push) Successful in 7m28s
Release / build (amd64, freebsd) (push) Successful in 2m52s
Release / build (amd64, linux) (push) Successful in 2m46s
Release / build (arm64, freebsd) (push) Successful in 2m22s
Release / build (arm64, linux) (push) Successful in 2m38s
Release / build (loong64, linux) (push) Successful in 2m7s
Release / build (riscv64, linux) (push) Successful in 2m17s
Release / release (push) Successful in 1m0s
Assisted-by: GLM 5.3
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
// Copyright (c) 2026 Petr Balvín <opensource@petrbalvin.org> (https://petrbalvin.org)
|
||||
// SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0
|
||||
|
||||
package admin
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"sourcedock.dev/petrbalvin/volumen/internal/i18n"
|
||||
"sourcedock.dev/petrbalvin/volumen/internal/payloads"
|
||||
"sourcedock.dev/petrbalvin/volumen/internal/post"
|
||||
)
|
||||
|
||||
func (a *Admin) handleImportForm(w http.ResponseWriter, r *http.Request) {
|
||||
data := a.pageData(r)
|
||||
data.Crumbs = []Crumb{
|
||||
{Label: "Posts", Href: "/admin/", UI: true},
|
||||
{Label: "Import", IsLast: true, UI: true},
|
||||
}
|
||||
a.renderPage(w, r, "import.html", data, http.StatusOK)
|
||||
}
|
||||
|
||||
func (a *Admin) handleImport(w http.ResponseWriter, r *http.Request) {
|
||||
if !a.requireCSRF(w, r) {
|
||||
return
|
||||
}
|
||||
data := a.pageData(r)
|
||||
data.Crumbs = []Crumb{
|
||||
{Label: "Posts", Href: "/admin/", UI: true},
|
||||
{Label: "Import", IsLast: true, UI: true},
|
||||
}
|
||||
fail := func(msg string) {
|
||||
data.Error = i18n.Admin.T(data.Lang, msg)
|
||||
a.renderPage(w, r, "import.html", data, http.StatusUnprocessableEntity)
|
||||
}
|
||||
|
||||
file, header, err := r.FormFile("file")
|
||||
if err != nil {
|
||||
fail("No file selected.")
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
if !strings.HasSuffix(strings.ToLower(header.Filename), ".md") {
|
||||
fail("Only .md files are accepted.")
|
||||
return
|
||||
}
|
||||
raw, err := readLimited(file, int64(a.deps.Config.Admin.MaxUploadBytes))
|
||||
if err != nil {
|
||||
fail("File is too large.")
|
||||
return
|
||||
}
|
||||
content := string(raw)
|
||||
p, err := post.Parse(content)
|
||||
if err != nil {
|
||||
fail(i18n.Admin.Tf(data.Lang, "The file could not be read as a post: %s", err.Error()))
|
||||
return
|
||||
}
|
||||
if p.Slug() == "" {
|
||||
base := strings.ToLower(filepath.Base(header.Filename))
|
||||
stem := strings.TrimSuffix(base, filepath.Ext(base))
|
||||
p.Metadata.Set("slug", strings.ReplaceAll(stem, " ", "-"))
|
||||
}
|
||||
if p.Lang() == "" {
|
||||
p.Metadata.Set("lang", a.deps.Config.Site.Language)
|
||||
}
|
||||
if err := payloads.CreationError(p, a.deps.Store, nil); err != nil {
|
||||
fail(err.Error())
|
||||
return
|
||||
}
|
||||
if _, err := a.deps.Store.Save(p); err != nil {
|
||||
fail("Import failed.")
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, "/admin/posts/"+p.Slug()+"/edit", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
// readLimited reads at most limit+1 bytes so callers can detect
|
||||
// oversize uploads.
|
||||
func readLimited(r io.Reader, limit int64) ([]byte, error) {
|
||||
raw, err := io.ReadAll(io.LimitReader(r, limit+1))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if int64(len(raw)) > limit {
|
||||
return nil, fmt.Errorf("payload too large")
|
||||
}
|
||||
return raw, nil
|
||||
}
|
||||
Reference in New Issue
Block a user