-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from msfidelis/feature/filesystem
Feature: fs operation
- Loading branch information
Showing
6 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package filesystem | ||
|
||
import ( | ||
"net/http" | ||
"os" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
type RequestCat struct { | ||
Path string `json:"path"` | ||
} | ||
|
||
func Cat(c *gin.Context) { | ||
var request RequestCat | ||
|
||
if err := c.ShouldBindJSON(&request); err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
content, err := os.ReadFile(request.Path) | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
c.String(http.StatusOK, string(content)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package filesystem | ||
|
||
import ( | ||
"net/http" | ||
"os" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
type RequestDelete struct { | ||
Path string `json:"path"` | ||
} | ||
|
||
func DeleteFile(c *gin.Context) { | ||
var request RequestDelete | ||
if err := c.ShouldBindJSON(&request); err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
if err := os.Remove(request.Path); err != nil { | ||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
c.JSON(http.StatusOK, gin.H{"message": "file deleted"}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package filesystem | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
type RequestLs struct { | ||
Path string `json:"path"` | ||
} | ||
|
||
type ResponseLs struct { | ||
Path string `json:"path"` | ||
Files []string `json:"files"` | ||
} | ||
|
||
func Ls(c *gin.Context) { | ||
|
||
var request RequestLs | ||
var response ResponseLs | ||
|
||
if err := c.ShouldBindJSON(&request); err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
files, err := os.ReadDir(request.Path) | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
var fileNames []string | ||
for _, file := range files { | ||
fileNames = append(fileNames, file.Name()) | ||
} | ||
|
||
fmt.Println(files) | ||
|
||
response.Path = request.Path | ||
response.Files = fileNames | ||
|
||
c.JSON(200, response) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package filesystem | ||
|
||
import ( | ||
"encoding/base64" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
type RequestWrite struct { | ||
Path string `json:"path"` | ||
Content string `json:"content"` | ||
} | ||
|
||
func WriteFile(c *gin.Context) { | ||
var request RequestWrite | ||
|
||
if err := c.ShouldBindJSON(&request); err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
decodedContent, err := base64.StdEncoding.DecodeString(request.Content) | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid base64 content"}) | ||
return | ||
} | ||
|
||
err = os.WriteFile(request.Path, decodedContent, 0644) | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusAccepted, gin.H{"message": "file written", "path": request.Path}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters