Skip to content

Commit dd0ced2

Browse files
committed
add go code
1 parent bcdb10b commit dd0ced2

File tree

6 files changed

+429
-0
lines changed

6 files changed

+429
-0
lines changed

go.mod

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module github.com/alexcoder04/gupload
2+
3+
go 1.20
4+
5+
require (
6+
github.com/alexcoder04/friendly/v2 v2.1.1 // indirect
7+
github.com/bytedance/sonic v1.8.0 // indirect
8+
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
9+
github.com/gin-contrib/sse v0.1.0 // indirect
10+
github.com/gin-gonic/gin v1.9.0 // indirect
11+
github.com/go-playground/locales v0.14.1 // indirect
12+
github.com/go-playground/universal-translator v0.18.1 // indirect
13+
github.com/go-playground/validator/v10 v10.11.2 // indirect
14+
github.com/goccy/go-json v0.10.0 // indirect
15+
github.com/json-iterator/go v1.1.12 // indirect
16+
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
17+
github.com/leodido/go-urn v1.2.1 // indirect
18+
github.com/mattn/go-isatty v0.0.17 // indirect
19+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
20+
github.com/modern-go/reflect2 v1.0.2 // indirect
21+
github.com/otiai10/copy v1.9.0 // indirect
22+
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
23+
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e // indirect
24+
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
25+
github.com/ugorji/go/codec v1.2.9 // indirect
26+
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect
27+
golang.org/x/crypto v0.5.0 // indirect
28+
golang.org/x/net v0.7.0 // indirect
29+
golang.org/x/sys v0.5.0 // indirect
30+
golang.org/x/text v0.7.0 // indirect
31+
google.golang.org/protobuf v1.28.1 // indirect
32+
gopkg.in/yaml.v3 v3.0.1 // indirect
33+
)

go.sum

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
github.com/alexcoder04/friendly/v2 v2.1.1 h1:r1rEkyquOfMPPvRw/ZjC31DVrPG+keMr4POrW2tV7BQ=
2+
github.com/alexcoder04/friendly/v2 v2.1.1/go.mod h1:QTi7RZmFEvo4C7/gztc4HM/uj85Qp72MJtqzefFPfKw=
3+
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
4+
github.com/bytedance/sonic v1.8.0 h1:ea0Xadu+sHlu7x5O3gKhRpQ1IKiMrSiHttPF0ybECuA=
5+
github.com/bytedance/sonic v1.8.0/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
6+
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
7+
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams=
8+
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
9+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
10+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
11+
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
12+
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
13+
github.com/gin-gonic/gin v1.9.0 h1:OjyFBKICoexlu99ctXNR2gg+c5pKrKMuyjgARg9qeY8=
14+
github.com/gin-gonic/gin v1.9.0/go.mod h1:W1Me9+hsUSyj3CePGrd1/QrKJMSJ1Tu/0hFEH89961k=
15+
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
16+
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
17+
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
18+
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
19+
github.com/go-playground/validator/v10 v10.11.2 h1:q3SHpufmypg+erIExEKUmsgmhDTyhcJ38oeKGACXohU=
20+
github.com/go-playground/validator/v10 v10.11.2/go.mod h1:NieE624vt4SCTJtD87arVLvdmjPAeV8BQlHtMnw9D7s=
21+
github.com/goccy/go-json v0.10.0 h1:mXKd9Qw4NuzShiRlOXKews24ufknHO7gx30lsDyokKA=
22+
github.com/goccy/go-json v0.10.0/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
23+
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
24+
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
25+
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
26+
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
27+
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
28+
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
29+
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
30+
github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
31+
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
32+
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
33+
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
34+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
35+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
36+
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
37+
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
38+
github.com/otiai10/copy v1.9.0 h1:7KFNiCgZ91Ru4qW4CWPf/7jqtxLagGRmIxWldPP9VY4=
39+
github.com/otiai10/copy v1.9.0/go.mod h1:hsfX19wcn0UWIHUQ3/4fHuehhk2UyArQ9dVFAn3FczI=
40+
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE=
41+
github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs=
42+
github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo=
43+
github.com/otiai10/mint v1.4.0/go.mod h1:gifjb2MYOoULtKLqUAEILUG/9KONW6f7YsJ6vQLTlFI=
44+
github.com/pelletier/go-toml/v2 v2.0.6 h1:nrzqCb7j9cDFj2coyLNLaZuJTLjWjlaz6nvTvIwycIU=
45+
github.com/pelletier/go-toml/v2 v2.0.6/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek=
46+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
47+
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0=
48+
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M=
49+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
50+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
51+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
52+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
53+
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
54+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
55+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
56+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
57+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
58+
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
59+
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
60+
github.com/ugorji/go/codec v1.2.9 h1:rmenucSohSTiyL09Y+l2OCk+FrMxGMzho2+tjr5ticU=
61+
github.com/ugorji/go/codec v1.2.9/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
62+
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 h1:18EFjUmQOcUvxNYSkA6jO9VAiXCnxFY6NyDX0bHDmkU=
63+
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
64+
golang.org/x/crypto v0.5.0 h1:U/0M97KRkSFvyD/3FSmdP5W5swImpNgle/EHFhOsQPE=
65+
golang.org/x/crypto v0.5.0/go.mod h1:NK/OQwhpMQP3MwtdjgLlYHnH9ebylxKWv3e0fK+mkQU=
66+
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
67+
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
68+
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
69+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
70+
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
71+
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
72+
golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
73+
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
74+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
75+
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
76+
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
77+
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
78+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
79+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
80+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
81+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
82+
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=

