init: 0.0.6

next: upload
This commit is contained in:
loveuer
2025-04-28 14:59:22 +08:00
parent 4801b3de08
commit 0cfd617922
19 changed files with 713 additions and 355 deletions

42
internal/handler/share.go Normal file
View File

@ -0,0 +1,42 @@
package handler
import (
"fmt"
"github.com/loveuer/nf"
"github.com/loveuer/ushare/internal/model"
"github.com/loveuer/ushare/internal/opt"
"github.com/pkg/errors"
"github.com/spf13/viper"
"net/http"
"os"
)
func Fetch() nf.HandlerFunc {
return func(c *nf.Ctx) error {
code := c.Query("code")
info := new(model.Meta)
_, err := os.Stat(opt.MetaPath(code))
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return c.Status(http.StatusNotFound).JSON(map[string]string{"msg": "文件不存在"})
}
return c.SendStatus(http.StatusInternalServerError)
}
viper.SetConfigFile(opt.MetaPath(code))
viper.SetConfigType("env")
if err = viper.ReadInConfig(); err != nil {
return c.SendStatus(http.StatusInternalServerError)
}
if err = viper.Unmarshal(info); err != nil {
return c.SendStatus(http.StatusInternalServerError)
}
c.SetHeader("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, info.Filename))
http.ServeFile(c.Writer, c.Request, opt.FilePath(code))
return nil
}
}