Skip to content

Commit 2bcdc36

Browse files
committed
More file upload fixes.
1 parent c8dd4fd commit 2bcdc36

File tree

7 files changed

+33
-13
lines changed

7 files changed

+33
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Headlines: Added, Changed, Deprecated, Removed, Fixed, Security
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8-
## [2.10.2] - 2024-03-18
8+
## [2.10.3] - 2024-03-18
99

1010
### Added
1111

src/lib/client/proxies.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,15 @@ export function filesProxy<
207207
formFiles.subscribe((files) => {
208208
if (!browser) return;
209209
const dt = new DataTransfer();
210-
if (files) files.forEach((file) => dt.items.add(file));
210+
211+
if (Array.isArray(files)) {
212+
if (files.length && files.every((f) => !f)) {
213+
formFiles.set([]);
214+
return;
215+
}
216+
217+
files.filter((f) => !!f).forEach((file) => dt.items.add(file));
218+
}
211219
filesProxy.set(dt.files);
212220
});
213221

@@ -219,7 +227,10 @@ export function filesProxy<
219227
if (!browser) return;
220228
if (!(files instanceof FileList)) {
221229
const dt = new DataTransfer();
222-
if (Array.isArray(files)) files.forEach((file) => dt.items.add(file));
230+
if (Array.isArray(files))
231+
files.forEach((file) => {
232+
if (file) dt.items.add(file);
233+
});
223234
else formFiles.set(files as File[]);
224235
files = dt.files;
225236
}

src/lib/client/superForm.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1419,7 +1419,7 @@ export function superForm<
14191419
rebind({
14201420
form: newForm as SuperValidated<T, M, In>,
14211421
untaint: successResult,
1422-
keepFiles: true
1422+
keepFiles: !Form_shouldReset(true, true)
14231423
});
14241424
}
14251425
}

src/lib/formData.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ function _parseFormData<T extends Record<string, unknown>>(
208208
}
209209

210210
if (info.types.includes('array') || info.types.includes('set')) {
211-
const items = property.items;
211+
// If no items, it could be a union containing the info
212+
const items = property.items ?? (info.union?.length == 1 ? info.union[0] : undefined);
212213
if (!items || typeof items == 'boolean' || (Array.isArray(items) && items.length != 1)) {
213214
throw new SchemaError(
214215
'Arrays must have a single "items" property that defines its type.',
@@ -222,7 +223,11 @@ function _parseFormData<T extends Record<string, unknown>>(
222223
const arrayInfo = schemaInfo(arrayType, info.isOptional, [key]);
223224
if (!arrayInfo) continue;
224225

226+
// Check for empty files being posted (and filtered)
227+
const isFileArray = entries.length && entries.some((e) => e && typeof e !== 'string');
225228
const arrayData = entries.map((e) => parseSingleEntry(key, e, arrayInfo));
229+
if (isFileArray && arrayData.every((file) => !file)) arrayData.length = 0;
230+
226231
output[key] = info.types.includes('set') ? new Set(arrayData) : arrayData;
227232
} else {
228233
output[key] = parseSingleEntry(key, entries[entries.length - 1], info);

src/routes/(v2)/v2/files-proxy/+page.server.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { zod } from '$lib/adapters/zod.js';
2-
import { removeFiles, message, superValidate } from '$lib/server/index.js';
2+
import { message, superValidate, withFiles } from '$lib/server/index.js';
33
import { fail } from '@sveltejs/kit';
44
import { schema } from './schema.js';
55

@@ -10,13 +10,15 @@ export const load = async () => {
1010

1111
export const actions = {
1212
default: async ({ request }) => {
13+
console.log('======================================================');
14+
1315
const formData = await request.formData();
1416
console.dir(formData, { depth: 10 });
1517

1618
const form = await superValidate(formData, zod(schema), { allowFiles: true });
1719
console.dir(form, { depth: 10 });
1820

19-
if (!form.valid) return fail(400, removeFiles({ form }));
21+
if (!form.valid) return fail(400, withFiles({ form }));
2022

2123
return message(form, 'Posted OK!');
2224
}

src/routes/(v2)/v2/files-proxy/+page.svelte

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
let show = true;
1010
1111
const superform = superForm(data.form, {
12-
validators: zod(schema)
12+
validators: zod(schema),
13+
resetForm: true
1314
});
1415
const { form, tainted, message, enhance, errors } = superform;
1516
@@ -69,12 +70,14 @@
6970
function clearFilesDirectly() {
7071
// @ts-expect-error Not optional
7172
$form.images = undefined;
73+
// @ts-expect-error Not nullable
7274
$form.images = null;
7375
}
7476
7577
function clearFilesWithProxy() {
7678
// @ts-expect-error Not optional
7779
files.values.set(undefined);
80+
// @ts-expect-error Not nullable
7881
files.values.set(null);
7982
}
8083
@@ -114,7 +117,7 @@
114117
Upload one file, max 10 Kb: <input
115118
bind:files={$file}
116119
accept="image/png, image/jpeg"
117-
name="images"
120+
name="image"
118121
type="file"
119122
/>
120123
{#if $errors.image}

src/routes/(v2)/v2/files-proxy/schema.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ import { z } from 'zod';
33
export const schema = z.object({
44
images: z
55
.custom<File>()
6-
.refine((f) => f instanceof File && f.size < 10000, 'Max 10Kb upload size.')
7-
.array()
8-
.nullable(),
6+
.refine((f) => f instanceof File && f.size < 100000, 'Max 100Kb upload size.')
7+
.array(),
98
image: z
109
.instanceof(File, { message: 'Please upload a file.' })
11-
.refine((f) => f.size < 10000, 'Max 10Kb upload size.')
10+
.refine((f) => f.size < 100000, 'Max 100Kb upload size.')
1211
.optional()
1312
});

0 commit comments

Comments
 (0)