Files
gitea-pages/pkg/core/vfs.go
ExplodingDragon 02df131beb 重构项目
2025-11-13 23:31:23 +08:00

85 lines
1.6 KiB
Go

package core
import (
"context"
"io"
"net/http"
"os"
)
type PageVFS struct {
backend Backend
client *http.Client
org string
repo string
commitID string
}
// todo: 限制最大文件加载大小
func NewPageVFS(
client *http.Client,
backend Backend,
org string,
repo string,
commitID string,
) *PageVFS {
return &PageVFS{
client: client,
backend: backend,
org: org,
repo: repo,
commitID: commitID,
}
}
func (p *PageVFS) NativeOpen(ctx context.Context, path string, headers http.Header) (*http.Response, error) {
return p.backend.Open(ctx, p.client, p.org, p.repo, p.commitID, path, headers)
}
func (p *PageVFS) Exists(ctx context.Context, path string) (bool, error) {
open, err := p.NativeOpen(ctx, path, nil)
if open != nil {
defer open.Body.Close()
}
if err != nil || open == nil {
return false, err
}
if open.StatusCode != http.StatusOK {
return false, nil
}
return true, nil
}
func (p *PageVFS) Open(ctx context.Context, path string) (io.ReadCloser, error) {
resp, err := p.NativeOpen(ctx, path, nil)
if err != nil {
if resp != nil {
_ = resp.Body.Close()
}
return nil, err
}
if resp.StatusCode != http.StatusOK {
_ = resp.Body.Close()
return nil, os.ErrNotExist
}
return resp.Body, nil
}
func (p *PageVFS) Read(ctx context.Context, path string) ([]byte, error) {
open, err := p.Open(ctx, path)
if err != nil {
return nil, err
}
defer open.Close()
return io.ReadAll(open)
}
func (p *PageVFS) ReadString(ctx context.Context, path string) (string, error) {
read, err := p.Read(ctx, path)
if err != nil {
return "", err
}
return string(read), nil
}