diff --git a/src/pages/api/upload/media.ts b/src/pages/api/upload/media.ts index a182c48..a69ff6c 100644 --- a/src/pages/api/upload/media.ts +++ b/src/pages/api/upload/media.ts @@ -21,8 +21,13 @@ interface UploadResult { const UPLOAD_CONFIG = { maxFileSize: 50 * 1024 * 1024, // 50MB allowedTypes: new Set([ + // Images 'image/jpeg', 'image/jpg', 'image/png', 'image/gif', 'image/webp', 'image/svg+xml', + + // Videos 'video/mp4', 'video/webm', 'video/ogg', 'video/avi', 'video/mov', + + // Documents 'application/pdf', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', @@ -30,7 +35,32 @@ const UPLOAD_CONFIG = { 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-powerpoint', 'application/vnd.openxmlformats-officedocument.presentationml.presentation', - 'text/plain', 'text/csv', 'application/json' + + // Text files + 'text/plain', + 'text/csv', + 'text/markdown', // Added markdown + 'text/x-markdown', // Alternative markdown MIME type + 'application/json', + 'application/xml', + 'text/xml', + 'text/html', + + // Archives + 'application/zip', + 'application/x-tar', + 'application/gzip', + 'application/x-gzip', + 'application/x-zip-compressed', + 'application/x-rar-compressed', + 'application/x-7z-compressed', + + // Additional useful formats + 'application/rtf', // Rich Text Format + 'text/richtext', + 'application/x-yaml', // YAML files + 'text/yaml', + 'application/yaml' ]), localUploadPath: process.env.LOCAL_UPLOAD_PATH || './public/uploads', publicBaseUrl: process.env.PUBLIC_BASE_URL || 'http://localhost:4321' diff --git a/src/pages/contribute/knowledgebase.astro b/src/pages/contribute/knowledgebase.astro index 5863579..31af7d0 100644 --- a/src/pages/contribute/knowledgebase.astro +++ b/src/pages/contribute/knowledgebase.astro @@ -114,8 +114,13 @@ const sortedTools = data.tools.sort((a: any, b: any) => a.name.localeCompare(b.n
- -
+
@@ -342,37 +347,49 @@ class KnowledgebaseForm { console.log('[UPLOAD] Response status:', response.status); console.log('[UPLOAD] Response headers:', Object.fromEntries(response.headers.entries())); + // FIXED: Read the response body only once + let responseData: any; + let responseText: string; + + try { + // Try to read as text first (works for both JSON and plain text) + responseText = await response.text(); + console.log('[UPLOAD] Raw response:', responseText.substring(0, 200)); + + // Then try to parse as JSON + try { + responseData = JSON.parse(responseText); + } catch (parseError) { + // If JSON parsing fails, treat as plain text + responseData = { error: responseText }; + } + } catch (readError) { + console.error('[UPLOAD] Failed to read response:', readError); + throw new Error('Failed to read server response'); + } + if (response.ok) { - const result = await response.json(); - console.log('[UPLOAD] Success result:', result); + console.log('[UPLOAD] Success result:', responseData); fileItem.uploaded = true; - fileItem.url = result.url; + fileItem.url = responseData.url; this.renderFileList(); this.showMessage('success', `Successfully uploaded ${fileItem.name}`); } else { - // Enhanced error handling - read the actual error from response + // Enhanced error handling with single response read let errorMessage = `Upload failed with status ${response.status}`; - try { - const errorData = await response.json(); - console.error('[UPLOAD] Error response data:', errorData); - - if (errorData.error) { - errorMessage = errorData.error; - } - - // Log additional details if available - if (errorData.details) { - console.error('[UPLOAD] Error details:', errorData.details); - errorMessage += ` (Details: ${errorData.details.join(', ')})`; - } - } catch (parseError) { - console.error('[UPLOAD] Could not parse error response:', parseError); - const errorText = await response.text(); - console.error('[UPLOAD] Raw error response:', errorText); - errorMessage += ` (Raw: ${errorText.substring(0, 100)})`; + if (responseData && responseData.error) { + errorMessage = responseData.error; + } else if (responseText) { + errorMessage += ` (${responseText.substring(0, 100)})`; + } + + // Log additional details if available + if (responseData && responseData.details) { + console.error('[UPLOAD] Error details:', responseData.details); + errorMessage += ` (Details: ${responseData.details.join(', ')})`; } throw new Error(errorMessage);