diff --git a/go.mod b/go.mod index 3b08af6..f239534 100644 --- a/go.mod +++ b/go.mod @@ -23,6 +23,7 @@ require ( github.com/google/go-github/v36 v36.0.0 github.com/googollee/go-socket.io v1.6.2 github.com/gorilla/websocket v1.5.0 + github.com/h2non/filetype v1.1.3 github.com/hirochachacha/go-smb2 v1.1.0 github.com/json-iterator/go v1.1.12 github.com/labstack/echo/v4 v4.10.0 diff --git a/go.sum b/go.sum index 7b51bff..733bad2 100644 --- a/go.sum +++ b/go.sum @@ -137,6 +137,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.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= 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/go.mod h1:8F1A4d5EZzrGu5R7PU163UcMRDJQl4FtcxjBfsY8TZE= github.com/ianlancetaylor/demangle v0.0.0-20220319035150-800ac71e25c2/go.mod h1:aYm2/VgdVmcIU8iMfdMvDMsRAQjcfZSKFby6HOFvi/w= diff --git a/route/v1/file.go b/route/v1/file.go index e5cd49c..efec489 100644 --- a/route/v1/file.go +++ b/route/v1/file.go @@ -30,6 +30,8 @@ import ( "github.com/gin-gonic/gin" uuid "github.com/satori/go.uuid" "go.uber.org/zap" + + "github.com/h2non/filetype" ) // @Summary 读取文件 @@ -201,33 +203,32 @@ func GetDownloadSingleFile(c *gin.Context) { // c.Header("Content-Disposition", "inline") 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 - - } - 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 - } + fi, err := os.Open(filePath) + if err != nil { + panic(err) } + // 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) if err != nil { c.JSON(common_err.SERVICE_ERROR, model.Result{ @@ -638,12 +639,18 @@ func PutFileContent(c *gin.Context) { return } // 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 { 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 } - 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 { 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