22
33import connectToDatabase from "@/lib/connect-db" ;
44import { getUserFromSession } from "@/lib/user-handlers" ;
5- import { getApikeyFromKeyId , getInternalApikey } from "@/lib/apikey-handlers" ;
6- import {
7- getPaginatedMedia ,
8- getMediaCount ,
9- getMedia as getMediaFromServer ,
10- } from "@/lib/media-handlers" ;
5+ import { getApikeyByUserId } from "@/lib/apikey-handlers" ;
116import { auth } from "@/auth" ;
127import { Media } from "@medialit/models" ;
8+ import { getMediaLitClient } from "@/lib/get-medialit-client" ;
139
1410export async function getMediaFiles (
1511 keyid : string ,
@@ -27,25 +23,18 @@ export async function getMediaFiles(
2723 throw new Error ( "User not found" ) ;
2824 }
2925
30- const internalApikey = await getInternalApikey ( dbUser . _id ) ;
31-
32- if ( ! internalApikey ) {
33- console . error ( "Internal apikey not found for user" , dbUser . _id ) ;
34- throw new Error ( "We messed up. Please try again later." ) ;
35- }
36- const apikey = await getApikeyFromKeyId ( dbUser . _id , keyid ) ;
26+ const apikey = await getApikeyByUserId ( {
27+ userId : dbUser . _id ,
28+ keyId : keyid ,
29+ } ) ;
3730
3831 if ( ! apikey ) {
3932 throw new Error ( "Apikey not found" ) ;
4033 }
4134
42- const media = await getPaginatedMedia ( {
43- apikey : apikey . key ,
44- internalApikey : internalApikey . key ,
45- page : page || 1 ,
46- } ) ;
35+ const client = getMediaLitClient ( apikey . key ) ;
4736
48- return media ;
37+ return await client . list ( page || 1 , 10 ) ;
4938}
5039
5140export async function getCount ( keyid : string ) {
@@ -61,23 +50,16 @@ export async function getCount(keyid: string) {
6150 throw new Error ( "User not found" ) ;
6251 }
6352
64- const internalApikey = await getInternalApikey ( dbUser . _id ) ;
65-
66- if ( ! internalApikey ) {
67- console . error ( "Internal apikey not found for user" , dbUser . _id ) ;
68- throw new Error ( "We messed up. Please try again later." ) ;
69- }
70-
71- const apikey = await getApikeyFromKeyId ( dbUser . _id , keyid ) ;
53+ const apikey = await getApikeyByUserId ( {
54+ userId : dbUser . _id ,
55+ keyId : keyid ,
56+ } ) ;
7257
7358 if ( ! apikey ) {
7459 throw new Error ( "Apikey not found" ) ;
7560 }
7661
77- const mediacount = await getMediaCount ( {
78- apikey : apikey . key ,
79- internalApikey : internalApikey . key ,
80- } ) ;
62+ const client = getMediaLitClient ( apikey . key ) ;
8163
82- return mediacount ;
64+ return await client . getCount ( ) ;
8365}
0 commit comments