fully added profile photo submission

This commit is contained in:
Daniel
2023-05-22 15:50:48 +03:30
parent 22d8178c88
commit 59e4dc471f
13 changed files with 194 additions and 70 deletions
+33 -8
View File
@@ -5,9 +5,38 @@
import { prisma } from "@/lib/api/db";
import { AccountSettings } from "@/types/global";
import fs from "fs";
import path from "path";
export default async function (user: AccountSettings, userId: number) {
console.log(typeof user);
console.log(console.log(user.profilePic));
const profilePic = user.profilePic;
if (profilePic && profilePic !== "DELETE") {
if ((user?.profilePic?.length as number) < 1572864) {
try {
const filePath = path.join(
process.cwd(),
`data/uploads/avatar/${userId}.jpg`
);
const base64Data = profilePic.replace(/^data:image\/jpeg;base64,/, "");
fs.writeFile(filePath, base64Data, "base64", function (err) {
console.log(err);
});
} catch (err) {
console.log("Error saving image:", err);
}
} else {
console.log("A file larger than 1.5MB was uploaded.");
}
} else if (profilePic === "DELETE") {
fs.unlink(`data/uploads/avatar/${userId}.jpg`, (err) => {
if (err) console.log(err);
});
}
const updatedUser = await prisma.user.update({
where: {
@@ -19,13 +48,9 @@ export default async function (user: AccountSettings, userId: number) {
collectionProtection: user.collectionProtection,
whitelistedUsers: user.whitelistedUsers,
},
select: {
name: true,
email: true,
collectionProtection: true,
whitelistedUsers: true,
},
});
return { response: updatedUser, status: 200 };
const { password, ...unsensitiveInfo } = updatedUser;
return { response: unsensitiveInfo, status: 200 };
}
+9
View File
@@ -0,0 +1,9 @@
export default async function fileExists(fileUrl: string): Promise<boolean> {
try {
const response = await fetch(fileUrl, { method: "HEAD" });
return response.ok;
} catch (error) {
console.error("Error checking file existence:", error);
return false;
}
}
+1 -1
View File
@@ -22,7 +22,7 @@ export default function () {
setCollections();
setTags();
setLinks();
setAccount(data.user.email as string);
setAccount(data.user.email as string, data.user.id);
}
}, [status]);
}
+17
View File
@@ -0,0 +1,17 @@
import Resizer from "react-image-file-resizer";
export const resizeImage = (file: File): Promise<Blob> =>
new Promise<Blob>((resolve) => {
Resizer.imageFileResizer(
file,
150, // target width
150, // target height
"JPEG", // output format
100, // quality
0, // rotation
(uri: any) => {
resolve(uri as Blob);
},
"blob" // output type
);
});