Skip to content

Commit 7780955

Browse files
committed
Move upload zip validation into separate file
Move upload zip validation into separate file Add wrongCase, wrongExtension, and noExtension errors
1 parent aa181e6 commit 7780955

File tree

2 files changed

+207
-192
lines changed

2 files changed

+207
-192
lines changed

builder/src/upload.tsx

Lines changed: 10 additions & 192 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { FormSelectField } from "./components/FormSelectField";
1919
import { CommunityCategorySelector } from "./components/CommunitySelector";
2020
import { FormRow } from "./components/FormRow";
2121
import { SubmitPackage } from "./api/packageSubmit";
22-
import { BlobReader, ZipReader } from "./vendor/zip-fs-full";
22+
import { validateZip } from "./uploadZipValidation";
2323

2424
function getUploadProgressBarcolor(uploadStatus: FileUploadStatus | undefined) {
2525
if (uploadStatus == FileUploadStatus.CANCELED) {
@@ -43,7 +43,7 @@ function getSubmissionProgressBarcolor(
4343
return "bg-warning";
4444
}
4545

46-
class FormErrors {
46+
export class FormErrors {
4747
teamError: string | null = null;
4848
communitiesError: string | null = null;
4949
categoriesError: string | null = null;
@@ -140,201 +140,19 @@ const SubmissionForm: React.FC<SubmissionFormProps> = observer((props) => {
140140

141141
if (file) {
142142
validateZip(file).then((result) => {
143-
if (result) {
144-
console.log("Zip successfully validated.");
145-
} else {
146-
console.log("Failed to validate zip.");
147-
}
148-
});
149-
}
150-
};
151-
152-
async function validateZip(file: File): Promise<boolean> {
153-
console.log("Selected file: " + file.name);
154-
155-
let errors = new FormErrors();
156-
157-
let blockUpload = false;
158-
159-
let isZip = true;
160-
if (!file.name.toLowerCase().endsWith(".zip")) {
161-
errors.fileErrors.push("The file you selected is not a .zip!");
162-
isZip = false;
163-
blockUpload = true;
164-
}
165-
166-
if (isZip) {
167-
try {
168-
const blobReader = new BlobReader(file);
169-
const zipReader = new ZipReader(blobReader);
170-
171-
const entries = await zipReader.getEntries();
172-
173-
let dllCount = 0;
174-
let hasBepInEx = false;
175-
let hasAssemblyCSharp = false;
176-
let maybeModpack = false;
177-
let noRootFiles = true;
178-
let rootManifest = false;
179-
let hasIcon = false;
180-
let rootIcon = false;
181-
let hasManifest = false;
182-
let hasReadMe = false;
183-
let rootReadMe = false;
184-
185-
for (const entry of entries) {
186-
// console.log(entry.filename);
187-
188-
if (!entry || !(typeof entry.getData === "function")) {
189-
continue;
190-
}
191-
192-
if (entry.filename.toLowerCase().endsWith(".dll")) {
193-
dllCount++;
194-
}
195-
196-
if (
197-
entry.filename.toLowerCase().split("/").pop() ==
198-
"assembly-csharp.dll"
199-
) {
200-
hasAssemblyCSharp = true;
201-
}
202-
203-
if (
204-
entry.filename.toLowerCase().split("/").pop() ==
205-
"bepinex.dll"
206-
) {
207-
hasBepInEx = true;
208-
maybeModpack = true;
209-
}
210-
211-
if (noRootFiles) {
212-
if (!entry.filename.includes("/")) {
213-
noRootFiles = false;
214-
}
215-
}
216-
if (
217-
entry.filename.toLowerCase().endsWith("manifest.json")
218-
) {
219-
hasManifest = true;
220-
if (entry.filename.toLowerCase() == "manifest.json") {
221-
rootManifest = true;
222-
}
223-
}
224-
if (entry.filename.toLowerCase().endsWith("icon.png")) {
225-
hasIcon = true;
226-
if (entry.filename.toLowerCase() == "icon.png") {
227-
rootIcon = true;
228-
}
229-
}
230-
if (entry.filename.toLowerCase().endsWith("readme.md")) {
231-
hasReadMe = true;
232-
if (entry.filename.toLowerCase() == "readme.md") {
233-
rootReadMe = true;
234-
}
235-
}
236-
}
237-
238-
if (hasBepInEx) {
239-
errors.fileErrors.push(
240-
"You have BepInEx.dll in your .zip file. BepInEx should probably be a dependency in your manifest.json file instead."
241-
);
242-
}
143+
if (result.errors.fileErrors.length > 0) {
144+
setFormErrors(result.errors);
243145

244-
if (hasAssemblyCSharp) {
245-
errors.fileErrors.push(
246-
"You have Assembly-CSharp.dll in your .zip file. Your mod may be removed if you do not have permission to distribute this file."
247-
);
248-
}
249-
250-
if (dllCount > 8) {
251-
errors.fileErrors.push(
252-
"You have " +
253-
dllCount +
254-
" .dll files in your .zip file. Some of these files may be unnecessary."
255-
);
256-
maybeModpack = true;
257-
}
258-
259-
if (maybeModpack) {
260-
errors.fileErrors.push(
261-
"If you're making a modpack, do not include the files for each mod in your .zip file. Instead, put the dependency string for each mod inside your manifest.json file."
262-
);
263-
}
264-
265-
if (
266-
noRootFiles &&
267-
hasManifest &&
268-
hasIcon &&
269-
hasReadMe &&
270-
!rootManifest &&
271-
!rootIcon &&
272-
!rootReadMe
273-
) {
274-
blockUpload = true;
275-
errors.fileErrors.push(
276-
"Your manifest, icon, and README files should be at the root of the .zip file. You can prevent this by compressing the contents of a folder, rather than the folder itself."
277-
);
278-
} else {
279-
if (!hasManifest) {
280-
blockUpload = true;
281-
errors.fileErrors.push(
282-
"Your package is missing a manifest.json file!"
283-
);
284-
} else if (!rootManifest) {
285-
blockUpload = true;
286-
errors.fileErrors.push(
287-
"Your manifest.json file is not at the root of the .zip!"
288-
);
289-
}
290-
291-
if (!hasIcon) {
292-
blockUpload = true;
293-
errors.fileErrors.push(
294-
"Your package is missing an icon.png file!"
295-
);
296-
} else if (!rootIcon) {
297-
blockUpload = true;
298-
errors.fileErrors.push(
299-
"Your icon.png file is not at the root of the .zip!"
300-
);
301-
}
302-
303-
if (!hasReadMe) {
304-
blockUpload = true;
305-
errors.fileErrors.push(
306-
"Your package is missing a README.md file!"
307-
);
308-
} else if (!rootReadMe) {
309-
blockUpload = true;
310-
errors.fileErrors.push(
311-
"Your README.md file is not at the root of the .zip!"
146+
if (result.blockUpload) {
147+
result.errors.generalErrors.push(
148+
"An error with your selected file is preventing submission."
312149
);
150+
setSubmissionStatus(SubmissionStatus.ERROR);
313151
}
314152
}
315-
316-
await zipReader.close();
317-
} catch (e) {
318-
console.log("Error reading zip: " + e);
319-
return false;
320-
}
321-
}
322-
323-
if (errors.fileErrors.length > 0) {
324-
setFormErrors(errors);
325-
326-
if (blockUpload) {
327-
errors.generalErrors.push(
328-
"An error with your selected file is preventing submission."
329-
);
330-
setSubmissionStatus(SubmissionStatus.ERROR);
331-
return false;
332-
}
333-
return true;
334-
} else {
335-
return true;
153+
});
336154
}
337-
}
155+
};
338156

339157
const onSubmit = async (data: any) => {
340158
// TODO: Convert to react-hook-form validation

0 commit comments

Comments
 (0)