Browse Source

enhance(nginx-log): do not seek file if it size is 0 #674

Jacky 8 months ago
parent
commit
cb5c64dec4
1 changed files with 10 additions and 5 deletions
  1. 10 5
      api/nginx/nginx_log.go

+ 10 - 5
api/nginx/nginx_log.go

@@ -49,7 +49,6 @@ func GetNginxLogPage(c *gin.Context) {
 	}
 
 	logPath, err := getLogPath(&control)
-
 	if err != nil {
 		c.JSON(http.StatusInternalServerError, nginxLogPageResp{
 			Error: err.Error(),
@@ -59,7 +58,6 @@ func GetNginxLogPage(c *gin.Context) {
 	}
 
 	logFileStat, err := os.Stat(logPath)
-
 	if err != nil {
 		c.JSON(http.StatusInternalServerError, nginxLogPageResp{
 			Error: err.Error(),
@@ -76,8 +74,16 @@ func GetNginxLogPage(c *gin.Context) {
 		return
 	}
 
-	f, err := os.Open(logPath)
+	// to fix: seek invalid argument #674
+	if logFileStat.Size() == 0 {
+		c.JSON(http.StatusOK, nginxLogPageResp{
+			Page:    1,
+			Content: "",
+		})
+		return
+	}
 
+	f, err := os.Open(logPath)
 	if err != nil {
 		c.JSON(http.StatusInternalServerError, nginxLogPageResp{
 			Error: err.Error(),
@@ -112,8 +118,7 @@ func GetNginxLogPage(c *gin.Context) {
 	}
 
 	n, err := f.Read(buf)
-
-	if err != nil && err != io.EOF {
+	if err != nil && !errors.Is(err, io.EOF) {
 		c.JSON(http.StatusInternalServerError, nginxLogPageResp{
 			Error: err.Error(),
 		})