Spaces:
Paused

File size: 5,413 Bytes
e81f4e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import { isRecord } from "../shared/types";
import {
	firstNonEmptyString,
	genericFilenameFromMime,
	imageFilenameFromMime,
	mimeFromFilename,
	sanitizeUploadFilename,
} from "./mime";
import {
	imageFilenameFromObject,
	normalizeUploadFileInput,
	type UploadFileInput,
} from "./input";
import { attachmentDrop } from "./notes";
import { appendExistingFileRefs } from "./refs";
import type {
	AttachmentCandidate,
	AttachmentDrop,
	AttachmentFileRef,
	AttachmentKind,
	AttachmentPlan,
} from "./types";

export const MAX_ATTACHMENTS_PER_REQUEST = 50;

type PlanState = {
	candidates: AttachmentCandidate[];
	existingFileRefs: AttachmentFileRef[];
	dropped: AttachmentDrop[];
	maxFiles: number;
	nextID: number;
};

type CreatePlanInput = {
	images?: unknown;
	files?: unknown;
	existingFileRefs?: unknown;
	maxFiles?: number;
};

export function createAttachmentPlan(
	input: CreatePlanInput = {},
): AttachmentPlan {
	const state: PlanState = {
		candidates: [],
		existingFileRefs: [],
		dropped: [],
		maxFiles: normalizeMaxFiles(input.maxFiles),
		nextID: 1,
	};
	appendExistingFileRefs(state.existingFileRefs, input.existingFileRefs);
	appendImageInputs(state, input.images);
	appendFileInputs(state, input.files);
	return finishPlan(state);
}

export function mergeAttachmentPlans(
	...plans: Array<AttachmentPlan | null | undefined>
): AttachmentPlan {
	const state: PlanState = {
		candidates: [],
		existingFileRefs: [],
		dropped: [],
		maxFiles: MAX_ATTACHMENTS_PER_REQUEST,
		nextID: 1,
	};
	for (const plan of plans) {
		if (!plan) continue;
		state.maxFiles = Math.min(state.maxFiles, normalizeMaxFiles(plan.maxFiles));
		appendExistingFileRefs(state.existingFileRefs, plan.existingFileRefs);
		for (const candidate of plan.candidates) {
			const meta: { filename?: string; mime?: string } = {};
			if (candidate.filename) meta.filename = candidate.filename;
			if (candidate.mime) meta.mime = candidate.mime;
			addCandidate(state, candidate.kind, candidate.source, meta);
		}
		state.dropped.push(...plan.dropped);
	}
	return finishPlan(state);
}

function appendImageInputs(state: PlanState, raw: unknown): void {
	if (!Array.isArray(raw)) return;
	for (let i = 0; i < raw.length; i++) {
		const image = raw[i];
		if (!isRecord(image)) continue;
		const b64 = image.b64;
		if (b64 == null) {
			state.dropped.push(
				attachmentDrop(
					"image",
					"invalid_image_input",
					"invalid image input",
					imageFilenameFromObject(image),
				),
			);
			continue;
		}
		const name =
			firstNonEmptyString(
				sanitizeUploadFilename(image.filename),
				sanitizeUploadFilename(image.name),
			) ||
			imageFilenameFromMime(
				image.mime || "image/png",
				state.candidates.length + 1,
			);
		const source = { type: "base64" as const, data: b64 };
		const mime = firstNonEmptyString(image.mime, "image/png");
		addCandidate(state, "image", source, {
			filename: name,
			mime,
		});
	}
}

function appendFileInputs(state: PlanState, raw: unknown): void {
	if (!Array.isArray(raw)) return;
	for (const item of raw) {
		const input = isNormalizedUploadFileInput(item)
			? item
			: normalizeUploadFileInput(item);
		if (!input) continue;
		appendUploadFileInput(state, input);
	}
}

function appendUploadFileInput(state: PlanState, input: UploadFileInput): void {
	const nameHint = firstNonEmptyString(
		sanitizeUploadFilename(input.filename),
		sanitizeUploadFilename(input.name),
	);
	const mime = firstNonEmptyString(input.mime, mimeFromFilename(nameHint));
	if (input.invalidReason) {
		state.dropped.push(
			attachmentDrop(
				"file",
				"invalid_file_input",
				String(input.invalidReason || "invalid file input"),
				nameHint,
			),
		);
		return;
	}
	if (input.b64 != null) {
		const meta: { filename?: string; mime?: string } = {};
		if (nameHint) meta.filename = nameHint;
		else if (mime)
			meta.filename = genericFilenameFromMime(
				mime,
				state.candidates.length + 1,
			);
		if (mime) meta.mime = mime;
		addCandidate(
			state,
			"file",
			{ type: "base64", data: input.b64 },
			{
				...meta,
			},
		);
	}
}

function addCandidate(
	state: PlanState,
	kind: AttachmentKind,
	source: AttachmentCandidate["source"],
	meta: { filename?: string; mime?: string } = {},
): void {
	if (state.candidates.length >= state.maxFiles) {
		state.dropped.push(
			attachmentDrop(
				kind,
				"too_many_files",
				`exceeded maximum of ${state.maxFiles} attachments per request`,
				meta.filename,
			),
		);
		return;
	}
	const candidate: AttachmentCandidate = {
		id: `att_${state.nextID++}`,
		kind,
		role: "request",
		source,
	};
	const filename = sanitizeUploadFilename(meta.filename);
	if (filename) candidate.filename = filename;
	const mime = firstNonEmptyString(meta.mime);
	if (mime) candidate.mime = mime;
	state.candidates.push(candidate);
}

function finishPlan(state: PlanState): AttachmentPlan {
	return {
		candidates: state.candidates,
		existingFileRefs: state.existingFileRefs.length
			? state.existingFileRefs
			: null,
		dropped: state.dropped,
		maxFiles: state.maxFiles,
	};
}

function normalizeMaxFiles(value: unknown): number {
	const n = Number(value);
	if (!Number.isFinite(n) || n <= 0) return MAX_ATTACHMENTS_PER_REQUEST;
	return Math.max(1, Math.floor(n));
}

function isNormalizedUploadFileInput(value: unknown): value is UploadFileInput {
	if (!isRecord(value)) return false;
	return "invalidReason" in value || "b64" in value;
}