Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,61 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = ["http_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/favorite/conf:go_default_library",
"//app/interface/main/favorite/service:go_default_library",
"//library/net/http/blademaster:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"http.go",
"topic.go",
"video.go",
],
importpath = "go-common/app/interface/main/favorite/http",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/favorite/conf:go_default_library",
"//app/interface/main/favorite/model:go_default_library",
"//app/interface/main/favorite/service:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/log/anticheat:go_default_library",
"//library/log/infoc:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/net/http/blademaster/middleware/antispam:go_default_library",
"//library/net/http/blademaster/middleware/auth:go_default_library",
"//library/net/http/blademaster/middleware/supervisor:go_default_library",
"//library/net/http/blademaster/middleware/verify:go_default_library",
"//library/xstr:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,146 @@
package http
import (
"net/http"
"strconv"
"go-common/app/interface/main/favorite/conf"
"go-common/app/interface/main/favorite/service"
"go-common/library/log"
"go-common/library/log/anticheat"
bm "go-common/library/net/http/blademaster"
"go-common/library/net/http/blademaster/middleware/antispam"
"go-common/library/net/http/blademaster/middleware/auth"
"go-common/library/net/http/blademaster/middleware/supervisor"
"go-common/library/net/http/blademaster/middleware/verify"
)
var (
favSvc *service.Service
authSvc *auth.Auth
verifySvc *verify.Verify
antispamM *antispam.Antispam
supervisorM *supervisor.Supervisor
collector *anticheat.AntiCheat
)
// Init init router
func Init(c *conf.Config, svc *service.Service) {
verifySvc = verify.New(c.Verify)
authSvc = auth.New(c.Auth)
antispamM = antispam.New(c.Antispam)
supervisorM = supervisor.New(c.Supervisor)
favSvc = svc
if c.Infoc2 != nil {
collector = anticheat.New(c.Infoc2)
}
// init outer router
engineOut := bm.DefaultServer(c.BM)
outerRouter(engineOut)
internalRouter(engineOut)
// init Out serve
if err := engineOut.Start(); err != nil {
log.Error("engineOut.Start() error(%v)", err)
panic(err)
}
}
// outerRouter init outer router api path
func outerRouter(e *bm.Engine) {
// init api
e.GET("/monitor/ping", ping)
folderG := e.Group("/x/v2/fav/folder")
{
folderG.GET("", authSvc.Guest, videoFolders)
folderG.POST("/add", authSvc.User, antispamM.ServeHTTP, supervisorM.ServeHTTP, addVideoFolder)
folderG.POST("/del", authSvc.User, antispamM.ServeHTTP, delVideoFolder)
folderG.POST("/rename", authSvc.User, antispamM.ServeHTTP, supervisorM.ServeHTTP, renameVideoFolder)
folderG.POST("/public", authSvc.User, antispamM.ServeHTTP, upStateVideoFolder)
folderG.POST("/sort", authSvc.User, antispamM.ServeHTTP, sortVideoFolders)
}
videoG := e.Group("/x/v2/fav/video")
{
videoG.GET("", authSvc.Guest, favVideo)
videoG.GET("/tlist", authSvc.Guest, tidList)
videoG.GET("/newest", authSvc.User, favVideoNewest)
videoG.POST("/add", authSvc.User, antispamM.ServeHTTP, addFavVideo)
videoG.POST("/del", authSvc.User, antispamM.ServeHTTP, delFavVideo)
videoG.POST("/mdel", authSvc.User, antispamM.ServeHTTP, delFavVideos)
videoG.POST("/move", authSvc.User, antispamM.ServeHTTP, moveFavVideos)
videoG.POST("/copy", authSvc.User, antispamM.ServeHTTP, copyFavVideos)
videoG.GET("/favoureds", authSvc.User, isFavoureds)
videoG.GET("/favoured", authSvc.User, isFavoured)
videoG.GET("/default", authSvc.User, inDefaultFav)
videoG.GET("/cleaned", authSvc.User, isCleaned)
videoG.POST("/clean", authSvc.User, cleanInvalidArcs)
}
topicG := e.Group("/x/v2/fav/topic")
{
topicG.POST("/add", authSvc.User, antispamM.ServeHTTP, addFavTopic)
topicG.POST("/del", authSvc.User, antispamM.ServeHTTP, delFavTopic)
topicG.GET("/favoured", authSvc.User, isTopicFavoured)
topicG.GET("", authSvc.User, favTopics)
}
}
// internalRouter init internal router api path
func internalRouter(e *bm.Engine) {
// init api
folderG := e.Group("/x/internal/v2/fav/folder")
{
folderG.GET("", verifySvc.Verify, setMid, videoFolders)
folderG.POST("/add", verifySvc.VerifyUser, addVideoFolder)
folderG.POST("/del", verifySvc.VerifyUser, delVideoFolder)
folderG.POST("/rename", verifySvc.VerifyUser, renameVideoFolder)
folderG.POST("/public", verifySvc.VerifyUser, upStateVideoFolder)
folderG.POST("/sort", verifySvc.VerifyUser, sortVideoFolders)
}
videoG := e.Group("/x/internal/v2/fav/video")
{
videoG.GET("", verifySvc.Verify, setMid, favVideo)
videoG.GET("/tlist", verifySvc.Verify, setMid, tidList)
videoG.GET("/newest", verifySvc.VerifyUser, favVideoNewest)
videoG.POST("/add", verifySvc.VerifyUser, addFavVideo)
videoG.POST("/del", verifySvc.VerifyUser, delFavVideo)
videoG.POST("/mdel", verifySvc.VerifyUser, delFavVideos)
videoG.POST("/move", verifySvc.VerifyUser, moveFavVideos)
videoG.POST("/copy", verifySvc.VerifyUser, copyFavVideos)
videoG.GET("/favoureds", verifySvc.VerifyUser, isFavoureds)
videoG.GET("/favoured", verifySvc.VerifyUser, isFavoured)
videoG.GET("/default", verifySvc.VerifyUser, inDefaultFav)
videoG.GET("/cleaned", verifySvc.VerifyUser, isCleaned)
videoG.POST("/clean", verifySvc.VerifyUser, cleanInvalidArcs)
}
topicG := e.Group("/x/internal/v2/fav/topic")
{
topicG.POST("/add", verifySvc.VerifyUser, addFavTopic)
topicG.POST("/del", verifySvc.VerifyUser, delFavTopic)
topicG.GET("/favoured", verifySvc.VerifyUser, isTopicFavoured)
topicG.GET("", verifySvc.VerifyUser, favTopics)
}
}
func setMid(c *bm.Context) {
var (
err error
mid int64
)
req := c.Request
midStr := req.Form.Get("mid")
if midStr != "" {
if mid, err = strconv.ParseInt(midStr, 10, 64); err != nil {
c.JSON(nil, err)
c.Abort()
return
}
}
c.Set("mid", mid)
}
// ping check server ok.
func ping(c *bm.Context) {
if err := favSvc.Ping(c); err != nil {
log.Error("favorite http service ping error(%v)", err)
c.AbortWithStatus(http.StatusServiceUnavailable)
}
}

