searcherside/core/services/tar_zst_index_archiver.go
Peter Kurfer 9ea9a8f658
Some checks failed
Go build / build (push) Failing after 1m58s
feat: continue basic setup
- setup ent scheme
- add command to create users
- document API
- add helpers to create migrations
- add command to run migrations
- add basic compose file
2024-06-19 21:19:37 +02:00

51 lines
1.2 KiB
Go

package services
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/klauspost/compress/zstd"
"code.icb4dc0.de/prskr/searcherside/core/ports"
"code.icb4dc0.de/prskr/searcherside/infrastructure/archive"
)
var _ ports.Archiver = (*TarZSTIndexArchiver)(nil)
type TarZSTIndexArchiver struct {
DataDirectory string
}
func (a TarZSTIndexArchiver) ArchiveIndex(req ports.ArchiveIndexRequest) (err error) {
archiveFile, err := os.Create(filepath.Join(a.DataDirectory, strings.Join([]string{req.Hash, req.Type.String(), "tar.zst"}, ".")))
if err != nil {
return fmt.Errorf("failed to create archive file: %w", err)
}
defer func() {
err = errors.Join(err, archiveFile.Close())
}()
encoder, err := zstd.NewWriter(archiveFile, zstd.WithEncoderLevel(zstd.SpeedBestCompression))
if err != nil {
return fmt.Errorf("failed to create zst encoder: %w", err)
}
defer func() {
err = errors.Join(err, encoder.Close())
}()
relativePath, err := filepath.Rel(a.DataDirectory, req.Path)
if err != nil {
return fmt.Errorf("failed to get relative path: %w", err)
}
return archive.TarDirectory(
os.DirFS(a.DataDirectory),
filepath.ToSlash(relativePath),
archiveFile,
)
}