2024-04-08 15:15:29 +00:00
|
|
|
import { getFileNameSize } from "@/next/file";
|
2024-02-24 08:18:57 +00:00
|
|
|
import { FILE_READER_CHUNK_SIZE, PICKED_UPLOAD_TYPE } from "constants/upload";
|
|
|
|
import { getElectronFileStream, getFileStream } from "services/readerService";
|
|
|
|
import { DataStream } from "types/upload";
|
|
|
|
import { getImportSuggestion } from "utils/upload";
|
2022-12-20 14:13:09 +00:00
|
|
|
|
2024-02-07 14:00:47 +00:00
|
|
|
// This was for used to verify that converting from the browser readable stream
|
|
|
|
// to the node readable stream correctly handles files that align on the 4 MB
|
|
|
|
// data boundary. This expects a zip file containing random files of various
|
|
|
|
// sizes starting from 1M to 20M.
|
2022-12-20 14:13:09 +00:00
|
|
|
export const testZipFileReading = async () => {
|
|
|
|
try {
|
2024-04-09 06:42:25 +00:00
|
|
|
const electron = globalThis.electron;
|
|
|
|
if (!electron) {
|
2024-02-24 08:18:57 +00:00
|
|
|
console.log("testZipFileReading Check is for desktop only");
|
2022-12-20 15:01:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!process.env.NEXT_PUBLIC_FILE_READING_TEST_ZIP_PATH) {
|
|
|
|
throw Error(
|
2024-02-24 08:18:57 +00:00
|
|
|
"upload test failed NEXT_PUBLIC_FILE_READING_TEST_ZIP_PATH missing",
|
2022-12-20 15:01:50 +00:00
|
|
|
);
|
|
|
|
}
|
2024-04-09 06:42:25 +00:00
|
|
|
const files = await electron.getElectronFilesFromGoogleZip(
|
2024-02-24 08:18:57 +00:00
|
|
|
process.env.NEXT_PUBLIC_FILE_READING_TEST_ZIP_PATH,
|
2022-12-20 15:01:50 +00:00
|
|
|
);
|
2022-12-20 14:13:09 +00:00
|
|
|
if (!files?.length) {
|
|
|
|
throw Error(
|
2024-02-07 12:15:33 +00:00
|
|
|
`testZipFileReading Check failed ❌
|
2024-02-24 08:18:57 +00:00
|
|
|
No files selected`,
|
2022-12-20 14:13:09 +00:00
|
|
|
);
|
|
|
|
}
|
2024-02-24 08:18:57 +00:00
|
|
|
console.log("test zip file reading check started");
|
2022-12-20 14:13:09 +00:00
|
|
|
let i = 0;
|
|
|
|
for (const file of files) {
|
|
|
|
i++;
|
|
|
|
let filedata: DataStream;
|
|
|
|
if (file instanceof File) {
|
|
|
|
filedata = getFileStream(file, FILE_READER_CHUNK_SIZE);
|
|
|
|
} else {
|
|
|
|
filedata = await getElectronFileStream(
|
|
|
|
file,
|
2024-02-24 08:18:57 +00:00
|
|
|
FILE_READER_CHUNK_SIZE,
|
2022-12-20 14:13:09 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
const streamReader = filedata.stream.getReader();
|
|
|
|
for (let i = 0; i < filedata.chunkCount; i++) {
|
|
|
|
const { done } = await streamReader.read();
|
|
|
|
if (done) {
|
|
|
|
throw Error(
|
|
|
|
`testZipFileReading Check failed ❌
|
|
|
|
${getFileNameSize(
|
2024-02-24 08:18:57 +00:00
|
|
|
file,
|
2022-12-20 14:13:09 +00:00
|
|
|
)} less than expected chunks, expected: ${
|
|
|
|
filedata.chunkCount
|
2024-02-24 08:18:57 +00:00
|
|
|
}, got ${i - 1}`,
|
2022-12-20 14:13:09 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const { done } = await streamReader.read();
|
|
|
|
|
|
|
|
if (!done) {
|
|
|
|
throw Error(
|
|
|
|
`testZipFileReading Check failed ❌
|
|
|
|
${getFileNameSize(
|
2024-02-24 08:18:57 +00:00
|
|
|
file,
|
2022-12-20 14:13:09 +00:00
|
|
|
)} more than expected chunks, expected: ${
|
2024-02-22 06:06:17 +00:00
|
|
|
filedata.chunkCount
|
2024-02-24 08:18:57 +00:00
|
|
|
}`,
|
2022-12-20 14:13:09 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
console.log(`${i}/${files.length} passed ✅`);
|
|
|
|
}
|
2024-02-24 08:18:57 +00:00
|
|
|
console.log("test zip file reading check passed ✅");
|
2022-12-20 14:13:09 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
};
|
2022-12-20 15:01:50 +00:00
|
|
|
|
2024-02-07 14:00:47 +00:00
|
|
|
// This was used when fixing a bug around handling a zip file that has a photo
|
|
|
|
// at the root.
|
2022-12-20 15:01:50 +00:00
|
|
|
export const testZipWithRootFileReadingTest = async () => {
|
|
|
|
try {
|
2024-04-09 06:42:25 +00:00
|
|
|
const electron = globalThis.electron;
|
|
|
|
if (!electron) {
|
2024-02-24 08:18:57 +00:00
|
|
|
console.log("testZipFileReading Check is for desktop only");
|
2022-12-20 15:01:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!process.env.NEXT_PUBLIC_ZIP_WITH_ROOT_FILE_PATH) {
|
|
|
|
throw Error(
|
2024-02-24 08:18:57 +00:00
|
|
|
"upload test failed NEXT_PUBLIC_ZIP_WITH_ROOT_FILE_PATH missing",
|
2022-12-20 15:01:50 +00:00
|
|
|
);
|
|
|
|
}
|
2024-04-09 06:42:25 +00:00
|
|
|
const files = await electron.getElectronFilesFromGoogleZip(
|
2024-02-24 08:18:57 +00:00
|
|
|
process.env.NEXT_PUBLIC_ZIP_WITH_ROOT_FILE_PATH,
|
2022-12-20 15:01:50 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const importSuggestion = getImportSuggestion(
|
|
|
|
PICKED_UPLOAD_TYPE.ZIPS,
|
2024-02-24 08:18:57 +00:00
|
|
|
files,
|
2022-12-20 15:01:50 +00:00
|
|
|
);
|
|
|
|
if (!importSuggestion.rootFolderName) {
|
|
|
|
throw Error(
|
|
|
|
`testZipWithRootFileReadingTest Check failed ❌
|
2024-02-24 08:18:57 +00:00
|
|
|
rootFolderName is missing`,
|
2022-12-20 15:01:50 +00:00
|
|
|
);
|
|
|
|
}
|
2024-02-24 08:18:57 +00:00
|
|
|
console.log("testZipWithRootFileReadingTest passed ✅");
|
2022-12-20 15:01:50 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
};
|