feat: modularize the code.
This commit is contained in:
parent
6f4757a1e9
commit
a59c892087
7 changed files with 293 additions and 243 deletions
53
upload.go
Normal file
53
upload.go
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/labstack/echo/v4"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
fileUrl := domain + "/files/" + file.Filename + "\n"
|
||||
|
||||
UserAgent := c.Request().UserAgent()
|
||||
|
||||
log.Print(UserAgent)
|
||||
|
||||
match, err := regexp.MatchString("^curl/.*", UserAgent)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if match {
|
||||
return c.String(http.StatusOK, fileUrl)
|
||||
}
|
||||
|
||||
return c.HTML(http.StatusOK, "File uploaded at url: <strong>"+fileUrl+"</strong>")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue