Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ const fs = require('fs');

let mainWindow;

const MAX_BACKUP_SIZE_BYTES = 20 * 1024 * 1024;
const MAX_BACKUP_SNIPPETS = 10000;

function isValidSnippetBackup(data) {
return Array.isArray(data) && data.length <= MAX_BACKUP_SNIPPETS && data.every(snippet =>
snippet && typeof snippet === 'object' && !Array.isArray(snippet) &&
Comment on lines +11 to +12

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep generated backups within the import limits

When a collection exceeds 10,000 snippets or its JSON exceeds 20 MiB, exportCodeSnipData still passes the full localStorage value to export-data, which writes it successfully, but the new import path rejects that backup. This can leave users with a backup CodeSnip reports as successful but cannot restore; enforce the same limits before exporting or allow those backups to be imported.

Useful? React with 👍 / 👎.

typeof snippet.id === 'string' && snippet.id.trim().length > 0 &&
typeof snippet.title === 'string' &&
typeof snippet.category === 'string' &&
typeof snippet.code === 'string' &&
(snippet.isFavorite === undefined || typeof snippet.isFavorite === 'boolean')
);
}

function createWindow() {
const isMac = process.platform === 'darwin';

Expand Down Expand Up @@ -89,12 +103,19 @@ ipcMain.handle('import-data', async () => {
}

const filePath = filePaths[0];
const fileStats = fs.statSync(filePath);
if (fileStats.size > MAX_BACKUP_SIZE_BYTES) {
return { success: false, message: 'Yedek dosyası 20 MB sınırını aşıyor.' };
}

const rawData = fs.readFileSync(filePath, 'utf-8');
const parsedData = JSON.parse(rawData);

// Yüklenen dosyanın geçerli bir JSON olup olmadığını doğrula
JSON.parse(rawData);
if (!isValidSnippetBackup(parsedData)) {
return { success: false, message: 'Yedek dosyasının yapısı geçersiz. CodeSnip kod kartı yedeği seçin.' };
}

return { success: true, data: rawData };
return { success: true, data: JSON.stringify(parsedData) };
} catch (error) {
return { success: false, message: 'Seçilen dosya geçerli bir JSON yedeği değil veya okunamadı!' };
}
Expand Down
10 changes: 7 additions & 3 deletions q3-5.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
}

function safeRenderSnips() {
const all = JSON.parse(localStorage.getItem("all_snippets_data") || "[]");
const all = window.readJsonStorage("all_snippets_data", []);
const container = document.getElementById("snip-list-container");
if (!container) return;
const current = lang();
Expand All @@ -90,6 +90,7 @@
container.replaceChildren();
const fragment = document.createDocumentFragment();
all.forEach(snip => {
if (!snip || typeof snip.id !== "string" || typeof snip.title !== "string" || typeof snip.category !== "string" || typeof snip.code !== "string") return;
if (currentCategory === "favorites" && !snip.isFavorite) return;
if (currentCategory !== "all" && currentCategory !== "favorites" && snip.category !== currentCategory) return;
const title = String(snip.title || ""), source = String(snip.code || "");
Expand Down Expand Up @@ -129,10 +130,13 @@
});
}
function refreshCategories() {
const categories = JSON.parse(localStorage.getItem("codesnip_categories") || "[]");
const categories = window.readJsonStorage("codesnip_categories", []);
const current = lang();
categories.forEach(category => {
if (!category.name) category.name = {tr:category.id,en:category.id};
if (!category || typeof category.id !== "string" || !category.id) return;
if (!category.name || typeof category.name !== "object" || Array.isArray(category.name)) {
category.name = {tr:category.id,en:category.id};
}
if (!category.name.es) category.name.es = category.name.en || category.name.tr || category.id;
});
localStorage.setItem("codesnip_categories", JSON.stringify(categories));
Expand Down
Loading
Loading