-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
d5d7354
commit bec0bf5
Showing
15 changed files
with
216 additions
and
65 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
api/prisma/migrations/20240728012747_add_mime_type_to_image/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- AlterTable | ||
ALTER TABLE "Image" ADD COLUMN "mime_type" TEXT NOT NULL DEFAULT ''; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,19 @@ | ||
import { Injectable, NotFoundException } from '@nestjs/common'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { ImageDao } from './daos/image.dao'; | ||
|
||
@Injectable() | ||
export class ImageService { | ||
constructor(private imageDao: ImageDao) {} | ||
|
||
async getImage(filename: string) { | ||
const { image } = await this.imageDao.getImage(filename); | ||
|
||
if (!image) { | ||
throw new NotFoundException('No image found'); | ||
} | ||
|
||
return image; | ||
async getImage(username: string) { | ||
return this.imageDao.getImage(username); | ||
} | ||
|
||
async createImage(filename: string, image: Buffer) { | ||
return this.imageDao.createImage(filename, image); | ||
async createImage(username: string, mimetype: string, image: Buffer) { | ||
return this.imageDao.createImage(username, mimetype, image); | ||
} | ||
|
||
async deleteImage(filename: string) { | ||
return await this.imageDao.deleteImage(filename); | ||
async deleteImage(username: string) { | ||
return this.imageDao.deleteImage(username); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export const PER_PAGE = 500; | ||
export const PAGE_NUMBER = 0; | ||
export const AVATAR_MAX_SIZE = 5 * 1024 * 1024; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { API_HOST } from '$env/static/private'; | ||
import { fail } from '@sveltejs/kit'; | ||
import type { Stats } from '../../../ambient'; | ||
import type { RequestEvent } from '../$types'; | ||
|
||
const API = process.env.API_HOST || API_HOST || 'http://localhost:4000'; | ||
|
||
/** @type {import('./$types').PageServerLoad} */ | ||
export const load = async ({ fetch, locals }) => { | ||
const { user } = locals; | ||
|
||
if (!user) { | ||
return {}; | ||
} | ||
|
||
try { | ||
const userStats = await fetch(`${API}/users/${user.id}/stats`, { | ||
method: 'GET', | ||
headers: { | ||
Authorization: locals.cookie as string | ||
} | ||
}); | ||
|
||
if (userStats.status !== 200) { | ||
return { user: user }; | ||
} | ||
|
||
const stats: Stats = await userStats.json(); | ||
|
||
return { user, stats }; | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}; | ||
|
||
/** @type {import('./$types').Actions} */ | ||
export const actions = { | ||
saveAvatar: async ({ request, fetch, locals }: RequestEvent) => { | ||
const data = await request.formData(); | ||
const avatar = data.get('file') as File; | ||
|
||
if (!(avatar as File).name || (avatar as File).name === 'undefined') { | ||
return fail(400, { | ||
error: true, | ||
message: 'You must provide a file to upload' | ||
}); | ||
} | ||
|
||
const image = new Blob([await avatar.arrayBuffer()], { type: avatar.type }); | ||
const formData = new FormData(); | ||
formData.set('image', image, avatar.name); | ||
|
||
try { | ||
const response = await fetch(`${API}/users/avatar`, { | ||
method: 'POST', | ||
headers: { | ||
Authorization: locals.cookie as string | ||
}, | ||
body: formData | ||
}); | ||
|
||
if (response.status !== 201) { | ||
return fail(response.status); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,64 @@ | ||
<script lang="ts"> | ||
/** @type {import('./$types').PageData} */ | ||
import { enhance, applyAction } from '$app/forms'; | ||
import Avatar from '$lib/Avatar.svelte'; | ||
import Nav from '$lib/Nav/Nav.svelte'; | ||
import type { User } from '../../../ambient'; | ||
import Toasts from '$lib/Toast/Toasts.svelte'; | ||
import { addToast } from '../../../store'; | ||
import { error, success } from './messages'; | ||
export let data: { | ||
user: User; | ||
export let data; | ||
const { user, stats } = data; | ||
let trigger = {}; | ||
const reload = () => { | ||
trigger = {}; | ||
}; | ||
const { user } = data; | ||
</script> | ||
|
||
<Toasts /> | ||
|
||
<Nav username="{user.username}" /> | ||
<div class="flex max-w-4xl mt-20"> | ||
<div class="w-1/5 mx-auto"> | ||
<div class="m-auto text-center"> | ||
<div class="sm:flex w-full justify-around mt-20 mx-auto"> | ||
<div class="max-w-64 text-center mx-auto mb-8 sm:mb-0"> | ||
{#key trigger} | ||
<Avatar username="{user.username}" isLarge /> | ||
<div class="mt-2">{user.username}</div> | ||
</div> | ||
{/key} | ||
<form | ||
method="POST" | ||
action="?/saveAvatar" | ||
use:enhance="{() => { | ||
return async ({ result }) => { | ||
if (result.type === 'failure') { | ||
addToast(error); | ||
} else if (result.type === 'success') { | ||
addToast(success); | ||
reload(); | ||
} | ||
await applyAction(result); | ||
}; | ||
}}" | ||
enctype="multipart/form-data" | ||
> | ||
<input | ||
type="file" | ||
name="file" | ||
accept="image/jpg, image/png" | ||
class="mt-4 p-0 file:mr-2 file:py-2 file:px-2 | ||
file:rounded-full file:border-0 | ||
file:text-sm file:bg-primary max-w-64" | ||
required | ||
/> | ||
<button type="submit" class="mt-4">Upload</button> | ||
</form> | ||
</div> | ||
<div class="flex-grow pl-2 pt-2"> | ||
<div class="mb-2">email: {user.email}</div> | ||
<div class="mb-2">lists: N/A</div> | ||
<div class="mb-2">watched movies: N/A</div> | ||
<div | ||
class="max-w-md sm:max-w-xl mx-auto text-xl sm:text-2xl grid content-evenly pl-8 sm:pl-0" | ||
> | ||
<div>email: {user.email}</div> | ||
<div>lists: {stats.listCount}</div> | ||
<div>movies: {stats.moviesCount}</div> | ||
<div>shared with you: {stats.sharedListCount}</div> | ||
<div>comments: {stats.commentsCount}</div> | ||
</div> | ||
</div> |
Oops, something went wrong.