Skip to content

Commit 318cf8f

Browse files
committed
Web app uses official MediaLit SDK; Removed internal API key concept;
1 parent 8fd723b commit 318cf8f

File tree

18 files changed

+98
-334
lines changed

18 files changed

+98
-334
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
db.apikeys.deleteMany({ name: "internal-apikey", internal: true });
2+
db.apikeys.updateMany({}, { $unset: { internal: "" } });

apps/api/src/apikey/middleware.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,6 @@ export default async function apikey(
2323
return res.status(401).json({ error: UNAUTHORISED });
2424
}
2525

26-
if (req.body.internalKey) {
27-
const internalApikey: Apikey | null = await getApiKeyUsingKeyId(
28-
req.body.internalKey,
29-
);
30-
if (!internalApikey) {
31-
return res.status(401).json({ error: UNAUTHORISED });
32-
}
33-
}
34-
3526
// const isSubscriptionValid = await validateSubscription(
3627
// apiKey!.userId.toString(),
3728
// );

apps/api/src/apikey/queries.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ import { getUniqueId } from "@medialit/utils";
55
export async function createApiKey(
66
userId: string,
77
name: string,
8-
internal?: boolean,
98
): Promise<Apikey> {
109
return await ApikeyModel.create({
1110
name,
1211
key: getUniqueId(),
1312
userId,
14-
internal: internal || false,
1513
});
1614
}
1715

@@ -38,15 +36,11 @@ export async function getApiKeyByUserId(
3836
{
3937
key: keyId,
4038
userId,
41-
internal: false,
4239
},
4340
projections,
4441
);
4542
} else {
46-
result = await ApikeyModel.find(
47-
{ userId, internal: false },
48-
projections,
49-
);
43+
result = await ApikeyModel.find({ userId }, projections);
5044
}
5145

5246
return result;

