init: 0.0.6
next: upload
This commit is contained in:
@ -5,6 +5,7 @@ import (
|
||||
"github.com/loveuer/nf"
|
||||
"github.com/loveuer/nf/nft/log"
|
||||
"github.com/loveuer/nf/nft/tool"
|
||||
"github.com/loveuer/ushare/internal/handler"
|
||||
"github.com/loveuer/ushare/internal/opt"
|
||||
"net"
|
||||
"net/http"
|
||||
@ -17,6 +18,8 @@ func Start(ctx context.Context) <-chan struct{} {
|
||||
return c.SendStatus(http.StatusOK)
|
||||
})
|
||||
|
||||
app.Get("/api/share/fetch", handler.Fetch())
|
||||
|
||||
ready := make(chan struct{})
|
||||
ln, err := net.Listen("tcp", opt.Cfg.Address)
|
||||
if err != nil {
|
||||
|
42
internal/handler/share.go
Normal file
42
internal/handler/share.go
Normal 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
|
||||
}
|
||||
}
|
8
internal/model/meta.go
Normal file
8
internal/model/meta.go
Normal file
@ -0,0 +1,8 @@
|
||||
package model
|
||||
|
||||
type Meta struct {
|
||||
Filename string `json:"filename" mapstructure:"filename"`
|
||||
CreatedAt int64 `json:"created_at" mapstructure:"created_at"`
|
||||
Size int64 `json:"size" mapstructure:"size"`
|
||||
UploaderIp string `json:"uploader_ip" mapstructure:"uploader_ip"`
|
||||
}
|
@ -1,8 +1,9 @@
|
||||
package opt
|
||||
|
||||
type config struct {
|
||||
Debug bool
|
||||
Address string
|
||||
Debug bool
|
||||
Address string
|
||||
DataPath string
|
||||
}
|
||||
|
||||
var (
|
||||
|
15
internal/opt/var.go
Normal file
15
internal/opt/var.go
Normal file
@ -0,0 +1,15 @@
|
||||
package opt
|
||||
|
||||
import "path/filepath"
|
||||
|
||||
const (
|
||||
Meta = ".meta."
|
||||
)
|
||||
|
||||
func FilePath(code string) string {
|
||||
return filepath.Join(Cfg.DataPath, code)
|
||||
}
|
||||
|
||||
func MetaPath(code string) string {
|
||||
return filepath.Join(Cfg.DataPath, Meta+code)
|
||||
}
|
Reference in New Issue
Block a user