feat: validate if zip is unity game file or otherwise

This commit is contained in:
Akuoko Daniel Jnr 2020-11-15 16:41:35 +00:00
parent 0ed17fc797
commit 2a03dd5384
No known key found for this signature in database
GPG Key ID: 1C95803CACD3E9DC
3 changed files with 17 additions and 2 deletions

View File

@ -3,6 +3,7 @@ import * as Store from "~/common/store";
import * as Constants from "~/common/constants";
import * as Credentials from "~/common/credentials";
import * as Strings from "~/common/strings";
import * as Validations from "~/common/validations";
import { dispatchCustomEvent } from "~/common/custom-events";
import { encode } from "blurhash";
@ -48,8 +49,9 @@ export const upload = async ({ file, context, bucketName, routes }) => {
return null;
}
const isFileZip =
const isZipFile =
file.type.startsWith("application/zip") || file.type.startsWith("application/x-zip-compressed");
const isUnityFile = await Validations.isUnityFile(file);
// TODO(jim): Put this somewhere else to handle conversion cases.
if (file.type.startsWith("image/heic")) {
@ -144,7 +146,7 @@ export const upload = async ({ file, context, bucketName, routes }) => {
}
let res;
if (isFileZip) {
if (isZipFile && isUnityFile) {
res = await _privateUploadMethod(`${zipUploadRoute}${file.name}`, file);
} else if (bucketName && bucketName === STAGING_DEAL_BUCKET) {
res = await _privateUploadMethod(`${storageDealRoute}${file.name}`, file);

View File

@ -1,5 +1,7 @@
import * as Strings from "~/common/strings";
import JSZip from "jszip";
const USERNAME_REGEX = new RegExp("^[a-zA-Z0-9_]{0,}[a-zA-Z]+[0-9]*$");
const MIN_PASSWORD_LENGTH = 8;
const EMAIL_REGEX = /^[\w-]+@[a-zA-Z0-9_]+?\.[a-zA-Z]{2,50}$/;
@ -144,3 +146,13 @@ export const isPreviewableImage = (type = "") => {
return type.startsWith("image/");
};
export const isUnityFile = async (file) => {
const zip = new JSZip();
const contents = await zip.loadAsync(file);
const fileNames = Object.keys(contents.files);
// NOTE(daniel): every Unity game file will have this file
return fileNames.includes("Build/UnityLoader.js");
};

View File

@ -57,6 +57,7 @@
"heic2any": "0.0.3",
"isomorphic-fetch": "^3.0.0",
"jsonwebtoken": "^8.5.1",
"jszip": "^3.5.0",
"knex": "^0.21.12",
"minisearch": "^2.5.1",
"moment": "^2.27.0",