276 lines
6.3 KiB
Go
276 lines
6.3 KiB
Go
package storage
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/base64"
|
|
"errors"
|
|
"fmt"
|
|
"mime/multipart"
|
|
"videoweb/pub/storage/proxy"
|
|
|
|
"strconv"
|
|
)
|
|
|
|
var (
|
|
// buffer = bytes.Buffer{}
|
|
// mtx sync.Mutex
|
|
)
|
|
|
|
// web后台上传使用, form-data方式上传
|
|
func UploadImage(ctx context.Context, f *multipart.FileHeader, domain string) (path string, err error) {
|
|
if f == nil {
|
|
return "", errors.New("file is nil!")
|
|
}
|
|
var file multipart.File
|
|
if file, err = f.Open(); err != nil {
|
|
return
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
//mtx.Lock()
|
|
//defer mtx.Unlock()
|
|
buffer := bytes.Buffer{}
|
|
if _, err = buffer.ReadFrom(file); err != nil {
|
|
return
|
|
}
|
|
return proxy.UploadImg(f.Filename, buffer.Bytes(), domain)
|
|
}
|
|
|
|
func UploadVideoBody(ctx context.Context, form *multipart.Form, domain string) (*proxy.Result, error) {
|
|
if form == nil {
|
|
return nil, errors.New("multipart from is nil")
|
|
}
|
|
sb, err := makeShardBodyByForm(form)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return proxy.UploadVideo(domain, sb)
|
|
}
|
|
|
|
func UploadVideoBodyV2(ctx context.Context, form *multipart.Form,
|
|
domain string, appIdIn int, publish string) (*proxy.Result, error) {
|
|
if form == nil {
|
|
return nil, errors.New("multipart from is nil")
|
|
}
|
|
sb, err := makeShardBodyByFormV2(form, appIdIn, publish)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return proxy.UploadVideoV2(domain, sb)
|
|
}
|
|
|
|
func VideoBody(ctx context.Context, taskId, typ, domain string, pos int, data []byte) (*proxy.Result, error) {
|
|
body := &proxy.ShardBody{
|
|
TaskId: taskId,
|
|
Pos: pos,
|
|
FileData: base64.StdEncoding.EncodeToString(data),
|
|
Type: typ,
|
|
}
|
|
if typ == "1" {
|
|
body.Type = "AV"
|
|
} else if typ == "2" {
|
|
body.Type = "SP"
|
|
}
|
|
return proxy.UploadVideo(domain, body)
|
|
}
|
|
|
|
func UploadVideoOK(ctx context.Context, in *proxy.ShardOk, domain string) (*proxy.VideoOkResult, error) {
|
|
return proxy.PostOk(in, domain)
|
|
}
|
|
func UploadVideoOKV2(ctx context.Context, in *proxy.ShardOk, domain string) (*proxy.VideoOkResult, error) {
|
|
return proxy.PostOkV2(in, domain)
|
|
}
|
|
|
|
func QueryMedia(ctx context.Context, in *proxy.MediaQueryeReq, domain string) (*proxy.VideoQueryResult, error) {
|
|
return proxy.QueryMedia(in, domain)
|
|
}
|
|
func QueryMedias(ctx context.Context, in *proxy.MediaQueryesReq, domain string) (*proxy.VideoQuerysResult, error) {
|
|
return proxy.QueryMedias(in, domain)
|
|
}
|
|
func QueryAdvitiseLocationList(ctx context.Context, in *proxy.AppAdvitiseInfoReq, domain string) (*proxy.AdvitiseLocationQuerysResult, error) {
|
|
return proxy.QueryAdvitiseLocation(in, domain)
|
|
}
|
|
|
|
func SyncMedia(in *proxy.MediaTaskSyncReq, domain string) (result *proxy.VideoQuerysResult, err error) {
|
|
return proxy.SyncMedia(in, domain)
|
|
}
|
|
func makeShardBodyByForm(form *multipart.Form) (*proxy.ShardBody, error) {
|
|
poss := form.Value["pos"]
|
|
taskIds := form.Value["taskId"]
|
|
types := form.Value["type"]
|
|
fs := form.File["file"]
|
|
totalPoss := form.Value["totalChunks"]
|
|
|
|
var (
|
|
pos, total int
|
|
taskId string
|
|
typ string
|
|
f *multipart.FileHeader
|
|
err error
|
|
)
|
|
|
|
if len(fs) == 0 || fs[0] == nil {
|
|
return nil, errors.New("no file")
|
|
}
|
|
f = fs[0]
|
|
|
|
if len(poss) == 0 || poss[0] == "" {
|
|
return nil, errors.New("no pos")
|
|
}
|
|
if pos, err = strconv.Atoi(poss[0]); err != nil {
|
|
return nil, fmt.Errorf("invalid pos")
|
|
}
|
|
if len(totalPoss) == 0 || totalPoss[0] == "" {
|
|
return nil, errors.New("no totalPos")
|
|
}
|
|
if total, err = strconv.Atoi(totalPoss[0]); err != nil {
|
|
return nil, fmt.Errorf("invalid totalPos")
|
|
}
|
|
|
|
if len(taskIds) == 0 || taskIds[0] == "" {
|
|
return nil, errors.New("no taskId")
|
|
}
|
|
taskId = taskIds[0]
|
|
|
|
if len(types) == 0 || types[0] == "" {
|
|
return nil, errors.New("no type")
|
|
}
|
|
if types[0] == "1" {
|
|
typ = "AV"
|
|
} else if types[0] == "2" {
|
|
typ = "SP"
|
|
} else if types[0] == "3" {
|
|
typ = "AI"
|
|
} else {
|
|
typ = types[0]
|
|
}
|
|
|
|
file, ne := f.Open()
|
|
if ne != nil {
|
|
return nil, ne
|
|
}
|
|
defer file.Close()
|
|
//mtx.Lock()
|
|
//defer mtx.Unlock()
|
|
buffer := bytes.Buffer{}
|
|
if _, err = buffer.ReadFrom(file); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
resp := &proxy.ShardBody{
|
|
TaskId: taskId,
|
|
Pos: pos,
|
|
FileData: base64.StdEncoding.EncodeToString(buffer.Bytes()),
|
|
Type: typ,
|
|
TotalPos: total,
|
|
}
|
|
return resp, nil
|
|
}
|
|
|
|
func makeShardBodyByFormV2(form *multipart.Form, appIdIn int, publishIn string) (
|
|
*proxy.ShardBodyV2, error) {
|
|
taskIds := form.Value["taskId"]
|
|
types := form.Value["type"]
|
|
poss := form.Value["pos"]
|
|
fs := form.File["file"]
|
|
totalPoss := form.Value["totalChunks"]
|
|
fileNames := form.Value["filename"]
|
|
appIds := form.Value["appId"]
|
|
publishs := form.Value["publish"]
|
|
|
|
var (
|
|
pos, total int
|
|
taskId string
|
|
f *multipart.FileHeader
|
|
fileName string
|
|
typ string
|
|
publish string
|
|
appId int
|
|
err error
|
|
)
|
|
|
|
if len(fs) == 0 || fs[0] == nil {
|
|
return nil, errors.New("no file")
|
|
}
|
|
f = fs[0]
|
|
|
|
if len(poss) == 0 || poss[0] == "" {
|
|
return nil, errors.New("no pos")
|
|
}
|
|
if pos, err = strconv.Atoi(poss[0]); err != nil {
|
|
return nil, fmt.Errorf("invalid pos")
|
|
}
|
|
if len(totalPoss) == 0 || totalPoss[0] == "" {
|
|
return nil, errors.New("no totalPos")
|
|
}
|
|
if total, err = strconv.Atoi(totalPoss[0]); err != nil {
|
|
return nil, fmt.Errorf("invalid totalPos")
|
|
}
|
|
|
|
if len(taskIds) == 0 || taskIds[0] == "" {
|
|
return nil, errors.New("no taskId")
|
|
}
|
|
taskId = taskIds[0]
|
|
|
|
if len(types) == 0 || types[0] == "" {
|
|
return nil, errors.New("no type")
|
|
}
|
|
if types[0] == "1" {
|
|
typ = "AV"
|
|
} else if types[0] == "2" {
|
|
typ = "SP"
|
|
} else if types[0] == "3" {
|
|
typ = "AI"
|
|
} else {
|
|
typ = types[0]
|
|
}
|
|
|
|
if len(fileNames) == 0 || fileNames[0] == "" {
|
|
return nil, errors.New("no fileName")
|
|
}
|
|
fileName = fileNames[0]
|
|
|
|
if len(publishs) == 0 || publishs[0] == "" {
|
|
return nil, errors.New("no publish")
|
|
}
|
|
publish = publishs[0]
|
|
if publish == "" {
|
|
publish = publishIn
|
|
}
|
|
|
|
if len(appIds) == 0 || appIds[0] == "" {
|
|
return nil, errors.New("no appId")
|
|
}
|
|
if appId, err = strconv.Atoi(appIds[0]); err != nil {
|
|
return nil, fmt.Errorf("invalid appId")
|
|
}
|
|
|
|
if appId == 0 {
|
|
appId = appIdIn
|
|
}
|
|
|
|
file, ne := f.Open()
|
|
if ne != nil {
|
|
return nil, ne
|
|
}
|
|
defer file.Close()
|
|
buffer := bytes.Buffer{}
|
|
if _, err = buffer.ReadFrom(file); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
resp := &proxy.ShardBodyV2{
|
|
TaskId: taskId,
|
|
Pos: pos,
|
|
FileData: base64.StdEncoding.EncodeToString(buffer.Bytes()),
|
|
Type: typ,
|
|
TotalPos: total,
|
|
FileName: fileName,
|
|
AppId: appId,
|
|
Publish: publish,
|
|
}
|
|
return resp, nil
|
|
}
|