package controller
-import "github.com/gin-gonic/gin"
+import (
+ "fmt"
+ "net/http"
+
+ "github.com/gin-gonic/gin"
+ pkgauth "github.com/seantywork/sorrylinus-again/pkg/auth"
+ "github.com/seantywork/sorrylinus-again/pkg/com"
+)
func GetIndex(c *gin.Context) {
func GetViewMypage(c *gin.Context) {
+ _, my_type, _ := pkgauth.WhoAmI(c)
+
+ if my_type != "admin" {
+
+ fmt.Printf("view my page: not admin\n")
+
+ c.JSON(http.StatusForbidden, com.SERVER_RE{Status: "error", Reply: "you're not admin"})
+
+ return
+
+ }
+
c.HTML(200, "mypage/index.html", gin.H{})
}
func GetViewMypageArticle(c *gin.Context) {
+ _, my_type, _ := pkgauth.WhoAmI(c)
+
+ if my_type != "admin" {
+
+ fmt.Printf("view my page: not admin\n")
+
+ c.JSON(http.StatusForbidden, com.SERVER_RE{Status: "error", Reply: "you're not admin"})
+
+ return
+
+ }
+
c.HTML(200, "mypage/article.html", gin.H{})
}
func GetViewMypageVideo(c *gin.Context) {
+ _, my_type, _ := pkgauth.WhoAmI(c)
+
+ if my_type != "admin" {
+
+ fmt.Printf("view my page: not admin\n")
+
+ c.JSON(http.StatusForbidden, com.SERVER_RE{Status: "error", Reply: "you're not admin"})
+
+ return
+
+ }
+
c.HTML(200, "mypage/video.html", gin.H{})
}
func GetViewMypageRoom(c *gin.Context) {
+ _, my_type, _ := pkgauth.WhoAmI(c)
+
+ if my_type != "admin" {
+
+ fmt.Printf("view my page: not admin\n")
+
+ c.JSON(http.StatusForbidden, com.SERVER_RE{Status: "error", Reply: "you're not admin"})
+
+ return
+
+ }
+
c.HTML(200, "mypage/room.html", gin.H{})
}
+func GetBase(c *gin.Context) {
+
+}
+
func GetViewContentArticle(c *gin.Context) {
c.HTML(200, "content/article.html", gin.H{})
e.GET("/mypage/room", GetViewMypageRoom)
+ e.GET("/api/base", GetBase)
+
e.GET("/content/article/:articleId", GetViewContentArticle)
e.GET("/content/video/:videoId", GetViewContentVideo)
pkgauth.InitAuth()
+ // sorrylinus
+
+ // e.POST("/api/sorrylinus/connect", pkgsoli.Connect)
+ // e.POST("/api/sorrylinus/disconnect", pkgsoli.Disconnect)
+ // e.POST("/api/sorrylinus/rt", pkgsoli.RoundTrip)
+
// edition
- // e.POST("/api/article/upload", pkgedition.PostArticleUpload)
+ e.POST("/api/article/upload", pkgedition.PostArticleUpload)
- // e.POST("/api/article/delete", pkgedition.PostArticleDelete)
+ e.POST("/api/article/delete", pkgedition.PostArticleDelete)
- // e.GET("/api/article/c/:contentId", pkgedition.GetArticleContentById)
+ e.GET("/api/article/c/:contentId", pkgedition.GetArticleContentById)
- // e.POST("/api/image/upload", pkgedition.PostImageUpload)
+ e.POST("/api/image/upload", pkgedition.PostImageUpload)
- // e.GET("/api/image/c/:contentId", pkgedition.GetImageContentById)
+ e.GET("/api/image/c/:contentId", pkgedition.GetImageContentById)
e.POST("/api/video/upload", pkgedition.PostVideoUpload)
pkgstream.InitWebRTCApi()
- e.POST("/api/cctv/create", pkgstream.PostCCTVCreate)
+ e.POST("/api/cctv/open", pkgstream.PostCCTVOpen)
- e.POST("/api/cctv/delete", pkgstream.PostCCTVDelete)
+ e.POST("/api/cctv/close", pkgstream.PostCCTVClose)
go pkgstream.InitRTMPServer()
return nil
}
+func UploadArticle(content string, plain_name string, new_name string) error {
+
+ ms := MediaStruct{}
+
+ ms.ISPublic = true
+ ms.Type = "article"
+ ms.PlainName = plain_name
+ ms.Extension = "json"
+
+ this_file_path := mediaPath + new_name + ".json"
+
+ this_article_path := articlePath + new_name + ".json"
+
+ content_b := []byte(content)
+
+ jb, err := json.Marshal(ms)
+
+ if err != nil {
+
+ return fmt.Errorf("failed to upload: %s", err.Error())
+ }
+
+ err = os.WriteFile(this_file_path, jb, 0644)
+
+ if err != nil {
+
+ return fmt.Errorf("failed to upload: %s", err.Error())
+ }
+
+ err = os.WriteFile(this_article_path, content_b, 0644)
+
+ if err != nil {
+
+ return fmt.Errorf("failed to upload: %s", err.Error())
+ }
+
+ return nil
+}
+
+func DeleteArticle(media_key string) error {
+
+ var ms MediaStruct
+
+ this_file_path := mediaPath + media_key + ".json"
+
+ file_b, err := os.ReadFile(this_file_path)
+
+ if err != nil {
+
+ return fmt.Errorf("failed to delete article: %s", err.Error())
+
+ }
+
+ err = json.Unmarshal(file_b, &ms)
+
+ if err != nil {
+
+ return fmt.Errorf("failed to delete article: marshal: %s", err.Error())
+ }
+
+ this_article_path := articlePath + media_key + "." + ms.Extension
+
+ err = os.Remove(this_article_path)
+
+ if err != nil {
+
+ return fmt.Errorf("failed to delete article: rmart: %s", err.Error())
+ }
+
+ err = os.Remove(this_file_path)
+
+ if err != nil {
+
+ return fmt.Errorf("failed to delete video: rmmd: %s", err.Error())
+ }
+
+ return nil
+}
+
+func GetArticle(media_key string) (string, error) {
+
+ var ms MediaStruct
+
+ var content string
+
+ this_file_path := mediaPath + media_key + ".json"
+
+ file_b, err := os.ReadFile(this_file_path)
+
+ if err != nil {
+
+ return "", fmt.Errorf("failed to get article: %s", err.Error())
+
+ }
+
+ err = json.Unmarshal(file_b, &ms)
+
+ if err != nil {
+
+ return "", fmt.Errorf("failed to get article: marshal: %s", err.Error())
+ }
+
+ if ms.Type != "article" {
+
+ return "", fmt.Errorf("failed to get article: %s: %s", "wrong type", ms.Type)
+
+ }
+
+ this_article_path := mediaPath + media_key + "." + ms.Extension
+
+ article_b, err := os.ReadFile(this_article_path)
+
+ if err != nil {
+
+ return "", fmt.Errorf("failed to get article: read file: %s", err.Error())
+
+ }
+
+ content = string(article_b)
+
+ return content, nil
+
+}
+
+func UploadImage(c *gin.Context, file *multipart.FileHeader, filename string, new_filename string, extension string) error {
+
+ ms := MediaStruct{}
+
+ this_file_path := mediaPath + new_filename + ".json"
+
+ this_image_path := imagePath + new_filename + "." + extension
+
+ ms.ISPublic = true
+ ms.Type = "image"
+ ms.PlainName = filename
+ ms.Extension = extension
+
+ jb, err := json.Marshal(ms)
+
+ if err != nil {
+
+ return fmt.Errorf("failed to upload: %s", err.Error())
+ }
+
+ err = os.WriteFile(this_file_path, jb, 0644)
+
+ if err != nil {
+
+ return fmt.Errorf("failed to upload: %s", err.Error())
+ }
+
+ err = c.SaveUploadedFile(file, this_image_path)
+
+ if err != nil {
+
+ return fmt.Errorf("failed to upload: %s", err.Error())
+ }
+
+ return nil
+}
+
+func DownloadImage(c *gin.Context, watchId string) error {
+
+ ms, err := GetByMediaKeyFromMedia(watchId)
+
+ if ms == nil {
+
+ return fmt.Errorf("failed to download image: %s", err.Error())
+
+ }
+
+ if ms.Type != "image" {
+
+ return fmt.Errorf("failed to download image: %s: %s", "wrong type", ms.Type)
+ }
+
+ this_image_path := imagePath + watchId + "." + ms.Extension
+
+ if _, err := os.Stat(this_image_path); err != nil {
+
+ return err
+
+ }
+
+ c.Header("Content-Type", "image/"+ms.Extension)
+
+ c.File(this_image_path)
+
+ return nil
+}
+
func UploadVideo(c *gin.Context, file *multipart.FileHeader, filename string, new_filename string, extension string) error {
ms := MediaStruct{}
package edition
+
+import (
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "strings"
+
+ "github.com/gin-gonic/gin"
+ pkgauth "github.com/seantywork/sorrylinus-again/pkg/auth"
+ "github.com/seantywork/sorrylinus-again/pkg/com"
+ "github.com/seantywork/sorrylinus-again/pkg/dbquery"
+ _ "github.com/seantywork/sorrylinus-again/pkg/dbquery"
+ pkgutils "github.com/seantywork/sorrylinus-again/pkg/utils"
+)
+
+type ArticleInfo struct {
+ Title string `json:"title"`
+ Content string `json:"content"`
+}
+
+func PostArticleUpload(c *gin.Context) {
+
+ _, my_type, _ := pkgauth.WhoAmI(c)
+
+ if my_type != "admin" {
+
+ fmt.Printf("article upload: not admin\n")
+
+ c.JSON(http.StatusForbidden, com.SERVER_RE{Status: "error", Reply: "you're not admin"})
+
+ return
+
+ }
+
+ var req com.CLIENT_REQ
+
+ var a_info ArticleInfo
+
+ if err := c.BindJSON(&req); err != nil {
+
+ fmt.Printf("article upload: failed to bind: %s\n", err.Error())
+
+ c.JSON(http.StatusBadRequest, com.SERVER_RE{Status: "error", Reply: "invalid format"})
+
+ return
+ }
+
+ err := json.Unmarshal([]byte(req.Data), &a_info)
+
+ if err != nil {
+
+ fmt.Printf("article upload: failed to unmarshal: %s\n", err.Error())
+
+ c.JSON(http.StatusBadRequest, com.SERVER_RE{Status: "error", Reply: "invalid format"})
+
+ return
+
+ }
+
+ new_file_name, _ := pkgutils.GetRandomHex(32)
+
+ plain_name := pkgauth.SanitizePlainNameValue(a_info.Title)
+
+ err = dbquery.UploadArticle(a_info.Content, plain_name, new_file_name)
+
+ if err != nil {
+
+ fmt.Printf("article upload: failed to upload: %s", err.Error())
+
+ c.JSON(http.StatusInternalServerError, com.SERVER_RE{Status: "error", Reply: "failed to upload"})
+
+ return
+ }
+
+ c.JSON(http.StatusOK, com.SERVER_RE{Status: "success", Reply: "uploaded"})
+
+}
+
+func PostArticleDelete(c *gin.Context) {
+
+ _, my_type, _ := pkgauth.WhoAmI(c)
+
+ if my_type != "admin" {
+
+ fmt.Printf("article delete: not admin\n")
+
+ c.JSON(http.StatusForbidden, com.SERVER_RE{Status: "error", Reply: "you're not admin"})
+
+ return
+
+ }
+
+ fmt.Println("delete article")
+
+ var req com.CLIENT_REQ
+
+ if err := c.BindJSON(&req); err != nil {
+
+ fmt.Printf("article delete: failed to bind: %s\n", err.Error())
+
+ c.JSON(http.StatusBadRequest, com.SERVER_RE{Status: "error", Reply: "invalid format"})
+
+ return
+ }
+
+ if !pkgauth.VerifyCodeNameValue(req.Data) {
+
+ fmt.Printf("article name verification failed: %s\n", req.Data)
+
+ c.JSON(http.StatusBadRequest, com.SERVER_RE{Status: "error", Reply: "invalid format"})
+
+ return
+
+ }
+
+ err := dbquery.DeleteArticle(req.Data)
+
+ if err != nil {
+
+ fmt.Printf("article delete: %s\n", err.Error())
+
+ c.JSON(http.StatusInternalServerError, com.SERVER_RE{Status: "error", Reply: "failed delete"})
+
+ return
+
+ }
+
+ c.JSON(http.StatusOK, com.SERVER_RE{Status: "success", Reply: "deleted"})
+
+}
+
+func GetArticleContentById(c *gin.Context) {
+
+ watchId := c.Param("contentId")
+
+ if !pkgauth.VerifyCodeNameValue(watchId) {
+
+ fmt.Printf("get article: illegal: %s\n", watchId)
+
+ c.JSON(http.StatusBadRequest, com.SERVER_RE{Status: "error", Reply: "invalid format"})
+
+ return
+
+ }
+
+ content, err := dbquery.GetArticle(watchId)
+
+ if err != nil {
+
+ fmt.Printf("failed to get article: %s\n", err.Error())
+
+ c.JSON(http.StatusBadRequest, com.SERVER_RE{Status: "error", Reply: "invalid format"})
+
+ return
+ }
+
+ c.JSON(http.StatusOK, com.SERVER_RE{Status: "success", Reply: content})
+
+}
+
+func PostImageUpload(c *gin.Context) {
+
+ _, my_type, _ := pkgauth.WhoAmI(c)
+
+ if my_type != "admin" {
+
+ fmt.Printf("image upload: not admin\n")
+
+ c.JSON(http.StatusForbidden, com.SERVER_RE{Status: "error", Reply: "you're not admin"})
+
+ return
+
+ }
+
+ file, _ := c.FormFile("file")
+
+ f_name := file.Filename
+
+ f_name_list := strings.Split(f_name, ".")
+
+ f_name_len := len(f_name_list)
+
+ if f_name_len < 1 {
+
+ fmt.Println("no extension specified")
+
+ c.JSON(http.StatusBadRequest, com.SERVER_RE{Status: "error", Reply: "invalid format"})
+
+ return
+ }
+
+ v_fname := pkgauth.SanitizePlainNameValue(f_name_list[0])
+
+ extension := f_name_list[f_name_len-1]
+
+ if !pkgutils.CheckIfSliceContains[string](EXTENSION_ALLOWLIST, extension) {
+
+ fmt.Println("extension not allowed")
+
+ c.JSON(http.StatusBadRequest, com.SERVER_RE{Status: "error", Reply: "invalid format"})
+
+ return
+
+ }
+
+ fmt.Printf("received: %s, size: %d\n", file.Filename, file.Size)
+
+ file_name, _ := pkgutils.GetRandomHex(32)
+
+ err := dbquery.UploadImage(c, file, v_fname, file_name, extension)
+
+ if err != nil {
+
+ fmt.Println(err.Error())
+
+ c.JSON(http.StatusInternalServerError, com.SERVER_RE{Status: "error", Reply: "failed to save"})
+
+ return
+
+ }
+
+ c.JSON(http.StatusOK, com.SERVER_RE{Status: "success", Reply: file_name})
+
+}
+
+func GetImageContentById(c *gin.Context) {
+
+ watchId := c.Param("contentId")
+
+ if !pkgauth.VerifyCodeNameValue(watchId) {
+
+ fmt.Printf("download image: illegal: %s\n", watchId)
+
+ c.JSON(http.StatusBadRequest, com.SERVER_RE{Status: "error", Reply: "invalid format"})
+
+ return
+
+ }
+
+ err := dbquery.DownloadImage(c, watchId)
+
+ if err != nil {
+
+ fmt.Printf("download image: %s\n", err.Error())
+
+ c.JSON(http.StatusBadRequest, com.SERVER_RE{Status: "error", Reply: "invalid format"})
+
+ return
+
+ }
+
+ fmt.Println("image download success")
+}
}
- fmt.Println("download success")
+ fmt.Println("video download success")
}
--- /dev/null
+package sorrylinus
+++ /dev/null
-package sorrylinushub
audioTrack *webrtc.TrackLocalStaticSample
}
-func PostCCTVCreate(c *gin.Context) {
+func PostCCTVOpen(c *gin.Context) {
log.Println("Incoming HTTP Request")
}
-func PostCCTVDelete(c *gin.Context) {
+func PostCCTVClose(c *gin.Context) {
/*