apps/api/src/index.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,11 @@ async function createAdminUser() {
7878
const user: User | null = await findByEmail(email);
7979

8080
if (!user) {
81-
const user = await createUser(
82-
email,
83-
undefined,
84-
// new Date(
85-
// new Date().setFullYear(new Date().getFullYear() + 100),
86-
// ),
87-
"subscribed",
88-
);
81+
const user = await createUser(email, undefined, "subscribed");
8982
const apikey: Apikey = await createApiKey(user.id, "App 1");
9083
console.log("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
9184
console.log(`@ API key: ${apikey.key} @`);
9285
console.log("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
93-
await createApiKey(user.id, Constants.internalApikeyName, true);
9486
}
9587
} catch (error) {
9688
logger.error({ error }, "Failed to create admin user");

apps/web/app/actions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import connectToDatabase from "@/lib/connect-db";
99
import verificationToken from "@/models/verification-token";
1010
import {
1111
createApiKey,
12-
getApiKeyByUserId,
12+
getApiKeysByUserId,
1313
deleteApiKey,
1414
editApiKey,
1515
getApikeyFromKeyId,
@@ -150,7 +150,7 @@ export async function getApiKeys() {
150150
return;
151151
}
152152

153-
const apikeys = await getApiKeyByUserId(dbUser._id);
153+
const apikeys = await getApiKeysByUserId(dbUser._id);
154154
return apikeys;
155155
}
156156

apps/web/app/app/[keyid]/files/actions.ts

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@
22

33
import connectToDatabase from "@/lib/connect-db";
44
import { 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";
116
import { auth } from "@/auth";
127
import { Media } from "@medialit/models";
8+
import { getMediaLitClient } from "@/lib/get-medialit-client";
139

1410
export 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

5140
export 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
}

apps/web/app/app/[keyid]/files/get-media/route.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ export const dynamic = "auto";
22

33
import connectToDatabase from "@/lib/connect-db";
44
import { getUserFromSession } from "@/lib/user-handlers";
5-
import { getApikeyFromKeyId, getInternalApikey } from "@/lib/apikey-handlers";
6-
import { getMedia as getMediaFromServer } from "@/lib/media-handlers";
5+
import { getApikeyByUserId } from "@/lib/apikey-handlers";
76
import { auth } from "@/auth";
7+
import { getMediaLitClient } from "@/lib/get-medialit-client";
88

99
export async function POST(request: Request) {
1010
const { mediaId, keyId } = await request.json();
@@ -25,23 +25,15 @@ export async function POST(request: Request) {
2525
throw new Error("User not found");
2626
}
2727

28-
const internalApikey = await getInternalApikey(dbUser._id);
29-
30-
if (!internalApikey) {
31-
console.error("Internal apikey not found for user", dbUser._id);
32-
throw new Error("We messed up. Please try again later.");
33-
}
34-
const apikey = await getApikeyFromKeyId(dbUser._id, keyId);
28+
const apikey = await getApikeyByUserId({ userId: dbUser._id, keyId });
3529

3630
if (!apikey) {
3731
throw new Error("Apikey not found");
3832
}
3933

40-
const media = await getMediaFromServer({
41-
mediaId: mediaId,
42-
apikey: apikey.key,
43-
internalApikey: internalApikey.key,
44-
});
34+
const client = getMediaLitClient(apikey.key);
35+
36+
const media = await client.get(mediaId);
4537

4638
return Response.json({ media });
4739
}

apps/web/app/app/[keyid]/files/page.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export default async function Media(props: {
3838
totalMediaCount = await getCount(keyid);
3939
medias = await getMediaFiles(keyid, +page);
4040
totalPages = medias
41-
? Math.ceil(totalMediaCount.count / Number(mediasPerPage))
41+
? Math.ceil(totalMediaCount / Number(mediasPerPage))
4242
: 0;
4343

4444
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
@@ -80,7 +80,7 @@ export default async function Media(props: {
8080
<div className="grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-5 gap-4 items-start mb-8">
8181
{medias.map((media: any, index: number) => (
8282
<FilePreview
83-
key={media.id}
83+
key={media.mediaId}
8484
media={media}
8585
keyid={keyid}
8686
/>
@@ -123,7 +123,7 @@ export default async function Media(props: {
123123
</PaginationItem>
124124
<PaginationItem className="text-sm">
125125
<span className="font-bold">{page}</span> of{" "}
126-
{totalPages} ({medias.length} Files)
126+
{totalPages} ({totalMediaCount} Files)
127127
</PaginationItem>
128128
<PaginationItem
129129
className={`

apps/web/app/app/[keyid]/settings/actions.tsx

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
"use server";
22

33
import { auth } from "@/auth";
4-
import { getApikeyFromKeyId, getInternalApikey } from "@/lib/apikey-handlers";
4+
import { getApikeyByUserId } from "@/lib/apikey-handlers";
55
import connectToDatabase from "@/lib/connect-db";
6-
import { getMediaTotalSize } from "@/lib/media-handlers";
6+
import { getMediaLitClient } from "@/lib/get-medialit-client";
77
import { getUserFromSession } from "@/lib/user-handlers";
88
import ApikeyModel from "@/models/apikey";
9+
import { MediaStats } from "medialit";
910

1011
export async function updateAppName(
1112
previousState: Record<string, unknown>,
@@ -49,7 +50,7 @@ export async function updateAppName(
4950

5051
export async function getTotalSpaceByApikey(
5152
keyid: string,
52-
): Promise<{ storage: number; maxStorage: number }> {
53+
): Promise<MediaStats> {
5354
const session = await auth();
5455
if (!session || !session.user) {
5556
throw new Error("Unauthenticated");
@@ -62,25 +63,19 @@ export async function getTotalSpaceByApikey(
6263
throw new Error("User not found");
6364
}
6465

65-
const internalApikey = await getInternalApikey(dbUser._id);
66-
67-
if (!internalApikey) {
68-
console.error("Internal apikey not found for user", dbUser._id);
69-
throw new Error("We messed up. Please try again later.");
70-
}
71-
const apikey = await getApikeyFromKeyId(dbUser._id, keyid);
66+
const apikey = await getApikeyByUserId({
67+
userId: dbUser._id,
68+
keyId: keyid,
69+
});
7270

7371
if (!apikey) {
7472
throw new Error("Apikey not found");
7573
}
7674

77-
try {
78-
const response = await getMediaTotalSize({
79-
apikey: apikey.key,
80-
internalApikey: internalApikey.key,
81-
});
75+
const client = getMediaLitClient(apikey.key);
8276

83-
return response;
77+
try {
78+
return await client.getStats();
8479
} catch (e: any) {
8580
console.error(e);
8681
return { storage: 0, maxStorage: 0 };

apps/web/auth.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,6 @@ export const { auth, signIn, signOut, handlers } = NextAuth({
5454
subscriptionStatus:
5555
Constants.SubscriptionStatus.NOT_SUBSCRIBED,
5656
});
57-
await Apikey.create({
58-
name: Constants.internalApikeyName,
59-
key: getUniqueId(),
60-
userId: user.id,
61-
internal: true,
62-
});
6357
try {
6458
await createUser({ email: sanitizedEmail });
6559
} catch (err: any) {

0 commit comments

Comments
 (0)