View File

@@ -0,0 +1,640 @@
package http
import (
"context"
"crypto/md5"
"encoding/hex"
"fmt"
"net/http"
"net/url"
"strconv"
"testing"
"time"
"go-common/app/interface/main/favorite/conf"
"go-common/app/interface/main/favorite/service"
xhttp "go-common/library/net/http/blademaster"
)
const (
_mid = 88888894
_vmid = 12345
_aid = 5463438
_aids = "5463438,5463439"
_fid = 1852
_delFid = 123
_oldFid = 1791
_newFid = 1792
_fidsSort = "1107,1852,1792,1791"
_name = "folder-name-test"
_rename = "folder-rename-test"
_public = 1
_tpid = 2659 // TopicID
_type = 1 // Article
_v3Fid = 0
_searchFid = 0
_oid = 123
_pn = 1
_ps = 30
// video folder
_videoFolders = "http://127.0.0.1:6010/x/internal/v2/fav/folder"
_addVideoFolder = "http://127.0.0.1:6010/x/internal/v2/fav/folder/add"
_delVideoFolder = "http://127.0.0.1:6010/x/internal/v2/fav/folder/del"
_renameVideoFolder = "http://127.0.0.1:6010/x/internal/v2/fav/folder/rename"
_upStateVideoFolder = "http://127.0.0.1:6010/x/internal/v2/fav/folder/public"
_sortVideoFolders = "http://127.0.0.1:6010/x/internal/v2/fav/folder/sort"
// video
_favVideo = "http://127.0.0.1:6010/x/internal/v2/fav/video"
_favVideoNewest = "http://127.0.0.1:6010/x/internal/v2/fav/video/newest"
_addFavVideo = "http://127.0.0.1:6010/x/internal/v2/fav/video/add"
_delFavVideo = "http://127.0.0.1:6010/x/internal/v2/fav/video/del"
_delFavVideos = "http://127.0.0.1:6010/x/internal/v2/fav/video/mdel"
_moveFavVideos = "http://127.0.0.1:6010/x/internal/v2/fav/video/move"
_copyFavVideos = "http://127.0.0.1:6010/x/internal/v2/fav/video/copy"
_isFavoureds = "http://127.0.0.1:6010/x/internal/v2/fav/video/favoureds"
_isFavoured = "http://127.0.0.1:6010/x/internal/v2/fav/video/favoured"
_inDefaultFav = "http://127.0.0.1:6010/x/internal/v2/fav/video/default"
// topic
_favTopics = "http://127.0.0.1:6010/x/internal/v2/fav/topic"
_addFavTopic = "http://127.0.0.1:6010/x/internal/v2/fav/topic/add"
_delFavTopic = "http://127.0.0.1:6010/x/internal/v2/fav/topic/del"
_isTopicFavoured = "http://127.0.0.1:6010/x/internal/v2/fav/topic/favoured"
// fav v3
_favorites = "http://127.0.0.1:6010/x/internal/v3/fav"
_addFav = "http://127.0.0.1:6010/x/internal/v3/fav/add"
_isFavored = "http://127.0.0.1:6010/x/internal/v3/fav/favored"
_delFav = "http://127.0.0.1:6010/x/internal/v3/fav/del"
)
func TestHttp(t *testing.T) {
if err := conf.Init(); err != nil {
t.Fatalf("conf.Init() error(%v)", err)
}
svr := service.New(conf.Conf)
client := xhttp.NewClient(conf.Conf.HTTPClient)
Init(conf.Conf, svr)
// video foler
testVideoFolders(client, t, _mid, _vmid, _aid)
testRenameVideoFolder(client, t, _mid, _fid, _rename)
testAddVideoFolder(client, t, _mid, _public, _name)
testSortVideoFolder(client, t, _mid, _fidsSort)
testUpStateVideoFolder(client, t, _mid, _fid, _public)
testDelVideoFolder(client, t, _mid, _delFid)
// video
testVideos(client, t, _mid, _vmid, _fid)
testFavVideoNewest(client, t, _mid, _vmid, _searchFid, _pn, _ps)
testAddFavVideo(client, t, _mid, _fid, _aid)
testMoveFavVideos(client, t, _mid, _oldFid, _newFid, _aids)
testCopyFavVideos(client, t, _mid, _oldFid, _newFid, _aids)
testIsFavoured(client, t, _mid, _aid)
testIsFavoureds(client, t, _mid, _aids)
testInDefaultFav(client, t, _mid, _aid)
testDelVideo(client, t, _mid, _fid, _aid)
testDelVideos(client, t, _mid, _fid, _aids)
// topic
testFavTopics(client, t, _mid, _pn, _ps)
testAddFavTopic(client, t, _mid, _tpid)
testIsTopicFavoured(client, t, _mid, _tpid)
testDelFavTopic(client, t, _mid, _tpid)
// fav v3
testFavorites(client, t, _type, _mid, _vmid, _v3Fid)
testAddFav(client, t, _type, _mid, _v3Fid, _oid)
testIsFavored(client, t, _type, _mid, _v3Fid, _oid)
testDelFav(client, t, _type, _mid, _v3Fid, _oid)
}
func testVideoFolders(client *xhttp.Client, t *testing.T, mid, vmid, aid int64) {
params := &url.Values{}
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("appkey", conf.Conf.App.Key)
params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
params.Set("sign", createSign(params.Encode()))
// send
req, err := http.NewRequest("GET", _videoFolders+"?"+params.Encode(), nil)
t.Log(req.URL.String())
if err != nil {
t.Errorf("NewRequest() error(%v)", err)
}
res := map[string]interface{}{}
if err = client.Do(context.TODO(), req, &res); err != nil {
t.Errorf("client.Do() error(%v)", err)
} else {
result("VideoFolders", t, res)
}
}
func testAddVideoFolder(client *xhttp.Client, t *testing.T, mid, public int64, name string) {
params := &url.Values{}
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("public", strconv.FormatInt(public, 10))
params.Set("name", name)
params.Set("appkey", conf.Conf.App.Key)
params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
params.Set("sign", createSign(params.Encode()))
// send
req, err := http.NewRequest("POST", _addVideoFolder+"?"+params.Encode(), nil)
t.Log(req.URL.String())
if err != nil {
t.Errorf("NewRequest() error(%v)", err)
}
res := map[string]interface{}{}
if err = client.Do(context.TODO(), req, &res); err != nil {
t.Errorf("client.Do() error(%v)", err)
} else {
result("DelVideoFolder", t, res)
}
}
func testRenameVideoFolder(client *xhttp.Client, t *testing.T, mid, fid int64, name string) {
params := &url.Values{}
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("fid", strconv.FormatInt(fid, 10))
params.Set("name", name)
params.Set("appkey", conf.Conf.App.Key)
params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
params.Set("sign", createSign(params.Encode()))
// send
req, err := http.NewRequest("POST", _renameVideoFolder+"?"+params.Encode(), nil)
t.Log(req.URL.String())
if err != nil {
t.Errorf("NewRequest() error(%v)", err)
}
res := map[string]interface{}{}
if err = client.Do(context.TODO(), req, &res); err != nil {
t.Errorf("client.Do() error(%v)", err)
} else {
result("RenameVideoFolder", t, res)
}
}
func testUpStateVideoFolder(client *xhttp.Client, t *testing.T, mid, fid, public int64) {
params := &url.Values{}
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("fid", strconv.FormatInt(fid, 10))
params.Set("public", strconv.FormatInt(public, 10))
params.Set("appkey", conf.Conf.App.Key)
params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
params.Set("sign", createSign(params.Encode()))
// send
req, err := http.NewRequest("POST", _upStateVideoFolder+"?"+params.Encode(), nil)
t.Log(req.URL.String())
if err != nil {
t.Errorf("NewRequest() error(%v)", err)
}
res := map[string]interface{}{}
if err = client.Do(context.TODO(), req, &res); err != nil {
t.Errorf("client.Do() error(%v)", err)
} else {
result("RenameVideoFolder", t, res)
}
}
func testSortVideoFolder(client *xhttp.Client, t *testing.T, mid int64, fids string) {
params := &url.Values{}
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("fids", fids)
params.Set("appkey", conf.Conf.App.Key)
params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
params.Set("sign", createSign(params.Encode()))
// send
req, err := http.NewRequest("POST", _sortVideoFolders+"?"+params.Encode(), nil)
t.Log(req.URL.String())
if err != nil {
t.Errorf("NewRequest() error(%v)", err)
}
res := map[string]interface{}{}
if err = client.Do(context.TODO(), req, &res); err != nil {
t.Errorf("client.Do() error(%v)", err)
} else {
result("RenameVideoFolder", t, res)
}
}
func testDelVideoFolder(client *xhttp.Client, t *testing.T, mid, fid int64) {
params := &url.Values{}
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("fid", strconv.FormatInt(fid, 10))
params.Set("appkey", conf.Conf.App.Key)
params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
params.Set("sign", createSign(params.Encode()))
// send
req, err := http.NewRequest("POST", _delVideoFolder+"?"+params.Encode(), nil)
t.Log(req.URL.String())
if err != nil {
t.Errorf("NewRequest() error(%v)", err)
}
res := map[string]interface{}{}
if err = client.Do(context.TODO(), req, &res); err != nil {
t.Errorf("client.Do() error(%v)", err)
} else {
result("DelVideoFolder", t, res)
}
}
func testVideos(client *xhttp.Client, t *testing.T, mid, vmid, fid int64) {
params := &url.Values{}
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("fid", strconv.FormatInt(fid, 10))
params.Set("appkey", conf.Conf.App.Key)
params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
params.Set("sign", createSign(params.Encode()))
// send
req, err := http.NewRequest("GET", _favVideo+"?"+params.Encode(), nil)
t.Log(req.URL.String())
if err != nil {
t.Errorf("NewRequest() error(%v)", err)
}
res := map[string]interface{}{}
if err = client.Do(context.TODO(), req, &res); err != nil {
t.Errorf("client.Do() error(%v)", err)
} else {
result("Videos", t, res)
}
}
func testFavVideoNewest(client *xhttp.Client, t *testing.T, mid, vmid, fid, pn, ps int64) {
params := &url.Values{}
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("fid", strconv.FormatInt(fid, 10))
params.Set("pn", strconv.FormatInt(pn, 10))
params.Set("ps", strconv.FormatInt(ps, 10))
params.Set("appkey", conf.Conf.App.Key)
params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
params.Set("sign", createSign(params.Encode()))
// send
req, err := http.NewRequest("GET", _favVideoNewest+"?"+params.Encode(), nil)
t.Log(req.URL.String())
if err != nil {
t.Errorf("NewRequest() error(%v)", err)
}
res := map[string]interface{}{}
if err = client.Do(context.TODO(), req, &res); err != nil {
t.Errorf("client.Do() error(%v)", err)
} else {
result("FavVideoNewest", t, res)
}
}
func testAddFavVideo(client *xhttp.Client, t *testing.T, mid, fid, aid int64) {
params := &url.Values{}
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("fid", strconv.FormatInt(fid, 10))
params.Set("aid", strconv.FormatInt(aid, 10))
params.Set("appkey", conf.Conf.App.Key)
params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
params.Set("sign", createSign(params.Encode()))
// send
req, err := http.NewRequest("POST", _addFavVideo+"?"+params.Encode(), nil)
t.Log(req.URL.String())
if err != nil {
t.Errorf("NewRequest() error(%v)", err)
}
res := map[string]interface{}{}
if err = client.Do(context.TODO(), req, &res); err != nil {
t.Errorf("client.Do() error(%v)", err)
} else {
result("AddFavVideo", t, res)
}
}
func testMoveFavVideos(client *xhttp.Client, t *testing.T, mid, oldFid, newFid int64, aids string) {
params := &url.Values{}
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("old_fid", strconv.FormatInt(oldFid, 10))
params.Set("new_fid", strconv.FormatInt(newFid, 10))
params.Set("aids", aids)
params.Set("appkey", conf.Conf.App.Key)
params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
params.Set("sign", createSign(params.Encode()))
// send
req, err := http.NewRequest("POST", _moveFavVideos+"?"+params.Encode(), nil)
t.Log(req.URL.String())
if err != nil {
t.Errorf("NewRequest() error(%v)", err)
}
res := map[string]interface{}{}
if err = client.Do(context.TODO(), req, &res); err != nil {
t.Errorf("client.Do() error(%v)", err)
} else {
result("MoveFavVideos", t, res)
}
}
func testCopyFavVideos(client *xhttp.Client, t *testing.T, mid, oldFid, newFid int64, aids string) {
params := &url.Values{}
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("old_fid", strconv.FormatInt(oldFid, 10))
params.Set("new_fid", strconv.FormatInt(newFid, 10))
params.Set("aids", aids)
params.Set("appkey", conf.Conf.App.Key)
params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
params.Set("sign", createSign(params.Encode()))
// send
req, err := http.NewRequest("POST", _copyFavVideos+"?"+params.Encode(), nil)
t.Log(req.URL.String())
if err != nil {
t.Errorf("NewRequest() error(%v)", err)
}
res := map[string]interface{}{}
if err = client.Do(context.TODO(), req, &res); err != nil {
t.Errorf("client.Do() error(%v)", err)
} else {
result("CopyFavVideos", t, res)
}
}
func testIsFavoured(client *xhttp.Client, t *testing.T, mid, aid int64) {
params := &url.Values{}
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("aid", strconv.FormatInt(aid, 10))
params.Set("appkey", conf.Conf.App.Key)
params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
params.Set("sign", createSign(params.Encode()))
// send
req, err := http.NewRequest("GET", _isFavoured+"?"+params.Encode(), nil)
t.Log(req.URL.String())
if err != nil {
t.Errorf("NewRequest() error(%v)", err)
}
res := map[string]interface{}{}
if err = client.Do(context.TODO(), req, &res); err != nil {
t.Errorf("client.Do() error(%v)", err)
} else {
result("IsFavoured", t, res)
}
}
func testIsFavoureds(client *xhttp.Client, t *testing.T, mid int64, aids string) {
params := &url.Values{}
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("aids", aids)
params.Set("appkey", conf.Conf.App.Key)
params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
params.Set("sign", createSign(params.Encode()))
// send
req, err := http.NewRequest("GET", _isFavoureds+"?"+params.Encode(), nil)
t.Log(req.URL.String())
if err != nil {
t.Errorf("NewRequest() error(%v)", err)
}
res := map[string]interface{}{}
if err = client.Do(context.TODO(), req, &res); err != nil {
t.Errorf("client.Do() error(%v)", err)
} else {
result("IsFavoureds", t, res)
}
}
func testInDefaultFav(client *xhttp.Client, t *testing.T, mid, aid int64) {
params := &url.Values{}
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("aid", strconv.FormatInt(aid, 10))
params.Set("appkey", conf.Conf.App.Key)
params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
params.Set("sign", createSign(params.Encode()))
// send
req, err := http.NewRequest("GET", _inDefaultFav+"?"+params.Encode(), nil)
t.Log(req.URL.String())
if err != nil {
t.Errorf("NewRequest() error(%v)", err)
}
res := map[string]interface{}{}
if err = client.Do(context.TODO(), req, &res); err != nil {
t.Errorf("client.Do() error(%v)", err)
} else {
result("IsFavoureds", t, res)
}
}
func testDelVideo(client *xhttp.Client, t *testing.T, mid, fid, aid int64) {
params := &url.Values{}
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("aid", strconv.FormatInt(aid, 10))
params.Set("appkey", conf.Conf.App.Key)
params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
params.Set("sign", createSign(params.Encode()))
// send
req, err := http.NewRequest("POST", _delFavVideo+"?"+params.Encode(), nil)
t.Log(req.URL.String())
if err != nil {
t.Errorf("NewRequest() error(%v)", err)
}
res := map[string]interface{}{}
if err = client.Do(context.TODO(), req, &res); err != nil {
t.Errorf("client.Do() error(%v)", err)
} else {
result("DelVideo", t, res)
}
}
func testDelVideos(client *xhttp.Client, t *testing.T, mid, fid int64, aids string) {
params := &url.Values{}
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("fid", strconv.FormatInt(fid, 10))
params.Set("aids", aids)
params.Set("appkey", conf.Conf.App.Key)
params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
params.Set("sign", createSign(params.Encode()))
// send
req, err := http.NewRequest("POST", _delFavVideos+"?"+params.Encode(), nil)
t.Log(req.URL.String())
if err != nil {
t.Errorf("NewRequest() error(%v)", err)
}
res := map[string]interface{}{}
if err = client.Do(context.TODO(), req, &res); err != nil {
t.Errorf("client.Do() error(%v)", err)
} else {
result("DelVideos", t, res)
}
}
func testFavTopics(client *xhttp.Client, t *testing.T, mid, pn, ps int64) {
params := &url.Values{}
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("pn", strconv.FormatInt(pn, 10))
params.Set("ps", strconv.FormatInt(ps, 10))
params.Set("appkey", conf.Conf.App.Key)
params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
params.Set("sign", createSign(params.Encode()))
// send
req, err := http.NewRequest("GET", _favTopics+"?"+params.Encode(), nil)
t.Log(req.URL.String())
if err != nil {
t.Errorf("NewRequest() error(%v)", err)
}
res := map[string]interface{}{}
if err = client.Do(context.TODO(), req, &res); err != nil {
t.Errorf("client.Do() error(%v)", err)
} else {
result("FavTopics", t, res)
}
}
func testAddFavTopic(client *xhttp.Client, t *testing.T, mid, tpid int64) {
params := &url.Values{}
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("tpid", strconv.FormatInt(tpid, 10))
params.Set("appkey", conf.Conf.App.Key)
params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
params.Set("sign", createSign(params.Encode()))
// send
req, err := http.NewRequest("POST", _addFavTopic+"?"+params.Encode(), nil)
t.Log(req.URL.String())
if err != nil {
t.Errorf("NewRequest() error(%v)", err)
}
res := map[string]interface{}{}
if err = client.Do(context.TODO(), req, &res); err != nil {
t.Errorf("client.Do() error(%v)", err)
} else {
result("AddFavTopic", t, res)
}
}
func testIsTopicFavoured(client *xhttp.Client, t *testing.T, mid, tpid int64) {
params := &url.Values{}
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("tpid", strconv.FormatInt(tpid, 10))
params.Set("appkey", conf.Conf.App.Key)
params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
params.Set("sign", createSign(params.Encode()))
// send
req, err := http.NewRequest("GET", _isTopicFavoured+"?"+params.Encode(), nil)
t.Log(req.URL.String())
if err != nil {
t.Errorf("NewRequest() error(%v)", err)
}
res := map[string]interface{}{}
if err = client.Do(context.TODO(), req, &res); err != nil {
t.Errorf("client.Do() error(%v)", err)
} else {
result("IsTopicFavoured", t, res)
}
}
func testDelFavTopic(client *xhttp.Client, t *testing.T, mid, tpid int64) {
params := &url.Values{}
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("tpid", strconv.FormatInt(tpid, 10))
params.Set("appkey", conf.Conf.App.Key)
params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
params.Set("sign", createSign(params.Encode()))
// send
req, err := http.NewRequest("POST", _delFavTopic+"?"+params.Encode(), nil)
t.Log(req.URL.String())
if err != nil {
t.Errorf("NewRequest() error(%v)", err)
}
res := map[string]interface{}{}
if err = client.Do(context.TODO(), req, &res); err != nil {
t.Errorf("client.Do() error(%v)", err)
} else {
result("DelFavTopic", t, res)
}
}
func testFavorites(client *xhttp.Client, t *testing.T, tp, mid, vmid, fid int64) {
params := &url.Values{}
params.Set("type", strconv.FormatInt(tp, 10))
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("fid", strconv.FormatInt(fid, 10))
params.Set("appkey", conf.Conf.App.Key)
params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
params.Set("sign", createSign(params.Encode()))
// send
req, err := http.NewRequest("GET", _favorites+"?"+params.Encode(), nil)
t.Log(req.URL.String())
if err != nil {
t.Errorf("NewRequest() error(%v)", err)
}
res := map[string]interface{}{}
if err = client.Do(context.TODO(), req, &res); err != nil {
t.Errorf("client.Do() error(%v)", err)
} else {
result("Favorites", t, res)
}
}
func testAddFav(client *xhttp.Client, t *testing.T, tp, mid, fid, oid int64) {
params := &url.Values{}
params.Set("type", strconv.FormatInt(tp, 10))
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("fid", strconv.FormatInt(fid, 10))
params.Set("oid", strconv.FormatInt(oid, 10))
params.Set("appkey", conf.Conf.App.Key)
params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
params.Set("sign", createSign(params.Encode()))
// send
req, err := http.NewRequest("POST", _addFav+"?"+params.Encode(), nil)
t.Log(req.URL.String())
if err != nil {
t.Errorf("NewRequest() error(%v)", err)
}
res := map[string]interface{}{}
if err = client.Do(context.TODO(), req, &res); err != nil {
t.Errorf("client.Do() error(%v)", err)
} else {
result("AddFav", t, res)
}
}
func testIsFavored(client *xhttp.Client, t *testing.T, tp, mid, fid, oid int64) {
params := &url.Values{}
params.Set("type", strconv.FormatInt(tp, 10))
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("fid", strconv.FormatInt(fid, 10))
params.Set("oid", strconv.FormatInt(oid, 10))
params.Set("appkey", conf.Conf.App.Key)
params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
params.Set("sign", createSign(params.Encode()))
// send
req, err := http.NewRequest("GET", _isFavored+"?"+params.Encode(), nil)
t.Log(req.URL.String())
if err != nil {
t.Errorf("NewRequest() error(%v)", err)
}
res := map[string]interface{}{}
if err = client.Do(context.TODO(), req, &res); err != nil {
t.Errorf("client.Do() error(%v)", err)
} else {
result("IsFavored", t, res)
}
}
func testDelFav(client *xhttp.Client, t *testing.T, tp, mid, fid, oid int64) {
params := &url.Values{}
params.Set("type", strconv.FormatInt(tp, 10))
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("fid", strconv.FormatInt(fid, 10))
params.Set("oid", strconv.FormatInt(oid, 10))
params.Set("appkey", conf.Conf.App.Key)
params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
params.Set("sign", createSign(params.Encode()))
// send
req, err := http.NewRequest("POST", _delFav+"?"+params.Encode(), nil)
t.Log(req.URL.String())
if err != nil {
t.Errorf("NewRequest() error(%v)", err)
}
res := map[string]interface{}{}
if err = client.Do(context.TODO(), req, &res); err != nil {
t.Errorf("client.Do() error(%v)", err)
} else {
result("DelFav", t, res)
}
}
func createSign(params string) string {
mh := md5.Sum([]byte(params + conf.Conf.App.Secret))
return hex.EncodeToString(mh[:])
}
func result(name string, t *testing.T, res map[string]interface{}) {
t.Log("[==========" + name + " Testing Result==========]")
if rs, ok := res["code"]; ok {
t.Log(fmt.Sprintf("code:%v, message:%s, data:%v", rs, res["message"], res["data"]))
}
t.Log("[↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑]\r\n")
}

