mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-11-13 20:53:49 +08:00
发布v2.18.6版本,更新内容请查看:https://github.com/bufanyun/hotgo/blob/v2.0/docs/guide-zh-CN/start-update-log.md
This commit is contained in:
@@ -6,10 +6,14 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
"bytes"
|
||||
"hotgo/utility/format"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
)
|
||||
|
||||
// 文件信息
|
||||
@@ -66,3 +70,34 @@ func MergeAbs(path string, fileName ...string) string {
|
||||
paths = append(paths, fileName...)
|
||||
return gfile.Join(paths...)
|
||||
}
|
||||
|
||||
// NewMultipartFileHeader 创建一个MultipartFileHeader
|
||||
func NewMultipartFileHeader(filename string, b []byte) (*multipart.FileHeader, error) {
|
||||
body := &bytes.Buffer{}
|
||||
writer := multipart.NewWriter(body)
|
||||
|
||||
part, err := writer.CreateFormFile("file", filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if _, err = io.Copy(part, bytes.NewReader(b)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err = writer.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
reader := multipart.NewReader(body, writer.Boundary())
|
||||
form, err := reader.ReadForm(int64(len(b)) + 1024)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer form.RemoveAll()
|
||||
|
||||
if files, ok := form.File["file"]; ok && len(files) > 0 {
|
||||
return files[0], nil
|
||||
}
|
||||
return nil, io.ErrUnexpectedEOF
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Package simple
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2023 HotGo CLI
|
||||
// @Copyright Copyright (c) 2025 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
package simple
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/gogf/gf/v2/encoding/gbase64"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
"github.com/gogf/gf/v2/os/glog"
|
||||
"hotgo/internal/consts"
|
||||
@@ -74,6 +75,40 @@ func CheckPassword(input, salt, hash string) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// GetHeaderLocale 获取请求头语言设置
|
||||
// gf支持格式:en/ja/ru/zh-CN/zh-TW
|
||||
func GetHeaderLocale(ctx context.Context) (lang string) {
|
||||
lang = g.Cfg().MustGet(ctx, "system.i18n.defaultLanguage", consts.SysDefaultLanguage).String()
|
||||
// 没有开启国际化,使用默认语言
|
||||
if !g.Cfg().MustGet(ctx, "system.i18n.switch", true).Bool() {
|
||||
return
|
||||
}
|
||||
|
||||
r := ghttp.RequestFromCtx(ctx)
|
||||
if r == nil {
|
||||
return
|
||||
}
|
||||
locale := r.Header.Get("Locale")
|
||||
// 简体中文
|
||||
if locale == "zh-CN" || locale == "zh-Hans" || locale == "zh" || locale == "ZH" {
|
||||
lang = "zh-CN"
|
||||
return
|
||||
}
|
||||
// 繁体
|
||||
if locale == "zh-TW" || locale == "zh-Hant" {
|
||||
lang = "zh-TW"
|
||||
return
|
||||
}
|
||||
// 英文
|
||||
if locale == "en" || locale == "EN" {
|
||||
lang = "en"
|
||||
return
|
||||
}
|
||||
// 更多语言
|
||||
// ...
|
||||
return
|
||||
}
|
||||
|
||||
// SafeGo 安全的调用协程,遇到错误时输出错误日志而不是抛出panic
|
||||
func SafeGo(ctx context.Context, f func(ctx context.Context), lv ...int) {
|
||||
g.Go(ctx, f, func(ctx context.Context, err error) {
|
||||
|
||||
Reference in New Issue
Block a user