Restored to '44d6f571a5a2b8bce0fd4e8a9eae76204284885b'
Replit-Restored-To: 44d6f571a5
This commit is contained in:
+37
-78
@@ -54,77 +54,29 @@ export interface IStorage {
|
||||
export class DatabaseStorage implements IStorage {
|
||||
// Video operations
|
||||
async getVideos(limit = 20, offset = 0, search?: string): Promise<Video[]> {
|
||||
try {
|
||||
let sqlQuery = sql`
|
||||
SELECT * FROM video_metadata
|
||||
`;
|
||||
|
||||
if (search && search.length >= 2) {
|
||||
const searchTerm = `%${search.toLowerCase()}%`;
|
||||
sqlQuery = sql`
|
||||
SELECT * FROM video_metadata
|
||||
WHERE LOWER(title) LIKE ${searchTerm}
|
||||
OR LOWER(description) LIKE ${searchTerm}
|
||||
`;
|
||||
}
|
||||
|
||||
sqlQuery = sql`${sqlQuery} ORDER BY created_at DESC LIMIT ${limit} OFFSET ${offset}`;
|
||||
|
||||
const result = await db.execute(sqlQuery);
|
||||
console.log(`📊 DatabaseStorage: Found ${result.rows.length} videos (search: "${search || 'none'}")`);
|
||||
|
||||
// Transform database rows to Video objects with original Bunny iframe URLs
|
||||
return result.rows.map((row: any) => ({
|
||||
id: row.id,
|
||||
title: row.title,
|
||||
description: row.description,
|
||||
thumbnailUrl: row.thumbnail_url,
|
||||
videoUrl: `https://iframe.mediadelivery.net/embed/384105/${row.id}`, // Original Bunny iframe
|
||||
duration: row.duration,
|
||||
views: row.views,
|
||||
category: row.category,
|
||||
tags: [],
|
||||
isPublic: true,
|
||||
uploadStatus: "completed",
|
||||
originalFileName: row.title,
|
||||
createdAt: row.created_at,
|
||||
updatedAt: row.updated_at,
|
||||
}));
|
||||
} catch (error) {
|
||||
console.error("❌ Database query failed:", error);
|
||||
return [];
|
||||
let query = db.select().from(videos);
|
||||
|
||||
if (search) {
|
||||
const searchTerm = `%${search}%`;
|
||||
query = query.where(
|
||||
or(
|
||||
like(videos.title, searchTerm),
|
||||
like(videos.description, searchTerm)
|
||||
)
|
||||
) as any;
|
||||
}
|
||||
|
||||
const result = await query
|
||||
.orderBy(desc(videos.createdAt))
|
||||
.limit(limit)
|
||||
.offset(offset);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
async getVideo(id: string): Promise<Video | undefined> {
|
||||
try {
|
||||
const result = await db.execute(sql`
|
||||
SELECT * FROM video_metadata WHERE id = ${id} LIMIT 1
|
||||
`);
|
||||
|
||||
if (result.rows.length === 0) return undefined;
|
||||
|
||||
const row = result.rows[0] as any;
|
||||
return {
|
||||
id: row.id,
|
||||
title: row.title,
|
||||
description: row.description,
|
||||
thumbnailUrl: row.thumbnail_url,
|
||||
videoUrl: `https://iframe.mediadelivery.net/embed/384105/${row.id}`, // Original Bunny iframe
|
||||
duration: row.duration,
|
||||
views: row.views,
|
||||
category: row.category,
|
||||
tags: [],
|
||||
isPublic: true,
|
||||
uploadStatus: "completed",
|
||||
originalFileName: row.title,
|
||||
createdAt: row.created_at,
|
||||
updatedAt: row.updated_at,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("❌ Database get video failed:", error);
|
||||
return undefined;
|
||||
}
|
||||
const result = await db.select().from(videos).where(eq(videos.id, id));
|
||||
return result[0];
|
||||
}
|
||||
|
||||
async createVideo(video: InsertVideo): Promise<Video> {
|
||||
@@ -144,13 +96,9 @@ export class DatabaseStorage implements IStorage {
|
||||
}
|
||||
|
||||
async updateVideoViews(id: string): Promise<void> {
|
||||
try {
|
||||
await db.execute(sql`
|
||||
UPDATE video_metadata SET views = views + 1 WHERE id = ${id}
|
||||
`);
|
||||
} catch (error) {
|
||||
console.error("❌ Database update views failed:", error);
|
||||
}
|
||||
await db.update(videos)
|
||||
.set({ views: sql`${videos.views} + 1` })
|
||||
.where(eq(videos.id, id));
|
||||
}
|
||||
|
||||
async getVideoCount(search?: string): Promise<number> {
|
||||
@@ -853,22 +801,33 @@ class BunnyStorage implements IStorage {
|
||||
}
|
||||
}
|
||||
|
||||
// Storage selection logic - use PostgreSQL for video metadata
|
||||
// Storage selection logic - choose DatabaseStorage if PostgreSQL is available
|
||||
let storage: IStorage;
|
||||
|
||||
const hasDatabase = process.env.DATABASE_URL;
|
||||
const hasBunnyConfig = process.env.BUNNY_API_KEY && process.env.BUNNY_LIBRARY_ID && process.env.BUNNY_HOSTNAME;
|
||||
|
||||
if (hasDatabase) {
|
||||
// Prioritize Bunny.net storage for user's video content
|
||||
if (hasBunnyConfig) {
|
||||
try {
|
||||
storage = new BunnyStorage();
|
||||
console.log('✅ Using Bunny.net storage with library ID:', process.env.BUNNY_LIBRARY_ID);
|
||||
} catch (error) {
|
||||
console.error('❌ Failed to initialize Bunny.net storage:', error);
|
||||
console.log('📁 Falling back to memory storage');
|
||||
storage = new MemStorage();
|
||||
}
|
||||
} else if (hasDatabase) {
|
||||
try {
|
||||
storage = new DatabaseStorage();
|
||||
console.log('✅ Using PostgreSQL database for video metadata storage');
|
||||
console.log('✅ Using PostgreSQL database storage');
|
||||
} catch (error) {
|
||||
console.error('❌ Failed to initialize database storage:', error);
|
||||
console.log('📁 Falling back to memory storage');
|
||||
storage = new MemStorage();
|
||||
}
|
||||
} else {
|
||||
console.log('📁 Using memory storage (no database found)');
|
||||
console.log('📁 Using memory storage (no database or Bunny.net config found)');
|
||||
storage = new MemStorage();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user