开发...
This commit is contained in:
99
internal/pkg/convert/any.go
Normal file
99
internal/pkg/convert/any.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package convert
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"reflect"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
var (
|
||||
errorType = reflect.TypeOf((*error)(nil)).Elem()
|
||||
fmtStringerType = reflect.TypeOf((*fmt.Stringer)(nil)).Elem()
|
||||
)
|
||||
|
||||
// Copied from html/template/content.go.
|
||||
// indirectToStringerOrError returns the value, after dereferencing as many times
|
||||
// as necessary to reach the base type (or nil) or an implementation of fmt.Stringer
|
||||
// or error,
|
||||
func indirectToStringerOrError(a any) any {
|
||||
if a == nil {
|
||||
return nil
|
||||
}
|
||||
v := reflect.ValueOf(a)
|
||||
for !v.Type().Implements(fmtStringerType) && !v.Type().Implements(errorType) && v.Kind() == reflect.Pointer && !v.IsNil() {
|
||||
v = v.Elem()
|
||||
}
|
||||
return v.Interface()
|
||||
}
|
||||
|
||||
// ToStringE converts any type to a string type.
|
||||
func ToStringE(i any) (string, error) {
|
||||
i = indirectToStringerOrError(i)
|
||||
|
||||
switch s := i.(type) {
|
||||
case string:
|
||||
return s, nil
|
||||
case bool:
|
||||
return strconv.FormatBool(s), nil
|
||||
case float64:
|
||||
return strconv.FormatFloat(s, 'f', -1, 64), nil
|
||||
case float32:
|
||||
return strconv.FormatFloat(float64(s), 'f', -1, 32), nil
|
||||
case int:
|
||||
return strconv.Itoa(s), nil
|
||||
case int64:
|
||||
return strconv.FormatInt(s, 10), nil
|
||||
case int32:
|
||||
return strconv.Itoa(int(s)), nil
|
||||
case int16:
|
||||
return strconv.FormatInt(int64(s), 10), nil
|
||||
case int8:
|
||||
return strconv.FormatInt(int64(s), 10), nil
|
||||
case uint:
|
||||
return strconv.FormatUint(uint64(s), 10), nil
|
||||
case uint64:
|
||||
return strconv.FormatUint(uint64(s), 10), nil
|
||||
case uint32:
|
||||
return strconv.FormatUint(uint64(s), 10), nil
|
||||
case uint16:
|
||||
return strconv.FormatUint(uint64(s), 10), nil
|
||||
case uint8:
|
||||
return strconv.FormatUint(uint64(s), 10), nil
|
||||
case json.Number:
|
||||
return s.String(), nil
|
||||
case []byte:
|
||||
return string(s), nil
|
||||
case template.HTML:
|
||||
return string(s), nil
|
||||
case template.URL:
|
||||
return string(s), nil
|
||||
case template.JS:
|
||||
return string(s), nil
|
||||
case template.CSS:
|
||||
return string(s), nil
|
||||
case template.HTMLAttr:
|
||||
return string(s), nil
|
||||
case nil:
|
||||
return "", nil
|
||||
case fmt.Stringer:
|
||||
return s.String(), nil
|
||||
case error:
|
||||
return s.Error(), nil
|
||||
default:
|
||||
return "", fmt.Errorf("unable to cast %#v of type %T to string", i, i)
|
||||
}
|
||||
}
|
||||
|
||||
// ToStringE converts any type to a string type.
|
||||
func ToByteE(i any) ([]byte, error) {
|
||||
i = indirectToStringerOrError(i)
|
||||
|
||||
switch s := i.(type) {
|
||||
case []byte:
|
||||
return s, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unable to cast %#v of type %T to string", i, i)
|
||||
}
|
||||
}
|
||||
@@ -1,24 +1,41 @@
|
||||
package convert
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
"github.com/zhang2092/mediahls/internal/pkg/fileutil"
|
||||
)
|
||||
|
||||
func ConvertHLS(savePath, filePath string) error {
|
||||
exist, _ := fileutil.PathExists(savePath)
|
||||
if !exist {
|
||||
err := os.MkdirAll(savePath, os.ModePerm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
binary, err := exec.LookPath("ffmpeg")
|
||||
if err != nil {
|
||||
log.Println("1: ", err)
|
||||
return err
|
||||
}
|
||||
|
||||
// ffmpeg -i web/statics/git.mp4 -profile:v baseline -level 3.0 -s 1920x1080 -start_number 0 -hls_time 10 -hls_list_size 0 -hls_segment_filename %d.ts -f hls web/statics/git.m3u8
|
||||
command := "-i " + filePath + " -profile:v baseline -level 3.0 -s 1920x1080 -start_number 0 -hls_time 10 -hls_list_size 0 -f hls " + savePath + "index.m3u8"
|
||||
log.Println(command)
|
||||
args := strings.Split(command, " ")
|
||||
cmd := exec.Command(binary, args...)
|
||||
_, err = cmd.Output()
|
||||
if err != nil {
|
||||
log.Println("2: ", err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
// ffmpeg -i upload/20231129/o6e6qKaMdk0VC1Ys2SHnr.mp4 -profile:v baseline -level 3.0 -s 1920x1080 -start_number 0 -f hls -hls_time 10 -segment_list web/statics/js/index.m3u8 web/statics/mmm/index.m3u8
|
||||
// ffmpeg -i upload/20231129/o6e6qKaMdk0VC1Ys2SHnr.mp4 -c copy -map 0 -f segment -segment_list web/statics/js/index.m3u8 -segment_time 20 web/statics/js/index_%3d.ts
|
||||
}
|
||||
41
internal/pkg/cookie/cookie.go
Normal file
41
internal/pkg/cookie/cookie.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package cookie
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
AuthorizeName = "authorize"
|
||||
)
|
||||
|
||||
func NewCookie(name, value string, expired time.Time) *http.Cookie {
|
||||
return &http.Cookie{
|
||||
Name: name,
|
||||
Value: value,
|
||||
Path: "/",
|
||||
Secure: false, // true->只能https站点操作
|
||||
HttpOnly: true, // true->js不能捕获
|
||||
Expires: expired,
|
||||
}
|
||||
}
|
||||
|
||||
func SetCookie(w http.ResponseWriter, name, value string, expired time.Time) {
|
||||
cookie := NewCookie(name, value, expired)
|
||||
http.SetCookie(w, cookie)
|
||||
}
|
||||
|
||||
func ReadCookie(r *http.Request, name string) (string, error) {
|
||||
cookie, err := r.Cookie(name)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return cookie.Value, nil
|
||||
}
|
||||
|
||||
func DeleteCookie(w http.ResponseWriter, name string) {
|
||||
cookie := NewCookie(name, "", time.Now().Add(time.Duration(-10)*time.Second))
|
||||
cookie.MaxAge = -1
|
||||
http.SetCookie(w, cookie)
|
||||
}
|
||||
Reference in New Issue
Block a user