mirror of
https://github.com/gtsteffaniak/filebrowser.git
synced 2026-11-24 23:49:41 -05:00
Beta/v2.0.1 (#2800)
Co-authored-by: Mattijs Bliek <mattijs@mattijsbliek.com> Co-authored-by: GammelSami <39372285+GammelSami@users.noreply.github.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
This commit is contained in:
100 files changed
+5906
-1135
No files matched your search
@@ -0,0 +1,168 @@
|
||||
// Setup go-fitz MuPDF headers for preview CGO (-tags=mupdf).
|
||||
// Run from the backend module root: go run ./scripts/setup-gofitz-cgo
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
backendDir, err := backendRoot()
|
||||
if err != nil {
|
||||
fatal(err)
|
||||
}
|
||||
|
||||
outPath := filepath.Join(backendDir, "internal", "preview", "gofitzinclude")
|
||||
includeDir, err := goFitzIncludeDir(backendDir)
|
||||
if err != nil {
|
||||
fatal(err)
|
||||
}
|
||||
|
||||
if err := removeExisting(outPath); err != nil {
|
||||
fatal(err)
|
||||
}
|
||||
if err := linkInclude(includeDir, outPath); err != nil {
|
||||
fatal(err)
|
||||
}
|
||||
|
||||
fmt.Println("linked go-fitz MuPDF headers at", outPath)
|
||||
}
|
||||
|
||||
func fatal(err error) {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func backendRoot() (string, error) {
|
||||
wd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(wd, "go.mod")); err == nil {
|
||||
return wd, nil
|
||||
}
|
||||
backend := filepath.Join(wd, "backend")
|
||||
if _, err := os.Stat(filepath.Join(backend, "go.mod")); err == nil {
|
||||
return backend, nil
|
||||
}
|
||||
return "", fmt.Errorf("could not find backend module root from %s", wd)
|
||||
}
|
||||
|
||||
func goFitzIncludeDir(backendDir string) (string, error) {
|
||||
cmd := exec.Command("go", "mod", "download", "-json", "github.com/gen2brain/go-fitz")
|
||||
cmd.Dir = backendDir
|
||||
out, err := cmd.Output()
|
||||
if err != nil {
|
||||
if ee, ok := err.(*exec.ExitError); ok {
|
||||
return "", fmt.Errorf("go mod download go-fitz: %w: %s", err, ee.Stderr)
|
||||
}
|
||||
return "", fmt.Errorf("go mod download go-fitz: %w", err)
|
||||
}
|
||||
|
||||
var modInfo struct {
|
||||
Dir string `json:"Dir"`
|
||||
}
|
||||
if err := json.Unmarshal(out, &modInfo); err != nil {
|
||||
return "", fmt.Errorf("parse go mod download output: %w", err)
|
||||
}
|
||||
if modInfo.Dir == "" {
|
||||
return "", fmt.Errorf("go-fitz module path not found after download")
|
||||
}
|
||||
|
||||
include := filepath.Join(modInfo.Dir, "include")
|
||||
if fi, err := os.Stat(include); err != nil || !fi.IsDir() {
|
||||
return "", fmt.Errorf("go-fitz include directory not found: %s", include)
|
||||
}
|
||||
return include, nil
|
||||
}
|
||||
|
||||
func removeExisting(path string) error {
|
||||
fi, err := os.Lstat(path)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
if fi.Mode()&os.ModeSymlink != 0 {
|
||||
return os.Remove(path)
|
||||
}
|
||||
if runtime.GOOS == "windows" {
|
||||
// Junction points are removed with rmdir without deleting the target directory.
|
||||
if err := exec.Command("cmd", "/c", "rmdir", path).Run(); err == nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return os.RemoveAll(path)
|
||||
}
|
||||
|
||||
func linkInclude(src, dst string) error {
|
||||
absSrc, err := filepath.Abs(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
absDst, err := filepath.Abs(dst)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if runtime.GOOS != "windows" {
|
||||
return os.Symlink(absSrc, absDst)
|
||||
}
|
||||
|
||||
if err := os.Symlink(absSrc, absDst); err == nil {
|
||||
return nil
|
||||
}
|
||||
if err := exec.Command("cmd", "/c", "mklink", "/J", absDst, absSrc).Run(); err == nil {
|
||||
return nil
|
||||
}
|
||||
return copyDir(absSrc, absDst)
|
||||
}
|
||||
|
||||
func copyDir(src, dst string) error {
|
||||
if err := os.MkdirAll(dst, 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
return filepath.WalkDir(src, func(path string, d os.DirEntry, walkErr error) error {
|
||||
if walkErr != nil {
|
||||
return walkErr
|
||||
}
|
||||
rel, err := filepath.Rel(src, path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
target := filepath.Join(dst, rel)
|
||||
if d.IsDir() {
|
||||
return os.MkdirAll(target, 0o755)
|
||||
}
|
||||
return copyFile(path, target)
|
||||
})
|
||||
}
|
||||
|
||||
func copyFile(src, dst string) error {
|
||||
in, err := os.Open(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer in.Close()
|
||||
|
||||
if err = os.MkdirAll(filepath.Dir(dst), 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
out, err := os.Create(dst)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
if _, err := io.Copy(out, in); err != nil {
|
||||
return err
|
||||
}
|
||||
return out.Close()
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCopyFile(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
srcDir := t.TempDir()
|
||||
dstDir := t.TempDir()
|
||||
|
||||
src := filepath.Join(srcDir, "header.h")
|
||||
if err := os.WriteFile(src, []byte("test content"), 0o644); err != nil {
|
||||
t.Fatalf("write source: %v", err)
|
||||
}
|
||||
|
||||
dst := filepath.Join(dstDir, "nested", "header.h")
|
||||
if err := copyFile(src, dst); err != nil {
|
||||
t.Fatalf("copyFile: %v", err)
|
||||
}
|
||||
|
||||
got, err := os.ReadFile(dst)
|
||||
if err != nil {
|
||||
t.Fatalf("read destination: %v", err)
|
||||
}
|
||||
if string(got) != "test content" {
|
||||
t.Fatalf("content = %q, want %q", string(got), "test content")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopyDir_nestedFiles(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
srcDir := t.TempDir()
|
||||
dstDir := t.TempDir()
|
||||
|
||||
nested := filepath.Join(srcDir, "mupdf")
|
||||
if err := os.MkdirAll(nested, 0o755); err != nil {
|
||||
t.Fatalf("mkdir nested: %v", err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(srcDir, "root.txt"), []byte("root"), 0o644); err != nil {
|
||||
t.Fatalf("write root: %v", err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(nested, "fitz.h"), []byte("header"), 0o644); err != nil {
|
||||
t.Fatalf("write nested: %v", err)
|
||||
}
|
||||
|
||||
if err := copyDir(srcDir, dstDir); err != nil {
|
||||
t.Fatalf("copyDir: %v", err)
|
||||
}
|
||||
|
||||
root, err := os.ReadFile(filepath.Join(dstDir, "root.txt"))
|
||||
if err != nil {
|
||||
t.Fatalf("read root copy: %v", err)
|
||||
}
|
||||
if string(root) != "root" {
|
||||
t.Fatalf("root content = %q, want root", string(root))
|
||||
}
|
||||
|
||||
nestedCopy, err := os.ReadFile(filepath.Join(dstDir, "mupdf", "fitz.h"))
|
||||
if err != nil {
|
||||
t.Fatalf("read nested copy: %v", err)
|
||||
}
|
||||
if string(nestedCopy) != "header" {
|
||||
t.Fatalf("nested content = %q, want header", string(nestedCopy))
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveExisting_directory(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
dir := t.TempDir()
|
||||
target := filepath.Join(dir, "gofitzinclude")
|
||||
if err := os.MkdirAll(filepath.Join(target, "mupdf"), 0o755); err != nil {
|
||||
t.Fatalf("mkdir target: %v", err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(target, "mupdf", "fitz.h"), []byte("x"), 0o644); err != nil {
|
||||
t.Fatalf("write file: %v", err)
|
||||
}
|
||||
|
||||
if err := removeExisting(target); err != nil {
|
||||
t.Fatalf("removeExisting: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(target); !os.IsNotExist(err) {
|
||||
t.Fatalf("target still exists after removeExisting: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveExisting_symlink(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
dir := t.TempDir()
|
||||
src := filepath.Join(dir, "include")
|
||||
if err := os.MkdirAll(src, 0o755); err != nil {
|
||||
t.Fatalf("mkdir src: %v", err)
|
||||
}
|
||||
|
||||
link := filepath.Join(dir, "gofitzinclude")
|
||||
if err := os.Symlink(src, link); err != nil {
|
||||
t.Skipf("symlinks not supported: %v", err)
|
||||
}
|
||||
|
||||
if err := removeExisting(link); err != nil {
|
||||
t.Fatalf("removeExisting symlink: %v", err)
|
||||
}
|
||||
if _, err := os.Lstat(link); !os.IsNotExist(err) {
|
||||
t.Fatalf("symlink still exists: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(src); err != nil {
|
||||
t.Fatalf("symlink target removed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveExisting_missingPath(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
dir := t.TempDir()
|
||||
missing := filepath.Join(dir, "missing")
|
||||
if err := removeExisting(missing); err != nil {
|
||||
t.Fatalf("removeExisting missing: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user