Compare commits
No commits in common. "f6cb0a2c62955c0b79e542882692faeafc055bdf" and "2c70e467bc1999b64b25efb1e30ecf3642bcd38c" have entirely different histories.
f6cb0a2c62
...
2c70e467bc
85
main.go
85
main.go
@ -8,7 +8,6 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"regexp"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
@ -61,18 +60,9 @@ type Template struct {
|
||||
templates *template.Template
|
||||
}
|
||||
|
||||
type File struct {
|
||||
Name string
|
||||
FileType string
|
||||
Content string
|
||||
}
|
||||
|
||||
type FilesData struct {
|
||||
Files []File
|
||||
}
|
||||
|
||||
type IndexData struct {
|
||||
Host string
|
||||
Host string
|
||||
UploadOnly bool
|
||||
}
|
||||
|
||||
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
|
||||
@ -81,79 +71,14 @@ func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Con
|
||||
|
||||
func Index(c echo.Context) error {
|
||||
data := IndexData{
|
||||
Host: c.Request().Host,
|
||||
Host: c.Request().Host,
|
||||
UploadOnly: false,
|
||||
}
|
||||
return c.Render(http.StatusOK, "index", data)
|
||||
}
|
||||
|
||||
func GetFileContentType(ouput *os.File) (string, error) {
|
||||
buf := make([]byte, 512)
|
||||
|
||||
_, err := ouput.Read(buf)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
contentType := http.DetectContentType(buf)
|
||||
|
||||
return contentType, nil
|
||||
}
|
||||
|
||||
func Files(c echo.Context) error {
|
||||
var files FilesData
|
||||
|
||||
filelist, err := os.ReadDir("./files/")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
var Type string
|
||||
var Content string
|
||||
|
||||
ImageMatch := regexp.MustCompile("^image/.*")
|
||||
VideoMatch := regexp.MustCompile("^video/.*")
|
||||
JsonMatch := regexp.MustCompile("application/json")
|
||||
TextMatch := regexp.MustCompile("^text/.*|application/octet-stream")
|
||||
|
||||
for _, f := range filelist {
|
||||
filePath := "files/" + f.Name()
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
|
||||
contentType, err := GetFileContentType(file)
|
||||
|
||||
switch {
|
||||
case ImageMatch.MatchString(contentType):
|
||||
Type = "image"
|
||||
Content = ""
|
||||
case VideoMatch.MatchString(contentType):
|
||||
Type = "video"
|
||||
Content = ""
|
||||
case JsonMatch.MatchString(contentType):
|
||||
Type = "text"
|
||||
b, _ := os.ReadFile(filePath)
|
||||
Content = string(b)
|
||||
case TextMatch.MatchString(contentType):
|
||||
Type = "text"
|
||||
b, _ := os.ReadFile(filePath)
|
||||
Content = string(b)
|
||||
default:
|
||||
Type = "else"
|
||||
Content = ""
|
||||
}
|
||||
|
||||
log.Print(contentType)
|
||||
files.Files = append(
|
||||
files.Files,
|
||||
File{Name: f.Name(), FileType: Type, Content: Content},
|
||||
)
|
||||
}
|
||||
|
||||
return c.Render(http.StatusOK, "files", files)
|
||||
return c.Render(http.StatusOK, "files", "test")
|
||||
}
|
||||
|
||||
func Upload(c echo.Context) error {
|
||||
|
@ -11,21 +11,6 @@
|
||||
|
||||
<body>
|
||||
<h1>File List</h1>
|
||||
{{range .Files}}
|
||||
<hr />
|
||||
<div class="file">
|
||||
{{if eq .FileType "image"}}
|
||||
<img src="/files/{{.Name}}" alt="{{.Name}}" />
|
||||
{{else if eq .FileType "video"}}
|
||||
<video controls src="/files/{{.Name}}" />
|
||||
{{else if eq .FileType "text"}}
|
||||
<pre>{{.Content}}</pre>
|
||||
{{end}}
|
||||
<div class="info">
|
||||
<a href="/files/{{.Name}}" download>{{.Name}}</a>
|
||||
</div>
|
||||
</div>
|
||||
{{end}}
|
||||
</body>
|
||||
|
||||
</html>
|
||||
|
Loading…
Reference in New Issue
Block a user