1package disk
2
3import (
4	"gitlab.com/gitlab-org/gitlab-pages/internal/config"
5	"gitlab.com/gitlab-org/gitlab-pages/internal/httperrors"
6	"gitlab.com/gitlab-org/gitlab-pages/internal/serving"
7	"gitlab.com/gitlab-org/gitlab-pages/internal/vfs"
8	"gitlab.com/gitlab-org/gitlab-pages/metrics"
9)
10
11// Disk describes a disk access serving
12type Disk struct {
13	reader Reader
14}
15
16// ServeFileHTTP serves a file from disk and returns true. It returns false
17// when a file could not been found.
18func (s *Disk) ServeFileHTTP(h serving.Handler) bool {
19	if s.reader.tryFile(h) {
20		return true
21	}
22
23	if s.reader.tryRedirects(h) {
24		return true
25	}
26
27	return false
28}
29
30// ServeNotFoundHTTP tries to read a custom 404 page
31func (s *Disk) ServeNotFoundHTTP(h serving.Handler) {
32	if s.reader.tryNotFound(h) {
33		return
34	}
35
36	// Generic 404
37	httperrors.Serve404(h.Writer)
38}
39
40// Reconfigure VFS
41func (s *Disk) Reconfigure(cfg *config.Config) error {
42	return s.reader.vfs.Reconfigure(cfg)
43}
44
45// New returns a serving instance that is capable of reading files
46// from the VFS
47func New(vfs vfs.VFS) serving.Serving {
48	return &Disk{
49		reader: Reader{
50			fileSizeMetric: metrics.DiskServingFileSize,
51			vfs:            vfs,
52		},
53	}
54}
55