|
1 | | -import fs from "node:fs" |
2 | | -import { redirect } from "@sveltejs/kit" |
3 | | -import { type } from "arktype" |
4 | | -import { typeToNumber } from "$lib/assetTypes" |
5 | | -import { authorise } from "$lib/server/auth" |
6 | | -import { createAsset, getAssetPrice } from "$lib/server/economy" |
7 | | -import formError from "$lib/server/formError" |
8 | | -import { randomAssetId } from "$lib/server/id" |
9 | | -import { |
10 | | - clothingAsset, |
11 | | - imageAsset, |
12 | | - thumbnail, |
13 | | - tShirt, |
14 | | - tShirtThumbnail, |
15 | | -} from "$lib/server/imageAsset" |
16 | | -import ratelimit from "$lib/server/ratelimit" |
17 | | -import requestRender from "$lib/server/requestRender" |
18 | | -import { db, Record } from "$lib/server/surreal" |
19 | | -import { arktype, superValidate } from "$lib/server/validate" |
20 | | -import { graphicAsset } from "$lib/server/xmlAsset" |
21 | | -import { encode } from "$lib/urlName" |
22 | | -import assetTypes from "./assetTypes" |
23 | | -import createAssetQuery from "./createAsset.surql" |
24 | | - |
25 | | -const schema = type({ |
26 | | - // Object.keys(assets) doesn't work |
27 | | - type: type.enumerated(...assetTypes).configure({ |
28 | | - problem: "must be a valid asset type", |
29 | | - }), |
30 | | - name: "3 <= string <= 50", |
31 | | - description: "(0 <= string <= 1000) | undefined", |
32 | | - price: "0 <= number.integer <= 999", |
33 | | - asset: "File", |
34 | | -}) |
35 | | - |
36 | | -// type assetType = (typeof assetTypes)[number] |
| 1 | +import { getAssetPrice } from "$lib/server/economy" |
37 | 2 |
|
38 | 3 | export async function load({ url }) { |
39 | 4 | const price = getAssetPrice() |
40 | | - // const urlType = url.searchParams.get("type") as assetType | null |
41 | 5 | return { |
42 | | - form: await superValidate( |
43 | | - arktype( |
44 | | - schema |
45 | | - // probably not needed |
46 | | - |
47 | | - // { |
48 | | - // defaults: { |
49 | | - // // ...(urlType && |
50 | | - // // assetTypes.includes(urlType) && { type: urlType }), |
51 | | - // type: |
52 | | - // urlType && assetTypes.includes(urlType) |
53 | | - // ? urlType |
54 | | - // : assetTypes[0], |
55 | | - // name: "", |
56 | | - // description: "", |
57 | | - // price: 0, |
58 | | - // asset: null as unknown as File, |
59 | | - // }, |
60 | | - // } |
61 | | - ) |
62 | | - ), |
63 | 6 | assetType: url.searchParams.get("asset"), |
64 | 7 | price, |
65 | 8 | } |
66 | 9 | } |
67 | | - |
68 | | -export const actions: import("./$types").Actions = {} |
69 | | -actions.default = async ({ fetch: f, locals, request, getClientAddress }) => { |
70 | | - const { user } = await authorise(locals) |
71 | | - const form = await superValidate(request, arktype(schema)) |
72 | | - if (!form.valid) return formError(form) |
73 | | - |
74 | | - const { type, name, description, price, asset } = form.data |
75 | | - form.data.asset = null as unknown as File // make sure to return as a POJO |
76 | | - |
77 | | - if (asset.size === 0) |
78 | | - return formError(form, ["asset"], ["You must upload an asset"]) |
79 | | - if (asset.size > 20e6) |
80 | | - return formError( |
81 | | - form, |
82 | | - ["asset"], |
83 | | - ["Asset must be less than 20MB in size"] |
84 | | - ) |
85 | | - |
86 | | - if (user.permissionLevel < 3) { |
87 | | - const limit = ratelimit(form, "assetCreation", getClientAddress, 30) |
88 | | - if (limit) return limit |
89 | | - } |
90 | | - |
91 | | - if (!fs.existsSync("../data/assets")) fs.mkdirSync("../data/assets") |
92 | | - if (!fs.existsSync("../data/thumbnails")) fs.mkdirSync("../data/thumbnails") |
93 | | - |
94 | | - let saveImages: ((id: number) => Promise<number> | Promise<void>)[] = [] |
95 | | - |
96 | | - try { |
97 | | - switch (type) { |
98 | | - case "T-Shirt": |
99 | | - saveImages = await Promise.all([ |
100 | | - tShirt(asset), |
101 | | - tShirtThumbnail(await asset.arrayBuffer()), |
102 | | - ]) |
103 | | - break |
104 | | - |
105 | | - case "Shirt": |
106 | | - case "Pants": |
107 | | - saveImages[0] = await clothingAsset(asset) |
108 | | - saveImages[1] = (id: number) => requestRender(f, "Clothing", id) |
109 | | - break |
110 | | - |
111 | | - case "Decal": |
112 | | - saveImages = await Promise.all([ |
113 | | - imageAsset(asset), |
114 | | - thumbnail(await asset.arrayBuffer()), |
115 | | - ]) |
116 | | - break |
117 | | - |
118 | | - case "Face": |
119 | | - if (user.permissionLevel < 3) |
120 | | - return formError( |
121 | | - form, |
122 | | - ["type"], |
123 | | - [ |
124 | | - "You do not have permission to upload this type of asset", |
125 | | - ] |
126 | | - ) |
127 | | - saveImages = await Promise.all([ |
128 | | - imageAsset(asset), |
129 | | - thumbnail(await asset.arrayBuffer()), |
130 | | - ]) |
131 | | - break |
132 | | - default: |
133 | | - } |
134 | | - } catch (e) { |
135 | | - console.log(e) |
136 | | - return formError(form, ["asset"], ["Asset failed to upload"]) |
137 | | - } |
138 | | - |
139 | | - const slug = encode(name) |
140 | | - const imageAssetId = randomAssetId() |
141 | | - const id = randomAssetId() |
142 | | - |
143 | | - if (user.permissionLevel < 3) { |
144 | | - const created = await createAsset(f, user.id, id, name, slug) |
145 | | - if (!created.ok) return formError(form, ["other"], [created.msg]) |
146 | | - } |
147 | | - |
148 | | - await db.query(createAssetQuery, { |
149 | | - name, |
150 | | - assetType: typeToNumber[type], |
151 | | - price, |
152 | | - description, |
153 | | - user: Record("user", user.id), |
154 | | - imageAssetId, |
155 | | - id, |
156 | | - visibility: user.permissionLevel < 3 ? "Pending" : "Visible", |
157 | | - }) |
158 | | - |
159 | | - await graphicAsset(type, imageAssetId, id) |
160 | | - |
161 | | - try { |
162 | | - await Promise.all([saveImages[0](imageAssetId), saveImages[1](id)]) |
163 | | - } catch (e) { |
164 | | - console.log("Rendering images failed!") |
165 | | - console.error(e) |
166 | | - } |
167 | | - |
168 | | - redirect(302, `/catalog/${id}/${name}`) |
169 | | -} |
0 commit comments