Skip to content

Commit a1354b3

Browse files
Fail when trying to edit a Directory like if it was File content (#78)
* Fail when trying to edit a `Directory` like if it was `File` content * use error.Is for file does not exist and directory does not exist * Use the same messages as in the asFilesystemError function --------- Co-authored-by: Quinten <67589015+QuintenQVD0@users.noreply.github.com>
1 parent 0566b83 commit a1354b3

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

router/router_server_files.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,15 @@ func getServerFileContents(c *gin.Context) {
3737
}
3838
f, st, err := s.Filesystem().File(p)
3939
if err != nil {
40-
if strings.Contains(err.Error(), "file does not exist") {
40+
if errors.Is(err, os.ErrNotExist) {
4141
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{
4242
"error": "The requested resources was not found on the system.",
4343
"request_id": c.Writer.Header().Get("X-Request-Id")})
44+
} else if strings.Contains(err.Error(), "filesystem: is a directory") {
45+
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
46+
"error": "Cannot perform that action: file is a directory.",
47+
"request_id": c.Writer.Header().Get("X-Request-Id"),
48+
})
4449
} else {
4550
middleware.CaptureAndAbort(c, err)
4651
}
@@ -89,6 +94,13 @@ func getServerListDirectory(c *gin.Context) {
8994
s := middleware.ExtractServer(c)
9095
dir := c.Query("directory")
9196
if stats, err := s.Filesystem().ListDirectory(dir); err != nil {
97+
// If the error is that the folder does not exist return a 404.
98+
if errors.Is(err, os.ErrNotExist) {
99+
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{
100+
"error": "The requested directory was not found on the server.",
101+
})
102+
return
103+
}
92104
middleware.CaptureAndAbort(c, err)
93105
} else {
94106
c.JSON(http.StatusOK, stats)

server/filesystem/filesystem.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ func (fs *Filesystem) File(p string) (ufs.File, Stat, error) {
8282
_ = f.Close()
8383
return nil, Stat{}, err
8484
}
85+
if st.IsDir() {
86+
return f, Stat{}, errors.New("filesystem: is a directory")
87+
}
8588
return f, st, nil
8689
}
8790

0 commit comments

Comments
 (0)