-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
68 lines (62 loc) · 1.69 KB
/
api.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
package main
import (
"io/ioutil"
"net/http"
"os"
"path/filepath"
"github.com/gin-gonic/gin"
)
func apiFileList(c *gin.Context) {
files, err := ioutil.ReadDir(getDocumentDir())
if err != nil {
c.JSON(400, gin.H{
"msg": err,
})
} else {
c.JSON(200, gin.H{
"files": createFileList(files),
})
}
}
func apiReadFileStructured(c *gin.Context) {
fileName := c.Param("path")
filePath := filepath.Join(getDocumentDir(), fileName)
result, err := readIniFile(filePath)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"content": result, "error": err})
} else {
c.JSON(http.StatusOK, gin.H{"content": result, "error": err})
}
}
func apiReadFileRaw(c *gin.Context) {
fileName := c.Param("path")
filePath := filepath.Join(getDocumentDir(), fileName)
file, err := ioutil.ReadFile(filePath)
if err != nil {
c.String(http.StatusNotFound, "No file found named %s.", filePath)
} else {
c.String(http.StatusOK, string(file))
}
}
func apiCloneFile(c *gin.Context) {
type Clone struct {
NewName string `form:"newName" json:"newName" xml:"newName" binding:"required"`
}
var json Clone
if err := c.ShouldBindJSON(&json); err != nil {
c.String(http.StatusBadRequest, "Error: Bad parse of data, fill the field 'newName'")
return
}
documentDir := getDocumentDir()
originalFileName := c.Param("path")
newFileName := filepath.Join(documentDir, json.NewName)
filePath := filepath.Join(documentDir, originalFileName)
file, err := ioutil.ReadFile(filePath)
if err != nil {
c.String(http.StatusNotFound, "No file found named %s.", filePath)
return
}
info, _ := os.Stat(originalFileName)
ioutil.WriteFile(newFileName, file, info.Mode())
c.String(http.StatusOK, string(file))
}