coyotte508 HF staff commited on
Commit
fa79853
1 Parent(s): 65b4f03

✨ Load pictures

Browse files
src/lib/server/db/index.ts CHANGED
@@ -1,9 +1,9 @@
1
  import { MONGODB_URL } from '$env/static/private';
2
  import { MongoClient } from 'mongodb';
3
  import { createPageCollection } from './page';
4
- // import { createPictureCollections } from './picture';
5
- // import { createProductCollection } from './product';
6
- // import { createUserCollection } from './user';
7
 
8
  const client = new MongoClient(MONGODB_URL, {
9
  directConnection: true
@@ -14,8 +14,8 @@ export const connectPromise = client.connect().catch(console.error);
14
  const db = client.db('bergere');
15
 
16
  const pages = createPageCollection(db, client);
17
- // const users = createUserCollection(db, client);
18
- // const Products = createProductCollection(db);
19
- // const { Pictures, PicturesFs } = createPictureCollections(db, client);
20
 
21
- export { client, db, pages /* users, Pictures, PicturesFs, Products */ };
 
1
  import { MONGODB_URL } from '$env/static/private';
2
  import { MongoClient } from 'mongodb';
3
  import { createPageCollection } from './page';
4
+ import { createPictureCollections } from './picture';
5
+ import { createProductCollection } from './product';
6
+ import { createUserCollection } from './user';
7
 
8
  const client = new MongoClient(MONGODB_URL, {
9
  directConnection: true
 
14
  const db = client.db('bergere');
15
 
16
  const pages = createPageCollection(db, client);
17
+ const users = createUserCollection(db, client);
18
+ const products = createProductCollection(db);
19
+ const { pictures, picturesFs } = createPictureCollections(db);
20
 
21
+ export { client, db, pages, users, pictures, picturesFs, products };
src/lib/server/db/picture.ts ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { PictureFs, Picture } from '$lib/types/Picture';
2
+ import type { Collection, Db } from 'mongodb';
3
+
4
+ export function createPictureCollections(db: Db): {
5
+ pictures: Collection<Picture>;
6
+ picturesFs: Collection<PictureFs>;
7
+ } {
8
+ const coll = db.collection<Picture>('pictures');
9
+ const fs = db.collection<PictureFs>('pictures.fs');
10
+
11
+ return { pictures: coll, picturesFs: fs };
12
+ }
src/lib/server/db/product.ts ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ import type { Product } from '$lib/types/Product';
2
+ import type { Collection, Db } from 'mongodb';
3
+
4
+ export function createProductCollection(db: Db): Collection<Product> {
5
+ const coll = db.collection<Product>('products');
6
+
7
+ return coll;
8
+ }
src/lib/server/db/user.ts ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { User } from '$lib/types/User';
2
+ import type { Collection, Db, MongoClient } from 'mongodb';
3
+
4
+ export function createUserCollection(db: Db, client: MongoClient): Collection<User> {
5
+ const coll = db.collection<User>('users');
6
+
7
+ client.on('open', () => {
8
+ coll
9
+ .createIndex(
10
+ {
11
+ email: 1
12
+ },
13
+ {
14
+ collation: {
15
+ locale: 'en',
16
+ strength: 1
17
+ }
18
+ }
19
+ )
20
+ .catch(console.error);
21
+ });
22
+
23
+ return coll;
24
+ }
src/lib/types/Product.ts ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { Picture } from './Picture';
2
+ import type { Timestamps } from './Timestamps';
3
+
4
+ export interface Product extends Timestamps {
5
+ _id: string;
6
+ name: string;
7
+ description: string;
8
+ price: number;
9
+ kind: 'armchair' | 'cushion';
10
+ state: 'draft' | 'published' | 'retired';
11
+
12
+ photos: Picture[];
13
+ }
src/lib/types/User.ts ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ import type { Timestamps } from './Timestamps';
2
+
3
+ export interface User extends Timestamps {
4
+ email: string;
5
+ hash: string;
6
+ token?: string;
7
+ authority?: 'admin';
8
+ }
src/lib/utils/filterNullish.ts ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ export function filterNullish<T>(arr: (T | null | undefined)[]): T[] {
2
+ return arr.filter((x): x is T => x !== undefined && x !== null);
3
+ }
src/routes/+layout.server.ts CHANGED
@@ -2,14 +2,21 @@ import type { LayoutServerLoad } from './$types';
2
  import '$lib/server/db';
3
  import { pages } from '$lib/server/db/page';
4
  import type { Picture } from '$lib/types/Picture';
 
 
5
 
6
  export const load: LayoutServerLoad = async (input) => {
7
  const pageId = input.url.pathname;
8
 
9
  if (pageId in pages) {
 
 
 
 
 
10
  return {
11
- pageData: pages[pageId as keyof typeof pages],
12
- pictures: [] as Picture[]
13
  };
14
  }
15
 
 
2
  import '$lib/server/db';
3
  import { pages } from '$lib/server/db/page';
4
  import type { Picture } from '$lib/types/Picture';
5
+ import { filterNullish } from '$lib/utils/filterNullish';
6
+ import { pictures } from '$lib/server/db';
7
 
8
  export const load: LayoutServerLoad = async (input) => {
9
  const pageId = input.url.pathname;
10
 
11
  if (pageId in pages) {
12
+ const pageData = pages[pageId as keyof typeof pages];
13
+
14
+ const pictureIds = filterNullish(Object.values(pageData.pictures));
15
+ const pics = await pictures.find({ _id: { $in: pictureIds } }).toArray();
16
+
17
  return {
18
+ pageData,
19
+ pictures: pics
20
  };
21
  }
22
 
src/routes/photos/raw/[id]/+server.ts ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { picturesFs } from '$lib/server/db';
2
+ import { error } from '@sveltejs/kit';
3
+ import type { RequestHandler } from './$types';
4
+
5
+ export const GET: RequestHandler = async ({ params }) => {
6
+ const fs = await picturesFs.findOne({ _id: params.id });
7
+
8
+ if (!fs) {
9
+ throw error(404, 'Image non trouvée');
10
+ }
11
+ return new Response(new Uint8Array(fs.data.buffer, 0, fs.data.buffer.byteLength), {
12
+ headers: { 'Content-Type': 'image/webp' }
13
+ });
14
+ };