View File

@@ -0,0 +1,93 @@
package http
import (
"strconv"
"go-common/app/interface/main/favorite/conf"
"go-common/app/interface/main/favorite/model"
"go-common/library/ecode"
"go-common/library/log"
bm "go-common/library/net/http/blademaster"
)
func addFavTopic(c *bm.Context) {
params := c.Request.Form
midIfc, _ := c.Get("mid")
tpStr := params.Get("tpid")
tp, err := strconv.ParseInt(tpStr, 10, 64)
if err != nil {
log.Error("strconv.ParseInt(%s) error(%s)", tpStr)
c.JSON(nil, ecode.RequestErr)
return
}
err = favSvc.AddFavTopic(c, midIfc.(int64), tp, c.Request.Header.Get("Cookie"), params.Get("access_key"))
c.JSON(nil, err)
}
func delFavTopic(c *bm.Context) {
params := c.Request.Form
mid, _ := c.Get("mid")
tpStr := params.Get("tpid")
tp, err := strconv.ParseInt(tpStr, 10, 64)
if err != nil {
log.Error("strconv.ParseInt(%s) error(%s)", tpStr)
c.JSON(nil, ecode.RequestErr)
return
}
err = favSvc.DelFavTopic(c, mid.(int64), tp)
c.JSON(nil, err)
}
// isTopicFavouried determine topic whether or not favouried by mid
func isTopicFavoured(c *bm.Context) {
params := c.Request.Form
mid, _ := c.Get("mid")
tpStr := params.Get("tpid")
tp, err := strconv.ParseInt(tpStr, 10, 64)
if err != nil {
log.Error("strconv.ParseInt(%s) error(%s)", tpStr)
c.JSON(nil, ecode.RequestErr)
return
}
faved, err := favSvc.IsTopicFavoured(c, mid.(int64), tp)
if err != nil {
c.JSON(nil, err)
return
}
data := map[string]interface{}{"favoured": faved}
c.JSON(data, nil)
}
func favTopics(c *bm.Context) {
var appInfo *model.AppInfo
params := c.Request.Form
mid, _ := c.Get("mid")
pnStr := params.Get("pn")
psStr := params.Get("ps")
pn, err := strconv.Atoi(pnStr)
if err != nil || pn < 1 {
pn = 1
}
ps, err := strconv.Atoi(psStr)
if err != nil || ps >= conf.Conf.Fav.MaxPagesize || ps <= 0 {
ps = conf.Conf.Fav.MaxPagesize
}
platformStr := params.Get("platform")
buildStr := params.Get("build")
mobiAppStr := params.Get("mobi_app")
deviceStr := params.Get("device")
if platformStr != "" && buildStr != "" {
appInfo = &model.AppInfo{
Platform: platformStr,
Build: buildStr,
MobiApp: mobiAppStr,
Device: deviceStr,
}
}
data, err := favSvc.FavTopics(c, mid.(int64), pn, ps, appInfo)
if err != nil {
c.JSON(nil, err)
return
}
c.JSON(data, nil)
}

