UpFast/main.go

248 lines
4.5 KiB
Go
Raw Permalink Normal View History

2023-12-19 13:22:13 +01:00
package main
import (
2023-12-20 12:26:29 +01:00
"embed"
2023-12-19 21:01:30 +01:00
"errors"
"flag"
2023-12-19 19:59:53 +01:00
"html/template"
"io"
2023-12-19 21:01:30 +01:00
"log"
2023-12-19 19:59:53 +01:00
"net/http"
2023-12-19 21:01:30 +01:00
"os"
2023-12-21 11:15:27 +01:00
"regexp"
"strconv"
2023-12-19 19:59:53 +01:00
"github.com/labstack/echo/v4"
2023-12-20 12:26:29 +01:00
"github.com/labstack/echo/v4/middleware"
)
2023-12-19 13:22:13 +01:00
2023-12-20 12:26:29 +01:00
//go:embed all:public/views/*.html
var templates embed.FS
//go:embed all:static/*
var static embed.FS
2023-12-21 18:31:50 +01:00
var domain string
2023-12-19 19:59:53 +01:00
func main() {
t := &Template{
2023-12-20 12:26:29 +01:00
templates: template.Must(template.ParseFS(templates, "public/views/*.html")),
2023-12-19 19:59:53 +01:00
}
2023-12-21 18:31:50 +01:00
p := flag.Int("p", 1323, "upfast port to listen on.")
a := flag.String("a", "127.0.0.1", "upfast ip to listen to")
d := flag.String("d", "127.0.0.1", "upfast domain")
flag.Parse()
2023-12-21 18:31:50 +01:00
host := *a + ":" + strconv.Itoa(*p)
domain = *d
2023-12-19 19:59:53 +01:00
e := echo.New()
e.Renderer = t
2023-12-20 17:35:54 +01:00
e.Use(middleware.Logger())
2023-12-19 19:59:53 +01:00
2023-12-20 12:26:29 +01:00
e.Use(middleware.StaticWithConfig(middleware.StaticConfig{
Root: "static",
Browse: false,
HTML5: true,
Filesystem: http.FS(static),
}))
2023-12-19 20:50:26 +01:00
2023-12-19 21:01:30 +01:00
files := "files"
if _, err := os.Stat(files); errors.Is(err, os.ErrNotExist) {
err := os.Mkdir(files, os.ModePerm)
if err != nil {
log.Println(err)
}
}
e.Static("/files", files)
2023-12-19 19:59:53 +01:00
e.GET("/", Index)
2023-12-19 21:41:36 +01:00
e.POST("/", Upload)
2023-12-19 21:17:59 +01:00
e.GET("/files/", Files)
e.DELETE("/files/:file", Delete)
e.Logger.Fatal(e.Start(host))
2023-12-19 19:59:53 +01:00
}
type Template struct {
templates *template.Template
}
2023-12-21 11:15:27 +01:00
type File struct {
Name string
FileType string
Content string
}
type FilesData struct {
2023-12-21 11:19:22 +01:00
Files []File
2023-12-21 11:15:27 +01:00
}
type IndexData struct {
2023-12-21 11:19:22 +01:00
Host string
}
2023-12-19 19:59:53 +01:00
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return t.templates.ExecuteTemplate(w, name, data)
}
func Index(c echo.Context) error {
data := IndexData{
2023-12-21 18:31:50 +01:00
Host: domain,
}
return c.Render(http.StatusOK, "index", data)
2023-12-19 13:22:13 +01:00
}
2023-12-19 21:17:59 +01:00
2023-12-21 11:15:27 +01:00
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
}
2023-12-19 21:17:59 +01:00
func Files(c echo.Context) error {
2023-12-21 11:15:27 +01:00
var files FilesData
filelist, err := os.ReadDir("./files/")
if err != nil {
log.Fatal(err)
}
UserAgent := c.Request().UserAgent()
log.Print(UserAgent)
match, err := regexp.MatchString("^curl/.*", UserAgent)
if err != nil {
log.Fatal(err)
}
if match {
2024-03-16 18:34:49 +01:00
out := ""
for _, f := range filelist {
out += f.Name() + "\n"
}
return c.String(http.StatusOK, out)
}
2023-12-21 11:15:27 +01:00
var Type string
var Content string
ImageMatch := regexp.MustCompile("^image/.*")
VideoMatch := regexp.MustCompile("^video/.*")
JsonMatch := regexp.MustCompile("application/json")
2024-03-03 11:18:28 +01:00
TextMatch := regexp.MustCompile("^text/.*")
2023-12-21 11:15:27 +01:00
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)
2023-12-19 21:17:59 +01:00
}
2023-12-19 21:41:36 +01:00
func Upload(c echo.Context) error {
file, err := c.FormFile("file")
if err != nil {
return err
}
if _, err := os.Stat("files/" + file.Filename); err == nil {
return c.String(http.StatusOK, "A file with the same name already exist's on the server.\n")
}
2023-12-19 21:41:36 +01:00
src, err := file.Open()
if err != nil {
return err
}
dst, err := os.Create("files/" + file.Filename)
if err != nil {
return err
}
defer dst.Close()
if _, err = io.Copy(dst, src); err != nil {
return err
}
2023-12-21 18:31:50 +01:00
fileUrl := domain + "/files/" + file.Filename + "\n"
2023-12-19 21:41:36 +01:00
UserAgent := c.Request().UserAgent()
2023-12-21 12:23:11 +01:00
log.Print(UserAgent)
match, err := regexp.MatchString("^curl/.*", UserAgent)
if err != nil {
log.Fatal(err)
}
if match {
return c.String(http.StatusOK, fileUrl)
}
2023-12-21 12:23:11 +01:00
2024-03-16 18:34:49 +01:00
return c.HTML(http.StatusOK, "File uploaded at url: <strong>"+fileUrl+"</strong>")
2023-12-19 21:41:36 +01:00
}
func Delete(c echo.Context) error {
file := c.Param("file")
filePath := "files/" + file
if _, err := os.Stat(filePath); err != nil {
return c.String(http.StatusOK, "That file doesn't exist on the server\n")
}
err := os.Remove(filePath)
if err != nil {
return c.String(http.StatusOK, "Error while deleting "+file+"\n")
}
return c.String(http.StatusOK, "Deleted file from server\n")
}