|
|
|
@ -1,6 +1,7 @@
@@ -1,6 +1,7 @@
|
|
|
|
|
package main |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"bytes" |
|
|
|
|
"fmt" |
|
|
|
|
"io" |
|
|
|
|
"net" |
|
|
|
@ -12,8 +13,9 @@ import (
@@ -12,8 +13,9 @@ import (
|
|
|
|
|
"github.com/h2non/filetype" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
func serveDirList(c net.Conn, serve *pathConfig, request *url.URL, dirPath string) int { |
|
|
|
|
func buildDirList(request *url.URL, dirPath string) ([]byte, error) { |
|
|
|
|
var ( |
|
|
|
|
b = &bytes.Buffer{} |
|
|
|
|
files []os.FileInfo |
|
|
|
|
numDirs int |
|
|
|
|
numFiles int |
|
|
|
@ -36,7 +38,7 @@ func serveDirList(c net.Conn, serve *pathConfig, request *url.URL, dirPath strin
@@ -36,7 +38,7 @@ func serveDirList(c net.Conn, serve *pathConfig, request *url.URL, dirPath strin
|
|
|
|
|
return nil |
|
|
|
|
}) |
|
|
|
|
if err != nil { |
|
|
|
|
return writeStatus(c, statusTemporaryFailure) |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
// List directories first
|
|
|
|
|
sort.Slice(files, func(i, j int) bool { |
|
|
|
@ -48,24 +50,22 @@ func serveDirList(c net.Conn, serve *pathConfig, request *url.URL, dirPath strin
@@ -48,24 +50,22 @@ func serveDirList(c net.Conn, serve *pathConfig, request *url.URL, dirPath strin
|
|
|
|
|
return i < j |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
writeSuccess(c, serve, geminiType, -1) |
|
|
|
|
|
|
|
|
|
fmt.Fprintf(c, "# %s%s", request.Path, newLine) |
|
|
|
|
fmt.Fprintf(b, "# %s%s", request.Path, newLine) |
|
|
|
|
if numDirs == 1 { |
|
|
|
|
c.Write([]byte("1 directory")) |
|
|
|
|
b.Write([]byte("1 directory")) |
|
|
|
|
} else { |
|
|
|
|
fmt.Fprintf(c, "%d directories", numDirs) |
|
|
|
|
fmt.Fprintf(b, "%d directories", numDirs) |
|
|
|
|
} |
|
|
|
|
c.Write([]byte(", ")) |
|
|
|
|
b.Write([]byte(", ")) |
|
|
|
|
if numDirs == 1 { |
|
|
|
|
c.Write([]byte("1 file")) |
|
|
|
|
b.Write([]byte("1 file")) |
|
|
|
|
} else { |
|
|
|
|
fmt.Fprintf(c, "%d files", numFiles) |
|
|
|
|
fmt.Fprintf(b, "%d files", numFiles) |
|
|
|
|
} |
|
|
|
|
c.Write([]byte(newLine + newLine)) |
|
|
|
|
b.Write([]byte(newLine + newLine)) |
|
|
|
|
|
|
|
|
|
if request.Path != "/" { |
|
|
|
|
c.Write([]byte("=> ../ ../" + newLine + newLine)) |
|
|
|
|
b.Write([]byte("=> ../ ../" + newLine + newLine)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for _, info := range files { |
|
|
|
@ -76,10 +76,10 @@ func serveDirList(c net.Conn, serve *pathConfig, request *url.URL, dirPath strin
@@ -76,10 +76,10 @@ func serveDirList(c net.Conn, serve *pathConfig, request *url.URL, dirPath strin
|
|
|
|
|
filePath += "/" |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
c.Write([]byte("=> " + fileName + " " + filePath + newLine)) |
|
|
|
|
b.Write([]byte("=> " + fileName + " " + filePath + newLine)) |
|
|
|
|
|
|
|
|
|
if info.IsDir() || info.Mode()&os.ModeSymlink != 0 { |
|
|
|
|
c.Write([]byte(newLine)) |
|
|
|
|
b.Write([]byte(newLine)) |
|
|
|
|
continue |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -87,8 +87,20 @@ func serveDirList(c net.Conn, serve *pathConfig, request *url.URL, dirPath strin
@@ -87,8 +87,20 @@ func serveDirList(c net.Conn, serve *pathConfig, request *url.URL, dirPath strin
|
|
|
|
|
if !info.ModTime().IsZero() { |
|
|
|
|
modified = info.ModTime().Format("2006-01-02 3:04 PM") |
|
|
|
|
} |
|
|
|
|
c.Write([]byte(modified + " - " + formatFileSize(info.Size()) + newLine + newLine)) |
|
|
|
|
b.Write([]byte(modified + " - " + formatFileSize(info.Size()) + newLine + newLine)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return b.Bytes(), nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func serveDirList(c net.Conn, serve *pathConfig, request *url.URL, dirPath string) int { |
|
|
|
|
dirList, err := buildDirList(request, dirPath) |
|
|
|
|
if err != nil { |
|
|
|
|
return writeStatus(c, statusTemporaryFailure) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
writeSuccess(c, serve, geminiType, -1) |
|
|
|
|
c.Write(dirList) |
|
|
|
|
return statusSuccess |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|