Skip to content

Commit ce62fba

Browse files
committed
feat: add directory index page option
1 parent 0f0eac8 commit ce62fba

File tree

6 files changed

+71
-3
lines changed

6 files changed

+71
-3
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ server [options]
5252
Use virtual empty directory as root directory.
5353
Useful to share alias directories only.
5454
55+
-I|--dir-index <file> ...
56+
Specify default index file for directory.
57+
Directly mounted files by alias are not considered.
58+
5559
-a|--alias <separator><url-path><separator><fs-path> ...
5660
Set path alias. e.g. ":/doc:/usr/share/doc"
5761

src/param/cli.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ func init() {
2626
err = options.AddFlags("emptyroot", []string{"-R", "--empty-root"}, "GHFS_EMPTY_ROOT", "use virtual empty root directory")
2727
serverErrHandler.CheckFatal(err)
2828

29+
err = options.AddFlagsValues("dirindexes", []string{"-I", "--dir-index"}, "GHFS_DIR_INDEX", nil, "default index page for directory")
30+
serverErrHandler.CheckFatal(err)
31+
2932
err = options.AddFlagsValues("aliases", []string{"-a", "--alias"}, "", nil, "set alias path, <sep><url><sep><path>, e.g. :/doc:/usr/share/doc")
3033
serverErrHandler.CheckFatal(err)
3134

@@ -197,6 +200,10 @@ func doParseCli() []*Param {
197200
root, _ = util.NormalizeFsPath(root)
198201
param.Root = root
199202

203+
// dir indexes
204+
dirIndexes, _ := result.GetStrings("dirindexes")
205+
param.DirIndexes = normalizeFilenames(dirIndexes)
206+
200207
// certificate
201208
key, _ := result.GetString("key")
202209
cert, _ := result.GetString("cert")

src/param/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ type Param struct {
1414
Root string
1515
EmptyRoot bool
1616

17-
Aliases map[string]string
17+
DirIndexes []string
18+
Aliases map[string]string
1819

1920
GlobalUpload bool
2021
UploadUrls []string

src/param/util.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package param
33
import (
44
"../util"
55
"path"
6+
"path/filepath"
67
"strings"
78
"unicode/utf8"
89
)
@@ -75,3 +76,25 @@ func normalizeFsPaths(inputs []string) []string {
7576

7677
return outputs
7778
}
79+
80+
func normalizeFilenames(inputs []string) []string {
81+
outputs := make([]string, 0, len(inputs))
82+
83+
for _, input := range inputs {
84+
if len(input) == 0 {
85+
continue
86+
}
87+
88+
if strings.IndexByte(input, '/') >= 0 {
89+
continue
90+
}
91+
92+
if filepath.Separator != '/' && strings.IndexByte(input, filepath.Separator) >= 0 {
93+
continue
94+
}
95+
96+
outputs = append(outputs, input)
97+
}
98+
99+
return outputs
100+
}

src/serverHandler/main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ type handler struct {
1515
emptyRoot bool
1616
urlPrefix string
1717

18-
aliases aliases
18+
dirIndexes []string
19+
aliases aliases
1920

2021
globalUpload bool
2122
uploadUrls []string
@@ -133,7 +134,8 @@ func NewHandler(
133134
emptyRoot: emptyRoot,
134135
urlPrefix: urlPrefix,
135136

136-
aliases: aliases,
137+
dirIndexes: p.DirIndexes,
138+
aliases: aliases,
137139

138140
globalUpload: p.GlobalUpload,
139141
uploadUrls: p.UploadUrls,

src/serverHandler/responseData.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,23 @@ func getStatusByErr(err error) int {
274274
}
275275
}
276276

277+
func (h *handler) stateIndexFile(baseItem os.FileInfo, baseDir string) (file *os.File, item os.FileInfo, err error) {
278+
if baseItem == nil || !baseItem.IsDir() || h.emptyRoot || len(h.dirIndexes) == 0 {
279+
return
280+
}
281+
282+
for _, index := range h.dirIndexes {
283+
file, item, err = stat(baseDir+"/"+index, true)
284+
if err != nil && os.IsNotExist(err) {
285+
continue
286+
} else {
287+
return
288+
}
289+
}
290+
291+
return nil, nil, nil
292+
}
293+
277294
func (h *handler) getResponseData(r *http.Request) (data *responseData) {
278295
requestUri := r.URL.Path
279296
tailSlash := requestUri[len(requestUri)-1] == '/'
@@ -303,6 +320,20 @@ func (h *handler) getResponseData(r *http.Request) (data *responseData) {
303320
status = getStatusByErr(_statErr)
304321
}
305322

323+
indexFile, indexItem, _statIdxErr := h.stateIndexFile(item, reqFsPath)
324+
if _statIdxErr != nil {
325+
errs = append(errs, _statIdxErr)
326+
status = getStatusByErr(_statIdxErr)
327+
} else if indexFile != nil {
328+
if indexItem != nil {
329+
file.Close()
330+
file = indexFile
331+
item = indexItem
332+
} else {
333+
indexFile.Close()
334+
}
335+
}
336+
306337
itemName := getItemName(item, r)
307338

308339
subInfos, _readdirErr := readdir(file, item, needResponseBody(r.Method))

0 commit comments

Comments
 (0)