2022-08-24 11:10:50 +08:00
|
|
|
package files
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-09-01 19:02:33 +08:00
|
|
|
"github.com/pkg/errors"
|
2022-08-24 11:10:50 +08:00
|
|
|
"github.com/spf13/afero"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FileInfo struct {
|
|
|
|
Fs afero.Fs `json:"-"`
|
|
|
|
Path string `json:"path"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
User string `json:"user"`
|
|
|
|
Group string `json:"group"`
|
|
|
|
Extension string `json:"extension"`
|
|
|
|
Content string `json:"content"`
|
|
|
|
Size int64 `json:"size"`
|
|
|
|
IsDir bool `json:"isDir"`
|
|
|
|
IsSymlink bool `json:"isSymlink"`
|
2022-08-31 16:00:51 +08:00
|
|
|
LinkPath string `json:"linkPath"`
|
2022-08-24 11:10:50 +08:00
|
|
|
Type string `json:"type"`
|
|
|
|
Mode string `json:"mode"`
|
2022-08-30 17:59:59 +08:00
|
|
|
MimeType string `json:"mimeType"`
|
2022-08-24 11:10:50 +08:00
|
|
|
UpdateTime time.Time `json:"updateTime"`
|
|
|
|
ModTime time.Time `json:"modTime"`
|
|
|
|
FileMode os.FileMode `json:"-"`
|
|
|
|
Items []*FileInfo `json:"items"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type FileOption struct {
|
|
|
|
Path string `json:"path"`
|
|
|
|
Search string `json:"search"`
|
|
|
|
Expand bool `json:"expand"`
|
2022-09-06 10:35:35 +08:00
|
|
|
Dir bool `json:"dir"`
|
2022-08-24 11:10:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewFileInfo(op FileOption) (*FileInfo, error) {
|
|
|
|
var appFs = afero.NewOsFs()
|
|
|
|
|
|
|
|
info, err := appFs.Stat(op.Path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
file := &FileInfo{
|
|
|
|
Fs: appFs,
|
|
|
|
Path: op.Path,
|
|
|
|
Name: info.Name(),
|
|
|
|
IsDir: info.IsDir(),
|
|
|
|
FileMode: info.Mode(),
|
|
|
|
ModTime: info.ModTime(),
|
|
|
|
Size: info.Size(),
|
|
|
|
IsSymlink: IsSymlink(info.Mode()),
|
|
|
|
Extension: filepath.Ext(info.Name()),
|
|
|
|
Mode: fmt.Sprintf("%04o", info.Mode().Perm()),
|
|
|
|
User: GetUsername(info.Sys().(*syscall.Stat_t).Uid),
|
|
|
|
Group: GetGroup(info.Sys().(*syscall.Stat_t).Gid),
|
2022-08-30 17:59:59 +08:00
|
|
|
MimeType: GetMimeType(op.Path),
|
2022-08-24 11:10:50 +08:00
|
|
|
}
|
2022-08-31 16:00:51 +08:00
|
|
|
if file.IsSymlink {
|
|
|
|
file.LinkPath = GetSymlink(op.Path)
|
2022-09-05 16:25:26 +08:00
|
|
|
}
|
|
|
|
if op.Search != "" {
|
|
|
|
|
2022-08-31 16:00:51 +08:00
|
|
|
}
|
2022-08-24 11:10:50 +08:00
|
|
|
if op.Expand {
|
|
|
|
if file.IsDir {
|
2022-09-06 10:35:35 +08:00
|
|
|
if err := file.listChildren(op.Dir); err != nil {
|
2022-08-24 11:10:50 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return file, nil
|
|
|
|
} else {
|
|
|
|
if err := file.getContent(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return file, nil
|
|
|
|
}
|
|
|
|
|
2022-09-06 10:35:35 +08:00
|
|
|
func (f *FileInfo) listChildren(dir bool) error {
|
2022-08-24 11:10:50 +08:00
|
|
|
afs := &afero.Afero{Fs: f.Fs}
|
2022-09-06 10:35:35 +08:00
|
|
|
files, err := afs.ReadDir(f.Path)
|
2022-08-24 11:10:50 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var items []*FileInfo
|
2022-09-06 10:35:35 +08:00
|
|
|
for _, df := range files {
|
|
|
|
if dir && !df.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-08-24 11:10:50 +08:00
|
|
|
name := df.Name()
|
|
|
|
fPath := path.Join(f.Path, df.Name())
|
|
|
|
|
|
|
|
isSymlink, isInvalidLink := false, false
|
|
|
|
if IsSymlink(df.Mode()) {
|
|
|
|
isSymlink = true
|
|
|
|
info, err := f.Fs.Stat(fPath)
|
|
|
|
if err == nil {
|
|
|
|
df = info
|
|
|
|
} else {
|
|
|
|
isInvalidLink = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
file := &FileInfo{
|
|
|
|
Fs: f.Fs,
|
|
|
|
Name: name,
|
|
|
|
Size: df.Size(),
|
|
|
|
ModTime: df.ModTime(),
|
|
|
|
FileMode: df.Mode(),
|
|
|
|
IsDir: df.IsDir(),
|
|
|
|
IsSymlink: isSymlink,
|
|
|
|
Extension: filepath.Ext(name),
|
|
|
|
Path: fPath,
|
|
|
|
Mode: fmt.Sprintf("%04o", df.Mode().Perm()),
|
|
|
|
User: GetUsername(df.Sys().(*syscall.Stat_t).Uid),
|
|
|
|
Group: GetGroup(df.Sys().(*syscall.Stat_t).Gid),
|
2022-08-30 17:59:59 +08:00
|
|
|
MimeType: GetMimeType(fPath),
|
2022-08-24 11:10:50 +08:00
|
|
|
}
|
2022-08-31 16:00:51 +08:00
|
|
|
if isSymlink {
|
|
|
|
file.LinkPath = GetSymlink(fPath)
|
|
|
|
}
|
2022-08-24 11:10:50 +08:00
|
|
|
|
|
|
|
if isInvalidLink {
|
|
|
|
file.Type = "invalid_link"
|
|
|
|
}
|
|
|
|
items = append(items, file)
|
|
|
|
}
|
|
|
|
f.Items = items
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FileInfo) getContent() error {
|
2022-09-01 19:02:33 +08:00
|
|
|
if f.Size <= 10*1024*1024 {
|
|
|
|
afs := &afero.Afero{Fs: f.Fs}
|
|
|
|
cByte, err := afs.ReadFile(f.Path)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
f.Content = string(cByte)
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return errors.New("file is too large!")
|
|
|
|
}
|
2022-08-24 11:10:50 +08:00
|
|
|
}
|