core/video/embed.go
Alex Dunmow 99fc63ddfd feat: WO-PS-002–008 SDK type packages
- rbac/: Role type, constants, HasPermission, RoleFromString
- blocks/: BlockFunc, BlockMeta, BlockContext, context accessors, BlockRegistry interface
- templates/: TemplateFunc, meta types, TemplateRegistry interface
- auth/: Claims, PublicClaims, context extractors
- content/: Content interface, AuthorProfile/PageInfo/PostInfo types
- settings/: Settings interface, map accessor helpers
- gating/: AccessRule, AccessResult, EvaluateAccess, Gating interface
- crypto/: Crypto interface (Encrypt/Decrypt)
- render/: BlockNoteToHTML standalone renderer
- video/: ParseEmbedURL, EmbedIframeURL
- ai/: ToolDefinition, ToolResult, ToolRegistry interface

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-30 22:32:29 +08:00

70 lines
1.8 KiB
Go

package video
import (
"fmt"
"net/url"
"regexp"
"strings"
)
// EmbedInfo holds parsed video embed information.
type EmbedInfo struct {
Provider string
VideoID string
URL string
}
var (
youtubeRegexps = []*regexp.Regexp{
regexp.MustCompile(`(?:youtube\.com/watch\?v=|youtu\.be/|youtube\.com/embed/)([a-zA-Z0-9_-]{11})`),
}
vimeoRegexp = regexp.MustCompile(`vimeo\.com/(\d+)`)
loomRegexp = regexp.MustCompile(`loom\.com/share/([a-zA-Z0-9]+)`)
)
// ParseEmbedURL parses a video URL and returns embed information.
func ParseEmbedURL(rawURL string) (*EmbedInfo, error) {
parsed, err := url.Parse(rawURL)
if err != nil {
return nil, fmt.Errorf("invalid URL: %w", err)
}
host := strings.ToLower(parsed.Host)
if strings.Contains(host, "youtube.com") || strings.Contains(host, "youtu.be") {
for _, re := range youtubeRegexps {
if matches := re.FindStringSubmatch(rawURL); len(matches) > 1 {
return &EmbedInfo{Provider: "youtube", VideoID: matches[1], URL: rawURL}, nil
}
}
}
if strings.Contains(host, "vimeo.com") {
if matches := vimeoRegexp.FindStringSubmatch(rawURL); len(matches) > 1 {
return &EmbedInfo{Provider: "vimeo", VideoID: matches[1], URL: rawURL}, nil
}
}
if strings.Contains(host, "loom.com") {
if matches := loomRegexp.FindStringSubmatch(rawURL); len(matches) > 1 {
return &EmbedInfo{Provider: "loom", VideoID: matches[1], URL: rawURL}, nil
}
}
return nil, fmt.Errorf("unsupported video provider for URL: %s", rawURL)
}
// EmbedIframeURL returns the iframe embed URL for a video provider and ID.
func EmbedIframeURL(provider, videoID string) string {
switch provider {
case "youtube":
return "https://www.youtube.com/embed/" + videoID
case "vimeo":
return "https://player.vimeo.com/video/" + videoID
case "loom":
return "https://www.loom.com/embed/" + videoID
default:
return ""
}
}