personal-website/wde/wde.go

60 lines
1.4 KiB
Go

package wde
import (
"path"
"personalwebsite/webfilesystem"
"github.com/gin-gonic/gin"
)
type WDE struct {
fs *webfilesystem.WebFileSystem
FilesWidget FilesWidget
}
func NewWDE(webFs *webfilesystem.WebFileSystem) *WDE {
return &WDE{
fs: webFs,
}
}
type FilesWidget struct {
}
func (w *WDE) RenderFileTileView(directory string, host string) (gin.H, error) {
list, err := w.fs.ListDir(directory)
if err != nil {
return nil, err
}
for _, file := range list {
file.Icon = w.GetIconPathForFile(file, directory, "32")
}
return gin.H{
"Files": list,
}, nil
}
func (w *WDE) GetIconPathForFile(fileHeader *webfilesystem.FileHeader, parentDir string, size string) string {
if fileHeader.Icon != "" {
return "/system/libs/img/icon/get?path=" + fileHeader.Icon + "&size=" + size
}
extension := fileHeader.GetExtension()
switch true {
case extension == "app":
return "/system/libs/img/icon/get?path=/Icons/GenericApp.icn&size=" + size
case extension == "jpeg":
fallthrough
case extension == "png":
fallthrough
case extension == "jpg":
return "/system/libs/img/get?path=" + path.Join(parentDir, fileHeader.Name) //+ "&size=" + size
case fileHeader.GetType() == "directory":
return "/system/libs/img/icon/get?path=/Icons/GenericFolder.icn&size=" + size
default:
return "/system/libs/img/icon/get?path=/Icons/GenericDocument.icn&size=" + size
}
}