init: 0.0.8

feat: 实现了分片上传
todo:
  1. 上传完成后 code 回显
  2. 清理不用的临时 code file
  3. 断点续传
  4. 多 chunk 并发上传
This commit is contained in:
loveuer
2025-04-28 14:59:22 +08:00
parent fb96934abe
commit 7ec8cd5c0e
22 changed files with 1110 additions and 356 deletions

View File

@ -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,10 @@ func Start(ctx context.Context) <-chan struct{} {
return c.SendStatus(http.StatusOK)
})
app.Get("/api/share/:code", handler.Fetch())
app.Put("/api/share/:filename", handler.ShareNew()) // 获取上传 code, 分片大小
app.Post("/api/share/:code", handler.ShareUpload()) // 分片上传接口
ready := make(chan struct{})
ln, err := net.Listen("tcp", opt.Cfg.Address)
if err != nil {

122
internal/controller/meta.go Normal file
View File

@ -0,0 +1,122 @@
package controller
import (
"context"
"fmt"
"github.com/loveuer/nf/nft/log"
"github.com/loveuer/ushare/internal/opt"
gonanoid "github.com/matoous/go-nanoid/v2"
"io"
"os"
"sync"
"time"
)
type metaInfo struct {
f *os.File
name string
create time.Time
last time.Time
size int64
cursor int64
ip string
}
func (m *metaInfo) generateMeta(code string) error {
content := fmt.Sprintf("filename=%s\ncreated_at=%d\nsize=%d\nuploader_ip=%s",
m.name, m.create.UnixMilli(), m.size, m.ip,
)
return os.WriteFile(opt.MetaPath(code), []byte(content), 0644)
}
type meta struct {
sync.Mutex
ctx context.Context
m map[string]*metaInfo
}
var (
MetaManager = &meta{m: make(map[string]*metaInfo)}
)
const letters = "1234567890abcdefghijklmnopqrstuvwxyz"
func (m *meta) New(size int64, filename, ip string) (string, error) {
now := time.Now()
code, err := gonanoid.Generate(letters, 16)
if err != nil {
return "", err
}
f, err := os.Create(opt.FilePath(code))
if err != nil {
return "", err
}
if err = f.Truncate(size); err != nil {
f.Close()
return "", err
}
m.Lock()
defer m.Unlock()
m.m[code] = &metaInfo{f: f, name: filename, last: now, size: size, cursor: 0, create: now, ip: ip}
return code, nil
}
func (m *meta) Write(code string, start, end int64, reader io.Reader) (total, cursor int64, err error) {
m.Lock()
defer m.Unlock()
if _, ok := m.m[code]; !ok {
return 0, 0, fmt.Errorf("code not exist")
}
w, err := io.CopyN(m.m[code].f, reader, end-start+1)
if err != nil {
return 0, 0, err
}
m.m[code].cursor += w
m.m[code].last = time.Now()
total = m.m[code].size
cursor = m.m[code].cursor
if m.m[code].cursor == m.m[code].size {
defer delete(m.m, code)
if err = m.m[code].generateMeta(code); err != nil {
return 0, 0, err
}
}
return total, cursor, nil
}
func (m *meta) Start(ctx context.Context) {
ticker := time.NewTicker(time.Minute)
m.ctx = ctx
go func() {
for {
select {
case <-ctx.Done():
return
case now := <-ticker.C:
for code, info := range m.m {
if now.Sub(info.last) > 1*time.Minute {
m.Lock()
info.f.Close()
os.RemoveAll(opt.FilePath(code))
delete(m.m, code)
m.Unlock()
log.Warn("MetaController: code timeout removed, code = %s", code)
}
}
}
}
}()
}

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

@ -0,0 +1,119 @@
package handler
import (
"fmt"
"github.com/loveuer/nf"
"github.com/loveuer/nf/nft/log"
"github.com/loveuer/ushare/internal/controller"
"github.com/loveuer/ushare/internal/model"
"github.com/loveuer/ushare/internal/opt"
"github.com/pkg/errors"
"github.com/spf13/cast"
"github.com/spf13/viper"
"net/http"
"os"
"regexp"
"strings"
)
func Fetch() nf.HandlerFunc {
return func(c *nf.Ctx) error {
code := c.Param("code")
log.Debug("handler.Fetch: code = %s", 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
}
}
func ShareNew() nf.HandlerFunc {
return func(c *nf.Ctx) error {
filename := strings.TrimSpace(c.Param("filename"))
if filename == "" {
return c.Status(http.StatusBadRequest).JSON(map[string]string{"msg": "filename required"})
}
size, err := cast.ToInt64E(c.Get(opt.HeaderSize))
if err != nil {
return c.Status(http.StatusBadRequest).JSON(map[string]string{"msg": "miss header: " + opt.HeaderSize})
}
code, err := controller.MetaManager.New(size, filename, c.IP())
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(map[string]string{"msg": ""})
}
return c.Status(http.StatusOK).JSON(map[string]string{"code": code})
}
}
func ShareUpload() nf.HandlerFunc {
rangeValidator := regexp.MustCompile(`^bytes=\d+-\d+$`)
return func(c *nf.Ctx) error {
code := strings.TrimSpace(c.Param("code"))
if len(code) != 16 {
return c.Status(http.StatusBadRequest).JSON(map[string]string{"msg": "invalid file code"})
}
log.Debug("handler.ShareUpload: code = %s", code)
ranger := strings.TrimSpace(c.Get("Range"))
if ranger == "" {
return c.Status(http.StatusBadRequest).JSON(map[string]string{"msg": "miss header: Range"})
}
log.Debug("handler.ShareUpload: code = %s, ranger = %s", code, ranger)
if !rangeValidator.MatchString(ranger) {
log.Warn("handler.ShareUpload: invalid range, ranger = %s", ranger)
return c.Status(http.StatusBadRequest).JSON(map[string]string{"msg": "Range invalid(1)"})
}
strs := strings.Split(strings.TrimPrefix(ranger, "bytes="), "-")
if len(strs) != 2 {
return c.Status(http.StatusBadRequest).JSON(map[string]string{"msg": "Range invalid(2)"})
}
start, err := cast.ToInt64E(strs[0])
if err != nil {
return c.Status(http.StatusBadRequest).JSON(map[string]string{"msg": "Range invalid(3)"})
}
end, err := cast.ToInt64E(strs[1])
if err != nil {
return c.Status(http.StatusBadRequest).JSON(map[string]string{"msg": "Range invalid(4)"})
}
log.Debug("handler.ShareUpload: code = %s, start = %d, end = %d", code, start, end)
total, cursor, err := controller.MetaManager.Write(code, start, end, c.Request.Body)
if err != nil {
log.Error("handler.ShareUpload: write error: %s", err)
return c.Status(http.StatusInternalServerError).JSON(map[string]string{"msg": ""})
}
return c.Status(http.StatusOK).JSON(map[string]any{"size": total, "cursor": cursor})
}
}

8
internal/model/meta.go Normal file
View 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"`
}

View File

@ -1,8 +1,9 @@
package opt
type config struct {
Debug bool
Address string
Debug bool
Address string
DataPath string
}
var (

16
internal/opt/var.go Normal file
View File

@ -0,0 +1,16 @@
package opt
import "path/filepath"
const (
Meta = ".meta."
HeaderSize = "X-File-Size"
)
func FilePath(code string) string {
return filepath.Join(Cfg.DataPath, code)
}
func MetaPath(code string) string {
return filepath.Join(Cfg.DataPath, Meta+code)
}