handlers.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"os"
7+
"path/filepath"
8+
"regexp"
9+
"time"
10+
11+
"github.com/alexcoder04/friendly/v2"
12+
"github.com/alexcoder04/friendly/v2/ffiles"
13+
"github.com/gin-gonic/gin"
14+
)
15+
16+
var AutoDeleteMap map[string]int = map[string]int{}
17+
18+
func HandlerIndex(c *gin.Context) {
19+
RetPage(c, []string{})
20+
}
21+
22+
func HandlerUpload(c *gin.Context) {
23+
form, err := c.MultipartForm()
24+
if err != nil {
25+
RetPage(c, []string{"upload failed"})
26+
return
27+
}
28+
29+
autodelete := form.Value["autodelete"]
30+
files := form.File["upload"]
31+
32+
messages := []string{}
33+
for _, file := range files {
34+
filename := file.Filename
35+
filename = regexp.MustCompile(`^[^a-zA-Z0-9_\-.]+$`).ReplaceAllString(filename, "_")
36+
37+
if err := c.SaveUploadedFile(file, filepath.Join(SharedDir, filename)); err != nil {
38+
RetPage(c, []string{"Upload failed"})
39+
continue
40+
}
41+
42+
messages = append(messages, fmt.Sprintf("'%s' was uploaded successfully", filename))
43+
if len(autodelete) > 0 && autodelete[0] == "on" {
44+
AutoDeleteMap[filename] = 60 * 1
45+
}
46+
47+
go func() {
48+
for {
49+
newMap := map[string]int{}
50+
for key, val := range AutoDeleteMap {
51+
if val <= 0 {
52+
os.Remove(filepath.Join(SharedDir, key))
53+
continue
54+
}
55+
newMap[key] = val - 5
56+
}
57+
AutoDeleteMap = newMap
58+
time.Sleep(5 * time.Second)
59+
}
60+
}()
61+
}
62+
63+
RetPage(c, messages)
64+
}
65+
66+
func HandlerQRCode(c *gin.Context) {
67+
c.Data(http.StatusOK, "image/png", QRCodeData)
68+
}
69+
70+
func HandlerDownload(c *gin.Context) {
71+
if !ffiles.IsDir(filepath.Join(SharedDir, c.Param("path"))) {
72+
c.File(filepath.Join(SharedDir, c.Param("path")))
73+
}
74+
c.Redirect(http.StatusMovedPermanently, fmt.Sprintf("/?view=%s", c.Param("path")))
75+
}
76+
77+
func HandlerZip(c *gin.Context) {
78+
if c.Param("path") == "" || !ffiles.IsDir(filepath.Join(SharedDir, c.Param("path"))) {
79+
c.Redirect(http.StatusBadRequest, "/")
80+
}
81+
82+
friendly.CompressFolder(filepath.Join(SharedDir, c.Param("path")), filepath.Join(os.TempDir(), c.Param("path")+".zip"))
83+
c.File(filepath.Join(os.TempDir(), c.Param("path")+".zip"))
84+
}

init.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package main
2+
3+
import (
4+
"embed"
5+
"fmt"
6+
"html/template"
7+
"io/fs"
8+
"net/http"
9+
"os"
10+
)
11+
12+
//go:embed templates/index.html
13+
var TemplateFiles embed.FS
14+
15+
//go:embed static
16+
var StaticFiles embed.FS
17+
var StaticFS http.FileSystem
18+
19+
var (
20+
Port string = "1234"
21+
Hostname string
22+
IP string
23+
SharedDir string
24+
Template *template.Template
25+
)
26+
27+
func init() {
28+
// find and prepare shared dir
29+
if !PrepareSharedDir() {
30+
err := os.MkdirAll(SharedDir, 0700)
31+
if err != nil {
32+
panic("failed to create shared dir")
33+
}
34+
}
35+
36+
// get hostname
37+
var err error
38+
Hostname, err = os.Hostname()
39+
if err != nil {
40+
Hostname = "unknown-pc"
41+
}
42+
43+
// get ip
44+
IP, err = GetLocalIP()
45+
if err != nil {
46+
panic("failed to get local ip address")
47+
}
48+
49+
// generate qr code
50+
err = QREncode(fmt.Sprintf("http://%s:%s/", IP, Port))
51+
if err != nil {
52+
panic("failed to create qr code")
53+
}
54+
55+
// load html template
56+
Template, err = LoadTemplate()
57+
if err != nil {
58+
panic("failed to load html template")
59+
}
60+
61+
// strip prefix from static fs
62+
sub, err := fs.Sub(StaticFiles, "static")
63+
if err != nil {
64+
panic("failed to strip 'static' from static files")
65+
}
66+
StaticFS = http.FS(sub)
67+
}

main.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"github.com/gin-gonic/gin"
5+
)
6+
7+
func main() {
8+
r := gin.Default()
9+
10+
// static files
11+
r.StaticFS("/static", StaticFS)
12+
r.GET("/qrcode.png", HandlerQRCode)
13+
14+
// main page
15+
r.GET("/", HandlerIndex)
16+
r.POST("/", HandlerUpload)
17+
18+
// download files
19+
r.GET("/download/:path", HandlerDownload)
20+
r.GET("/zip/:path", HandlerZip)
21+
22+
r.Run(":" + Port)
23+
}

0 commit comments

Comments
 (0)