Work on header parsing & saving other files

This commit is contained in:
2024-10-08 18:16:47 +03:00
parent 7e849feffe
commit 91f8e69fdf
7 changed files with 138 additions and 80 deletions

33
fs.go
View File

@@ -3,8 +3,6 @@ package main
import (
"fmt"
"net/url"
"os"
"path"
"path/filepath"
"strings"
)
@@ -60,34 +58,3 @@ func calcFilePath(rootPath, urlPath string) (string, error) {
return finalPath, nil
}
func SaveResult(rootPath string, s *Snapshot) {
parentPath := path.Join(rootPath, s.Url.Hostname)
urlPath := s.Url.Path
// If path is empty, add `index.gmi` as the file to save
if urlPath == "" || urlPath == "." {
urlPath = fmt.Sprintf("index.gmi")
}
// If path ends with '/' then add index.gmi for the
// directory to be created.
if strings.HasSuffix(urlPath, "/") {
urlPath = strings.Join([]string{urlPath, "index.gmi"}, "")
}
finalPath, err := calcFilePath(parentPath, urlPath)
if err != nil {
LogError("Error saving %s: %w", s.Url, err)
return
}
// Ensure the directory exists
dir := filepath.Dir(finalPath)
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
LogError("Failed to create directory: %w", err)
return
}
err = os.WriteFile(finalPath, []byte((*s).Data), 0666)
if err != nil {
LogError("Error saving %s: %w", s.Url.Full, err)
}
LogInfo("[%s] Saved to %s", s.Url.Full, finalPath)
}