novitacarlen commited on
Commit
163b681
·
1 Parent(s): 81c8014

feat: upload code with auth

Browse files
src/app/api/share-link/route.ts ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { NextRequest, NextResponse } from 'next/server';
2
+
3
+ const AUTH_TOKEN = "zf2iteadhSNNaGqo3GZ";
4
+ const BASE_URL = "https://anysite-gallery.novita.ai";
5
+
6
+ export async function POST(request: NextRequest) {
7
+ try {
8
+ const { filename, code } = await request.json();
9
+
10
+ const response = await fetch(`${BASE_URL}/api/upload-code`, {
11
+ method: 'POST',
12
+ headers: {
13
+ 'Content-Type': 'application/json',
14
+ 'x-token': AUTH_TOKEN,
15
+ },
16
+ body: JSON.stringify({
17
+ filename,
18
+ code,
19
+ }),
20
+ });
21
+
22
+ if (!response.ok) {
23
+ return NextResponse.json(
24
+ { success: false, message: `Failed to upload: ${response.status} ${response.statusText}` },
25
+ { status: response.status }
26
+ );
27
+ }
28
+
29
+ const result = await response.json();
30
+ return NextResponse.json(result);
31
+ } catch (error) {
32
+ console.error('Error in share-link API:', error);
33
+ return NextResponse.json(
34
+ { success: false, message: 'Internal server error' },
35
+ { status: 500 }
36
+ );
37
+ }
38
+ }
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
  }