-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
152 lines (127 loc) · 3.15 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"fmt"
"html/template"
"io"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/gin-gonic/gin"
)
var hostname string
func indexPage(c *gin.Context) {
c.HTML(200, "index.tmpl", gin.H{
"page": "index",
})
}
func showFile(file_folder string) []string {
var pathDirOut []string
var pathOut []string
var allOut []string
files, _ := ioutil.ReadDir(file_folder)
for _, file := range files {
if file.IsDir() {
path := showFile(file_folder + "/" + file.Name())
pathDirOut = append(pathDirOut, path...)
} else {
path := (file_folder + "/" + file.Name())
pathOut = append(pathOut, path)
}
}
for _, out := range pathDirOut {
allOut = append(allOut, strings.ReplaceAll(out, "upload_file/", hostname+"/static/"))
}
for _, out := range pathOut {
allOut = append(allOut, strings.ReplaceAll(out, "upload_file/", hostname+"/file/"))
}
return allOut
}
func uploadPage(c *gin.Context) {
c.HTML(200, "upload.tmpl", gin.H{
"page": "upload",
})
}
func listPage(c *gin.Context) {
hostname = c.Request.Host
var showFileOut []string
var outStr string
showFileOut = showFile("upload_file")
for _, out := range showFileOut {
filename := strings.ReplaceAll(out, hostname, "")
outStr += fmt.Sprintf("<a href='//%s' class='item'><i class='file icon'></i> %s</a>", out, filename)
if out != "" {
outStr = fmt.Sprintf("%s", outStr) + "\n"
}
}
if outStr == "" {
outStr = "唉呀!目前沒有任何檔案 嗚嗚"
}
c.HTML(200, "list.tmpl", gin.H{
"showFile": template.HTML(outStr),
"IPAddress": c.ClientIP(),
"page": "list",
})
}
func pageNotAvailable(c *gin.Context) {
c.HTML(404, "404.tmpl", gin.H{
"page": "404",
})
}
func uploadFile(c *gin.Context) {
type Result struct {
Success bool
Message string
File_Name string
}
var r Result
file, header, err := c.Request.FormFile("upload_file")
if err != nil {
r = Result{false, "Bad Request!", ""}
c.JSON(400, r)
return
}
if _, err := os.Stat("upload_file"); os.IsNotExist(err) {
err = os.Mkdir("upload_file", 0755)
if err != nil {
fmt.Println(err)
}
}
nsec := time.Now().UnixNano()
filename := strconv.FormatInt(nsec, 10) + "_" + header.Filename
out, err := os.Create("upload_file/" + filename)
if err != nil {
r = Result{false, "Error!", ""}
c.JSON(400, r)
}
defer out.Close()
_, err = io.Copy(out, file)
if err != nil {
r = Result{false, "Error!", ""}
c.JSON(400, r)
} else {
r = Result{true, "Upload Success!", filename}
c.JSON(201, r)
}
}
func main() {
fmt.Print("\n-------------------")
fmt.Print("\nFile and Folder System")
fmt.Print("\nPort listing at 80")
fmt.Print("\nRepo: https://github.com/steveyiyo/file_folder")
fmt.Print("\nAuthor: SteveYi")
fmt.Print("\n-------------------\n\n")
router := gin.New()
router.Use(gin.Logger(), gin.Recovery())
router.LoadHTMLGlob("static/*")
router.GET("/", indexPage)
router.GET("/list", listPage)
router.GET("/upload", uploadPage)
router.POST("/upload", uploadFile)
router.StaticFS("file/", http.Dir("./upload_file"))
router.NoRoute(pageNotAvailable)
router.Run(":80")
// router.RunTLS(":443", "certificate/ssl.pem", "certificate/ssl.key")
}