Work on header parsing & saving other files
This commit is contained in:
33
fs.go
33
fs.go
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user