Files
comicverse/editor/container.go

70 lines
1.5 KiB
Go

package editor
import (
"bytes"
_ "embed"
"encoding/xml"
"errors"
"fmt"
"log/slog"
"path"
"sync"
"time"
"code.capytal.cc/capytal/comicverse/editor/epub"
"code.capytal.cc/capytal/comicverse/editor/internals/shortid"
"code.capytal.cc/capytal/comicverse/editor/storage"
"code.capytal.cc/loreddev/x/tinyssert"
"github.com/google/uuid"
"golang.org/x/net/html"
)
type Container struct {
id uuid.UUID
pkg epub.Package
storage storage.Storage
log *slog.Logger
assert tinyssert.Assertions
flushed bool
}
func (p *Container) Flush() error {
p.assert.NotZero(p.pkg, "invalid ePUB: package must be set")
p.assert.NotZero(p.pkg.Metadata, "invalid ePUB: package must have metadata")
p.assert.NotZero(p.pkg.Metadata.ID, "invalid ePUB: ID must always be specified")
p.assert.NotZero(p.pkg.Metadata.Language, "invalid ePUB: Language must always be specified")
p.assert.NotZero(p.pkg.Metadata.Title, "invalid ePUB: Title must always be specified")
p.mu.Lock()
defer p.mu.Unlock()
p.log.Debug("Flushing state of publication")
if p.flushed {
p.log.Debug("Publication doesn't have unsaved changes, skipping flush")
return nil
}
defer p.log.Debug("Publication's state flushed")
b, err := xml.MarshalIndent(p.pkg, "", " ")
if err != nil {
return fmt.Errorf("editor.Publication: failed to marshal package: %w", err)
}
if _, err = p.storage.Write("content.opf", b); err != nil {
return fmt.Errorf("editor.Publication: failed to write content.opf: %w", err)
}
return nil
}