diff --git a/src/controllers/document.controller.ts b/src/controllers/document.controller.ts index e351929..56be8e1 100644 --- a/src/controllers/document.controller.ts +++ b/src/controllers/document.controller.ts @@ -389,7 +389,13 @@ export const uploadDocument = async (req: AuthenticatedRequest, res: Response) = } // 2. Upload File to Supabase Storage - const filePath = `${user_id}/${Date.now()}_${file.originalname}`; + const sanitizeFilename = (name: string) => { + const charMap: Record = { 'ç': 'c', 'ğ': 'g', 'ı': 'i', 'ö': 'o', 'ş': 's', 'ü': 'u', 'Ç': 'C', 'Ğ': 'G', 'İ': 'I', 'Ö': 'O', 'Ş': 'S', 'Ü': 'U' }; + const engName = name.replace(/[çğıöşüÇĞİÖŞÜ]/g, m => charMap[m]); + return engName.replace(/[^a-zA-Z0-9.\-_]/g, '_'); + }; + const safeName = sanitizeFilename(file.originalname); + const filePath = `${user_id}/${Date.now()}_${safeName}`; const { error: uploadError } = await supabase.storage .from('case-documents') .upload(filePath, file.buffer, { diff --git a/src/controllers/template.controller.ts b/src/controllers/template.controller.ts index 0a07672..8f01b1f 100644 --- a/src/controllers/template.controller.ts +++ b/src/controllers/template.controller.ts @@ -29,7 +29,14 @@ export const uploadTemplate = async (req: AuthenticatedRequest, res: Response) = console.error('Template Text Extraction Error:', err); } - const storagePath = `templates/${userId}/${Date.now()}_${file.originalname}`; + const sanitizeFilename = (name: string) => { + const charMap: Record = { 'ç': 'c', 'ğ': 'g', 'ı': 'i', 'ö': 'o', 'ş': 's', 'ü': 'u', 'Ç': 'C', 'Ğ': 'G', 'İ': 'I', 'Ö': 'O', 'Ş': 'S', 'Ü': 'U' }; + const engName = name.replace(/[çğıöşüÇĞİÖŞÜ]/g, m => charMap[m]); + return engName.replace(/[^a-zA-Z0-9.\-_]/g, '_'); + }; + + const safeName = sanitizeFilename(file.originalname); + const storagePath = `templates/${userId}/${Date.now()}_${safeName}`; const { error: uploadError } = await supabase.storage .from('case-documents') .upload(storagePath, file.buffer, { contentType: file.mimetype }); diff --git a/test_db.js b/test_db.js new file mode 100644 index 0000000..f1a7ed5 --- /dev/null +++ b/test_db.js @@ -0,0 +1,10 @@ +require('dotenv').config(); +const { createClient } = require('@supabase/supabase-js'); +const supabase = createClient(process.env.SUPABASE_URL, process.env.SUPABASE_SERVICE_ROLE_KEY); + +async function run() { + const { data, error } = await supabase.from('templates').select('*').limit(1); + if (error) console.error('Error:', error.message); + else console.log('Success:', data); +} +run(); diff --git a/test_upload.js b/test_upload.js new file mode 100644 index 0000000..8704a0d --- /dev/null +++ b/test_upload.js @@ -0,0 +1,12 @@ +require('dotenv').config(); +const { createClient } = require('@supabase/supabase-js'); +const supabaseUrl = process.env.SUPABASE_URL; +const supabaseKey = process.env.SUPABASE_SERVICE_ROLE_KEY || process.env.SUPABASE_ANON_KEY; +const supabase = createClient(supabaseUrl, supabaseKey); + +async function run() { + const { data, error } = await supabase.storage.from('case-documents').upload('templates/test_user_id/test.txt', Buffer.from('hello'), { contentType: 'text/plain' }); + if (error) console.error('Error:', error); + else console.log('Success:', data); +} +run(); diff --git a/test_upload2.js b/test_upload2.js new file mode 100644 index 0000000..ed9819b --- /dev/null +++ b/test_upload2.js @@ -0,0 +1,10 @@ +require('dotenv').config(); +const { createClient } = require('@supabase/supabase-js'); +const supabase = createClient(process.env.SUPABASE_URL, process.env.SUPABASE_SERVICE_ROLE_KEY); + +async function run() { + const { data, error } = await supabase.storage.from('case-documents').upload('templates/test_user_id/şablon dosyası.udf', Buffer.from('hello'), { contentType: 'application/octet-stream' }); + if (error) console.error('Error:', error); + else console.log('Success:', data); +} +run();