Share link with auth

#4
by CarlenBai - opened
src/app/api/share-link/route.ts ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { NextRequest, NextResponse } from 'next/server';
2
+
3
+ const BASE_URL = "https://anysite-gallery.novita.ai";
4
+
5
+ export async function POST(request: NextRequest) {
6
+ try {
7
+ const { filename, code } = await request.json();
8
+
9
+ const response = await fetch(`${BASE_URL}/api/upload-code`, {
10
+ method: 'POST',
11
+ headers: {
12
+ 'Content-Type': 'application/json',
13
+ 'x-token': process.env.ANYSITE_GALLERY_AUTH_TOKEN || "",
14
+ },
15
+ body: JSON.stringify({
16
+ filename,
17
+ code,
18
+ }),
19
+ });
20
+
21
+ if (!response.ok) {
22
+ return NextResponse.json(
23
+ { success: false, message: `Failed to upload: ${response.status} ${response.statusText}` },
24
+ { status: response.status }
25
+ );
26
+ }
27
+
28
+ const result = await response.json();
29
+ return NextResponse.json(result);
30
+ } catch (error) {
31
+ console.error('Error in share-link API:', error);
32
+ return NextResponse.json(
33
+ { success: false, message: 'Internal server error' },
34
+ { status: 500 }
35
+ );
36
+ }
37
+ }
src/lib/sharelink.ts CHANGED
@@ -1,6 +1,5 @@
1
  import { nanoid } from "nanoid";
2
  const FILE_NAME_KEY = "novita-anysite-share-filename";
3
- const BASE_URL = "https://anysite-gallery.novita.ai";
4
 
5
  function getShareFilename(): string {
6
  let filename = localStorage.getItem(FILE_NAME_KEY);
@@ -16,7 +15,7 @@ function getShareFilename(): string {
16
  export async function generateShareLink(html: string) {
17
  const filename = getShareFilename();
18
 
19
- const response = await fetch(`${BASE_URL}/api/upload-code`, {
20
  method: "POST",
21
  headers: {
22
  "Content-Type": "application/json",
@@ -43,5 +42,5 @@ export async function generateShareLink(html: string) {
43
  if (!uploadedUrl) {
44
  throw new Error("No URL returned from upload service");
45
  }
46
- return `${BASE_URL}/${filename}`;
47
  }
 
1
  import { nanoid } from "nanoid";
2
  const FILE_NAME_KEY = "novita-anysite-share-filename";
 
3
 
4
  function getShareFilename(): string {
5
  let filename = localStorage.getItem(FILE_NAME_KEY);
 
15
  export async function generateShareLink(html: string) {
16
  const filename = getShareFilename();
17
 
18
+ const response = await fetch("/api/share-link", {
19
  method: "POST",
20
  headers: {
21
  "Content-Type": "application/json",
 
42
  if (!uploadedUrl) {
43
  throw new Error("No URL returned from upload service");
44
  }
45
+ return `https://anysite-gallery.novita.ai/${filename}`;
46
  }