Skip to content

Commit

Permalink
Check compression before importing
Browse files Browse the repository at this point in the history
  • Loading branch information
kepano committed Nov 26, 2024
1 parent ad8e53f commit 987f77e
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions src/utils/import-export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,21 +391,27 @@ async function importAllSettingsFromJson(jsonContent: string): Promise<void> {
const templateIds = importData.template_list || [];
for (const id of templateIds) {
const key = `template_${id}`;
if (importData[key] && typeof importData[key] === 'object') {
if (importData[key]) {
try {
// Compress the template data
const templateStr = JSON.stringify(importData[key]);
const compressedData = compressToUTF16(templateStr);

// Split into chunks
const chunks: string[] = [];
const CHUNK_SIZE = 8000;
for (let i = 0; i < compressedData.length; i += CHUNK_SIZE) {
chunks.push(compressedData.slice(i, i + CHUNK_SIZE));
// Check if the data is already compressed (will be an array of strings)
const isAlreadyCompressed = Array.isArray(importData[key]) &&
importData[key].every((chunk: any) => typeof chunk === 'string');

if (!isAlreadyCompressed) {
// Compress the template data
const templateStr = JSON.stringify(importData[key]);
const compressedData = compressToUTF16(templateStr);

// Split into chunks
const chunks: string[] = [];
const CHUNK_SIZE = 8000;
for (let i = 0; i < compressedData.length; i += CHUNK_SIZE) {
chunks.push(compressedData.slice(i, i + CHUNK_SIZE));
}
importData[key] = chunks;
}
importData[key] = chunks;
} catch (error) {
console.error(`Failed to compress template ${id}:`, error);
console.error(`Failed to process template ${id}:`, error);
}
}
}
Expand Down

0 comments on commit 987f77e

Please sign in to comment.