]> git.feebdaed.xyz Git - gomehub.git/commitdiff
Working base, test, sorrylinus
authorseantywork <seantywork@gmail.com>
Thu, 11 Jul 2024 10:14:50 +0000 (19:14 +0900)
committerseantywork <seantywork@gmail.com>
Thu, 11 Jul 2024 10:14:50 +0000 (19:14 +0900)
ctl/base.go
ctl/ctl.go
pkg/dbquery/dbquery.go
pkg/edition/edition.go
pkg/edition/edition_video.go
pkg/sorrylinus/sorrylinus.go [new file with mode: 0644]
pkg/sorrylinushub/sorrylinushub.go [deleted file]
pkg/stream/stream_cctv.go

index 1a402aab8238aad7085d362a25cc54122cf193dd..5c0d22b4e1cec1683a53a1c6a8b00518b4ae1433 100644 (file)
@@ -1,6 +1,13 @@
 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) {
 
@@ -15,28 +22,80 @@ func GetViewSignin(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{})
index a86a70ba8b6fb5539ab996468ef86615d7cde8d0..29ff3ead21436d8e481e40f847c27b138f4d4934 100644 (file)
@@ -97,6 +97,8 @@ func RegisterRoutes(e *gin.Engine) {
 
        e.GET("/mypage/room", GetViewMypageRoom)
 
+       e.GET("/api/base", GetBase)
+
        e.GET("/content/article/:articleId", GetViewContentArticle)
 
        e.GET("/content/video/:videoId", GetViewContentVideo)
@@ -119,17 +121,23 @@ func RegisterRoutes(e *gin.Engine) {
 
        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)
 
@@ -141,9 +149,9 @@ func RegisterRoutes(e *gin.Engine) {
 
        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()
 
index dd22078bea80fc843a897f1efc097954805556d1..62aef1cf0b4360fc2d14f129bd680923ca0988c6 100644 (file)
@@ -396,6 +396,197 @@ func MakeSessionForUser(session_key string, id string, duration_seconds int) err
        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{}
index 5b0b4e998adaa9586f4b7311b25dfeacfef13fc1..2f0ca3122475378e26e16a932f83d78a39eb09d7 100644 (file)
@@ -1 +1,254 @@
 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")
+}
index c8725b908968c20bc4022eca39e151b77517b71e..ee31e1b2859bd6fb1f3e5faa125bf3b12ff65895 100644 (file)
@@ -159,6 +159,6 @@ func GetVideoContentByID(c *gin.Context) {
 
        }
 
-       fmt.Println("download success")
+       fmt.Println("video download success")
 
 }
diff --git a/pkg/sorrylinus/sorrylinus.go b/pkg/sorrylinus/sorrylinus.go
new file mode 100644 (file)
index 0000000..499fd2b
--- /dev/null
@@ -0,0 +1 @@
+package sorrylinus
diff --git a/pkg/sorrylinushub/sorrylinushub.go b/pkg/sorrylinushub/sorrylinushub.go
deleted file mode 100644 (file)
index 32f6165..0000000
+++ /dev/null
@@ -1 +0,0 @@
-package sorrylinushub
index 0703e4f7dcfc730eeb86766cc42a399bda7c7953..8197ee73643929d8e0781de61337c95685d255c4 100644 (file)
@@ -48,7 +48,7 @@ type RTMPWebRTCPeer struct {
        audioTrack     *webrtc.TrackLocalStaticSample
 }
 
-func PostCCTVCreate(c *gin.Context) {
+func PostCCTVOpen(c *gin.Context) {
 
        log.Println("Incoming HTTP Request")
 
@@ -158,7 +158,7 @@ func PostCCTVCreate(c *gin.Context) {
 
 }
 
-func PostCCTVDelete(c *gin.Context) {
+func PostCCTVClose(c *gin.Context) {
 
        /*