Minio part 2
This commit is contained in:
@@ -22,7 +22,7 @@
|
|||||||
<div class="dropdown dropdown-bottom dropdown-end" tabindex="0" role="button">
|
<div class="dropdown dropdown-bottom dropdown-end" tabindex="0" role="button">
|
||||||
<div class="avatar placeholder">
|
<div class="avatar placeholder">
|
||||||
<div class="bg-neutral text-neutral-content rounded-full w-10 ml-4">
|
<div class="bg-neutral text-neutral-content rounded-full w-10 ml-4">
|
||||||
<span class="text-2xl -mt-0.5">{icon}</span>
|
<img src={user.icon} alt="" />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<!-- svelte-ignore a11y-missing-attribute -->
|
<!-- svelte-ignore a11y-missing-attribute -->
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
import minioClient from "$lib/server/minio.js";
|
import minioClient from "$lib/server/minio.js";
|
||||||
import type { RequestEvent } from "@sveltejs/kit";
|
import type { RequestEvent } from "@sveltejs/kit";
|
||||||
import { generateId } from "lucia";
|
import { generateId } from "lucia";
|
||||||
|
import { MINIO_URL } from "$env/static/private";
|
||||||
|
|
||||||
export async function POST(event: RequestEvent): Promise<Response> {
|
export async function POST(event: RequestEvent): Promise<Response> {
|
||||||
try {
|
try {
|
||||||
@@ -10,8 +11,6 @@ export async function POST(event: RequestEvent): Promise<Response> {
|
|||||||
const fileExtension = contentType.split("/").pop();
|
const fileExtension = contentType.split("/").pop();
|
||||||
const fileName = `${generateId(25)}.${fileExtension}`;
|
const fileName = `${generateId(25)}.${fileExtension}`;
|
||||||
|
|
||||||
console.log(fileName);
|
|
||||||
|
|
||||||
const fileBuffer = await event.request.arrayBuffer();
|
const fileBuffer = await event.request.arrayBuffer();
|
||||||
const metaData = {
|
const metaData = {
|
||||||
"Content-Type": contentType,
|
"Content-Type": contentType,
|
||||||
@@ -21,6 +20,28 @@ export async function POST(event: RequestEvent): Promise<Response> {
|
|||||||
|
|
||||||
if (!found) {
|
if (!found) {
|
||||||
await minioClient.makeBucket("profile-pics");
|
await minioClient.makeBucket("profile-pics");
|
||||||
|
// Set a bucket policy to allow public read access
|
||||||
|
const bucketPolicy = {
|
||||||
|
Version: "2012-10-17",
|
||||||
|
Statement: [
|
||||||
|
{
|
||||||
|
Effect: "Allow",
|
||||||
|
Principal: "*",
|
||||||
|
Action: ["s3:GetBucketLocation", "s3:ListBucket"],
|
||||||
|
Resource: `arn:aws:s3:::profile-pics`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Effect: "Allow",
|
||||||
|
Principal: "*",
|
||||||
|
Action: "s3:GetObject",
|
||||||
|
Resource: `arn:aws:s3:::profile-pics/*`,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
await minioClient.setBucketPolicy(
|
||||||
|
"profile-pics",
|
||||||
|
JSON.stringify(bucketPolicy)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
await minioClient.putObject(
|
await minioClient.putObject(
|
||||||
@@ -34,9 +55,9 @@ export async function POST(event: RequestEvent): Promise<Response> {
|
|||||||
fileName
|
fileName
|
||||||
);
|
);
|
||||||
|
|
||||||
console.log(fileUrl);
|
const publicUrl = `${MINIO_URL}/profile-pics/${fileName}`;
|
||||||
|
|
||||||
return new Response(JSON.stringify({ fileUrl }), {
|
return new Response(JSON.stringify({ publicUrl }), {
|
||||||
status: 200,
|
status: 200,
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
|
|||||||
@@ -28,7 +28,6 @@ export const actions: Actions = {
|
|||||||
let lastName = formData.get("last_name") as string;
|
let lastName = formData.get("last_name") as string;
|
||||||
let icon = formData.get("icon") as string;
|
let icon = formData.get("icon") as string;
|
||||||
let profilePicture = formData.get("profilePicture") as File;
|
let profilePicture = formData.get("profilePicture") as File;
|
||||||
console.log(profilePicture);
|
|
||||||
|
|
||||||
let password = formData.get("password") as string;
|
let password = formData.get("password") as string;
|
||||||
|
|
||||||
@@ -68,7 +67,6 @@ export const actions: Actions = {
|
|||||||
|
|
||||||
if (password) {
|
if (password) {
|
||||||
let hashedPassword = await new Argon2id().hash(password);
|
let hashedPassword = await new Argon2id().hash(password);
|
||||||
console.log(hashedPassword);
|
|
||||||
await db
|
await db
|
||||||
.update(userTable)
|
.update(userTable)
|
||||||
.set({
|
.set({
|
||||||
@@ -84,14 +82,14 @@ export const actions: Actions = {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
console.log("DATA" + data.publicUrl);
|
||||||
|
icon = data.publicUrl;
|
||||||
|
|
||||||
if (data.error) {
|
if (data.error) {
|
||||||
throw error(400, {
|
throw error(400, {
|
||||||
message: "Error uploading profile picture",
|
message: "Error uploading profile picture",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await db
|
await db
|
||||||
|
|||||||
@@ -47,14 +47,6 @@
|
|||||||
id="last_name"
|
id="last_name"
|
||||||
class="block mb-2 input input-bordered w-full max-w-xs"
|
class="block mb-2 input input-bordered w-full max-w-xs"
|
||||||
/><br />
|
/><br />
|
||||||
<label for="icon">Profile Icon (emoji)</label>
|
|
||||||
<input
|
|
||||||
type="emoji"
|
|
||||||
bind:value={icon}
|
|
||||||
name="icon"
|
|
||||||
id="icon"
|
|
||||||
class="block mb-2 input input-bordered w-full max-w-xs"
|
|
||||||
/><br />
|
|
||||||
<label for="profilePicture">Profile Picture</label>
|
<label for="profilePicture">Profile Picture</label>
|
||||||
<input
|
<input
|
||||||
type="file"
|
type="file"
|
||||||
|
|||||||
Reference in New Issue
Block a user