Skip to content

Commit c4ddf3d

Browse files
committed
clean up code, do real delete when click delete, update style set content width
1 parent d772e6f commit c4ddf3d

File tree

4 files changed

+90
-76
lines changed

4 files changed

+90
-76
lines changed

cmd.go

-54
This file was deleted.

http.go

+25-13
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,17 @@ import (
1515
//go:embed web
1616
var webDir embed.FS
1717

18+
// tmpFile struct
1819
type tmpFile struct {
19-
T string `json:"t"`
20-
N string `json:"n"`
21-
Ns string `json:"ns"`
20+
T string `json:"t"` // 0: normal file
21+
N string `json:"n"` // file url
22+
Ns string `json:"ns"` // file name
2223
}
2324

2425
var (
2526
sData string
26-
imgList []string
27-
fileList []tmpFile
27+
imgList []string // img url list
28+
fileList []tmpFile // file list
2829
)
2930

3031
func runServer() error {
@@ -37,14 +38,14 @@ func runServer() error {
3738

3839
setRouter(r)
3940

40-
// Set memory limit for multipart forms (default is 32 MiB)
41-
r.MaxMultipartMemory = 50 << 20
41+
loadOldFiles()
42+
43+
// Set memory limit for multipart forms
44+
r.MaxMultipartMemory = 20 << 20 // 20 MiB
4245

4346
ads := address + ":" + port
4447
fmt.Println("server started at http://" + ads)
4548

46-
loadOldFiles()
47-
4849
return r.Run(ads)
4950

5051
}
@@ -56,19 +57,21 @@ func setRouter(r *gin.Engine) {
5657

5758
r.StaticFS("/static", http.FS(webDir))
5859
r.GET("/", func(c *gin.Context) {
59-
c.HTML(http.StatusOK, "index.html", gin.H{"data": sData, "imaList": imgList, "fileList": fileList})
60+
c.HTML(http.StatusOK, "index.html", gin.H{"data": sData, "imgList": imgList, "fileList": fileList})
6061
})
6162

6263
r.POST("/", func(c *gin.Context) {
6364
sData = c.PostForm("sData")
64-
c.HTML(http.StatusOK, "index.html", gin.H{"data": sData, "imaList": imgList, "fileList": fileList})
65+
c.HTML(http.StatusOK, "index.html", gin.H{"data": sData, "imgList": imgList, "fileList": fileList})
6566
})
6667

6768
r.POST("/clearFile", func(c *gin.Context) {
69+
clearTmpFile()
6870
fileList = nil
6971
imgList = nil
70-
c.HTML(http.StatusOK, "index.html", gin.H{"data": sData, "imaList": imgList, "fileList": fileList})
72+
c.HTML(http.StatusOK, "index.html", gin.H{"data": sData, "imgList": imgList, "fileList": fileList})
7173
})
74+
7275
r.POST("/upload", func(c *gin.Context) {
7376
// Single file
7477
file, err := c.FormFile("file")
@@ -77,7 +80,6 @@ func setRouter(r *gin.Engine) {
7780
c.String(http.StatusBadRequest, err.Error())
7881
}
7982

80-
// saveName := strconv.FormatInt(time.Now().UnixNano(), 10) + path.Ext(file.Filename)
8183
saveName := file.Filename
8284

8385
err = c.SaveUploadedFile(file, tmpFileDir+"/"+saveName)
@@ -119,7 +121,17 @@ func loadOldFiles() {
119121
fileList = append(fileList, tmpFile{T: "0", N: fileUrl, Ns: f.Name()})
120122
}
121123
}
124+
}
122125

126+
func clearTmpFile() {
127+
err := os.RemoveAll(tmpFileDir)
128+
if err != nil {
129+
log.Fatal(err)
130+
}
131+
err = os.MkdirAll(tmpFileDir, os.ModePerm)
132+
if err != nil {
133+
log.Fatal(err)
134+
}
123135
}
124136

125137
func isImgSimple(name string) bool {

main.go

+53
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,58 @@
11
package main
22

3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
8+
"github.com/spf13/cobra"
9+
)
10+
11+
var (
12+
address string
13+
port string
14+
tmpFileDir string
15+
)
16+
17+
func init() {
18+
19+
rootCmd.PersistentFlags().StringVarP(&port, "port", "p", "7777", "listen port")
20+
rootCmd.PersistentFlags().StringVarP(&address, "address", "a", "127.0.0.1", "listen address")
21+
rootCmd.PersistentFlags().StringVarP(&tmpFileDir, "folder", "f", "tmpFile", "tmp file folder")
22+
23+
}
24+
25+
var rootCmd = &cobra.Command{
26+
Use: "simpleshare",
27+
Short: "Share texts and files in local network",
28+
Long: `
29+
A Simple http service for share texts and files in local network
30+
built in Go.
31+
source: https://github.com/keller0/simpleshare`,
32+
CompletionOptions: cobra.CompletionOptions{HiddenDefaultCmd: true},
33+
Version: "0.0.2",
34+
Example: "./simpleshare -a 127.0.0.1 -p 7777 -f tmpFile",
35+
Run: func(cmd *cobra.Command, args []string) {
36+
tDir := filepath.Join(".", tmpFileDir)
37+
err := os.MkdirAll(tDir, os.ModePerm)
38+
if err != nil {
39+
panic(err)
40+
}
41+
fmt.Println("tmp file folder:", tDir)
42+
err = runServer()
43+
if err != nil {
44+
fmt.Println(err)
45+
}
46+
},
47+
}
48+
49+
func Execute() {
50+
if err := rootCmd.Execute(); err != nil {
51+
fmt.Println(err)
52+
os.Exit(1)
53+
}
54+
}
55+
356
func main() {
457
Execute()
558
}

web/index.html

+12-9
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111

1212
<div style="text-align: center;max-width: 920px; margin: auto">
1313
<div><p style="font-weight: bold;">Share texts and files in your local network</p></div>
14-
<textarea name="" id="" cols="100" rows="15">{{ .data }}</textarea>
14+
<textarea name="" id="showDataArea" cols="100" rows="15">{{ .data }}</textarea>
1515
<div id="div-mobile" class="up-box">
1616
<!-- <input name="photo" type="file" id="upLoadOneFile" accept="image/*"> -->
1717
<input name="photo" type="file" id="upLoadOneFile" multiple onchange="handleSelectFilesChange(this)" >
1818
</div>
1919
<form action="/" method="POST" id="dForm">
20-
<textarea name="sData" id="sData" cols="100" rows="4"
20+
<textarea name="sData" id="inputDataArea" cols="100" rows="4"
2121
placeholder="Input text and submit or drop files on the page"></textarea>
2222
<br>
2323
</form>
@@ -38,7 +38,7 @@
3838
</div>
3939
<br>
4040
<div id="imgList">
41-
{{ range .imaList }}
41+
{{ range .imgList }}
4242
<img style="width: 300px; padding: 5px" src="{{ . }}" alt="">
4343
{{ end }}
4444
</div>
@@ -57,9 +57,18 @@
5757
max-width: 95%;
5858
resize:vertical;
5959
}
60+
.progress-bar {
61+
width: 90%;
62+
margin: auto;
63+
background-color: #ddd;
64+
}
6065
@media screen and (min-width: 769px) {
6166
#div-mobile { display: none; }
6267
#div-desktop { display: block; }
68+
69+
#inputDataArea {width: 700px;}
70+
#showDataArea {width: 700px;}
71+
.progress-bar{width: 700px;}
6372
}
6473

6574
@media screen and (max-width: 768px) {
@@ -70,12 +79,6 @@
7079
margin: 5px;
7180
text-align: left;
7281
}
73-
74-
.progress-bar {
75-
width: 90%;
76-
margin: auto;
77-
background-color: #ddd;
78-
}
7982
#progress {
8083
width: 0%;
8184
height: 1em;

0 commit comments

Comments
 (0)