View File

@@ -0,0 +1,647 @@
package http
import (
"strconv"
"strings"
"go-common/app/interface/main/favorite/conf"
"go-common/library/ecode"
"go-common/library/log"
"go-common/library/log/infoc"
bm "go-common/library/net/http/blademaster"
"go-common/library/xstr"
)
// videoFolders get all favorite folders
func videoFolders(c *bm.Context) {
var (
uid int64
mid int64
vmid int64
aid int64
err error
isSelf bool
mediaList bool
fromWeb bool
)
req := c.Request
if midI, ok := c.Get("mid"); ok {
mid = midI.(int64)
}
if req.Form.Get("medialist") == "1" {
mediaList = true
}
params := req.URL.Query()
app := params.Get("mobi_app")
build, _ := strconv.ParseInt(params.Get("build"), 10, 64)
device := params.Get("device")
if (app == "android" && build >= 5360001 && build <= 5361000) || (app == "iphone" && build == 8300 && device == "phone") {
mediaList = true
}
if app == "" {
fromWeb = true
}
vmidStr := req.Form.Get("vmid")
if vmidStr != "" {
uid, err = strconv.ParseInt(vmidStr, 10, 64)
if err != nil {
c.JSON(nil, err)
return
}
if uid <= 0 {
c.JSON(nil, ecode.RequestErr)
return
}
isSelf = mid == uid
vmid = uid
} else if mid != 0 {
uid = mid
isSelf = true
} else {
c.JSON(nil, ecode.RequestErr)
return
}
aidStr := req.Form.Get("aid")
if aidStr != "" {
if aid, err = strconv.ParseInt(aidStr, 10, 64); err != nil {
log.Error("strconv.ParseInt(aid:%s)", aidStr)
return
}
}
data, err := favSvc.FavFolders(c, mid, vmid, uid, aid, isSelf, mediaList, fromWeb)
c.JSON(data, err)
}
// addVideoFolder add a folder.
func addVideoFolder(c *bm.Context) {
mid, _ := c.Get("mid")
params := c.Request.Form
name := params.Get("name")
pubStr := params.Get("public")
if name == "" || len([]rune(name)) > conf.Conf.Fav.MaxNameLen {
log.Warn("arg name(%s) is empty or it's length more than %d", name, conf.Conf.Fav.MaxNameLen)
c.JSON(nil, ecode.FavNameTooLong)
return
}
var (
pub int64
err error
)
if pubStr != "" {
if pub, err = strconv.ParseInt(pubStr, 10, 64); err != nil || pub < 0 || pub > 1 {
pub = 0
}
}
var fid int64
if fid, err = favSvc.AddFavFolder(c, mid.(int64), name, c.Request.Header.Get("Cookie"), params.Get("access_key"), int32(pub)); err != nil {
c.JSON(nil, err)
return
}
data := map[string]int64{
"fid": fid,
}
c.JSON(data, nil)
}
// renameVideoFolder rename folder.
func renameVideoFolder(c *bm.Context) {
mid, _ := c.Get("mid")
params := c.Request.Form
fidStr := params.Get("fid")
name := params.Get("name")
if fidStr == "" {
log.Warn("arg fid is empty")
c.JSON(nil, ecode.RequestErr)
return
}
if name == "" || len([]rune(name)) > conf.Conf.Fav.MaxNameLen {
log.Warn("arg name(%s) is empty or it's length more than %d", name, conf.Conf.Fav.MaxNameLen)
c.JSON(nil, ecode.FavNameTooLong)
return
}
fid, err := strconv.ParseInt(fidStr, 10, 64)
if err != nil {
log.Error("strconv.ParseInt(%s) error(%v)", fidStr, err)
c.JSON(nil, ecode.RequestErr)
return
}
err = favSvc.UpFavName(c, mid.(int64), fid, name, c.Request.Header.Get("Cookie"), params.Get("access_key"))
c.JSON(nil, err)
}
// upStateVideoFolder update folder's state.
func upStateVideoFolder(c *bm.Context) {
mid, _ := c.Get("mid")
params := c.Request.Form
fidStr := params.Get("fid")
pubStr := params.Get("public")
if fidStr == "" || pubStr == "" {
log.Warn("method fid(%s) public(%s) is empty", fidStr, pubStr)
c.JSON(nil, ecode.RequestErr)
return
}
fid, err := strconv.ParseInt(fidStr, 10, 64)
if err != nil {
log.Error("strconv.ParseInt(%s) error(%v)", fidStr, err)
c.JSON(nil, ecode.RequestErr)
return
}
public, err := strconv.Atoi(pubStr)
if err != nil {
log.Error("strconv.ParseInt(%s) error(%v)", pubStr, err)
c.JSON(nil, ecode.RequestErr)
return
}
err = favSvc.UpFavState(c, mid.(int64), fid, int32(public), c.Request.Header.Get("Cookie"), params.Get("access_key"))
c.JSON(nil, err)
}
// delVideoFolder delete folder.
func delVideoFolder(c *bm.Context) {
mid, _ := c.Get("mid")
params := c.Request.Form
fidStr := params.Get("fid")
if fidStr == "" {
log.Warn("method fid(%s) is empty", fidStr)
c.JSON(nil, ecode.RequestErr)
return
}
fid, err := strconv.ParseInt(fidStr, 10, 64)
if err != nil {
log.Error("strconv.ParseInt(%s) error(%v)", fidStr, err)
c.JSON(nil, ecode.RequestErr)
return
}
err = favSvc.DelVideoFolder(c, mid.(int64), fid)
c.JSON(nil, err)
}
// sortFavFolders sort all favorite folders
func sortVideoFolders(c *bm.Context) {
var (
fids []int64
err error
)
params := c.Request.Form
fidStr := params.Get("fids")
mid, _ := c.Get("mid")
if fidStr == "" {
log.Error("arg fids is empty")
c.JSON(nil, ecode.RequestErr)
return
}
fids, err = xstr.SplitInts(fidStr)
if err != nil {
log.Error("xstr.SplitInts(%s) error(%v)", fidStr, err)
c.JSON(nil, ecode.RequestErr)
return
}
err = favSvc.SetVideoFolderSort(c, mid.(int64), fids)
c.JSON(nil, err)
}
// favVideo return all videos in the fid folder.
func favVideo(c *bm.Context) {
// params
params := c.Request.Form
vmidStr := params.Get("vmid")
fidStr := params.Get("fid")
tidStr := params.Get("tid")
keywordStr := params.Get("keyword")
orderStr := params.Get("order")
pnStr := params.Get("pn")
psStr := params.Get("ps")
var (
err error
mid int64
vmid int64
uid int64
)
if midI, ok := c.Get("mid"); ok {
mid = midI.(int64)
}
if vmidStr != "" {
if uid, err = strconv.ParseInt(vmidStr, 10, 64); err != nil || uid <= 0 {
log.Error("vmid(%s) need a number > 0 error(%v)", vmidStr, err)
c.JSON(nil, ecode.RequestErr)
return
}
vmid = uid
} else if mid != 0 {
uid = mid
} else {
log.Warn("mid(%d) && vmidStr(%s)) is empty", mid, vmidStr)
c.JSON(nil, ecode.RequestErr)
return
}
fid, _ := strconv.ParseInt(fidStr, 10, 64)
tid, err := strconv.Atoi(tidStr)
if err != nil {
tid = 0
}
pn, err := strconv.Atoi(pnStr)
if err != nil || pn < 1 {
pn = 1
}
ps, err := strconv.Atoi(psStr)
if err != nil || ps > conf.Conf.Fav.MaxPagesize || ps <= 0 {
ps = conf.Conf.Fav.MaxPagesize
}
// fav video
data, err := favSvc.FavVideo(c, mid, vmid, uid, fid, keywordStr, orderStr, tid, pn, ps)
if err != nil {
c.JSON(nil, err)
return
}
c.JSON(data, nil)
}
// tidList return all tids in the fid folder.
func tidList(c *bm.Context) {
// params
params := c.Request.Form
vmidStr := params.Get("vmid")
fidStr := params.Get("fid")
var (
err error
mid int64
vmid int64
uid int64
)
if midI, ok := c.Get("mid"); ok {
mid = midI.(int64)
}
if vmidStr != "" {
if uid, err = strconv.ParseInt(vmidStr, 10, 64); err != nil || uid <= 0 {
log.Error("vmid(%s) need a number > 0 error(%v)", vmidStr, err)
c.JSON(nil, ecode.RequestErr)
return
}
vmid = uid
} else if mid != 0 {
uid = mid
} else {
log.Warn("mid(%d) && vmidStr(%s)) is empty", mid, vmidStr)
c.JSON(nil, ecode.RequestErr)
return
}
fid, _ := strconv.ParseInt(fidStr, 10, 64)
// fav video
data, err := favSvc.TidList(c, mid, vmid, uid, fid)
if err != nil {
c.JSON(nil, err)
return
}
c.JSON(data, nil)
}
// favVideoNewest return the newest videos in the all folder.
func favVideoNewest(c *bm.Context) {
midIf, _ := c.Get("mid")
mid := midIf.(int64)
params := c.Request.URL.Query()
pnStr := params.Get("pn")
psStr := params.Get("ps")
pn, err := strconv.Atoi(pnStr)
if err != nil || pn < 1 {
pn = 1
}
ps, err := strconv.Atoi(psStr)
if err != nil || ps > conf.Conf.Fav.MaxPagesize || ps <= 0 {
ps = conf.Conf.Fav.MaxPagesize
}
data, err := favSvc.RecentArcs(c, mid, pn, ps)
if err != nil {
c.JSON(nil, err)
return
}
c.JSON(data, nil)
}
// addFavVideo add a video into folder.
func addFavVideo(c *bm.Context) {
midIf, _ := c.Get("mid")
mid := midIf.(int64)
params := c.Request.Form
fidsStr := params.Get("fid")
aidStr := params.Get("aid")
if aidStr == "" {
log.Warn("params aid(%s) is empty", aidStr)
c.JSON(nil, ecode.RequestErr)
return
}
fids, err := xstr.SplitInts(fidsStr)
if err != nil {
log.Warn("xstr.PlitInts(fids:%s) err(%v)", fidsStr, err)
}
aid, err := strconv.ParseInt(aidStr, 10, 64)
if err != nil {
log.Error("strconv.ParseInt(%s) error(%v)", aidStr, err)
c.JSON(nil, ecode.RequestErr)
return
}
if len(fids) == 0 {
if err = favSvc.AddArc(c, mid, 0, aid, c.Request.Header.Get("Cookie"), params.Get("access_key")); err != nil {
c.JSON(nil, err)
return
}
}
for _, fid := range fids {
if err = favSvc.AddArc(c, mid, fid, aid, c.Request.Header.Get("Cookie"), params.Get("access_key")); err != nil {
c.JSON(nil, err)
return
}
}
if collector != nil {
collector.InfoAntiCheat2(c, "", aidStr, strconv.FormatInt(mid, 10), fidsStr, infoc.ItemTypeAv, infoc.ActionFav, "")
}
c.JSON(nil, err)
}
// delFavVideo delete a video from folder.
func delFavVideo(c *bm.Context) {
midIf, _ := c.Get("mid")
mid := midIf.(int64)
params := c.Request.Form
fidsStr := params.Get("fid")
aidStr := params.Get("aid")
if aidStr == "" {
log.Warn("method aid(%s) is empty", aidStr)
c.JSON(nil, ecode.RequestErr)
return
}
fids, err := xstr.SplitInts(fidsStr)
if err != nil {
log.Warn("xstr.SplitInts(fidsStr:%v) err(%v)", fidsStr, err)
}
aid, err := strconv.ParseInt(aidStr, 10, 64)
if err != nil {
log.Error("strconv.ParseInt(%s) error(%v)", aidStr, err)
c.JSON(nil, ecode.RequestErr)
return
}
if len(fids) == 0 {
err = favSvc.DelArc(c, mid, 0, aid)
c.JSON(nil, err)
return
}
for _, fid := range fids {
err = favSvc.DelArc(c, mid, fid, aid)
}
c.JSON(nil, err)
}
// moveFavVideos move some video into other folder.
func moveFavVideos(c *bm.Context) {
mid, _ := c.Get("mid")
params := c.Request.Form
ofidStr := params.Get("old_fid")
nfidStr := params.Get("new_fid")
aidsStr := params.Get("aids")
if aidsStr == "" || ofidStr == "" || nfidStr == "" {
log.Warn("method aids(%s) old_fid(%s) new_fid(%s) is empty", aidsStr, ofidStr, nfidStr)
c.JSON(nil, ecode.RequestErr)
return
}
if ofidStr == nfidStr {
log.Warn("move videos to the same folder...")
c.JSON(nil, ecode.FavFolderSame)
return
}
ofid, err := strconv.ParseInt(ofidStr, 10, 64)
if err != nil {
log.Error("strconv.ParseInt(%s) error(%v)", ofidStr, err)
c.JSON(nil, ecode.RequestErr)
return
}
nfid, err := strconv.ParseInt(nfidStr, 10, 64)
if err != nil {
log.Error("strconv.ParseInt(%s) error(%v)", nfidStr, err)
c.JSON(nil, ecode.RequestErr)
return
}
aidArr := strings.Split(aidsStr, ",")
if len(aidArr) == 0 {
c.JSON(nil, ecode.RequestErr)
return
}
if len(aidArr) > conf.Conf.Fav.MaxOperationNum {
c.JSON(nil, ecode.FavMaxOperNum)
return
}
aids := make([]int64, len(aidArr))
var aid int64
for i, aidStr := range aidArr {
aid, err = strconv.ParseInt(aidStr, 10, 64)
if err != nil {
log.Error("strconv.ParseInt(%s) error(%v)", aidStr, err)
c.JSON(nil, ecode.RequestErr)
return
}
aids[i] = aid
}
err = favSvc.MoveArcs(c, mid.(int64), ofid, nfid, aids)
c.JSON(nil, err)
}
// copyFavVideos move some video into other folder.
func copyFavVideos(c *bm.Context) {
midIf, _ := c.Get("mid")
mid := midIf.(int64)
params := c.Request.Form
omidStr := params.Get("old_mid")
ofidStr := params.Get("old_fid")
nfidStr := params.Get("new_fid")
aidsStr := params.Get("aids")
if aidsStr == "" || ofidStr == "" || nfidStr == "" {
log.Warn("method aids(%s) old_fid(%s) new_mid(%s) is empty", aidsStr, ofidStr, nfidStr)
c.JSON(nil, ecode.RequestErr)
return
}
if ofidStr == nfidStr {
log.Warn("copy videos to the same folder...")
c.JSON(nil, ecode.FavFolderSame)
return
}
omid, err := strconv.ParseInt(omidStr, 10, 64)
if err != nil {
log.Warn("strconv.ParseInt(%s) error(%v)", omidStr, err)
omid = mid
}
ofid, err := strconv.ParseInt(ofidStr, 10, 64)
if err != nil {
log.Error("strconv.ParseInt(%s) error(%v)", ofidStr, err)
c.JSON(nil, ecode.RequestErr)
return
}
nfid, err := strconv.ParseInt(nfidStr, 10, 64)
if err != nil {
log.Error("strconv.ParseInt(%s) error(%v)", nfidStr, err)
c.JSON(nil, ecode.RequestErr)
return
}
aidArr := strings.Split(aidsStr, ",")
if len(aidArr) == 0 {
c.JSON(nil, ecode.RequestErr)
return
}
if len(aidArr) > conf.Conf.Fav.MaxOperationNum {
c.JSON(nil, ecode.FavMaxOperNum)
return
}
aids := make([]int64, len(aidArr))
var aid int64
for i, aidStr := range aidArr {
aid, err = strconv.ParseInt(aidStr, 10, 64)
if err != nil {
log.Error("strconv.ParseInt(%s) error(%v)", aidStr, err)
c.JSON(nil, ecode.RequestErr)
return
}
aids[i] = aid
}
err = favSvc.CopyArcs(c, mid, omid, ofid, nfid, aids)
c.JSON(nil, err)
}
// delVideos delete some video from folder.
func delFavVideos(c *bm.Context) {
mid, _ := c.Get("mid")
params := c.Request.Form
fidStr := params.Get("fid")
aidsStr := params.Get("aids")
if aidsStr == "" {
log.Warn("method aid(%s) is empty", aidsStr)
c.JSON(nil, ecode.RequestErr)
return
}
fid, err := strconv.ParseInt(fidStr, 10, 64)
if err != nil {
log.Warn("strconv.ParseInt(%s) error(%v)", fidStr, err)
}
aidArr := strings.Split(aidsStr, ",")
if len(aidArr) == 0 {
c.JSON(nil, ecode.RequestErr)
return
}
if len(aidArr) > conf.Conf.Fav.MaxOperationNum {
c.JSON(nil, ecode.FavMaxOperNum)
return
}
aids := make([]int64, len(aidArr))
var aid int64
for i, aidStr := range aidArr {
aid, err = strconv.ParseInt(aidStr, 10, 64)
if err != nil {
log.Error("strconv.ParseInt(%s) error(%v)", aidStr, err)
c.JSON(nil, ecode.RequestErr)
return
}
aids[i] = aid
}
err = favSvc.DelArcs(c, mid.(int64), fid, aids)
c.JSON(nil, err)
}
// isFavoured detemine video whether or not favoured by mid.
func isFavoured(c *bm.Context) {
mid, _ := c.Get("mid")
params := c.Request.URL.Query()
aidStr := params.Get("aid")
aid, err := strconv.ParseInt(aidStr, 10, 64)
if err != nil {
log.Error("strconv.ParseInt(%s) error(%v)", aidStr, err)
c.JSON(nil, ecode.RequestErr)
return
}
faved, count, err := favSvc.IsFaved(c, mid.(int64), aid)
if err != nil {
log.Error("favSvr.IsFaved() err(%v)", err)
return
}
data := map[string]interface{}{"favoured": faved, "count": count}
c.JSON(data, nil)
}
// isFavoureds detemine video whether or not favoured by mid.
func isFavoureds(c *bm.Context) {
var (
aids []int64
err error
)
mid, _ := c.Get("mid")
params := c.Request.URL.Query()
aidStr := params.Get("aids")
if aidStr == "" {
log.Warn("method aid(%s) is empty", aidStr)
c.JSON(nil, ecode.RequestErr)
return
}
aids, err = xstr.SplitInts(aidStr)
if err != nil {
log.Error("strconv.ParseInt(%s) error(%v)", aidStr, err)
c.JSON(nil, ecode.RequestErr)
return
}
faved, _ := favSvc.IsFaveds(c, mid.(int64), aids)
c.JSON(faved, nil)
}
// inDefaultFav detemine video whether or not favoured in default folder.
func inDefaultFav(c *bm.Context) {
mid, _ := c.Get("mid")
params := c.Request.URL.Query()
aidStr := params.Get("aid")
if aidStr == "" {
log.Warn("method aid(%s) is empty", aidStr)
c.JSON(nil, ecode.RequestErr)
return
}
aid, err := strconv.ParseInt(aidStr, 10, 64)
if err != nil {
log.Error("strconv.ParseInt(%s) error(%v)", aidStr, err)
c.JSON(nil, ecode.RequestErr)
return
}
var isIn bool
isIn, err = favSvc.InDef(c, mid.(int64), aid)
data := map[string]bool{"default": isIn}
c.JSON(data, err)
}
// isCleaned check the clean action's cool down time and access
func isCleaned(c *bm.Context) {
mid, _ := c.Get("mid")
params := c.Request.URL.Query()
fidStr := params.Get("fid")
fid, err := strconv.ParseInt(fidStr, 10, 64)
if err != nil {
log.Error("strconv.ParseInt(%s) error(%v)", fidStr, err)
c.JSON(nil, ecode.RequestErr)
return
}
cleanState, err := favSvc.CleanState(c, mid.(int64), fid)
if err != nil {
log.Error("favSvc.IsCleaned(%d,%d) error(%v)", mid, fid, err)
c.JSON(nil, err)
return
}
data := map[string]interface{}{"state": cleanState}
c.JSON(data, nil)
}
func cleanInvalidArcs(c *bm.Context) {
mid, _ := c.Get("mid")
params := c.Request.Form
fidStr := params.Get("fid")
fid, err := strconv.ParseInt(fidStr, 10, 64)
if err != nil {
log.Error("strconv.ParseInt(%s) error(%v)", fidStr, err)
c.JSON(nil, ecode.RequestErr)
return
}
if err = favSvc.CleanInvalidArcs(c, mid.(int64), fid); err != nil {
log.Error("favSvc.CleanInvalidArcs(%d,%d) error(%v)", mid, fid, err)
}
c.JSON(nil, err)
}