Skip to content

Commit

Permalink
fix: Use url.PathEscape to encode spaces in file names as URL-encoded…
Browse files Browse the repository at this point in the history
… %20 (#18)

* style: GoLand auto-formatting

This commit reflects changes made through GoLand's automatic code
formatting, ensuring consistent and standardized code style throughout
the project.

* fix: Use url.PathEscape to encode spaces in file names as URL-encoded %20

* fix: Decode the 'name' retrieved from the database using url.PathUnescape

Closes #15
  • Loading branch information
JIeJaitt authored Mar 10, 2024
1 parent bca46d2 commit 8108ac3
Show file tree
Hide file tree
Showing 13 changed files with 126 additions and 118 deletions.
16 changes: 8 additions & 8 deletions api/v0/checkpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import (

// CheckPointHandler 断点续传
//
// @Summary 断点续传
// @Description 断点续传
// @Tags 断点续传
// @Accept application/json
// @Param uid query string true "文件uid"
// @Produce application/json
// @Success 200 {object} web.Response{data=[]int}
// @Router /api/storage/v0/checkpoint [get]
// @Summary 断点续传
// @Description 断点续传
// @Tags 断点续传
// @Accept application/json
// @Param uid query string true "文件uid"
// @Produce application/json
// @Success 200 {object} web.Response{data=[]int}
// @Router /api/storage/v0/checkpoint [get]
func CheckPointHandler(c *gin.Context) {
uidStr := c.Query("uid")
uid, err := strconv.ParseInt(uidStr, 10, 64)
Expand Down
30 changes: 15 additions & 15 deletions api/v0/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@ import (

// DownloadHandler 下载数据
//
// @Summary 下载数据
// @Description 下载数据
// @Tags 下载
// @Accept application/json
// @Param uid query string true "文件uid"
// @Param name query string true "文件名称"
// @Param online query string true "是否在线"
// @Param date query string true "链接生成时间"
// @Param expire query string true "过期时间"
// @Param bucket query string true "存储桶"
// @Param object query string true "存储名称"
// @Param signature query string true "签名"
// @Produce application/json
// @Success 200 {object} web.Response
// @Router /api/storage/v0/download [get]
// @Summary 下载数据
// @Description 下载数据
// @Tags 下载
// @Accept application/json
// @Param uid query string true "文件uid"
// @Param name query string true "文件名称"
// @Param online query string true "是否在线"
// @Param date query string true "链接生成时间"
// @Param expire query string true "过期时间"
// @Param bucket query string true "存储桶"
// @Param object query string true "存储名称"
// @Param signature query string true "签名"
// @Produce application/json
// @Success 200 {object} web.Response
// @Router /api/storage/v0/download [get]
func DownloadHandler(c *gin.Context) {
// 校验参数
uidStr := c.Query("uid")
Expand Down
28 changes: 14 additions & 14 deletions api/v0/healthcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ var lgLogger *bootstrap.LangGoLogger

// PingHandler 测试
//
// @Summary 测试接口
// @Description 测试接口
// @Tags 测试
// @Accept application/json
// @Produce application/json
// @Success 200 {object} web.Response
// @Router /api/storage/v0/ping [get]
// @Summary 测试接口
// @Description 测试接口
// @Tags 测试
// @Accept application/json
// @Produce application/json
// @Success 200 {object} web.Response
// @Router /api/storage/v0/ping [get]
func PingHandler(c *gin.Context) {
var lgDB = new(plugins.LangGoDB).Use("default").NewDB()

Expand All @@ -46,13 +46,13 @@ func PingHandler(c *gin.Context) {

// HealthCheckHandler 健康检查
//
// @Summary 健康检查
// @Description 健康检查
// @Tags 检查
// @Accept application/json
// @Produce application/json
// @Success 200 {object} web.Response
// @Router /api/storage/v0/health [get]
// @Summary 健康检查
// @Description 健康检查
// @Tags 检查
// @Accept application/json
// @Produce application/json
// @Success 200 {object} web.Response
// @Router /api/storage/v0/health [get]
func HealthCheckHandler(c *gin.Context) {
web.Success(c, "Health...")
return
Expand Down
32 changes: 16 additions & 16 deletions api/v0/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ import (

// UploadLinkHandler 初始化上传连接
//
// @Summary 初始化上传连接
// @Description 初始化上传连接
// @Tags 链接
// @Accept application/json
// @Param RequestBody body models.GenUpload true "生成上传链接请求体"
// @Produce application/json
// @Success 200 {object} web.Response{data=models.GenUploadResp}
// @Router /api/storage/v0/link/upload [post]
// @Summary 初始化上传连接
// @Description 初始化上传连接
// @Tags 链接
// @Accept application/json
// @Param RequestBody body models.GenUpload true "生成上传链接请求体"
// @Produce application/json
// @Success 200 {object} web.Response{data=models.GenUploadResp}
// @Router /api/storage/v0/link/upload [post]
func UploadLinkHandler(c *gin.Context) {
var genUploadReq models.GenUpload
if err := c.ShouldBindJSON(&genUploadReq); err != nil {
Expand Down Expand Up @@ -98,14 +98,14 @@ func UploadLinkHandler(c *gin.Context) {

// DownloadLinkHandler 获取下载连接
//
// @Summary 获取下载连接
// @Description 获取下载连接
// @Tags 链接
// @Accept application/json
// @Param RequestBody body models.GenDownload true "下载链接请求体"
// @Produce application/json
// @Success 200 {object} web.Response{data=models.GenDownloadResp}
// @Router /api/storage/v0/link/download [post]
// @Summary 获取下载连接
// @Description 获取下载连接
// @Tags 链接
// @Accept application/json
// @Param RequestBody body models.GenDownload true "下载链接请求体"
// @Produce application/json
// @Success 200 {object} web.Response{data=models.GenDownloadResp}
// @Router /api/storage/v0/link/download [post]
func DownloadLinkHandler(c *gin.Context) {
var genDownloadReq models.GenDownload
if err := c.ShouldBindJSON(&genDownloadReq); err != nil {
Expand Down
16 changes: 8 additions & 8 deletions api/v0/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import (

// IsOnCurrentServerHandler .
//
// @Summary 询问文件是否在当前服务
// @Description 询问文件是否在当前服务
// @Tags proxy
// @Accept application/json
// @Param uid query string true "uid"
// @Produce application/json
// @Success 200 {object} web.Response
// @Router /api/storage/v0/proxy [get]
// @Summary 询问文件是否在当前服务
// @Description 询问文件是否在当前服务
// @Tags proxy
// @Accept application/json
// @Param uid query string true "uid"
// @Produce application/json
// @Success 200 {object} web.Response
// @Router /api/storage/v0/proxy [get]
func IsOnCurrentServerHandler(c *gin.Context) {
uidStr := c.Query("uid")
_, err := strconv.ParseInt(uidStr, 10, 64)
Expand Down
16 changes: 8 additions & 8 deletions api/v0/resume.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ import (

// ResumeHandler 秒传&断点续传
//
// @Summary 秒传&断点续传
// @Description 秒传&断点续传
// @Tags 秒传
// @Accept application/json
// @Param RequestBody body models.ResumeReq true "秒传请求体"
// @Produce application/json
// @Success 200 {object} web.Response{data=[]models.ResumeResp}
// @Router /api/storage/v0/resume [post]
// @Summary 秒传&断点续传
// @Description 秒传&断点续传
// @Tags 秒传
// @Accept application/json
// @Param RequestBody body models.ResumeReq true "秒传请求体"
// @Produce application/json
// @Success 200 {object} web.Response{data=[]models.ResumeResp}
// @Router /api/storage/v0/resume [post]
func ResumeHandler(c *gin.Context) {
resumeReq := models.ResumeReq{}
if err := c.ShouldBindJSON(&resumeReq); err != nil {
Expand Down
82 changes: 41 additions & 41 deletions api/v0/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ import (

// UploadSingleHandler 上传单个文件
//
// @Summary 上传单个文件
// @Description 上传单个文件
// @Tags 上传
// @Accept multipart/form-data
// @Param file formData file true "上传的文件"
// @Param uid query string true "文件uid"
// @Param md5 query string true "md5"
// @Param date query string true "链接生成时间"
// @Param expire query string true "过期时间"
// @Param signature query string true "签名"
// @Produce application/json
// @Success 200 {object} web.Response
// @Router /api/storage/v0/upload [put]
// @Summary 上传单个文件
// @Description 上传单个文件
// @Tags 上传
// @Accept multipart/form-data
// @Param file formData file true "上传的文件"
// @Param uid query string true "文件uid"
// @Param md5 query string true "md5"
// @Param date query string true "链接生成时间"
// @Param expire query string true "过期时间"
// @Param signature query string true "签名"
// @Produce application/json
// @Success 200 {object} web.Response
// @Router /api/storage/v0/upload [put]
func UploadSingleHandler(c *gin.Context) {
uidStr := c.Query("uid")
md5 := c.Query("md5")
Expand Down Expand Up @@ -251,20 +251,20 @@ func UploadSingleHandler(c *gin.Context) {

// UploadMultiPartHandler 上传分片文件
//
// @Summary 上传分片文件
// @Description 上传分片文件
// @Tags 上传
// @Accept multipart/form-data
// @Param file formData file true "上传的文件"
// @Param uid query string true "文件uid"
// @Param md5 query string true "md5"
// @Param chunkNum query string true "当前分片id"
// @Param date query string true "链接生成时间"
// @Param expire query string true "过期时间"
// @Param signature query string true "签名"
// @Produce application/json
// @Success 200 {object} web.Response
// @Router /api/storage/v0/upload/multi [put]
// @Summary 上传分片文件
// @Description 上传分片文件
// @Tags 上传
// @Accept multipart/form-data
// @Param file formData file true "上传的文件"
// @Param uid query string true "文件uid"
// @Param md5 query string true "md5"
// @Param chunkNum query string true "当前分片id"
// @Param date query string true "链接生成时间"
// @Param expire query string true "过期时间"
// @Param signature query string true "签名"
// @Produce application/json
// @Success 200 {object} web.Response
// @Router /api/storage/v0/upload/multi [put]
func UploadMultiPartHandler(c *gin.Context) {
uidStr := c.Query("uid")
md5 := c.Query("md5")
Expand Down Expand Up @@ -439,20 +439,20 @@ func UploadMultiPartHandler(c *gin.Context) {

// UploadMergeHandler 合并分片文件
//
// @Summary 合并分片文件
// @Description 合并分片文件
// @Tags 上传
// @Accept multipart/form-data
// @Param uid query string true "文件uid"
// @Param md5 query string true "md5"
// @Param num query string true "总分片数量"
// @Param size query string true "文件总大小"
// @Param date query string true "链接生成时间"
// @Param expire query string true "过期时间"
// @Param signature query string true "签名"
// @Produce application/json
// @Success 200 {object} web.Response
// @Router /api/storage/v0/upload/merge [put]
// @Summary 合并分片文件
// @Description 合并分片文件
// @Tags 上传
// @Accept multipart/form-data
// @Param uid query string true "文件uid"
// @Param md5 query string true "md5"
// @Param num query string true "总分片数量"
// @Param size query string true "文件总大小"
// @Param date query string true "链接生成时间"
// @Param expire query string true "过期时间"
// @Param signature query string true "签名"
// @Produce application/json
// @Success 200 {object} web.Response
// @Router /api/storage/v0/upload/merge [put]
func UploadMergeHandler(c *gin.Context) {
uidStr := c.Query("uid")
md5 := c.Query("md5")
Expand Down
7 changes: 6 additions & 1 deletion app/pkg/base/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/qinguoyi/osproxy/app/pkg/utils"
"github.com/qinguoyi/osproxy/bootstrap"
"github.com/qinguoyi/osproxy/bootstrap/plugins"
"net/url"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -87,6 +88,7 @@ func GenUploadSingle(filename string, expire int, respChan chan models.GenUpload
}
uidStr := strconv.FormatInt(uid, 10)
name := filepath.Base(filename)
name = url.PathEscape(name)
storageName := fmt.Sprintf("%s.%s", uidStr, GetExtension(filename))
objectName := fmt.Sprintf("%s/%s", bucket, storageName)

Expand Down Expand Up @@ -136,7 +138,10 @@ func GenDownloadSingle(meta models.MetaDataInfo, expire string, respChan chan mo
defer wg.Done()
uid := meta.UID
bucketName := meta.Bucket
srcName := meta.Name
srcName, err := url.PathUnescape(meta.Name)
if err != nil {
fmt.Println("文件名解码失败:", err)
}
objectName := meta.StorageName

// 生成加密query
Expand Down
2 changes: 1 addition & 1 deletion app/pkg/utils/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const (
WorkID = "workId"
LinkLimit = 50
EncryKey = "*&^@#$storage"
LocalStore = "/storage/localstore"
LocalStore = "localstore"
ServiceRedisPrefix = "service:proxy"
ServiceRedisTTl = time.Second * 3 * 60
S3StoragePutThreadNum = 10
Expand Down
10 changes: 5 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (
"github.com/qinguoyi/osproxy/bootstrap/plugins"
)

// @title ObjectStorageProxy
// @version 1.0
// @title ObjectStorageProxy
// @version 1.0
// @description
// @contact.name qinguoyi
// @host 127.0.0.1:8888
// @BasePath /
// @contact.name qinguoyi
// @host 127.0.0.1:8888
// @BasePath /
func main() {
// config log
lgConfig := bootstrap.NewConfig("conf/config.yaml")
Expand Down
Binary file added test/7620 65921.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions test/example file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is example file with block of text
4 changes: 3 additions & 1 deletion test/httptest.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ func minH(a, b int64) int64 {
func main() {
// 基础信息
baseUrl := "http://127.0.0.1:8888"
uploadFilePath := "./xxx.jpg"
uploadFilePath := "test/example file??@$.txt"
// uploadFilePath := "test/7620 65921.jpg"
//uploadFilePath := "test/xxx.jpg"
uploadFile := filepath.Base(uploadFilePath)

// ##################### 获取上传连接 ###################
Expand Down

0 comments on commit 8108ac3

Please sign in to comment.