mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
Modules declare go 1.27.1 and the build images follow. go fix applied the modernizers; omitzero is not applied, since it changes what the JSON says. Co-authored-by: Hanzo Dev <[email protected]>
426 lines
10 KiB
Go
426 lines
10 KiB
Go
// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
|
|
// Portions Copyright 2023 The OpenAgent Authors. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package controllers
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/hanzoai/ai/audio"
|
|
"github.com/hanzoai/ai/conf"
|
|
"github.com/hanzoai/ai/object"
|
|
"github.com/hanzoai/ai/storage"
|
|
"github.com/hanzoai/ai/util"
|
|
"github.com/hanzoai/ai/video"
|
|
)
|
|
|
|
// GetGlobalVideos
|
|
// @Title GetGlobalVideos
|
|
// @Tag Video API
|
|
// @Description get global videos
|
|
// @Success 200 {array} object.Video The Response object
|
|
// @router /get-global-videos [get]
|
|
func (c *ApiController) GetGlobalVideos() {
|
|
videos, err := object.GetGlobalVideos()
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
c.ResponseOk(videos)
|
|
}
|
|
|
|
// GetVideos
|
|
// @Title GetVideos
|
|
// @Tag Video API
|
|
// @Description get videos
|
|
// @Param owner query string true "The owner of videos"
|
|
// @Success 200 {array} object.Video The Response object
|
|
// @router /get-videos [get]
|
|
func (c *ApiController) GetVideos() {
|
|
user, ok := c.RequireSignedInUser()
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
// UploadVideo stores the uploader's user name in Owner, so a listing scopes on
|
|
// the USER, not the organization — which is why GetScopedOwner's answer never
|
|
// matched a row here. The reserved org lists whoever it names, or everyone;
|
|
// every other caller gets their own uploads.
|
|
owner := user.Name
|
|
if util.IsSuperAdmin(user) {
|
|
owner = c.Input().Get("owner")
|
|
}
|
|
|
|
limit := c.Input().Get("pageSize")
|
|
page := c.Input().Get("p")
|
|
field := c.Input().Get("field")
|
|
value := c.Input().Get("value")
|
|
sortField := c.Input().Get("sortField")
|
|
sortOrder := c.Input().Get("sortOrder")
|
|
|
|
if limit == "" || page == "" {
|
|
videos, err := object.GetVideos(owner, c.GetAcceptLanguage())
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
c.ResponseOk(videos)
|
|
} else {
|
|
limit := util.ParseInt(limit)
|
|
count, err := object.GetVideoCount(owner, field, value)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
paginator := util.NewPaginator(c.PageAsked(), limit, count)
|
|
videos, err := object.GetPaginationVideos(owner, paginator.Offset(), limit, field, value, sortField, sortOrder, c.GetAcceptLanguage())
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
c.ResponseOk(videos, paginator.Nums())
|
|
}
|
|
}
|
|
|
|
// GetVideo
|
|
// @Title GetVideo
|
|
// @Tag Video API
|
|
// @Description get video
|
|
// @Param id query string true "The id of video"
|
|
// @Success 200 {object} object.Video The Response object
|
|
// @router /get-video [get]
|
|
func (c *ApiController) GetVideo() {
|
|
id := c.Input().Get("id")
|
|
|
|
v, err := object.GetVideo(id)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
if v == nil {
|
|
c.ResponseOk(nil)
|
|
return
|
|
}
|
|
|
|
// The row says whether it is public, and the public listing links straight
|
|
// here, so a public video answers anyone. Every other video answers the person
|
|
// whose uploads it is among — the name this table's listing scopes on — or a
|
|
// platform admin. A caller who is allowed neither gets the same answer as one
|
|
// asking for a video that is not there, which is all they are owed.
|
|
if !v.IsPublic {
|
|
user := c.GetSessionUser()
|
|
if user == nil || (user.Name != v.Owner && !util.IsSuperAdmin(user)) {
|
|
c.ResponseOk(nil)
|
|
return
|
|
}
|
|
}
|
|
|
|
if err = v.Play(c.GetAcceptLanguage()); err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
if err = v.Populate(c.GetAcceptLanguage()); err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
c.ResponseOk(v)
|
|
}
|
|
|
|
// UpdateVideo
|
|
// @Title UpdateVideo
|
|
// @Tag Video API
|
|
// @Description update video
|
|
// @Param id query string true "The id (owner/name) of the video"
|
|
// @Param body body object.Video true "The details of the video"
|
|
// @Success 200 {object} controllers.Response The Response object
|
|
// @router /update-video [post]
|
|
func (c *ApiController) UpdateVideo() {
|
|
owner, signedIn := c.RequireSignedIn()
|
|
if !signedIn {
|
|
return
|
|
}
|
|
user, ok := c.RequireSignedInUser()
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
id := c.Input().Get("id")
|
|
// The id names the row that gets written, so the id is what has to belong to
|
|
// the caller. Stamping the body's Owner reads like the check and is not one:
|
|
// UpdateVideo takes the row's owner from the id and overwrites the stamp.
|
|
if !writable(c, owner, id) {
|
|
return
|
|
}
|
|
|
|
var video object.Video
|
|
err := json.Unmarshal(c.Body(), &video)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
if util.IsVideoNormalUser(user) {
|
|
if len(video.Remarks) > 0 || len(video.Remarks2) > 0 || video.State != "Draft" {
|
|
c.ResponseError(c.T("video:The video can only be updated when there are no remarks and the state is \"Draft\""))
|
|
return
|
|
}
|
|
}
|
|
|
|
success, err := object.UpdateVideo(id, &video)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
c.ResponseOk(success)
|
|
}
|
|
|
|
// AddVideo
|
|
// @Title AddVideo
|
|
// @Tag Video API
|
|
// @Description add video
|
|
// @Param body body object.Video true "The details of the video"
|
|
// @Success 200 {object} controllers.Response The Response object
|
|
// @router /add-video [post]
|
|
func (c *ApiController) AddVideo() { stored(c, c.RequireSignedIn, object.AddVideo) }
|
|
|
|
// DeleteVideo
|
|
// @Title DeleteVideo
|
|
// @Tag Video API
|
|
// @Description delete video
|
|
// @Param body body object.Video true "The details of the video"
|
|
// @Success 200 {object} controllers.Response The Response object
|
|
// @router /delete-video [post]
|
|
func (c *ApiController) DeleteVideo() { stored(c, c.RequireSignedIn, object.DeleteVideo) }
|
|
|
|
func updateVideoCoverUrl(id string, videoId string) error {
|
|
for range 30 {
|
|
coverUrl := video.GetVideoCoverUrl(videoId)
|
|
if coverUrl != "" {
|
|
v, err := object.GetVideo(id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// This poll outlives the answer the upload gave, so the row it is filling
|
|
// in can be deleted while it waits.
|
|
if v == nil {
|
|
return nil
|
|
}
|
|
if v.CoverUrl != "" {
|
|
break
|
|
}
|
|
|
|
v.CoverUrl = coverUrl
|
|
_, err = object.UpdateVideo(id, v)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
break
|
|
}
|
|
|
|
time.Sleep(time.Second * 2)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func copyBuffer(original *bytes.Buffer) *bytes.Buffer {
|
|
bufCopy := make([]byte, original.Len())
|
|
copy(bufCopy, original.Bytes())
|
|
return bytes.NewBuffer(bufCopy)
|
|
}
|
|
|
|
func getSpeaker(s string) string {
|
|
if s == "0" {
|
|
return "Teacher"
|
|
} else if s == "1" {
|
|
return "Student"
|
|
} else {
|
|
return fmt.Sprintf("Student %s", s)
|
|
}
|
|
}
|
|
|
|
func getAudioSegments(userName string, filename string, fileBuffer *bytes.Buffer, lang string) (string, []*object.Label, error) {
|
|
audioStorageProviderName := conf.GetConfigString("audioStorageProvider")
|
|
if audioStorageProviderName == "" {
|
|
return "", []*object.Label{}, nil
|
|
}
|
|
|
|
fileBuffer2 := copyBuffer(fileBuffer)
|
|
|
|
audioBuffer, err := audio.GetAudioFromVideo(fileBuffer2)
|
|
if err != nil {
|
|
return "", nil, err
|
|
}
|
|
|
|
audioStorageProvider, err := storage.NewIamProvider(audioStorageProviderName, lang)
|
|
if err != nil {
|
|
return "", nil, err
|
|
}
|
|
|
|
audioFilename := strings.Replace(filename, ".mp4", ".mp3", 1)
|
|
audioUrl, err := audioStorageProvider.PutObject(userName, "Uploaded-Audio", audioFilename, audioBuffer)
|
|
if err != nil {
|
|
return "", nil, err
|
|
}
|
|
|
|
tmpInputFile, err := os.CreateTemp("", "cloud-audio-*.mp3")
|
|
if err != nil {
|
|
return "", nil, err
|
|
}
|
|
defer os.Remove(tmpInputFile.Name())
|
|
|
|
_, err = io.Copy(tmpInputFile, audioBuffer)
|
|
if err != nil {
|
|
return "", nil, err
|
|
}
|
|
tmpInputFile.Close()
|
|
|
|
segments := []*object.Label{}
|
|
oSegments, err := audio.GetSegmentsFromAudio(tmpInputFile.Name(), lang)
|
|
if err != nil {
|
|
return "", nil, err
|
|
}
|
|
|
|
for i, item := range oSegments {
|
|
segment := &object.Label{
|
|
Id: strconv.Itoa(i),
|
|
StartTime: util.ParseFloat(item.Bg) / 1000,
|
|
EndTime: util.ParseFloat(item.Ed) / 1000,
|
|
Text: item.Onebest,
|
|
Speaker: getSpeaker(item.Speaker),
|
|
}
|
|
segments = append(segments, segment)
|
|
}
|
|
|
|
return audioUrl, segments, nil
|
|
}
|
|
|
|
// UploadVideo
|
|
// @Title UploadVideo
|
|
// @Tag Video API
|
|
// @Description upload video
|
|
// @Param file formData file true "The video file to upload"
|
|
// @Success 200 {object} string "The fileId of the uploaded video"
|
|
// @router /upload-video [post]
|
|
func (c *ApiController) UploadVideo() {
|
|
userName, ok := c.RequireSignedIn()
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
file, header, err := c.GetFile("file")
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
defer file.Close()
|
|
|
|
filename := header.Filename
|
|
fileId := util.RemoveExt(filename)
|
|
ext := filepath.Ext(filename)
|
|
|
|
fileBuffer := bytes.NewBuffer(nil)
|
|
_, err = io.Copy(fileBuffer, file)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
fileType := "unknown"
|
|
contentType := header.Header.Get("Content-Type")
|
|
// Parse MIME type to extract the media type (e.g., "video" from "video/mp4")
|
|
parts := strings.Split(contentType, "/")
|
|
if len(parts) > 0 {
|
|
fileType = parts[0]
|
|
}
|
|
if fileType != "video" {
|
|
c.ResponseError(fmt.Sprintf("contentType: %s is not video", contentType))
|
|
return
|
|
}
|
|
|
|
err = object.SetDefaultVodClient(c.GetAcceptLanguage())
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
videoId, err := video.UploadVideo(fileId, filename, fileBuffer)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
if videoId == "" {
|
|
c.ResponseError(c.T("video:UploadVideo() error, videoId should not be empty"))
|
|
return
|
|
}
|
|
|
|
audioUrl, segments, err := getAudioSegments(userName, filename, fileBuffer, c.GetAcceptLanguage())
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
v := &object.Video{
|
|
Owner: userName,
|
|
Name: fileId,
|
|
CreatedTime: util.GetCurrentTime(),
|
|
DisplayName: fileId,
|
|
Tag: "",
|
|
Type: ext,
|
|
VideoId: videoId,
|
|
CoverUrl: "",
|
|
AudioUrl: audioUrl,
|
|
Labels: []*object.Label{},
|
|
Segments: segments,
|
|
DataUrls: []string{},
|
|
DataUrl: "",
|
|
TagOnPause: false,
|
|
Remarks: []*object.Remark{},
|
|
Remarks2: []*object.Remark{},
|
|
State: "Draft",
|
|
Keywords: []string{},
|
|
}
|
|
_, err = object.AddVideo(v)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
id := util.GetIdFromOwnerAndName(userName, fileId)
|
|
|
|
err = updateVideoCoverUrl(id, videoId)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
c.ResponseOk(fileId)
|
|
}
|