From 871906808ce54005b8acdc9a184b9385f7459b7f Mon Sep 17 00:00:00 2001 From: Miao_Spring <539943419@qq.com> Date: Fri, 23 Aug 2024 19:42:35 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=BA=86=E5=BD=93?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=90=8D=E4=B8=BAindex.html=E6=97=B6?= =?UTF-8?q?=E8=A2=ABgolang=E8=B4=B4=E5=BF=83=E8=87=AA=E5=8A=A8=E9=87=8D?= =?UTF-8?q?=E5=AE=9A=E5=90=91=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/getHandler.go | 112 +++++++++++++++++++++++++--------------------- 1 file changed, 62 insertions(+), 50 deletions(-) diff --git a/api/getHandler.go b/api/getHandler.go index 3d79845..34e6776 100644 --- a/api/getHandler.go +++ b/api/getHandler.go @@ -1,59 +1,71 @@ package api import ( - "github.com/gin-gonic/gin" - "net/http" - "os" - "path/filepath" - "io/ioutil" + "io/ioutil" + "net/http" + "os" + "path/filepath" + "strings" + "time" + + "github.com/gin-gonic/gin" ) - func GetHandler(c *gin.Context) { - decodedPath, err := filepath.Abs(filepath.Join(staticPath, c.Param("path"))) - if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to access path"}) - return - } + decodedPath, err := filepath.Abs(filepath.Join(staticPath, c.Param("path"))) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to access path"}) + return + } - fileInfo, err := os.Stat(decodedPath) - if os.IsNotExist(err) { - c.JSON(http.StatusNotFound, gin.H{"error": "Path not found"}) - return - } + fileInfo, err := os.Stat(decodedPath) + if os.IsNotExist(err) { + c.JSON(http.StatusNotFound, gin.H{"error": "Path not found"}) + return + } - if fileInfo.IsDir() { - files, err := ioutil.ReadDir(decodedPath) - if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to scan directory"}) - return - } + if fileInfo.IsDir() { + files, err := ioutil.ReadDir(decodedPath) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to scan directory"}) + return + } - var fileDetails []gin.H - for _, file := range files { - fileDetails = append(fileDetails, gin.H{ - "name": file.Name(), - "size": file.Size(), - "type": func() string { - if file.IsDir() { - return "directory" - } - return "file" - }(), - "stats": gin.H{ - "atimeMs": file.ModTime().UnixNano() / 1e6, - "birthtimeMs": file.ModTime().UnixNano() / 1e6, - "ctimeMs": file.ModTime().UnixNano() / 1e6, - "mtimeMs": file.ModTime().UnixNano() / 1e6, - }, - }) - } - if len(fileDetails) == 0 { - c.JSON(http.StatusOK, []gin.H{}) - } else { - c.JSON(http.StatusOK, fileDetails) - } - } else { - c.File(decodedPath) - } -} \ No newline at end of file + var fileDetails []gin.H + for _, file := range files { + fileDetails = append(fileDetails, gin.H{ + "name": file.Name(), + "size": file.Size(), + "type": func() string { + if file.IsDir() { + return "directory" + } + return "file" + }(), + "stats": gin.H{ + "atimeMs": file.ModTime().UnixNano() / 1e6, + "birthtimeMs": file.ModTime().UnixNano() / 1e6, + "ctimeMs": file.ModTime().UnixNano() / 1e6, + "mtimeMs": file.ModTime().UnixNano() / 1e6, + }, + }) + } + if len(fileDetails) == 0 { + c.JSON(http.StatusOK, []gin.H{}) + } else { + c.JSON(http.StatusOK, fileDetails) + } + } else { + if strings.HasSuffix(decodedPath, "index.html") { + f, err := os.Open(decodedPath) + if err != nil { + c.Writer.WriteHeader(http.StatusInternalServerError) + c.Writer.Write([]byte("Internal Server Error")) + return + } + http.ServeContent(c.Writer, c.Request, "index.html", time.Now(), f) + } else { + c.File(decodedPath) + } + } +}