Browse Source

Modify file permissions (#860)

link 2 years ago
parent
commit
7501833cf9
3 changed files with 36 additions and 26 deletions
  1. 1 0
      go.mod
  2. 2 0
      go.sum
  3. 33 26
      route/v1/file.go

+ 1 - 0
go.mod

@@ -23,6 +23,7 @@ require (
 	github.com/google/go-github/v36 v36.0.0
 	github.com/google/go-github/v36 v36.0.0
 	github.com/googollee/go-socket.io v1.6.2
 	github.com/googollee/go-socket.io v1.6.2
 	github.com/gorilla/websocket v1.5.0
 	github.com/gorilla/websocket v1.5.0
+	github.com/h2non/filetype v1.1.3
 	github.com/hirochachacha/go-smb2 v1.1.0
 	github.com/hirochachacha/go-smb2 v1.1.0
 	github.com/json-iterator/go v1.1.12
 	github.com/json-iterator/go v1.1.12
 	github.com/labstack/echo/v4 v4.10.0
 	github.com/labstack/echo/v4 v4.10.0

+ 2 - 0
go.sum

@@ -141,6 +141,8 @@ github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB7
 github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
 github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
 github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
 github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
 github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
 github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
+github.com/h2non/filetype v1.1.3 h1:FKkx9QbD7HR/zjK1Ia5XiBsq9zdLi5Kf3zGyFTAFkGg=
+github.com/h2non/filetype v1.1.3/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY=
 github.com/hirochachacha/go-smb2 v1.1.0 h1:b6hs9qKIql9eVXAiN0M2wSFY5xnhbHAQoCwRKbaRTZI=
 github.com/hirochachacha/go-smb2 v1.1.0 h1:b6hs9qKIql9eVXAiN0M2wSFY5xnhbHAQoCwRKbaRTZI=
 github.com/hirochachacha/go-smb2 v1.1.0/go.mod h1:8F1A4d5EZzrGu5R7PU163UcMRDJQl4FtcxjBfsY8TZE=
 github.com/hirochachacha/go-smb2 v1.1.0/go.mod h1:8F1A4d5EZzrGu5R7PU163UcMRDJQl4FtcxjBfsY8TZE=
 github.com/ianlancetaylor/demangle v0.0.0-20220319035150-800ac71e25c2/go.mod h1:aYm2/VgdVmcIU8iMfdMvDMsRAQjcfZSKFby6HOFvi/w=
 github.com/ianlancetaylor/demangle v0.0.0-20220319035150-800ac71e25c2/go.mod h1:aYm2/VgdVmcIU8iMfdMvDMsRAQjcfZSKFby6HOFvi/w=

+ 33 - 26
route/v1/file.go

@@ -30,6 +30,8 @@ import (
 	"github.com/gin-gonic/gin"
 	"github.com/gin-gonic/gin"
 	uuid "github.com/satori/go.uuid"
 	uuid "github.com/satori/go.uuid"
 	"go.uber.org/zap"
 	"go.uber.org/zap"
+
+	"github.com/h2non/filetype"
 )
 )
 
 
 // @Summary 读取文件
 // @Summary 读取文件
@@ -201,33 +203,32 @@ func GetDownloadSingleFile(c *gin.Context) {
 	// c.Header("Content-Disposition", "inline")
 	// c.Header("Content-Disposition", "inline")
 	c.Header("Content-Disposition", "attachment; filename*=utf-8''"+url2.PathEscape(fileName))
 	c.Header("Content-Disposition", "attachment; filename*=utf-8''"+url2.PathEscape(fileName))
 
 
-	storage, _ := service.MyService.FsService().GetStorage(filePath)
-	if storage != nil {
-		if shouldProxy(storage, fileName) {
-			Proxy(c)
-			return
-		} else {
-			link, _, err := service.MyService.FsService().Link(c, filePath, model.LinkArgs{
-				IP:     c.ClientIP(),
-				Header: c.Request.Header,
-				Type:   c.Query("type"),
-			})
-			if err != nil {
-				c.JSON(common_err.SERVICE_ERROR, model.Result{
-					Success: common_err.SERVICE_ERROR,
-					Message: common_err.GetMsg(common_err.SERVICE_ERROR),
-					Data:    err.Error(),
-				})
-				return
+	fi, err := os.Open(filePath)
+	if err != nil {
+		panic(err)
+	}
 
 
-			}
-			c.Header("Referrer-Policy", "no-referrer")
-			c.Header("Cache-Control", "max-age=0, no-cache, no-store, must-revalidate")
-			c.Redirect(302, link.URL)
-			return
-		}
+	// We only have to pass the file header = first 261 bytes
+	buffer := make([]byte, 261)
+
+	_, _ = fi.Read(buffer)
+
+	kind, _ := filetype.Match(buffer)
+	if kind != filetype.Unknown {
+		c.Header("Content-Type", kind.MIME.Value)
 	}
 	}
+	node, err := os.Stat(filePath)
+	// Set the Last-Modified header to the timestamp
+	c.Header("Last-Modified", node.ModTime().UTC().Format(http.TimeFormat))
 
 
+	knownSize := node.Size() >= 0
+	if knownSize {
+		c.Header("Content-Length", strconv.FormatInt(node.Size(), 10))
+	}
+	http.ServeContent(c.Writer, c.Request, fileName, node.ModTime(), fi)
+	//http.ServeFile(c.Writer, c.Request, filePath)
+	defer fi.Close()
+	return
 	fileTmp, err := os.Open(filePath)
 	fileTmp, err := os.Open(filePath)
 	if err != nil {
 	if err != nil {
 		c.JSON(common_err.SERVICE_ERROR, model.Result{
 		c.JSON(common_err.SERVICE_ERROR, model.Result{
@@ -638,12 +639,18 @@ func PutFileContent(c *gin.Context) {
 		return
 		return
 	}
 	}
 	// err := os.Remove(path)
 	// err := os.Remove(path)
-	err := os.RemoveAll(fi.FilePath)
+	f, err := os.Stat(fi.FilePath)
+	if err != nil {
+		c.JSON(common_err.SERVICE_ERROR, model.Result{Success: common_err.FILE_ALREADY_EXISTS, Message: common_err.GetMsg(common_err.FILE_ALREADY_EXISTS)})
+		return
+	}
+	fm := f.Mode()
 	if err != nil {
 	if err != nil {
 		c.JSON(common_err.SERVICE_ERROR, model.Result{Success: common_err.FILE_DELETE_ERROR, Message: common_err.GetMsg(common_err.FILE_DELETE_ERROR), Data: err})
 		c.JSON(common_err.SERVICE_ERROR, model.Result{Success: common_err.FILE_DELETE_ERROR, Message: common_err.GetMsg(common_err.FILE_DELETE_ERROR), Data: err})
 		return
 		return
 	}
 	}
-	err = file.CreateFileAndWriteContent(fi.FilePath, fi.FileContent)
+	os.OpenFile(fi.FilePath, os.O_CREATE, fm)
+	err = file.WriteToFullPath([]byte(fi.FileContent), fi.FilePath, fm)
 	if err != nil {
 	if err != nil {
 		c.JSON(common_err.SERVICE_ERROR, model.Result{Success: common_err.SERVICE_ERROR, Message: common_err.GetMsg(common_err.SERVICE_ERROR), Data: err.Error()})
 		c.JSON(common_err.SERVICE_ERROR, model.Result{Success: common_err.SERVICE_ERROR, Message: common_err.GetMsg(common_err.SERVICE_ERROR), Data: err.Error()})
 		return
 		return