Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
docimin committed Nov 24, 2023
1 parent 5790ab3 commit 72e4916
Show file tree
Hide file tree
Showing 11 changed files with 395 additions and 416 deletions.
4 changes: 4 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ const nextConfig = {
{
protocol: "https",
hostname: "*.headpat.de",
},
{
protocol: "https",
hostname: "placekitten.com",
}
]
},
Expand Down
22 changes: 18 additions & 4 deletions src/app/account/frontpage/page.client.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export default function AccountPage() {
avatar: "",
});

const getAvatarImageUrl = (galleryId) => {
return `${process.env.NEXT_PUBLIC_API_URL}/v1/storage/buckets/655842922bac16a94a25/files/${galleryId}/preview?project=6557c1a8b6c2739b3ecf&width=400`;
};

useEffect(() => {
const fetchData = async () => {
setIsLoading(true);
Expand All @@ -35,7 +39,7 @@ export default function AccountPage() {
pronouns: userDataResponseData.documents[0].pronouns || "",
location: userDataResponseData.documents[0].location || "",
avatar:
userDataResponseData.documents[0].avatar?.data?.attributes?.url ||
getAvatarImageUrl(userDataResponseData.documents[0].avatarId) ||
"/logos/logo.webp", // Set the avatar value or a placeholder image
});
setIsLoading(false);
Expand Down Expand Up @@ -76,6 +80,7 @@ export default function AccountPage() {

const formData = new FormData();
formData.append("file", selectedFile);
formData.append("fileId", "unique()");

try {
const userResponse = await fetch("/api/user/getUserSelf", {
Expand Down Expand Up @@ -226,9 +231,18 @@ export default function AccountPage() {
type="file"
onChange={handleAvatarChange}
/>
<p className="mt-2 text-xs leading-5 dark:text-gray-400 text-gray-900">
JPG, GIF or PNG. 2MB max.
</p>
<div className="flex justify-between items-center">
<p className="mt-2 text-xs leading-5 dark:text-gray-400 text-gray-900">
JPG, GIF or PNG. 2MB max.
</p>
<button
type="submit"
onClick={handleSubmitAVatar}
className="mt-2 bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
>
Submit
</button>
</div>
<p className="mt-2 text-xs leading-5 dark:text-gray-400 text-gray-900">
1024x1024 max. resolution
</p>
Expand Down
5 changes: 3 additions & 2 deletions src/app/account/gallery/page.client.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ export default function FetchGallery() {
? `queries[]=equal("userId","${userId}")&queries[]=equal("nsfw",false)`
: `queries[]=equal("userId","${userId}")`;

const pageSize = 25;
const apiUrl = `/api/gallery/getUserGallery?${filters}&pagination[pageSize]=${pageSize}&pagination[page]=${currentPage}`;
const pageSize = 500; // Number of items per page
const offset = (currentPage - 1) * pageSize; // Calculate offset based on current page
const apiUrl = `/api/gallery/getUserGallery?${filters}&queries[]=limit(${pageSize})&queries[]=offset(${offset})`;

setIsLoading(true);

Expand Down
14 changes: 10 additions & 4 deletions src/app/api/gallery/getTotalGallery/route.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
import { NextResponse } from "next/server";
import { headers } from "next/headers";

export const runtime = "edge";

export async function GET(request) {
try {
const headersList = headers();
const cookieHeader = headersList.get("cookie");

// Extract query parameters from the incoming request
const queryParams = new URLSearchParams(
request.url.split("?")[1]
).toString();

// Construct the URL for the external fetch
const fetchURL = `${process.env.NEXT_PUBLIC_DOMAIN_API}/api/galleries?${queryParams}`;
const fetchURL = `${process.env.NEXT_PUBLIC_API_URL}/v1/databases/65527f2aafa5338cdb57/collections/655cb829dbf6102f2436/documents?${queryParams}`;

const response = await fetch(fetchURL, {
method: "GET",
headers: {
Authorization: `Bearer ${process.env.DOMAIN_API_KEY}`,
"Content-Type": "application/json",
"X-Appwrite-Project": `${process.env.NEXT_PUBLIC_APPWRITE_PROJECT_ID}`,
"X-Appwrite-Response-Format": "1.4.0",
Cookie: cookieHeader,
},
});

Expand All @@ -25,8 +31,8 @@ export async function GET(request) {
}

const data = await response.json();
return NextResponse.json(data);
return NextResponse.json(data, { status: 200 });
} catch (error) {
return NextResponse.error(500, error.message);
return NextResponse.error(error.message, { status: 500 });
}
}
146 changes: 106 additions & 40 deletions src/app/api/user/avatarChange/[uniqueId]/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export async function POST(request) {
const headersList = headers();
const cookieHeader = headersList.get("cookie");

const uniqueId = new URL(request.url).pathname.split("/").pop();

// Read the entire stream and buffer it
const requestData = await request.arrayBuffer();

Expand All @@ -16,59 +18,123 @@ export async function POST(request) {
requestData.slice(0, 100).toString()
);*/

// Construct the URL for the external fetch
const fetchURL = `${process.env.NEXT_PUBLIC_API_URL}/v1/storage/buckets/655842922bac16a94a25/files`;
//console.log("Forwarding request to:", fetchURL);

const uploadImage = await fetch(fetchURL, {
method: "POST",
headers: {
"Content-Type":
request.headers.get("Content-Type") || "multipart/form-data",
"X-Appwrite-Project": `${process.env.NEXT_PUBLIC_APPWRITE_PROJECT_ID}`,
"X-Appwrite-Response-Format": "1.4.0",
Cookie: cookieHeader,
},
body: requestData,
});
// See if user has an avatar already
const avatarResponse = await fetch(
`${process.env.NEXT_PUBLIC_API_URL}/v1/databases/65527f2aafa5338cdb57/collections/655f6354b7c3fff1d687/documents?queries[]=equal("userId","${uniqueId}")`,
{
method: "GET",
headers: {
"Content-Type": "application/json",
"X-Appwrite-Project": `${process.env.NEXT_PUBLIC_APPWRITE_PROJECT_ID}`,
"X-Appwrite-Response-Format": "1.4.0",
Cookie: cookieHeader,
},
}
);

const avatarData = await avatarResponse.json();
const avatarDocumentId = avatarData?.documents[0]?.$id;
const avatarGalleryId = avatarData?.documents[0]?.gallery_id;
// If user has an avatar, delete it

if (avatarData.documents.length !== 0) {
const deleteURL = `${process.env.NEXT_PUBLIC_API_URL}/v1/storage/buckets/655842922bac16a94a25/files/${avatarGalleryId}`;

const deleteResponse = await fetch(deleteURL, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
"X-Appwrite-Project": `${process.env.NEXT_PUBLIC_APPWRITE_PROJECT_ID}`,
"X-Appwrite-Response-Format": "1.4.0",
Cookie: cookieHeader,
},
});

//const deleteData = await deleteResponse.json();

const deleteDocURL = `${process.env.NEXT_PUBLIC_API_URL}/v1/databases/65527f2aafa5338cdb57/collections/655f6354b7c3fff1d687/documents/${avatarDocumentId}`;

const deleteDocResponse = await fetch(deleteDocURL, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
"X-Appwrite-Project": `${process.env.NEXT_PUBLIC_APPWRITE_PROJECT_ID}`,
"X-Appwrite-Response-Format": "1.4.0",
Cookie: cookieHeader,
},
});
}

const uploadImage = await fetch(
`${process.env.NEXT_PUBLIC_API_URL}/v1/storage/buckets/655842922bac16a94a25/files`,
{
method: "POST",
headers: {
"Content-Type":
request.headers.get("Content-Type") || "multipart/form-data",
"X-Appwrite-Project": `${process.env.NEXT_PUBLIC_APPWRITE_PROJECT_ID}`,
"X-Appwrite-Response-Format": "1.4.0",
Cookie: cookieHeader,
},
body: requestData,
}
);

const imageData = await uploadImage.json();

const postURL = `${process.env.NEXT_PUBLIC_API_URL}/v1/databases/65527f2aafa5338cdb57/collections/655cb829dbf6102f2436/documents`;

const response = await fetch(postURL, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Appwrite-Project": `${process.env.NEXT_PUBLIC_APPWRITE_PROJECT_ID}`,
"X-Appwrite-Response-Format": "1.4.0",
Cookie: cookieHeader,
},
body: JSON.stringify({
documentId: "unique()",
data: {
gallery_id: imageData.$id,
siteOriginal: imageData.sizeOriginal,
const response = await fetch(
`${process.env.NEXT_PUBLIC_API_URL}/v1/databases/65527f2aafa5338cdb57/collections/655f6354b7c3fff1d687/documents`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Appwrite-Project": `${process.env.NEXT_PUBLIC_APPWRITE_PROJECT_ID}`,
"X-Appwrite-Response-Format": "1.4.0",
Cookie: cookieHeader,
},
}),
});
body: JSON.stringify({
documentId: "unique()",
data: {
gallery_id: imageData.$id,
sizeOriginal: imageData.sizeOriginal,
userId: uniqueId,
},
}),
}
);

//console.log("External API Response Status:", response.status);
/*console.log(
"External API Response Headers:",
JSON.stringify([...response.headers])
);*/
// PATCH the userdata with the new avatar id
const patchResponse = await fetch(
`${process.env.NEXT_PUBLIC_API_URL}/v1/databases/65527f2aafa5338cdb57/collections/65564fa28d1942747a72/documents/${uniqueId}`,
{
method: "PATCH",
headers: {
"Content-Type": "application/json",
"X-Appwrite-Project": `${process.env.NEXT_PUBLIC_APPWRITE_PROJECT_ID}`,
"X-Appwrite-Response-Format": "1.4.0",
Cookie: cookieHeader,
},
body: JSON.stringify({
data: {
avatarId: imageData.$id,
},
}),
}
);

if (!response.ok) {
const errorData = await response.text();
throw new Error(`Failed to forward data: ${errorData}`);
throw new Error(`Failed to POST data: ${errorData}`);
}

if (!patchResponse.ok) {
const errorData = await patchResponse.text();
throw new Error(`Failed to PATCH data: ${errorData}`);
}

const data = await response.json();
//console.log("External API Response Body:", data);
return NextResponse.json(data, { status: 201 });
} catch (error) {
//console.log("Error:", error.message);
return NextResponse.json(error.message, { status: 500 });
}
}
43 changes: 0 additions & 43 deletions src/app/api/user/getUser/[uniqueId]/route.js

This file was deleted.

Loading

0 comments on commit 72e4916

Please sign in to comment.