Improve social media sharing with better video linking
Refactor server logic to support finding videos by short or long IDs and update meta tags for social sharing previews. Replit-Commit-Author: Agent Replit-Commit-Session-Id: 2cd2c0bc-434c-4bc9-ad3f-b99d3897a0d1 Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/8cc42625-c1f5-4e43-99bd-77f2c4dedee2/2cd2c0bc-434c-4bc9-ad3f-b99d3897a0d1/gueMD9C
This commit is contained in:
+28
-20
@@ -1,6 +1,6 @@
|
||||
import express, { type Request, Response, NextFunction } from "express";
|
||||
import compression from "compression";
|
||||
import { registerRoutes } from "./routes";
|
||||
import { registerRoutes, findVideoByAnyId } from "./routes";
|
||||
import { videoSyncService } from "./videoSync";
|
||||
import { setupVite, serveStatic, log } from "./vite";
|
||||
import { storage } from "./storage";
|
||||
@@ -91,21 +91,16 @@ app.use((req, res, next) => {
|
||||
|
||||
if (isSocialBot) {
|
||||
try {
|
||||
// Support both short and long video IDs for social media
|
||||
let video;
|
||||
|
||||
// If it's a short ID (8 chars), find video by short ID
|
||||
if (videoId.length === 8) {
|
||||
const allVideosResponse = await storage.getVideos({ limit: 200, offset: 0 });
|
||||
video = allVideosResponse.videos.find((v: any) => v.id.replace(/-/g, '').substring(0, 8) === videoId);
|
||||
} else {
|
||||
// Try as full ID
|
||||
video = await storage.getVideo(videoId);
|
||||
}
|
||||
console.log(`🤖 Social bot detected: ${userAgent}, looking for video: ${videoId}`);
|
||||
// Use the same findVideoByAnyId function as in routes
|
||||
const video = await findVideoByAnyId(videoId);
|
||||
|
||||
if (!video) {
|
||||
console.log(`❌ Video not found for social bot: ${videoId}`);
|
||||
return next(); // Če video ne obstaja, preusmerimo na običajno SPA
|
||||
}
|
||||
|
||||
console.log(`✅ Video found for social bot: ${video.title}`);
|
||||
|
||||
// Preberemo osnovni HTML template
|
||||
const clientTemplate = path.resolve(import.meta.dirname, "..", "client", "index.html");
|
||||
@@ -116,8 +111,10 @@ app.use((req, res, next) => {
|
||||
|
||||
// Zamenimo meta oznake z video specifičnimi
|
||||
const baseUrl = req.protocol + '://' + req.get('host');
|
||||
const videoUrl = `${baseUrl}/video/${video.id}`;
|
||||
const thumbnailUrl = `${baseUrl}/api/video-thumbnail/${video.id}`;
|
||||
// Use short ID for sharing URLs
|
||||
const shortId = video.id.replace(/-/g, '').substring(0, 8);
|
||||
const videoUrl = `${baseUrl}/video/${shortId}`;
|
||||
const thumbnailUrl = video.thumbnailUrl || `${baseUrl}/api/video-thumbnail/${video.id}`;
|
||||
|
||||
// Zamenjamo osnovne meta oznake
|
||||
template = template.replace(
|
||||
@@ -141,15 +138,17 @@ app.use((req, res, next) => {
|
||||
`<meta property="og:description" content="${escapeHtml(video.description || `Watch ${video.title} on go4.video`)}"`
|
||||
);
|
||||
|
||||
// Replace og:image - handle both with and without id attribute
|
||||
template = template.replace(
|
||||
/<meta property="og:image" content="[^"]*"/,
|
||||
`<meta property="og:image" content="${thumbnailUrl}"`
|
||||
/<meta property="og:image"[^>]*>/,
|
||||
`<meta property="og:image" id="og-image" content="${thumbnailUrl}"`
|
||||
);
|
||||
|
||||
// Zamenjamo tudi Twitter image
|
||||
// Replace Twitter image - handle both with and without id attribute
|
||||
template = template.replace(
|
||||
/<meta name="twitter:image" content="[^"]*"/,
|
||||
`<meta name="twitter:image" content="${thumbnailUrl}"`
|
||||
/<meta name="twitter:image"[^>]*>/,
|
||||
`<meta name="twitter:image" id="twitter-image" content="${thumbnailUrl}"`
|
||||
);
|
||||
|
||||
template = template.replace(
|
||||
@@ -162,13 +161,22 @@ app.use((req, res, next) => {
|
||||
`<meta name="twitter:description" content="${escapeHtml(video.description || `Watch ${video.title} on go4.video`)}"`
|
||||
);
|
||||
|
||||
// Also update URL and secure image tags that have id attributes
|
||||
template = template.replace(
|
||||
/<meta property="og:url"[^>]*>/,
|
||||
`<meta property="og:url" id="og-url" content="${videoUrl}">`
|
||||
);
|
||||
|
||||
template = template.replace(
|
||||
/<meta property="og:image:secure_url"[^>]*>/,
|
||||
`<meta property="og:image:secure_url" id="og-image-secure" content="${thumbnailUrl}">`
|
||||
);
|
||||
|
||||
// Dodamo dodatne OG oznake za video z detajlnimi informacijami
|
||||
const duration = Math.floor(video.duration / 60) + ':' + (video.duration % 60).toString().padStart(2, '0');
|
||||
const additionalMeta = `
|
||||
<meta property="og:url" content="${videoUrl}">
|
||||
<meta property="og:type" content="video.other">
|
||||
<meta property="og:video:duration" content="${video.duration}">
|
||||
<meta property="og:site_name" content="go4.video">
|
||||
<meta property="video:duration" content="${video.duration}">
|
||||
<meta property="video:release_date" content="${video.createdAt?.toISOString()}">
|
||||
|
||||
|
||||
Reference in New Issue
Block a user