英文:
Golang alternative for python/flask send_from_directory()
问题
我有这些图片的URL:
/book/cover/Computer_Science.png
但实际上图片的位置是在
/uploads/img/Computer_Science.png
我正在使用 Gin框架。在Gin或内置的Golang函数中是否有类似Flask的send_from_directory()
命令?
如果没有,你能分享一段代码片段来演示如何实现吗?
谢谢!
英文:
I have this image urls:
/book/cover/Computer_Science.png
but the location of the image actually exists under
/uploads/img/Computer_Science.png
I'm using Gin framework. Is there any command like Flask's send_from_directory()
in Gin or in in-built Golang functions?
If not could you share a snippet of how to do it?
Thanks!
答案1
得分: 2
使用gin的Context.File
方法来提供文件内容。该方法内部调用了http.ServeFile内置函数。代码片段如下:
import "path/filepath"
// ...
router := gin.Default()
// ...
router.GET("/book/cover/:filename", func(c *gin.Context) {
rootDir := "/uploads/img/"
name := c.Param("filename")
filePath, err := filepath.Abs(rootDir + name)
if err != nil {
c.AbortWithStatus(404)
}
// 只允许访问rootDir下的文件/目录
// 以下代码仅供示例,因为HasPrefix已被弃用。
// 修复链接:https://github.com/golang/dep/issues/296
if !filepath.HasPrefix(filePath, rootDir) {
c.AbortWithStatus(404)
}
c.File(filePath)
})
更新
如zerkms所指出,传递给Context.File
的路径名必须经过清理。在代码片段中添加了一个简单的清理器,请根据需要进行调整。
英文:
Use gin's Context.File
to serve file content. This method internally calls http.ServeFile builtin function. The code snippets will be:
import "path/filepath"
// ...
router := gin.Default()
// ...
router.GET("/book/cover/:filename", func(c *gin.Context) {
rootDir := "/uploads/img/"
name := c.Param("filename")
filePath, err := filepath.Abs(rootDir + name)
if err != nil {
c.AbortWithStatus(404)
}
//Only allow access to file/directory under rootDir
//The following code is for ilustration since HasPrefix is deprecated.
//Replace with correct one when https://github.com/golang/dep/issues/296 fixed
if !filepath.HasPrefix(filePath, rootDir) {
c.AbortWithStatus(404)
}
c.File(filePath)
})
Update
As pointed by zerkms, the path name must be sanitized before passing it Context.File
. Simple sanitizer is added in the snippet. Please adapt to your needs.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论