// SPDX-License-Identifier: AGPL-3.0-or-later package directory import ( "context" "encoding/json" "path" "regexp" "strings" "unicode/utf8" "docwell/server/internal/access" "docwell/server/internal/apperr" "docwell/server/internal/convert" ) // ---- Import and export ----------------------------------------------------- // maxImportBytes caps one imported file. const maxImportBytes = 5 << 20 // SetPublicURL is docwell-server's external URL; exported files link // attachments there. func (s *Service) SetPublicURL(u string) { s.publicURL = strings.TrimRight(u, "/") } // Export is a page rendered as a file. type Export struct { Filename string ContentType string Body []byte } var unsafeFilename = regexp.MustCompile(`[^\p{L}\p{N} ._-]+`) func exportName(title, ext string) string { name := strings.TrimSpace(unsafeFilename.ReplaceAllString(title, "")) if name == "" { name = "Untitled" } if utf8.RuneCountInString(name) > 100 { name = string([]rune(name)[:100]) } return name + ext } // ExportPage renders a page as Markdown ("markdown") or HTML ("html") (viewer). func (s *Service) ExportPage(ctx context.Context, c access.Caller, id, format string) (Export, error) { p, _, err := s.pageFor(ctx, c, id) if err != nil { return Export{}, err } raw, err := s.docs.Content(ctx, p.ID) if err != nil { return Export{}, err } doc, err := convert.Parse(raw) if err != nil { return Export{}, err } opts := convert.Options{FileURL: func(path string) string { return s.publicURL + path }} switch format { case "markdown", "md", "": return Export{ Filename: exportName(p.Title, ".md"), ContentType: "text/markdown; charset=utf-8", Body: []byte(convert.MarkdownPage(p.Title, doc, opts)), }, nil case "html": return Export{ Filename: exportName(p.Title, ".html"), ContentType: "text/html; charset=utf-8", Body: []byte(convert.HTMLPage(p.Title, doc, opts)), }, nil } return Export{}, apperr.InvalidFields(map[string]string{"format": "Format must be markdown or html."}) } // ImportFile is one uploaded document. type ImportFile struct { Name string Data []byte } // ImportPages creates a page per file under parent ("" = top level) (editor). // Markdown (.md, .markdown, .txt) and HTML (.html, .htm) are understood; a // leading level-1 heading becomes the title, otherwise the file name does. func (s *Service) ImportPages(ctx context.Context, c access.Caller, spaceID, parent string, files []ImportFile) ([]Page, error) { a, err := s.spaceFor(ctx, c, spaceID) if err != nil { return nil, err } if err := access.EditPages(a.role); err != nil { return nil, err } if len(files) == 0 { return nil, apperr.InvalidFields(map[string]string{"file": "Choose at least one file."}) } type parsed struct { title string doc convert.Node } var docs []parsed for _, f := range files { if len(f.Data) > maxImportBytes { return nil, apperr.InvalidFields(map[string]string{"file": f.Name + " is larger than 5 MB."}) } if !utf8.Valid(f.Data) { return nil, apperr.InvalidFields(map[string]string{"file": f.Name + " is not UTF-8 text."}) } ext := strings.ToLower(path.Ext(f.Name)) var doc convert.Node switch ext { case ".md", ".markdown", ".txt": doc, err = convert.FromMarkdown(string(f.Data)) case ".html", ".htm": doc, err = convert.FromHTML(string(f.Data)) default: return nil, apperr.InvalidFields(map[string]string{"file": f.Name + ": only Markdown and HTML files can be imported."}) } if err != nil { return nil, apperr.InvalidFields(map[string]string{"file": f.Name + " could not be read."}) } title, body := convert.Title(doc) if title == "" { title = strings.TrimSuffix(path.Base(strings.ReplaceAll(f.Name, "\\", "/")), path.Ext(f.Name)) } if len(body.Content) == 0 { body.Content = []convert.Node{{Type: "paragraph"}} } docs = append(docs, parsed{title: strings.TrimSpace(title), doc: body}) } out := make([]Page, 0, len(docs)) for _, d := range docs { title := d.title if len(title) > 500 { title = title[:500] } page, err := s.CreatePage(ctx, c, a.space.ID, CreatePageInput{Parent: parent, Title: title}) if err != nil { return out, err } raw, err := json.Marshal(d.doc) if err != nil { return out, err } if err := s.docs.Replace(ctx, page.ID, c.ID, raw, "import"); err != nil { return out, err } out = append(out, page) } return out, nil }