Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ You can download pre-built binary in the
- [x] Serve index.html
- [x] Sorting
- [x] Searching
- [ ] Preview
- [x] Preview

## Usage

Expand Down
12 changes: 9 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module github.com/ndtoan96/gofs
go 1.23.6

require (
github.com/alecthomas/chroma/v2 v2.15.0
github.com/gomarkdown/markdown v0.0.0-20250207164621-7a1f277a159e
github.com/lithammer/fuzzysearch v1.1.8
github.com/otiai10/copy v1.14.1
github.com/spf13/pflag v1.0.6
Expand All @@ -11,10 +13,14 @@ require (

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dlclark/regexp2 v1.11.4 // indirect
github.com/ebitengine/purego v0.8.0 // indirect
github.com/gen2brain/go-fitz v1.24.14 // indirect
github.com/jupiterrider/ffi v0.2.0 // indirect
github.com/otiai10/mint v1.6.3 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/sync v0.10.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/text v0.21.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
29 changes: 24 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
github.com/alecthomas/assert/v2 v2.11.0 h1:2Q9r3ki8+JYXvGsDyBXwH3LcJ+WK5D0gc5E8vS6K3D0=
github.com/alecthomas/assert/v2 v2.11.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k=
github.com/alecthomas/chroma/v2 v2.15.0 h1:LxXTQHFoYrstG2nnV9y2X5O94sOBzf0CIUpSTbpxvMc=
github.com/alecthomas/chroma/v2 v2.15.0/go.mod h1:gUhVLrPDXPtp/f+L1jo9xepo9gL4eLwRuGAunSZMkio=
github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc=
github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dlclark/regexp2 v1.11.4 h1:rPYF9/LECdNymJufQKmri9gV604RvvABwgOA8un7yAo=
github.com/dlclark/regexp2 v1.11.4/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/ebitengine/purego v0.8.0 h1:JbqvnEzRvPpxhCJzJJ2y0RbiZ8nyjccVUrSM3q+GvvE=
github.com/ebitengine/purego v0.8.0/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=
github.com/gen2brain/go-fitz v1.24.14 h1:09weRkjVtLYNGo7l0J7DyOwBExbwi8SJ9h8YPhw9WEo=
github.com/gen2brain/go-fitz v1.24.14/go.mod h1:0KaZeQgASc20Yp5R/pFzyy7SmP01XcoHKNF842U2/S4=
github.com/gomarkdown/markdown v0.0.0-20250207164621-7a1f277a159e h1:ESHlT0RVZphh4JGBz49I5R6nTdC8Qyc08vU25GQHzzQ=
github.com/gomarkdown/markdown v0.0.0-20250207164621-7a1f277a159e/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/jupiterrider/ffi v0.2.0 h1:tMM70PexgYNmV+WyaYhJgCvQAvtTCs3wXeILPutihnA=
github.com/jupiterrider/ffi v0.2.0/go.mod h1:yqYqX5DdEccAsHeMn+6owkoI2llBLySVAF8dwCDZPVs=
github.com/lithammer/fuzzysearch v1.1.8 h1:/HIuJnjHuXS8bKaiTMeeDlW2/AyIWk2brx1V8LFgLN4=
github.com/lithammer/fuzzysearch v1.1.8/go.mod h1:IdqeyBClc3FFqSzYq/MXESsS4S0FsZ5ajtkr5xPLts4=
github.com/otiai10/copy v1.14.1 h1:5/7E6qsUMBaH5AnQ0sSLzzTg1oTECmcCmT6lvF45Na8=
Expand All @@ -24,25 +42,26 @@ golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
Expand Down
34 changes: 34 additions & 0 deletions io_helper/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io_helper

import (
"bufio"
"os"
"unicode/utf8"
)

func ReadPart(filePath string, n int) ([]byte, error) {
f, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer f.Close()
buffer := make([]byte, n)
bufReader := bufio.NewReader(f)
numBytes, err := bufReader.Read(buffer)
if err != nil {
return nil, err
}
return buffer[:numBytes], nil
}

func IsTextFile(filePath string) (bool, error) {
content, err := ReadPart(filePath, 1024)
if err != nil {
return false, err
}
if utf8.Valid(content) {
return true, nil
} else {
return false, nil
}
}
66 changes: 20 additions & 46 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@ package main

import (
"archive/zip"
"bufio"
"bytes"
"fmt"
"html/template"
"io"
"log"

"net"
"net/http"
"net/url"
"os"
"path"
"sort"
"unicode/utf8"

"strings"

"github.com/lithammer/fuzzysearch/fuzzy"
"github.com/ndtoan96/gofs/io_helper"
"github.com/ndtoan96/gofs/model"
"github.com/ndtoan96/gofs/preview"
"github.com/ndtoan96/gofs/search"
cp "github.com/otiai10/copy"
"github.com/spf13/pflag"

Expand Down Expand Up @@ -86,6 +87,8 @@ func servePath(w http.ResponseWriter, r *http.Request) {
} else {
descb = true
}
previewName := r.URL.Query().Get("preview")
htmlPreview := preview.GetHtmlPreview(p, previewName)

info, err := os.Stat(p)
if os.IsNotExist(err) {
Expand All @@ -108,7 +111,15 @@ func servePath(w http.ResponseWriter, r *http.Request) {
d, _ := os.ReadDir(e.Name())
items = append(items, model.Item{IsDir: true, Name: e.Name(), LastModified: info.ModTime(), Size: model.DirSize(len(d))})
} else {
items = append(items, model.Item{IsDir: false, Name: e.Name(), LastModified: info.ModTime(), Size: model.FileSize(info.Size())})
if ext := path.Ext(e.Name()); ext == ".zip" || ext == ".7z" || ext == ".gz" || ext == ".rar" || ext == ".tar" || ext == ".jar" || ext == ".bz" || ext == ".epub" {
items = append(items, model.Item{IsDir: false, Name: e.Name(), LastModified: info.ModTime(), Size: model.FileSize(info.Size()), FileType: "archive"})
} else if ext := path.Ext(e.Name()); ext == ".jpg" || ext == ".png" || ext == ".gif" || ext == ".webp" || ext == ".jpeg" || ext == ".svg" {
items = append(items, model.Item{IsDir: false, Name: e.Name(), LastModified: info.ModTime(), Size: model.FileSize(info.Size()), FileType: "image"})
} else if yes, _ := io_helper.IsTextFile(path.Join(p, e.Name())); yes {
items = append(items, model.Item{IsDir: false, Name: e.Name(), LastModified: info.ModTime(), Size: model.FileSize(info.Size()), FileType: "text"})
} else {
items = append(items, model.Item{IsDir: false, Name: e.Name(), LastModified: info.ModTime(), Size: model.FileSize(info.Size()), FileType: "binary"})
}
}
}
sort.Slice(items, func(i, j int) bool {
Expand Down Expand Up @@ -140,7 +151,7 @@ func servePath(w http.ResponseWriter, r *http.Request) {
return strings.ToLower(items[first].Name) < strings.ToLower(items[second].Name)
}
})
if err := tmpl["files"].Execute(w, model.FilesPageModel{Path: model.Path(p), Items: items, AllowWrite: allowWrite, SelectState: selectState, SortField: sortField, Desc: descb}); err != nil {
if err := tmpl["files"].Execute(w, model.FilesPageModel{Path: model.Path(p), Items: items, AllowWrite: allowWrite, SelectState: selectState, SortField: sortField, Desc: descb, Preview: htmlPreview, PreviewName: previewName}); err != nil {
log.Fatalln("[ERROR]", err)
}

Expand Down Expand Up @@ -186,17 +197,9 @@ func action(w http.ResponseWriter, r *http.Request) {
case "edit":
editableFiles := make([]string, 0)
for _, name := range names {
f, tmp_err := os.Open(path.Join(p, name))
if tmp_err == nil {
buffer := make([]byte, 1024)
bufReader := bufio.NewReader(f)
n, tmp_err := bufReader.Read(buffer)
if tmp_err == nil && utf8.Valid(buffer[:n]) {
editableFiles = append(editableFiles, name)
} else {
fmt.Printf("%v", tmp_err)
}
f.Close()
yes, _ := io_helper.IsTextFile(path.Join(p, name))
if yes {
editableFiles = append(editableFiles, name)
}
}
if len(editableFiles) > 0 {
Expand Down Expand Up @@ -321,7 +324,7 @@ func action(w http.ResponseWriter, r *http.Request) {
return
}
var results []model.SearchResult
results, err = doSearch(p, text)
results, err = search.Search(p, text)
if err == nil {
err = tmpl["search"].Execute(w, model.SearchPageModel{Search: text, Results: results, Path: model.Path(p)})
}
Expand All @@ -334,35 +337,6 @@ func action(w http.ResponseWriter, r *http.Request) {
}
}

func doSearch(dir string, text string) ([]model.SearchResult, error) {
results := make([]model.SearchResult, 0)
err := recursiveSearch(&results, dir, text)
if err != nil {
return nil, err
}
sort.Slice(results, func(i int, j int) bool {
return results[i].Score > results[j].Score
})
return results, nil
}

func recursiveSearch(results *[]model.SearchResult, dir string, text string) error {
entries, err := os.ReadDir(dir)
if err != nil {
return err
}
for _, entry := range entries {
score := fuzzy.RankMatchFold(text, path.Join(dir, entry.Name()))
if score != -1 {
*results = append(*results, model.SearchResult{Path: path.Join(dir, entry.Name()), IsDir: entry.IsDir(), Score: score})
}
if entry.IsDir() {
recursiveSearch(results, path.Join(dir, entry.Name()), text)
}
}
return nil
}

func delete(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
p := r.FormValue("path")
Expand Down
4 changes: 4 additions & 0 deletions model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package model

import (
"fmt"
"html/template"
"path"
"slices"
"strings"
Expand Down Expand Up @@ -37,6 +38,7 @@ type Item struct {
LastModified time.Time
Size ISize
IsDir bool
FileType string
}

type FilesPageModel struct {
Expand All @@ -46,6 +48,8 @@ type FilesPageModel struct {
SelectState string
SortField string
Desc bool
Preview template.HTML
PreviewName string
}

type DeletePageModel struct {
Expand Down
Loading