-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
212 lines (175 loc) · 5.04 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"net/http"
"os"
"strconv"
"time"
"github.com/gin-gonic/gin"
)
type FileMetadata struct {
Type string `json:"type"`
Name string `json:"name"`
Size int64 `json:"size"`
Modified time.Time `json:"modified"`
}
func main() {
r := gin.Default()
files := r.Group("/files")
{
files.GET("/contents", getServerFileContents)
files.GET("/list-directory", getServerListDirectory)
files.PUT("/rename", putServerRenameFiles)
files.POST("/copy", postServerCopyFile)
files.POST("/write", postServerWriteFile)
files.POST("/delete", postServerDeleteFiles)
files.POST("/compress", postServerCompressFiles)
files.POST("/decompress", postServerDecompressFiles)
files.POST("/chmod", postServerChmodFile)
}
r.Run() // listen and serve on 0.0.0.0:8080
}
// Handler for getting file contents
func getServerFileContents(c *gin.Context) {
path := c.Query("path")
if path == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Path query parameter is required"})
return
}
content, err := os.ReadFile(path)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"content": string(content)})
}
// Handler for listing directory contents
func getServerListDirectory(c *gin.Context) {
path := c.Query("path")
if path == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Path query parameter is required"})
return
}
files, err := os.ReadDir(path)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
var fileMetadata []FileMetadata
for _, file := range files {
//filePath := filepath.Join(path, file.Name())
// Get file info
fileInfo, err := file.Info()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
// Determine file type
fileType := "file"
if fileInfo.IsDir() {
fileType = "directory"
}
// Format size
size := fileInfo.Size()
// Create FileMetadata object
meta := FileMetadata{
Type: fileType,
Name: file.Name(),
Size: size,
Modified: fileInfo.ModTime(),
}
fileMetadata = append(fileMetadata, meta)
}
c.JSON(http.StatusOK, gin.H{"files": fileMetadata})
}
// Handler for renaming files
func putServerRenameFiles(c *gin.Context) {
oldPath := c.Query("old_path")
newPath := c.Query("new_path")
if oldPath == "" || newPath == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Old path and new path query parameters are required"})
return
}
err := os.Rename(oldPath, newPath)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"message": "File renamed successfully"})
}
// Handler for copying files
func postServerCopyFile(c *gin.Context) {
src := c.Query("src")
dest := c.Query("dest")
if src == "" || dest == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Source and destination query parameters are required"})
return
}
input, err := os.ReadFile(src)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
err = os.WriteFile(dest, input, 0644)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"message": "File copied successfully"})
}
// Handler for writing files
func postServerWriteFile(c *gin.Context) {
path := c.Query("path")
content := c.PostForm("content")
if path == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Path query parameter is required"})
return
}
err := os.WriteFile(path, []byte(content), 0644)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"message": "File written successfully"})
}
// Handler for deleting files
func postServerDeleteFiles(c *gin.Context) {
path := c.Query("path")
if path == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Path query parameter is required"})
return
}
err := os.Remove(path)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"message": "File deleted successfully"})
}
// Handler for compressing files
func postServerCompressFiles(c *gin.Context) {
// Implement your compression logic here
}
// Handler for decompressing files
func postServerDecompressFiles(c *gin.Context) {
// Implement your decompression logic here
}
// Handler for changing file permissions
func postServerChmodFile(c *gin.Context) {
path := c.Query("path")
mode := c.Query("mode")
if path == "" || mode == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Path and mode query parameters are required"})
return
}
parsedMode, err := strconv.ParseUint(mode, 8, 32)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid mode"})
return
}
err = os.Chmod(path, os.FileMode(parsedMode))
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"message": "File permissions changed successfully"})
}