diff --git a/backend/main.go b/backend/main.go index 34d9b763..eb8d1c30 100644 --- a/backend/main.go +++ b/backend/main.go @@ -89,6 +89,7 @@ func main() { } migrateTo3(tx, myLogger) + migrateIframeVideoUrl(tx, myLogger) e.HideBanner = true err = e.Start(fmt.Sprintf(":%d", cfg.Port)) diff --git a/backend/migrate.go b/backend/migrate.go index 9ab95722..91e46efe 100644 --- a/backend/migrate.go +++ b/backend/migrate.go @@ -4,13 +4,15 @@ import ( "encoding/json" "errors" "fmt" + "regexp" + "strings" + "github.com/kingwrcy/moments/db" "github.com/kingwrcy/moments/handler" "github.com/kingwrcy/moments/vo" "github.com/rs/zerolog" "github.com/tidwall/gjson" "gorm.io/gorm" - "strings" ) func migrateTo3(tx *gorm.DB, log zerolog.Logger) { @@ -173,3 +175,87 @@ WHERE ((createdAt NOT LIKE '%-%' AND length(createdAt) = 13) OR (updatedAt NOT LIKE '%-%' AND length(updatedAt) = 13))`) } + +func migrateIframeVideoUrl(tx *gorm.DB, log zerolog.Logger) { + var memos []db.Memo + tx.Find(&memos) + + bilibiliUrlReg := regexp.MustCompile(`src=['"](?:https?:)?(?:\/)*([^'"]+)['"]`) + youtubeUrlRegList := []*regexp.Regexp{ + regexp.MustCompile(`v=([^&#]+)`), + regexp.MustCompile(`youtu\.be\/([^\/\?]+)`), + } + + for _, memo := range memos { + var ext vo.MemoExt + err := json.Unmarshal([]byte(memo.Ext), &ext) + if err != nil { + log.Warn().Msgf("memo id: %d 的 ext 不是标准的 json 格式 => %s", memo.Id, memo.Ext) + continue + } + + // 测试数据开始 + // if ext.Video.Value != "" { + // ext.Video.Type = "bilibili" + // ext.Video.Value = `` + // ext.Video.Value = `//player.bilibili.com/player.html?isOutside=true&aid=123&bvid=FDA1FAD&cid=123&p=1` + // ext.Video.Value = `https://player.bilibili.com/player.html?isOutside=true&aid=123&bvid=FDA1FAD&cid=123&p=1` + // } + + // if ext.Video.Value != "" { + // ext.Video.Type = "youtube" + // ext.Video.Value = "https://www.youtube.com/watch?v=hacdT_G2Ara&q=123" + // ext.Video.Value = "https://youtu.be/hacdT_G2Ara?si=aa_a_a_aaa" + // ext.Video.Value = "https://youtu.be/hacdT_G2Ara" + // ext.Video.Value = "//www.youtube.com/embed/hacdT_G2Ara" + // ext.Video.Value = "https://www.youtube.com/embed/hacdT_G2Ara" + // } + // 测试数据结束 + + if ext.Video.Value == "" || + strings.HasPrefix(ext.Video.Value, "https://player.bilibili.com/player.html") || + strings.HasPrefix(ext.Video.Value, "https://www.youtube.com/embed") { + continue + } + + log.Info().Msgf("开始迁移 memo id: %d 的 %s url: %s", memo.Id, ext.Video.Type, ext.Video.Value) + + if strings.HasPrefix(ext.Video.Value, "//") { + ext.Video.Value = fmt.Sprintf("https:%s", ext.Video.Value) + } else if strings.HasPrefix(ext.Video.Value, "http://") { + ext.Video.Value = strings.Replace(ext.Video.Value, "http://", "https://", 1) + } else if ext.Video.Type == "bilibili" { + matchResult := bilibiliUrlReg.FindStringSubmatch(ext.Video.Value) + if matchResult == nil { + continue + } + + ext.Video.Value = fmt.Sprintf(`https://%s`, matchResult[1]) + } else if ext.Video.Type == "youtube" { + for _, youtubeUrlReg := range youtubeUrlRegList { + matchResult := youtubeUrlReg.FindStringSubmatch(ext.Video.Value) + if matchResult == nil { + continue + } + + ext.Video.Value = fmt.Sprintf( + `https://www.youtube.com/embed/%s`, + matchResult[1], + ) + break + } + } else { + log.Info().Msgf("视频地址无需迁移") + continue + } + + log.Info().Msgf("迁移后的 url: %s", ext.Video.Value) + extContent, _ := json.Marshal(ext) + memo.Ext = string(extContent) + if err = tx.Save(&memo).Error; err == nil { + log.Info().Msgf("迁移 memo id: %d 成功", memo.Id) + } else { + log.Error().Msgf("迁移 memo id: %d 失败, 原因:%v", memo.Id, err) + } + } +} diff --git a/front/components/BilibiliPreview.vue b/front/components/BilibiliPreview.vue deleted file mode 100644 index e57a1d0c..00000000 --- a/front/components/BilibiliPreview.vue +++ /dev/null @@ -1,22 +0,0 @@ - - - - - \ No newline at end of file diff --git a/front/components/Memo.vue b/front/components/Memo.vue index 030dfe17..91775ec1 100644 --- a/front/components/Memo.vue +++ b/front/components/Memo.vue @@ -53,12 +53,8 @@ - - - + +
- - +
diff --git a/front/components/UploadVideo.vue b/front/components/UploadVideo.vue index 5ad58a8f..d36b1281 100644 --- a/front/components/UploadVideo.vue +++ b/front/components/UploadVideo.vue @@ -51,7 +51,7 @@ 确定 - 清空 + 清空 @@ -59,24 +59,24 @@ - + diff --git a/front/components/VideoPreviewIframe.vue b/front/components/VideoPreviewIframe.vue new file mode 100644 index 00000000..56ce7255 --- /dev/null +++ b/front/components/VideoPreviewIframe.vue @@ -0,0 +1,25 @@ + + + + + diff --git a/front/components/YoutubePreview.vue b/front/components/YoutubePreview.vue deleted file mode 100644 index 36c4317b..00000000 --- a/front/components/YoutubePreview.vue +++ /dev/null @@ -1,27 +0,0 @@ - - - - - \ No newline at end of file